From 8791867e332da338b35abf9e54a609e7f857bf83 Mon Sep 17 00:00:00 2001 From: Artem Yegorov Date: Sat, 21 Sep 2024 22:44:51 +0300 Subject: [PATCH] #195: calculate total number of CI builds --- judges/quantity-of-deliverables/count-all.yml | 1 + .../quantity-of-deliverables.rb | 6 ++ test/judges/test-quantity-of-deliverables.rb | 85 +++++++++++++++++++ 3 files changed, 92 insertions(+) diff --git a/judges/quantity-of-deliverables/count-all.yml b/judges/quantity-of-deliverables/count-all.yml index a4b04f387..4d4d174ca 100644 --- a/judges/quantity-of-deliverables/count-all.yml +++ b/judges/quantity-of-deliverables/count-all.yml @@ -45,3 +45,4 @@ expected: - /fb/f[total_pulls_submitted != 0] - /fb/f[total_releases_published != 0] - /fb/f[total_reviews_submitted = 0] + - /fb/f[total_builds_ran != 0] diff --git a/judges/quantity-of-deliverables/quantity-of-deliverables.rb b/judges/quantity-of-deliverables/quantity-of-deliverables.rb index 6fc089bc5..a0ced35c8 100644 --- a/judges/quantity-of-deliverables/quantity-of-deliverables.rb +++ b/judges/quantity-of-deliverables/quantity-of-deliverables.rb @@ -71,4 +71,10 @@ end end end + + # Total number of CI builds executed in all repositories from since + f.total_builds_ran = + Fbe.unmask_repos.sum do |repo| + Fbe.octo.repository_workflow_runs(repo, created: ">#{f.since.utc.iso8601[0..9]}")[:total_count] + end end diff --git a/test/judges/test-quantity-of-deliverables.rb b/test/judges/test-quantity-of-deliverables.rb index cb6a63e40..0ca75d43d 100644 --- a/test/judges/test-quantity-of-deliverables.rb +++ b/test/judges/test-quantity-of-deliverables.rb @@ -94,6 +94,10 @@ def test_counts_commits body: [{ id: 1, draft: false, published_at: Time.parse('2024-08-01 21:00:00 UTC') }] ) stub_github('https://api.github.com/repos/foo/foo/pulls?per_page=100&state=all', body: []) + stub_github( + 'https://api.github.com/repos/foo/foo/actions/runs?created=%3E2024-07-15&per_page=100', + body: { total_count: 0, workflow_runs: [] } + ) fb = Factbase.new Time.stub(:now, Time.parse('2024-08-12 21:00:00 UTC')) do load_it('quantity-of-deliverables', fb) @@ -139,6 +143,10 @@ def test_processes_empty_repository body: [{ id: 1, draft: false, published_at: Time.parse('2024-08-01 21:00:00 UTC') }] ) stub_github('https://api.github.com/repos/foo/foo/pulls?per_page=100&state=all', body: []) + stub_github( + 'https://api.github.com/repos/foo/foo/actions/runs?created=%3E2024-07-15&per_page=100', + body: { total_count: 0, workflow_runs: [] } + ) fb = Factbase.new Time.stub(:now, Time.parse('2024-08-12 21:00:00 UTC')) do load_it('quantity-of-deliverables', fb) @@ -191,6 +199,10 @@ def test_quantity_of_deliverables_total_releases_published ] ) stub_github('https://api.github.com/repos/foo/foo/pulls?per_page=100&state=all', body: []) + stub_github( + 'https://api.github.com/repos/foo/foo/actions/runs?created=%3E2024-08-02&per_page=100', + body: { total_count: 0, workflow_runs: [] } + ) fb = Factbase.new f = fb.insert f.what = 'pmp' @@ -370,6 +382,13 @@ def test_quantity_of_deliverables_total_reviews_submitted } ] ) + stub_github( + 'https://api.github.com/repos/foo/foo/actions/runs?created=%3E2024-08-02&per_page=100', + body: { + total_count: 0, + workflow_runs: [] + } + ) fb = Factbase.new f = fb.insert f.what = 'pmp' @@ -384,4 +403,70 @@ def test_quantity_of_deliverables_total_reviews_submitted assert_equal(5, f.total_reviews_submitted) end end + + def test_quantity_of_deliverables_total_builds_ran + WebMock.disable_net_connect! + stub_github( + 'https://api.github.com/repos/foo/foo', + body: { id: 42, full_name: 'foo/foo', open_issues: 0, size: 100 } + ) + stub_github( + 'https://api.github.com/repos/foo/foo/commits?per_page=100&since=2024-08-02T21:00:00%2B00:00', + body: [] + ) + stub_github( + 'https://api.github.com/repos/foo/foo/issues?per_page=100&since=%3E2024-08-02', + body: [{ pull_request: {} }] + ) + stub_github('https://api.github.com/repos/foo/foo/releases?per_page=100', body: []) + stub_github('https://api.github.com/repos/foo/foo/pulls?per_page=100&state=all', body: []) + stub_github( + 'https://api.github.com/repos/foo/foo/actions/runs?created=%3E2024-08-02&per_page=100', + body: { + total_count: 3, + workflow_runs: [ + { + id: 710, + display_title: 'some title', + run_number: 2615, + event: 'dynamic', + status: 'completed', + conclusion: 'success', + workflow_id: 141 + }, + { + id: 708, + display_title: 'some title', + run_number: 2612, + event: 'schedule', + status: 'completed', + conclusion: 'success', + workflow_id: 141 + }, + { + id: 705, + display_title: 'some title', + run_number: 2610, + event: 'push', + status: 'completed', + conclusion: 'failure', + workflow_id: 141 + } + ] + } + ) + fb = Factbase.new + f = fb.insert + f.what = 'pmp' + f.area = 'scope' + f.qod_days = 7 + f.qod_interval = 3 + Time.stub(:now, Time.parse('2024-08-09 21:00:00 UTC')) do + load_it('quantity-of-deliverables', fb) + f = fb.query('(eq what "quantity-of-deliverables")').each.to_a.first + assert_equal(Time.parse('2024-08-03 00:00:00 +03:00'), f.since) + assert_equal(Time.parse('2024-08-09 21:00:00 UTC'), f.when) + assert_equal(3, f.total_builds_ran) + end + end end