23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/github_repo_statistics/review_report.rb', line 23
def report
client = Octokit::Client.new(access_token: @token)
client.auto_paginate = true
pull_requests = client.list_issues(@repo, state: 'closed', since: DateTime.now - @duration_in_days)
pull_requests_by_week = pull_requests.group_by { |pr| pr[:closed_at].strftime('%Y-%W') }
weeks = @duration_in_days / 7
pull_requests_by_week.keys.sort[-weeks..].each do |week|
total_time_seconds = 0
total_count = 0
total_count_calc = 0
total_reviews = 0
= 0
change_requested_reviews = 0
pull_requests_by_week[week].each do |issue|
pull_request = client.pull_request(@repo, issue.number)
last_commit_sha = pull_request[:head][:sha]
last_commit = client.commit(@repo, last_commit_sha)
reviews = []
review_page = 1
loop do
response = client.pull_request_reviews(@repo, pull_request[:number], per_page: 100,
page: review_page)
break if response.empty?
reviews.concat(response)
review_page += 1
end
total_reviews += reviews.size
next unless reviews.any?
last_review = reviews.max_by { |review| review[:submitted_at] }
time_taken_seconds = (last_review[:submitted_at] - last_commit[:commit][:author][:date]).abs
if time_taken_seconds / 3600.0 < 8 && time_taken_seconds / 3600.0 > 0.3
total_time_seconds += time_taken_seconds
total_count_calc += 1
end
total_count += 1
+= reviews.count do |review|
= client.(@repo, pull_request[:number], review[:id])
.any?
end
change_requested_reviews += reviews.count { |review| review[:state] == 'CHANGES_REQUESTED' }
end
if total_count.positive?
average_time_hours = total_time_seconds / total_count_calc / 3600.0
puts "Calendar week #{week}: Average time taken for #{total_count} pull requests is #{average_time_hours.round(2)} hours."
puts " Total reviews: #{total_reviews}"
puts " Reviews with comments: #{reviews_with_comments}"
puts " Change requested reviews: #{change_requested_reviews}"
if ENV['BQ_CREDENTIALS']
require 'google/cloud/bigquery'
require 'json'
creds = JSON.parse(ENV['BQ_CREDENTIALS'])
bigquery = Google::Cloud::Bigquery.new(
project_id: 'hellofresh-android',
credentials: creds
)
dataset = bigquery.dataset 'github_data'
query = " MERGE INTO pr_reviews AS target\n USING (SELECT '\#{week}' AS calendar_week, '\#{@repo}' AS platform) AS source\n ON target.calendar_week = source.calendar_week AND target.platform = source.platform\n WHEN MATCHED THEN\n UPDATE SET\n target.change_requested_reviews = \#{change_requested_reviews},\n target.reviews_with_comments = \#{reviews_with_comments},\n target.total_reviews = \#{total_reviews},\n target.average_review_time_hours = \#{average_time_hours.round(2)},\n target.total_prs = \#{total_count},\n target.platform = '\#{@repo}'\n WHEN NOT MATCHED THEN\n INSERT (calendar_week, total_prs, average_review_time_hours, total_reviews, reviews_with_comments, change_requested_reviews, platform)\n VALUES ('\#{week}', \#{total_count}, \#{average_time_hours.round(2)}, \#{total_reviews}, \#{reviews_with_comments}, \#{change_requested_reviews}, '\#{@repo}');\n SQL\n\n dataset.query(query)\n end\n else\n puts \"\#{week}: No pull requests with reviews.\"\n end\n end\nend\n"
|