forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-misc.php
451 lines (405 loc) · 10.4 KB
/
github-misc.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
/**
* Misc functions relating to GitHub API, but
* not do not submit directly to it nor directly
* process raw HTTP results.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Given a patch-file, the function will return an
* associative array, mapping the patch-file
* to the raw committed file.
*
* In the resulting array, the keys represent every
* line in the patch (except for the "@@" lines),
* while the values represent line-number in the
* raw committed line. Some keys might point
* to empty values, in which case there is no
* relation between the two.
*
* @param string $local_git_repo Path to local git repository.
* @param string $repo_owner Owner of GitHub repository.
* @param string $repo_name Name of GitHub repository.
* @param string $github_token GitHub access token to use.
* @param string $pr_base_sha Commit-ID of base of pull request.
* @param string $commit_id Commit-ID of current commit.
* @param string $file_name File name.
*
* @return array Array, keys representing lines in patch,
* values line-number in raw committed file.
*/
function vipgoci_patch_changed_lines(
string $local_git_repo,
string $repo_owner,
string $repo_name,
string $github_token,
string $pr_base_sha,
string $commit_id,
string $file_name
): ?array {
/*
* Fetch patch for all files of the pull request
*/
$patch_arr = vipgoci_git_diffs_fetch(
$local_git_repo,
$repo_owner,
$repo_name,
$github_token,
$pr_base_sha,
$commit_id,
false,
false,
false
);
/*
* No such file found, return with error
*/
if ( ! isset(
$patch_arr['files'][ $file_name ]
) ) {
return null;
}
/*
* Get patch for the relevant file
* our caller is interested in
*/
$lines_arr = explode(
"\n",
$patch_arr['files'][ $file_name ]
);
$lines_changed = array();
$i = 1;
foreach ( $lines_arr as $line ) {
preg_match_all(
'/^@@\s+[-\+]([0-9]+,[0-9]+)\s+[-\+]([0-9]+,[0-9]+)\s+@@/',
$line,
$matches
);
if ( ! empty( $matches[0] ) ) {
$start_end = explode(
',',
$matches[2][0]
);
$i = $start_end[0];
$lines_changed[] = null;
} elseif ( empty( $matches[0] ) ) {
if ( empty( $line[0] ) ) {
// Do nothing.
continue;
} elseif (
( '-' === $line[0] ) ||
( '\\' === $line[0] )
) {
$lines_changed[] = null;
} elseif (
( '+' === $line[0] ) ||
( ' ' === $line[0] ) ||
( "\t" === $line[0] )
) {
$lines_changed[] = $i++;
}
}
}
/*
* In certain edge-cases, line 1 in the patch
* will refer to line 0 in the code, which
* is not what we want. In these cases, we
* simply hard-code line 1 in the patch to match
* with line 1 in the code.
*/
if (
( isset( $lines_changed[1] ) ) &&
(
( null === $lines_changed[1] ) ||
( 0 === $lines_changed[1] )
)
||
( ! isset( $lines_changed[1] ) )
) {
$lines_changed[1] = 1;
}
return $lines_changed;
}
/**
* Remove any draft pull requests from the array
* provided.
*
* @param array $prs_array Array to process.
*
* @return array Processed array, without draft pull requests.
*/
function vipgoci_github_pr_remove_drafts(
array $prs_array
) :array {
$prs_array = array_filter(
$prs_array,
function( $pr_item ) {
if ( true === (bool) $pr_item->draft ) {
return false;
}
return true;
}
);
return $prs_array;
}
/**
* Go through the given blame-log, and
* return only the items from the log that
* are found in $relevant_commit_ids.
*
* @param array $blame_log Array with blame log.
* @param array $relevant_commit_ids Array with relevant commit IDs.
*
* @return array Items from blame log found in $relevant_commit_ids.
*/
function vipgoci_blame_filter_commits(
$blame_log,
$relevant_commit_ids
) {
/*
* Loop through each file, get a
* 'git blame' log for the file, so
* so we can filter out issues not
* stemming from commits that are a
* part of the current pull request.
*/
$blame_log_filtered = array();
foreach ( $blame_log as $blame_log_item ) {
if ( ! in_array(
$blame_log_item['commit_id'],
$relevant_commit_ids,
true
) ) {
continue;
}
$blame_log_filtered[] = $blame_log_item;
}
return $blame_log_filtered;
}
/**
* Return ASCII-art for GitHub, which will then
* be turned into something more fancy. This is
* intended to be called when preparing messages/comments
* to be submitted to GitHub.
*
* @param string $text_string String to transform.
*
* @return string Transformed string, or empty string if invalid type of string is provided.
*/
function vipgoci_github_transform_to_emojis( $text_string ) {
switch ( strtolower( $text_string ) ) {
case VIPGOCI_ISSUE_TYPE_WARNING:
return ':warning:';
case VIPGOCI_ISSUE_TYPE_ERROR:
return ':no_entry_sign:';
case VIPGOCI_ISSUE_TYPE_INFO:
return ':information_source:';
}
return '';
}
/**
* Add pagebreak to a Markdown-style comment
* string -- but only if a pagebreak is not
* already the latest addition to the comment.
* If whitespacing is present just after the
* pagebreak, ignore it and act as if it does
* not exist.
*
* @param string $comment Comment to add pagebreak to.
* @param string $pagebreak_style Style of pagebreak.
*
* @return void
*/
function vipgoci_markdown_comment_add_pagebreak(
string &$comment,
string $pagebreak_style = '***'
) :void {
/*
* Get rid of any \n\r strings, and other
* whitespaces from $comment.
*/
$comment_copy = rtrim( $comment );
$comment_copy = rtrim( $comment_copy, " \n\r" );
/*
* If there is no comment, do not add pagebreak.
*/
if ( empty( $comment_copy ) ) {
return;
}
/*
* Find the last pagebreak in the comment.
*/
$pagebreak_location = strrpos(
$comment_copy,
$pagebreak_style
);
/*
* If pagebreak is found, and is
* at the end of the comment, bail
* out and do nothing to the comment.
*/
if (
( false !== $pagebreak_location ) &&
(
$pagebreak_location +
strlen( $pagebreak_style )
)
===
strlen( $comment_copy )
) {
return;
}
$comment .= $pagebreak_style . "\n\r";
}
/**
* Construct and return URLs to pull requests
* specified in $prs_arr
*
* @param array $prs_arr Pull requests.
* @param string $separator Separator to use between URLs.
*
* @return string URLs to pull requests.
*/
function vipgoci_github_prs_urls_get(
array $prs_arr,
string $separator = ', '
) :string {
$prs_urls = '';
foreach ( $prs_arr as $pr_item ) {
if ( ! empty( $prs_urls ) ) {
$prs_urls .= $separator;
}
$prs_urls .= $pr_item->head->repo->html_url .
'/pull/' .
rawurlencode( (string) $pr_item->number );
}
return $prs_urls;
}
/**
* Construct array of files affected -- altered, added, deleted -- by
* each pull request implicated by the commit. Will also include list
* of all files affected.
*
* @param array $options Options array for the program.
* @param string $commit_id Commit-ID of current commit.
* @param array $commit_skipped_files Information about skipped files (reference).
* @param bool $renamed_files_also If to include renamed files in results.
* @param bool $removed_files_also If to include removed files in results.
* @param bool $permission_changes_also If to include files whose permissions were changed in results.
* @param null|array $filter Filter to apply.
* @param bool $always_define_pr_number When true, will define array-key for a pull request in results even when no files are placed in the value.
*
* @return array Returns associative array with key as pull request number and value as array of affected files. Includes special key 'all' which includes all files altered by all pull requests. Example:
* Array(
* [all] => Array(
* [0] => folder1/test.php
* [1] => folder2/test2.php
* [2] => testing/file.php
* ),
* [17] => Array(
* [0] => folder1/test.php
* [1] => testing/file.php
* ),
* [20] => Array(
* [0] => folder1/test.php
* [1] => folder2/test2.php
* )
* )
*/
function vipgoci_github_files_affected_by_commit(
array $options,
string $commit_id,
array &$commit_skipped_files,
bool $renamed_files_also = false,
bool $removed_files_also = true,
bool $permission_changes_also = false,
array $filter = null,
bool $always_define_pr_number = true
) :array {
vipgoci_log(
'Fetching list of all files affected by each pull request ' .
'implicated by the commit',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'commit_id' => $options['commit'],
)
);
// Fetch list of all pull requests which the commit is a part of.
$prs_implicated = vipgoci_github_prs_implicated(
$options['repo-owner'],
$options['repo-name'],
$commit_id,
$options['token'],
$options['branches-ignore'],
$options['skip-draft-prs']
);
$pr_item_files_changed = array(
'all' => array(),
);
foreach ( $prs_implicated as $pr_item ) {
/*
* If requested, ensure that the PR is defined in the array.
*/
if (
( true === $always_define_pr_number ) &&
( ! isset( $pr_item_files_changed[ $pr_item->number ] ) )
) {
$pr_item_files_changed[ $pr_item->number ] = array();
}
/*
* Get list of all files changed
* in this pull request.
*/
$pr_item_files_tmp = vipgoci_git_diffs_fetch(
$options['local-git-repo'],
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$pr_item->base->sha,
$options['commit'],
$renamed_files_also,
$removed_files_also,
$permission_changes_also,
$filter,
);
foreach (
array_keys( $pr_item_files_tmp['files'] ) as
$pr_item_file_name
) {
/*
* Check for too long file.
*/
if (
( isset(
$commit_skipped_files[ $pr_item->number ]['issues'][ VIPGOCI_VALIDATION_MAXIMUM_LINES ]
) )
&&
( true === in_array(
$pr_item_file_name,
$commit_skipped_files[ $pr_item->number ]['issues'][ VIPGOCI_VALIDATION_MAXIMUM_LINES ],
true
) )
) {
continue;
}
/*
* Add file to arrays, if not already there.
*/
vipgoci_array_push_uniquely(
$pr_item_files_changed['all'],
$pr_item_file_name
);
if ( ! isset( $pr_item_files_changed[ $pr_item->number ] ) ) {
$pr_item_files_changed[ $pr_item->number ] = array();
}
vipgoci_array_push_uniquely(
$pr_item_files_changed[ $pr_item->number ],
$pr_item_file_name
);
}
}
return $pr_item_files_changed;
}