forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support-level-label.php
543 lines (469 loc) · 11.2 KB
/
support-level-label.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<?php
/*
* Fetch meta-data for repository from
* repo-meta API, cache the results in memory.
*/
function vipgoci_repo_meta_api_data_fetch(
$repo_meta_api_base_url,
$repo_meta_api_user_id,
$repo_meta_api_access_token,
$repo_owner,
$repo_name
) {
$cached_id = array(
__FUNCTION__, $repo_meta_api_base_url, $repo_meta_api_user_id,
$repo_meta_api_access_token, $repo_owner, $repo_name
);
$cached_data = vipgoci_cache( $cached_id );
vipgoci_log(
'Fetching repository meta-data from repo-meta API' .
vipgoci_cached_indication_str( $cached_data ),
array(
'repo_meta_api_base_url' => $repo_meta_api_base_url,
'repo_meta_api_user_id' => $repo_meta_api_user_id,
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
)
);
if ( false !== $cached_data ) {
return $cached_data;
}
$curl_retries = 0;
do {
$resp_data = false;
$resp_data_parsed = null;
$endpoint_url =
$repo_meta_api_base_url .
'/v1' .
'/sites?' .
'active=1&' .
'page=1&' .
'pagesize=20&' .
'source_repo=' . rawurlencode( $repo_owner . '/' . $repo_name );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endpoint_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt(
$ch,
CURLOPT_USERAGENT,
VIPGOCI_CLIENT_ID
);
$endpoint_send_headers = array(
);
if ( ! empty( $repo_meta_api_user_id ) ) {
$endpoint_send_headers[] =
'API-User-ID: ' . $repo_meta_api_user_id;
}
if ( ! empty( $repo_meta_api_access_token ) ) {
$endpoint_send_headers[] =
'Access-Token: ' . $repo_meta_api_access_token;
}
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
$endpoint_send_headers
);
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
'vipgoci_curl_headers'
);
vipgoci_curl_set_security_options(
$ch
);
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'repo_meta_data_endpoint_api_request' );
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'repo_meta_data_endpoint_api'
);
$resp_data = curl_exec( $ch );
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'repo_meta_data_endpoint_api_request' );
if ( false !== $resp_data ) {
$resp_data_parsed = json_decode(
$resp_data,
true
);
}
$resp_headers = vipgoci_curl_headers(
null,
null
);
if (
( false === $resp_data ) ||
( null === $resp_data_parsed ) ||
(
( isset($resp_data_parsed['status'] ) ) &&
( 'error' === $resp_data_parsed['status'] )
)
) {
vipgoci_log(
'Failed fetching or parsing data...',
array(
'resp_data' => $resp_data,
'resp_data_parsed' => $resp_data_parsed,
'curl_error' => curl_error( $ch ),
'http_status' => (
isset($resp_headers['status']) ?
$resp_headers['status'] :
null
)
)
);
/*
* For the while() below
*/
if ( ! isset( $resp_data_parsed['status'] ) ) {
$resp_data = false;
}
}
curl_close( $ch );
} while (
( false === $resp_data ) &&
( $curl_retries++ < 2 )
);
vipgoci_cache(
$cached_id,
$resp_data_parsed
);
return $resp_data_parsed;
}
/*
* Fetch data from repo-meta API, then try
* to match fields and their values with
* the data. The fields and values are those
* found in a particular $option parameter
* specified as an argument here ($option_name).
*
* If there is a match, return true. Otherwise,
* return false.
*/
function vipgoci_repo_meta_api_data_match(
$options,
$option_name,
&$option_no_match
) {
if (
( empty( $option_name ) ) ||
( empty( $options['repo-meta-api-base-url'] ) ) ||
( empty( $options[ $option_name ] ) )
) {
vipgoci_log(
'Not attempting to match repo-meta API field-value ' .
'to a criteria due to invalid configuration',
array(
'option_name'
=> $option_name,
'repo_meta_api_base_url'
=> isset( $options['repo-meta-api-base-url'] ) ?
$options['repo-meta-api-base-url'] : '',
'repo_meta_match'
=> ( ( ! empty( $option_name ) ) && ( isset( $options[ $option_name ] ) ) ) ?
$options[ $option_name ] : '',
)
);
return false;
}
else {
vipgoci_log(
'Attempting to match repo-meta API field-value to a criteria',
array(
'option_name' => $option_name,
'repo_meta_match' => $options[ $option_name ],
'repo_meta_api_base_url' => $options['repo-meta-api-base-url'],
)
);
}
$repo_meta_data = vipgoci_repo_meta_api_data_fetch(
$options['repo-meta-api-base-url'],
$options['repo-meta-api-user-id'],
$options['repo-meta-api-access-token'],
$options['repo-owner'],
$options['repo-name']
);
if (
( empty(
$repo_meta_data['data']
) )
||
( 'error' === $repo_meta_data['status'] )
) {
return false;
}
/*
* Loop through possible match in the
* option array -- bail out once we
* find a match.
*/
foreach(
array_keys( $options[ $option_name ] ) as
$option_name_key_no
) {
$found_fields = vipgoci_find_fields_in_array(
$options[ $option_name ][ $option_name_key_no ],
$repo_meta_data['data']
);
/*
* If we find one data-item that had
* all fields matching the criteria given,
* we return true.
*/
$ret_val = false;
foreach(
$found_fields as
$found_field_item_key => $found_field_item_value
) {
if ( $found_field_item_value === true ) {
$ret_val = true;
}
}
if ( true === $ret_val ) {
$option_no_match = $option_name_key_no;
break;
}
}
vipgoci_log(
'Repo-meta API matching returning',
array(
'found_fields_in_repo_meta_data' => $found_fields,
'repo_meta_data_item_cnt' => count( $repo_meta_data['data'] ),
'ret_val' => $ret_val,
'option_no_match' => $option_no_match,
)
);
return $ret_val;
}
/*
* Attach support level label to
* Pull-Requests, if configured to
* do so. Will fetch information
* about support-level from an API.
*/
function vipgoci_support_level_label_set(
$options
) {
if ( true !== $options['set-support-level-label'] ) {
vipgoci_log(
'Not attaching support label to Pull-Requests ' .
'implicated by commit, as not configured ' .
'to do so',
array(
'set_support_level_label'
=> $options['set-support-level-label']
)
);
return false;
}
vipgoci_log(
'Attaching support-level label to Pull-Requests implicated by commit',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'commit' => $options['commit'],
'repo_meta_api_base_url' =>
( ! empty( $options['repo-meta-api-base-url'] ) ?
$options['repo-meta-api-base-url'] : '' ),
),
0,
true
);
if (
( empty( $options['repo-meta-api-base-url'] ) )
) {
vipgoci_log(
'Missing configuration for repo-meta API, skipping'
);
return false;
}
/*
* Get information from API about the
* repository, including support level.
*/
$repo_meta_data = vipgoci_repo_meta_api_data_fetch(
$options['repo-meta-api-base-url'],
$options['repo-meta-api-user-id'],
$options['repo-meta-api-access-token'],
$options['repo-owner'],
$options['repo-name']
);
/*
* Construct support-level label
* from information found in API,
* if available.
*/
if ( ! empty( $options['set-support-level-label-prefix'] ) ) {
$support_label_prefix = $options['set-support-level-label-prefix'];
}
else {
$support_label_prefix = '[Support Level]';
}
$support_label_from_api = '';
if (
( ! empty(
$repo_meta_data['data']
) )
&&
( ! empty(
$repo_meta_data['data'][0]['support_tier']
) )
) {
/*
* Construct the label itself
* from prefix and support level
* found in API.
*/
$support_label_from_api =
$support_label_prefix .
' ' .
ucfirst( strtolower(
$repo_meta_data['data'][0]['support_tier']
) );
}
/*
* No support label found in API, so
* do not do anything.
*/
if ( empty( $support_label_from_api ) ) {
vipgoci_log(
'Found no valid support level in repo-meta API, so not ' .
'attaching any label (nor removing)',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'repo_meta_api_base_url' => $options['repo-meta-api-base-url'],
'support_tier' => (
isset( $repo_meta_data['data'][0]['support_tier'] ) ?
$repo_meta_data['data'][0]['support_tier'] :
''
),
)
);
return false;
}
else {
vipgoci_log(
'Found valid support level in API, making alterations as needed',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'repo_meta_api_base_url' => $options['repo-meta-api-base-url'],
'support_tier' => (
isset( $repo_meta_data['data'][0]['support_tier'] ) ?
$repo_meta_data['data'][0]['support_tier'] :
''
),
'support_label_from_api' => $support_label_from_api,
)
);
}
/*
* Get Pull-Requests associated with the
* commit and repository.
*/
$prs_implicated = vipgoci_github_prs_implicated(
$options['repo-owner'],
$options['repo-name'],
$options['commit'],
$options['token'],
$options['branches-ignore'],
$options['skip-draft-prs']
);
/*
* Loop through each Pull-Request,
* remove any invalid support levels
* and add a correct one.
*
* If everything is correct, will not
* make any alterations.
*/
foreach ( $prs_implicated as $pr_item ) {
$pr_correct_support_label_found = false;
/*
* Get labels for PR.
*/
$pr_item_labels =
vipgoci_github_pr_labels_get(
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$pr_item->number
);
/*
* If no found, substitute boolean for empty array.
*/
if ( false === $pr_item_labels ) {
$pr_item_labels = array();
}
/*
* Loop through each label found for
* Pull-Request, figure out if is support
* label, remove if not the same as is supposed
* to be set.
*/
foreach(
$pr_item_labels as $pr_item_label
) {
if ( strpos(
$pr_item_label->name,
$support_label_prefix . ' '
) !== 0 ) {
/*
* Not support level
* label, skip.
*/
continue;
}
if ( $pr_item_label->name === $support_label_from_api ) {
$pr_correct_support_label_found = true;
/*
* We found correct support label
* associated, note that for later
* use.
*/
continue;
}
/*
* All conditions met; is support level
* label, but incorrect one, so remove label.
*/
vipgoci_github_pr_label_remove(
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$pr_item->number,
$pr_item_label->name,
false
);
}
/*
* Add support label if found in API but
* not if correct one is associated on
* GitHub already.
*/
if ( $pr_correct_support_label_found === true ) {
vipgoci_log(
'Correct support label already attached to Pull-Request, skipping',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'support_label_from_api' => $support_label_from_api,
)
);
}
/*
* A support label was found in API and
* a correct one was not associated on
* GitHub already, so add one.
*/
else if ( $pr_correct_support_label_found === false ) {
vipgoci_github_label_add_to_pr(
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$pr_item->number,
$support_label_from_api
);
}
}
return true;
}