forked from sbulen/sjrbTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_dump.php
282 lines (240 loc) · 6.95 KB
/
github_dump.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
<?php
//
// A utility to dump github open issue & pr information.
//
// Usage guidelines:
// (1) Update User Configuration section below with info on your repo & user credentials.
// (2) Run it.
// (3) github_dump.csv will be in the directory where it was launched.
// by sbulen
//
//*** User Configuration
$owner = 'SimpleMachines';
$repo = 'SMF2.1';
$user = 'xxx';
$pwd = 'xxx';
//*** End User Configuration
//*** Main program
doStartup();
getInfo();
cleanUnusedColumns();
mapIssues();
exportInfo();
doWrapUp();
return;
//*** Startup
function doStartup() {
global $owner, $repo;
// Without this header, flushes don't work...
header( 'Content-type: text/html; charset=utf-8' );
echo("<br>***********************************<br>");
echo("******** Github export utility **********<br>");
echo("***********************************<br><br>");
echo("Owner: " . $owner . "<br>");
echo("Repository: " . $repo . "<br>");
// Yes, both flushes necessary
@ob_flush();
@flush();
return;
}
//*** Get the full set of info from Github
function getInfo() {
global $githubAll, $user, $pwd, $owner, $repo, $gh_calls_remaining, $gh_calls_reset;
// startup curl
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_VERBOSE => true,
CURLOPT_USERAGENT => $user,
CURLOPT_USERPWD => $pwd,
CURLOPT_HEADERFUNCTION =>
function($curl, $header) use (&$gha_header)
{
$len = strlen($header);
// Split header into field & val
$pos = strpos($header, ':');
if ($pos === false) {
$field = trim($header);
$val = '';
}
else {
$field = trim(substr($header, 0, $pos));
$val = trim(substr($header, $pos + 1));
}
// Discard that empty row...
if (empty($field))
return $len;
// Make this one readable, translate from unix timestamp...
if ($field == 'X-RateLimit-Reset')
$val = gmdate('M d Y H:i:s', $val) . ' GMT';
// Populate gha_header. Allow for multiples, e.g., happens with Vary
if (!array_key_exists($field, $gha_header))
$gha_header[$field] = $val;
else
$gha_header[$field] .= ', ' . $val;
return $len;
},
));
// Loop thru all the pages - 100 rows at a time
$githubAll = array();
$more = true;
$page = 0;
while ($more) {
$more = false;
$page++;
// Init github API Header prior to each call
$gha_header = array();
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/' . $owner . '/' . $repo . '/issues?per_page=100&page=' . $page);
$githubAll_json = curl_exec($ch);
curlErr($ch);
// Save rate limit info
if (isset($gha_header['X-RateLimit-Remaining']))
$gh_calls_remaining = $gha_header['X-RateLimit-Remaining'];
if (isset($gha_header['X-RateLimit-Reset']))
$gh_calls_reset = $gha_header['X-RateLimit-Reset'];
// Check status accessing repos
if ($gha_header['Status'] != '200 OK') {
if (isset($gh_calls_remaining))
echo '<br>X-RateLimit-Remaining: ' . $gh_calls_remaining . '<br>';
if (isset($gh_calls_reset))
echo 'X-RateLimit-Reset: ' . $gh_calls_reset . '<br>';
die('<br>Error accessing Github repository: ' . $gha_header['Status'] . '<br>');
}
// if next page link exists, there is more data to get & you have calls available to do so...
if (!empty($gha_header['Link']) && strpos($gha_header['Link'], 'rel="next"') && !empty($gh_calls_reset))
$more = true;
// If successful response, dump it into an array
if ($githubAll_json !== false)
$githubAll = array_merge($githubAll, json_decode($githubAll_json, true));
}
curl_close($ch);
return;
}
//*** Way too much stuff, strip most of it...
function cleanUnusedColumns() {
global $githubAll;
// Put the header row in there...
$githubTemp[0] = array(
'type',
'number',
'title',
'login',
'labels',
'assignees',
'milestone',
'comments',
'fixes issues',
'issue milestones',
'body',
'created_at',
);
foreach($githubAll as $row) {
$githubTemp[$row['number']] = array(
empty($row['pull_request']) ? 'Issue' : 'PR',
$row['number'],
$row['title'],
$row['user']['login'],
col2csv($row['labels'], 'name'),
col2csv($row['assignees'], 'login'),
$row['milestone']['title'],
$row['comments'],
'', // populated later
'', // populated later
$row['body'],
substr($row['created_at'],0,10),
);
}
$githubAll = $githubTemp;
return;
}
//*** Pluck a column out of array into a comma delimited string
function col2csv($labels, $col) {
$values = array_column($labels, $col);
$lstring = implode(', ', $values);
return $lstring;
}
//*** Find issues and issue milestones associated with PRs
function mapIssues() {
global $githubAll;
$pattern = '/(\/|#)(\d{1,8})/';
// check whole table
foreach($githubAll as $ix => $row) {
//Look at each PR...
if ($row[0] == 'PR') {
// allow for multiple matches of #9999 or /9999 (when folks use links) in body of issue (10th field)
preg_match_all($pattern, $row[10], $matches);
foreach ($matches[2] AS $match) {
// finally look for active issue entries
if (array_key_exists($match, $githubAll) && $githubAll[$match][0] == 'Issue') {
// add issue number
if (empty($githubAll[$ix][8]))
$githubAll[$ix][8] = $githubAll[$match][1];
else
$githubAll[$ix][8] .= ', ' . $githubAll[$match][1];
// add milestone info
if (!empty($githubAll[$match][6])) {
if (empty($githubAll[$ix][9]))
$githubAll[$ix][9] = $githubAll[$match][6];
else
$githubAll[$ix][9] .= ', ' . $githubAll[$match][6];
}
}
}
}
}
// Done with the message body - delete it
foreach($githubAll AS $ix => $row)
unset($githubAll[$ix][10]);
return;
}
// Dump to screen and to .csv
function exportInfo() {
global $githubAll;
dumpTable($githubAll);
$fp = fopen('github_dump.csv', 'w');
if ($fp === false)
die ("Cannot open github_dump.csv");
foreach($githubAll AS $row)
fputcsv($fp, $row);
fclose($fp);
return;
}
//*** Check for curl error
function curlErr($ch) {
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
die("cURL error ({$errno}):\n {$error_message}");
}
return;
}
//*** Wrap Up
function doWrapUp() {
global $gh_calls_remaining, $gh_calls_reset;
echo "<br>Completed!<br><br>";
if (isset($gh_calls_remaining))
echo 'X-RateLimit-Remaining: ' . $gh_calls_remaining . '<br>';
if (isset($gh_calls_reset))
echo 'X-RateLimit-Reset: ' . $gh_calls_reset . '<br><br>';
// Yes, both flushes necessary
@ob_flush();
@flush();
return;
}
//*** Dump Table
// Takes a simple 2 dimensional array & dumps it in table format
function dumpTable($passedArray) {
echo '<br><table border="1" cellpadding="3" frame="border" rules="all">';
foreach($passedArray as $row) {
echo '<tr><td>';
echo implode('</td><td>', $row);
echo '</td></tr>';
}
echo '</table><br>';
@ob_flush();
@flush();
return;
}