Skip to content

Commit

Permalink
#190: calculate average review time for pr
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegorov committed Sep 2, 2024
1 parent 5548472 commit 11e3a0d
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 7 deletions.
12 changes: 12 additions & 0 deletions judges/quality-of-service/quality-of-service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@
end
f.average_pull_hoc_size = hocs.empty? ? 0 : hocs.sum.to_f / hocs.size
f.average_pull_files_size = files.empty? ? 0 : files.sum.to_f / files.size

# Average review time
review_times = []
Fbe.unmask_repos.each do |repo|
Fbe.octo.search_issues(
"repo:#{repo} type:pr is:merged closed:>#{f.since.utc.iso8601[0..9]}"
)[:items].each do |pr|
review = Fbe.octo.pull_request_reviews(repo, pr[:number]).min_by { |r| r[:submitted_at] }
review_times << (pr[:merged_at] - review[:submitted_at]).to_i if review
end
end
f.average_review_time = review_times.empty? ? 0 : review_times.sum.to_f / review_times.size
end
1 change: 1 addition & 0 deletions judges/quality-of-service/simple-collect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ expected:
- /fb/f[average_pull_rejection_rate != 0]
- /fb/f[average_pull_hoc_size != 0]
- /fb/f[average_pull_files_size != 0]
- /fb/f[average_review_time != 0]
185 changes: 178 additions & 7 deletions test/judges/test-quality-of-service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,30 @@ def test_runs_when_run_duration_ms_is_nil
'https://api.github.com/search/issues?per_page=100&' \
'q=repo:foo/foo%20type:pr%20is:merged%20closed:%3E2024-07-15',
body: {
total_count: 1, incomplete_results: false, items: [{ id: 50, number: 12, title: 'Awesome 12' }]
total_count: 1, incomplete_results: false, items: [
{ id: 50, number: 12, title: 'Awesome 12', merged_at: Time.parse('2024-08-23 18:30:00 UTC') }
]
}
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12',
body: { id: 50, number: 12, additions: 12, deletions: 5, changed_files: 3 }
)
stub_github(
'https://api.github.com/search/issues?per_page=100&q=repo:foo/foo%20type:pr%20created:%3E2024-08-02',
body: {
total_count: 1, incomplete_results: false,
items: [{ id: 50, number: 12, title: 'Awesome 12', created_at: Time.parse('2024-08-20 22:00:00 UTC') }]
}
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12/reviews?per_page=100',
body: [{ id: 22_449_326, submitted_at: Time.parse('2024-08-21 22:00:00 UTC') }]
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12/reviews?per_page=100',
body: [{ id: 22_449_326, submitted_at: Time.parse('2024-07-21 22:00:00 UTC') }]
)
fb = Factbase.new
Time.stub(:now, Time.parse('2024-08-12 21:00:00 UTC')) do
load_it('quality-of-service', fb)
Expand Down Expand Up @@ -237,6 +254,17 @@ def test_quality_of_service_average_build_mttr
}
]
)
stub_github(
'https://api.github.com/search/issues?per_page=100&q=repo:foo/foo%20type:pr%20created:%3E2024-08-02',
body: {
total_count: 1, incomplete_results: false,
items: [{ id: 50, number: 12, title: 'Awesome 12', created_at: Time.parse('2024-08-20 22:00:00 UTC') }]
}
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12/reviews?per_page=100',
body: [{ id: 22_449_326, submitted_at: Time.parse('2024-08-21 22:00:00 UTC') }]
)
fb = Factbase.new
f = fb.insert
f.what = 'pmp'
Expand Down Expand Up @@ -327,6 +355,17 @@ def test_quality_of_service_average_build_mttr_when_failure_several_times_in_a_r
}
]
)
stub_github(
'https://api.github.com/search/issues?per_page=100&q=repo:foo/foo%20type:pr%20created:%3E2024-08-02',
body: {
total_count: 1, incomplete_results: false,
items: [{ id: 50, number: 12, title: 'Awesome 12', created_at: Time.parse('2024-08-20 22:00:00 UTC') }]
}
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12/reviews?per_page=100',
body: [{ id: 22_449_326, submitted_at: Time.parse('2024-08-21 22:00:00 UTC') }]
)
fb = Factbase.new
f = fb.insert
f.what = 'pmp'
Expand Down Expand Up @@ -384,11 +423,11 @@ def test_quality_of_service_average_hocs_and_files
body: {
total_count: 1, incomplete_results: false,
items: [
{ id: 50, number: 12, title: 'Awesome 12' },
{ id: 52, number: 14, title: 'Awesome 14' },
{ id: 54, number: 16, title: 'Awesome 16' },
{ id: 56, number: 18, title: 'Awesome 18' },
{ id: 58, number: 20, title: 'Awesome 20' }
{ id: 50, number: 12, title: 'Awesome 12', merged_at: Time.parse('2024-08-23 18:30:00 UTC') },
{ id: 52, number: 14, title: 'Awesome 14', merged_at: Time.parse('2024-08-23 18:30:00 UTC') },
{ id: 54, number: 16, title: 'Awesome 16', merged_at: Time.parse('2024-08-23 18:30:00 UTC') },
{ id: 56, number: 18, title: 'Awesome 18', merged_at: Time.parse('2024-08-23 18:30:00 UTC') },
{ id: 58, number: 20, title: 'Awesome 20', merged_at: Time.parse('2024-08-23 18:30:00 UTC') }
]
}
)
Expand Down Expand Up @@ -442,6 +481,22 @@ def test_quality_of_service_average_hocs_and_files
changed_files: 4
}
)
stub_github(
'https://api.github.com/search/issues?per_page=100&q=repo:foo/foo%20type:pr%20created:%3E2024-08-02',
body: {
total_count: 1, incomplete_results: false,
items: [{ id: 50, number: 12, title: 'Awesome 12', created_at: Time.parse('2024-08-20 22:00:00 UTC') }]
}
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12/reviews?per_page=100',
body: [{ id: 22_449_326, submitted_at: Time.parse('2024-08-21 22:00:00 UTC') }]
)
stub_github('https://api.github.com/repos/foo/foo/pulls/12/reviews?per_page=100', body: [])
stub_github('https://api.github.com/repos/foo/foo/pulls/14/reviews?per_page=100', body: [])
stub_github('https://api.github.com/repos/foo/foo/pulls/16/reviews?per_page=100', body: [])
stub_github('https://api.github.com/repos/foo/foo/pulls/18/reviews?per_page=100', body: [])
stub_github('https://api.github.com/repos/foo/foo/pulls/20/reviews?per_page=100', body: [])
fb = Factbase.new
f = fb.insert
f.what = 'pmp'
Expand All @@ -458,6 +513,113 @@ def test_quality_of_service_average_hocs_and_files
end
end

def test_quality_of_service_average_review_time
WebMock.disable_net_connect!
stub_github('https://api.github.com/repos/foo/foo', body: { id: 42, full_name: 'foo/foo' })
stub_github(
'https://api.github.com/repos/foo/foo/actions/runs?created=%3E2024-08-02&per_page=100',
body: { total_count: 0, workflow_runs: [] }
)
stub_github(
'https://api.github.com/repos/foo/foo/releases?per_page=100',
body: []
)
stub_github(
'https://api.github.com/search/issues?per_page=100&q=repo:foo/foo%20type:issue%20closed:%3E2024-08-02',
body: {
total_count: 1, incomplete_results: false, items: [{ number: 42, labels: [{ name: 'bug' }] }]
}
)
stub_github(
'https://api.github.com/search/issues?per_page=100&q=repo:foo/foo%20type:pr%20closed:%3E2024-08-02',
body: {
total_count: 2, incomplete_results: false,
items: [{ id: 42, number: 10, title: 'Awesome 10' }, { id: 43, number: 11, title: 'Awesome 11' }]
}
)
(Date.parse('2024-08-02')..Date.parse('2024-08-09')).each do |date|
stub_github(
'https://api.github.com/search/issues?per_page=100&' \
"q=repo:foo/foo%20type:issue%20created:2024-08-02..#{date}",
body: { total_count: 0, items: [] }
)
end
stub_github(
'https://api.github.com/search/issues?per_page=100&' \
'q=repo:foo/foo%20type:pr%20is:unmerged%20closed:%3E2024-08-02',
body: {
total_count: 1, incomplete_results: false, items: [{ id: 42, number: 10, title: 'Awesome 10' }]
}
)
stub_github(
'https://api.github.com/search/issues?per_page=100&' \
'q=repo:foo/foo%20type:pr%20is:merged%20closed:%3E2024-08-02',
body: {
total_count: 1, incomplete_results: false,
items: [
{ id: 50, number: 12, title: 'Awesome 12', created_at: Time.parse('2024-08-20 22:00:00 UTC'),
merged_at: Time.parse('2024-08-27 18:30:00 UTC') },
{ id: 51, number: 14, title: 'Awesome 14', created_at: Time.parse('2024-08-23 12:00:00 UTC'),
merged_at: Time.parse('2024-08-27 18:30:00 UTC') },
{ id: 52, number: 16, title: 'Awesome 16', created_at: Time.parse('2024-08-25 12:00:00 UTC'),
merged_at: Time.parse('2024-08-27 18:30:00 UTC') }
]
}
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12',
body: { id: 50, number: 12, additions: 10, deletions: 5, changed_files: 1 }
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/14',
body: { id: 51, number: 14, additions: 10, deletions: 5, changed_files: 1 }
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/16',
body: { id: 52, number: 16, additions: 10, deletions: 5, changed_files: 1 }
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12/reviews?per_page=100',
body: [
{
id: 22_449_328, body: 'Some text 3', state: 'CHANGES_REQUESTED',
author_association: 'CONTRIBUTOR', submitted_at: Time.parse('2024-08-23 10:00:00 UTC')
},
{
id: 22_449_327, body: 'Some text 2', state: 'CHANGES_REQUESTED',
author_association: 'CONTRIBUTOR', submitted_at: Time.parse('2024-08-22 10:00:00 UTC')
},
{
id: 22_449_326, body: 'Some text 1', state: 'CHANGES_REQUESTED',
author_association: 'CONTRIBUTOR', submitted_at: Time.parse('2024-08-21 22:00:00 UTC')
}
]
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/14/reviews?per_page=100',
body: [
{
id: 22_449_329, body: 'Some text 1', state: 'CHANGES_REQUESTED',
author_association: 'CONTRIBUTOR', submitted_at: Time.parse('2024-08-23 15:30:00 UTC')
}
]
)
stub_github('https://api.github.com/repos/foo/foo/pulls/16/reviews?per_page=100', body: [])
fb = Factbase.new
f = fb.insert
f.what = 'pmp'
f.area = 'quality'
f.qos_days = 7
f.qos_interval = 3
Time.stub(:now, Time.parse('2024-08-09 21:00:00 UTC')) do
load_it('quality-of-service', fb)
f = fb.query('(eq what "quality-of-service")').each.to_a.first
assert_equal(Time.parse('2024-08-02 21:00:00 UTC'), f.since)
assert_equal(Time.parse('2024-08-09 21:00:00 UTC'), f.when)
assert_in_delta(431_100, f.average_review_time)
end
end

private

def stub_workflow_runs(workflow_runs)
Expand Down Expand Up @@ -594,12 +756,21 @@ def stub_workflow_runs(workflow_runs)
'q=repo:foo/foo%20type:pr%20is:merged%20closed:%3E2024-08-02',
body: {
total_count: 1, incomplete_results: false,
items: [{ id: 50, number: 12, title: 'Awesome 12' }]
items: [{ id: 50, number: 12, title: 'Awesome 12', merged_at: Time.parse('2024-08-23 18:30:00 UTC') }]
}
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12',
body: { id: 50, number: 12, additions: 12, deletions: 5, changed_files: 3 }
)
stub_github(
'https://api.github.com/repos/foo/foo/pulls/12/reviews?per_page=100',
body: [
{
id: 22_449_329, body: 'Some text 1', state: 'CHANGES_REQUESTED',
author_association: 'CONTRIBUTOR', submitted_at: Time.parse('2024-08-23 15:30:00 UTC')
}
]
)
end
end

0 comments on commit 11e3a0d

Please sign in to comment.