forked from nf-core/website
-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_issue_stats.php
378 lines (356 loc) · 16 KB
/
update_issue_stats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
//
// nfcore_issue_stats.json
// ---------------------------
// To get nice statistics like how fast we respond to issues and who
// creates and responds to issues, we query the GitHub API to get issue stats.
//
// Note that the resulting file (nfcore_issue_stats.json) is
// ignored in the .gitignore file and will not be tracked in git history.
//
// Manual usage: on command line, simply execute this script:
// $ php update_issue_stats.php
$debug = true;
$num_api_calls = 0;
$max_repos = false;
$max_comments = false;
// Allow PHP fopen to work with remote links
ini_set('allow_url_fopen', 1);
// Use same updated time for everything
$updated = time();
// Final filename to write JSON to
$results_fn = dirname(__FILE__) . '/nfcore_issue_stats.json';
$results = ['updated' => $updated];
// Load a copy of the existing JSON file, if it exists
if (file_exists($results_fn)) {
if ($debug) {
echo "Loading previous stats: $results_fn\n";
}
$results = json_decode(file_get_contents($results_fn), true);
}
$prev_updated = $results['updated'];
$results['updated'] = $updated;
// First check if we've pulled these in the past hour
$skip_issue_update = false;
// if ($updated - $prev_updated < 60 * 60) {
// $results['updated'] = $prev_updated;
// $updated = $prev_updated;
// $skip_issue_update = true;
// if ($debug) {
// echo "Skipping repo comments pull as cache is from past hour\n";
// }
// } elseif ($debug) {
// echo 'Issues results are ' . ($updated - $prev_updated) . " seconds old - pulling again\n";
// }
if (!$skip_issue_update) {
$base_stats = [
'count' => 0,
'open_count' => 0,
'comments_count' => 0,
'authors_count' => [],
'median_close_time' => 0,
'median_response_time' => 0,
];
$results['stats'][$updated]['issues'] = $base_stats;
$results['stats'][$updated]['prs'] = $base_stats;
$results['stats']['issues']['daily_opened'] = [];
$results['stats']['issues']['daily_closed'] = [];
$results['stats']['issues']['close_times'] = [];
$results['stats']['prs']['daily_opened'] = [];
$results['stats']['prs']['daily_closed'] = [];
$results['stats']['prs']['close_times'] = [];
}
// Get auth secrets
$config = parse_ini_file('config.ini');
$gh_auth = base64_encode($config['github_username'] . ':' . $config['github_access_token']);
//
//
// GitHub API calls
//
//
// HTTP header to use on GitHub API GET requests
$gh_api_opts = stream_context_create([
'http' => [
'method' => 'GET',
'header' => ['User-Agent: PHP', "Authorization: Basic $gh_auth"],
],
]);
// Load details of the pipelines
$pipelines_json = json_decode(file_get_contents(dirname(__FILE__) . '/public_html/pipelines.json'), true);
$pipelines = $pipelines_json['remote_workflows'];
// Get list of other repos
$repos = []; //parse_ini_file('ignored_repos.ini')['repos'];
foreach ($pipelines as $pipeline) {
$repos[] = $pipeline['name'];
}
// Delete cached pipelines stats for pipelines that have been deleted
if (array_key_exists('repos', $results)) {
foreach (array_keys($results['repos']) as $repo_name) {
if (!in_array($repo_name, $repos)) {
echo "\nRemoving $repo_name from the cached results as it appears to have been deleted.\n";
unset($results['repos'][$repo_name]);
}
}
}
// Get all issues for all repos
// Get the current number of organisation members
// Remember pagination!
$num_repos = 0;
foreach ($repos as $repo) {
$num_repos += 1;
// First check if we've pulled these in the past hour and skip if so
if ($skip_issue_update) {
break;
}
if (is_numeric($max_repos)) {
if ($num_repos > $max_repos) {
break;
}
}
if (!isset($results['repos'][$repo])) {
$results['repos'][$repo] = [
'issues' => [],
'prs' => [],
];
}
$gh_issues_url = 'https://api.github.com/repos/sanger-tol/' . $repo . '/issues?state=all';
$first_page = true;
$next_page = false;
while ($first_page || $next_page) {
// reset loop vars
$first_page = false;
// Get GitHub API results
if ($next_page) {
$gh_issues_url = $next_page;
}
if ($debug) {
echo "Fetching $gh_issues_url\n";
}
$num_api_calls += 1;
$gh_issues = json_decode(file_get_contents($gh_issues_url, false, $gh_api_opts), true);
if (!preg_match('/HTTP\/\d\.?\d? 200/', $http_response_header[0])) {
var_dump($http_response_header);
echo "\nCould not fetch nf-core/$repo issues! $gh_issues_url\n";
continue;
}
// Don't clobber what we already have - set keys individually
foreach ($gh_issues as $issue) {
// Set up array keys
$issue_type = isset($issue['pull_request']) ? 'prs' : 'issues';
$id = $issue['number'];
$author = $issue['user']['login'];
// Save fields of interest
$results['repos'][$repo][$issue_type][$id]['url'] = $issue['url'];
$results['repos'][$repo][$issue_type][$id]['comments_url'] = $issue['comments_url'];
$results['repos'][$repo][$issue_type][$id]['html_url'] = $issue['html_url'];
$results['repos'][$repo][$issue_type][$id]['state'] = $issue['state'];
$results['repos'][$repo][$issue_type][$id]['num_comments'] = $issue['comments'];
$results['repos'][$repo][$issue_type][$id]['created_at'] = $issue['created_at'];
$results['repos'][$repo][$issue_type][$id]['updated_at'] = $issue['updated_at'];
$results['repos'][$repo][$issue_type][$id]['closed_at'] = $issue['closed_at'];
if ($issue['closed_at']) {
$closed_wait = strtotime($issue['closed_at']) - strtotime($issue['created_at']);
$results['repos'][$repo][$issue_type][$id]['closed_wait'] = $closed_wait;
$results['stats'][$issue_type]['close_times'][] = $closed_wait;
}
$results['repos'][$repo][$issue_type][$id]['created_by'] = $author;
// Contributor stats
if (!isset($results['authors'][$author][$issue_type]['num_created'])) {
$results['authors'][$author][$issue_type]['num_created'] = 0;
}
$results['authors'][$author][$issue_type]['num_created'] += 1;
if (!isset($results['authors'][$author]['first_contribution'])) {
$results['authors'][$author]['first_contribution'] = strtotime($issue['created_at']);
}
$results['authors'][$author]['first_contribution'] = min(
$results['authors'][$author]['first_contribution'],
strtotime($issue['created_at']),
);
// Global stats
$results['stats'][$updated][$issue_type]['count'] += 1;
if ($issue['state'] == 'open') {
$results['stats'][$updated][$issue_type]['open_count'] += 1;
}
$results['stats'][$updated][$issue_type]['comments_count'] += $issue['comments'];
$results['stats'][$updated][$issue_type]['authors'][$author] = 0;
// Daily opened / closed stats
$created_at_day = date('d-m-Y', strtotime($issue['created_at']));
if (!isset($results['stats'][$issue_type]['daily_opened'][$created_at_day])) {
$results['stats'][$issue_type]['daily_opened'][$created_at_day] = 0;
}
$results['stats'][$issue_type]['daily_opened'][$created_at_day] += 1;
if ($issue['closed_at']) {
$closed_at_day = date('d-m-Y', strtotime($issue['closed_at']));
if (!isset($results['stats'][$issue_type]['daily_closed'][$closed_at_day])) {
$results['stats'][$issue_type]['daily_closed'][$closed_at_day] = 0;
}
$results['stats'][$issue_type]['daily_closed'][$created_at_day] += 1;
}
}
// Look for URL to next page of API results
$next_page = false;
$m_array = preg_grep('/rel="next"/', $http_response_header);
if (count($m_array) > 0) {
preg_match('/<([^>]+)>; rel="next"/', array_values($m_array)[0], $matches);
if (isset($matches[1])) {
$next_page = $matches[1];
}
}
}
}
// Get details of first comment for every issue
$num_calls = 0;
foreach ($repos as $repo) {
foreach (['prs', 'issues'] as $issue_type) {
if (!isset($results['repos'][$repo][$issue_type])) {
continue;
}
foreach ($results['repos'][$repo][$issue_type] as $id => $issue) {
// For developing - if $max_calls is set, check if we should bail
$num_calls += 1;
if (is_numeric($max_comments)) {
if ($num_calls > $max_comments) {
break 2; // break the outer loop too
}
}
// Skip issues with no comments
if ($issue['num_comments'] == 0) {
continue;
}
// Check we if we have pulled this already
$comments_fn = dirname(__FILE__) . '/api_cache/issue_comments/' . $repo . '/' . $id . '.json';
$fetch_json = true;
if (file_exists($comments_fn)) {
if ($debug) {
echo "Loading previous comments: $comments_fn\n";
}
$gh_comments = json_decode(file_get_contents($comments_fn), true);
// Check that there haven't been new comments since we cached this
if ($issue['num_comments'] == count($gh_comments)) {
$fetch_json = false;
} elseif ($debug) {
echo "New comments for nf-core/$repo issue #$id: now " .
htmlspecialchars($issue['num_comments'], ENT_QUOTES, 'UTF-8') .
', previously had ' .
count($gh_comments) .
"\n";
}
}
if ($fetch_json) {
$gh_comments = [];
$gh_comments_url = $issue['comments_url'];
$first_page = true;
$next_page = false;
while ($first_page || $next_page) {
$first_page = false;
if ($next_page) {
$gh_comments_url = $next_page;
}
if ($debug) {
echo "Fetching $gh_comments_url\n";
}
$num_api_calls += 1;
$gh_new_comments = json_decode(file_get_contents($gh_comments_url, false, $gh_api_opts), true);
$gh_comments = array_merge($gh_comments, $gh_new_comments);
if (!preg_match('/HTTP\/\d\.?\d? 200/', $http_response_header[0])) {
var_dump($http_response_header);
echo "\nCould not fetch nf-core/$repo issue #$id! $gh_comments_url\n";
continue;
}
// Look for URL to next page of API results
$next_page = false;
$m_array = preg_grep('/rel="next"/', $http_response_header);
if (count($m_array) > 0) {
preg_match('/<([^>]+)>; rel="next"/', array_values($m_array)[0], $matches);
if (isset($matches[1])) {
$next_page = $matches[1];
}
}
}
// Save for next time
if (!file_exists(dirname($comments_fn))) {
mkdir(dirname($comments_fn), 0777, true);
}
$gh_comments_json = json_encode($gh_comments, JSON_PRETTY_PRINT) . "\n";
file_put_contents($comments_fn, $gh_comments_json);
}
// Ok, lets go through the comments
$comments_by_created = [];
$comment_timestamps = [];
foreach ($gh_comments as $comment) {
// Only save first comment if it wasn't the issue author
$ts = strtotime($comment['created_at']);
$author = $comment['user']['login'];
if ($issue['created_by'] !== $author) {
$comment_timestamps[] = $ts;
$comments_by_created[$ts] = $comment;
}
// Contributor stats
if (!isset($results['authors'][$author][$issue_type]['num_replies'])) {
$results['authors'][$author][$issue_type]['num_replies'] = 0;
}
$results['authors'][$author][$issue_type]['num_replies'] += 1;
if (!isset($results['authors'][$author]['first_contribution'])) {
$results['authors'][$author]['first_contribution'] = strtotime($issue['created_at']);
}
$results['authors'][$author]['first_contribution'] = min(
$results['authors'][$author]['first_contribution'],
strtotime($issue['created_at']),
);
// Global stats
$results['stats'][$updated][$issue_type]['authors'][$author] = 0;
}
// Special case - the only comments are from the original issue author
if (count($comment_timestamps) == 0) {
$results['repos'][$repo][$issue_type][$id]['talking_to_myself'] = $issue['num_comments'];
} else {
$first_comment_ts = min($comment_timestamps);
$results['repos'][$repo][$issue_type][$id]['first_reply'] =
$comments_by_created[$first_comment_ts]['created_at'];
$response_wait = $first_comment_ts - strtotime($issue['created_at']);
$results['repos'][$repo][$issue_type][$id]['first_reply_wait'] = $response_wait;
$results['stats'][$issue_type]['response_times'][] = $response_wait;
// Contributor - first responder
$first_responder = $comments_by_created[$first_comment_ts]['user']['login'];
$results['repos'][$repo][$issue_type][$id]['first_reply_by'] = $first_responder;
if (!isset($results['authors'][$first_responder][$issue_type]['num_first_response'])) {
$results['authors'][$first_responder][$issue_type]['num_first_response'] = 0;
}
$results['authors'][$first_responder][$issue_type]['num_first_response'] += 1;
}
}
}
}
// Count the author stats
// NB: This will be a bit wrong if we haven't grabbed issues, as won't be counting people who created issues but haven't had a comment
if (array_key_exists('stats', $results)) {
$results['stats'][$updated]['issues']['authors_count'] = count($results['stats'][$updated]['issues']['authors']);
$results['stats'][$updated]['prs']['authors_count'] = count($results['stats'][$updated]['prs']['authors']);
unset($results['stats'][$updated]['issues']['authors']);
unset($results['stats'][$updated]['prs']['authors']);
// Calculate the median times
function array_median($arr) {
arsort($arr);
$keys = array_keys($arr);
return $arr[$keys[floor(count($keys) / 2)]];
}
$results['stats'][$updated]['issues']['median_close_time'] = array_median(
$results['stats']['issues']['close_times'],
);
$results['stats'][$updated]['issues']['median_response_time'] = array_median(
$results['stats']['issues']['response_times'],
);
$results['stats'][$updated]['prs']['median_close_time'] = array_median($results['stats']['prs']['close_times']);
$results['stats'][$updated]['prs']['median_response_time'] = array_median(
$results['stats']['prs']['response_times'],
);
}
//
//
// DONE - save results to JSON file
//
//
// Print results to a file
$results_json = json_encode($results, JSON_PRETTY_PRINT) . "\n";
file_put_contents($results_fn, $results_json);
echo "\nupdate_issue_stats done with $num_api_calls API calls - " . date('Y-m-d h:i:s') . "\n\n";