forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vip-go-ci.php
executable file
·1958 lines (1696 loc) · 51.2 KB
/
vip-go-ci.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/php
<?php
require_once( __DIR__ . '/defines.php' );
require_once( __DIR__ . '/github-api.php' );
require_once( __DIR__ . '/git-repo.php' );
require_once( __DIR__ . '/misc.php' );
require_once( __DIR__ . '/results.php' );
require_once( __DIR__ . '/options.php' ) ;
require_once( __DIR__ . '/statistics.php' );
require_once( __DIR__ . '/phpcs-scan.php' );
require_once( __DIR__ . '/lint-scan.php' );
require_once( __DIR__ . '/auto-approval.php' );
require_once( __DIR__ . '/ap-file-types.php' );
require_once( __DIR__ . '/ap-nonfunctional-changes.php' );
require_once( __DIR__ . '/ap-hashes-api.php' );
require_once( __DIR__ . '/ap-svg-files.php' );
require_once( __DIR__ . '/svg-scan.php' );
require_once( __DIR__ . '/other-web-services.php' );
require_once( __DIR__ . '/support-level-label.php' );
/**
* Determine exit status.
*
* If any 'error'-type issues were submitted to
* GitHub we announce a failure to our parent-process
* by returning with a non-zero exit-code.
*
* If we only submitted warnings, we do not announce failure.
*/
function vipgoci_exit_status( $results ) {
foreach (
array_keys(
$results['stats']
)
as $stats_type
) {
if (
! isset( $results['stats'][ $stats_type ] ) ||
null === $results['stats'][ $stats_type ]
) {
/* In case the type of scan was not performed, skip */
continue;
}
foreach (
array_keys(
$results['stats'][ $stats_type ]
)
as $pr_number
) {
if (
0 !== $results['stats']
[ $stats_type ]
[ $pr_number ]
['error']
) {
// Some errors were found, return non-zero
return 250;
}
}
}
return 0;
}
/**
* Main invocation function.
*
* @codeCoverageIgnore
*/
function vipgoci_run() {
global $argv;
global $vipgoci_debug_level;
/*
* Clear the internal
* cache before doing anything.
*/
vipgoci_cache(
VIPGOCI_CACHE_CLEAR
);
$hashes_oauth_arguments =
array(
'hashes-oauth-token',
'hashes-oauth-token-secret',
'hashes-oauth-consumer-key',
'hashes-oauth-consumer-secret'
);
vipgoci_log(
'Initializing...',
array(
'debug_info' => array(
'vipgoci_version' => VIPGOCI_VERSION,
'php_version' => phpversion(),
'hostname' => gethostname(),
'php_uname' => php_uname(),
)
)
);
/*
* Refuse to run as root.
*/
if ( 0 === posix_getuid() ) {
vipgoci_sysexit(
'Will not run as root. Please run as non-privileged user.',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
/*
* Set how to deal with errors:
* Report all errors, and display them.
*/
ini_set( 'error_log', '' );
error_reporting( E_ALL );
ini_set( 'display_errors', 'on' );
// Set with a temp value for now, user value set later
$vipgoci_debug_level = 0;
$startup_time = time();
$options_recognized =
array(
'env-options:',
'repo-owner:',
'repo-name:',
'commit:',
'token:',
'results-comments-sort:',
'review-comments-max:',
'review-comments-total-max:',
'review-comments-ignore:',
'dismiss-stale-reviews:',
'dismissed-reviews-repost-comments:',
'dismissed-reviews-exclude-reviews-from-team:',
'branches-ignore:',
'output:',
'dry-run:',
'informational-url:',
'post-generic-pr-support-comments:',
'post-generic-pr-support-comments-on-drafts:',
'post-generic-pr-support-comments-string:',
'post-generic-pr-support-comments-branches:',
'post-generic-pr-support-comments-repo-meta-match:',
'set-support-level-label:',
'set-support-level-label-prefix:',
'repo-meta-api-base-url:',
'repo-meta-api-user-id:',
'repo-meta-api-access-token:',
'phpcs-path:',
'phpcs-standard:',
'phpcs-severity:',
'phpcs-sniffs-include:',
'phpcs-sniffs-exclude:',
'phpcs-runtime-set:',
'phpcs-skip-scanning-via-labels-allowed:',
'phpcs-skip-folders:',
'phpcs-skip-folders-in-repo-options-file:',
'repo-options:',
'repo-options-allowed:',
'hashes-api-url:',
'hashes-oauth-token:',
'hashes-oauth-token-secret:',
'hashes-oauth-consumer-key:',
'hashes-oauth-consumer-secret:',
'irc-api-url:',
'irc-api-token:',
'irc-api-bot:',
'irc-api-room:',
'pixel-api-url:',
'pixel-api-groupprefix:',
'php-path:',
'local-git-repo:',
'lint:',
'lint-skip-folders:',
'lint-skip-folders-in-repo-options-file:',
'phpcs:',
'svg-checks:',
'svg-scanner-path:',
'autoapprove:',
'autoapprove-filetypes:',
'autoapprove-label:',
'autoapprove-php-nonfunctional-changes:',
'help',
'debug-level:',
'hashes-api:',
);
/*
* Try to read options from command-line parameters.
*/
$options = getopt(
null,
$options_recognized
);
/*
* Try to read configuration from
* environmental variables.
*/
vipgoci_option_array_handle(
$options,
'env-options',
array(),
null,
',',
false
);
vipgoci_options_read_env(
$options,
$options_recognized
);
// Validate args
if (
! isset( $options['repo-owner'] ) ||
! isset( $options['repo-name'] ) ||
! isset( $options['commit'] ) ||
! isset( $options['token'] ) ||
! isset( $options['local-git-repo']) ||
isset( $options['help'] )
) {
print 'Usage: ' . $argv[0] . PHP_EOL .
"\t" . 'Options --repo-owner, --repo-name, --commit, --token, --local-git-repo, --phpcs-path are ' . PHP_EOL .
"\t" . 'mandatory, while others are optional.' . PHP_EOL .
PHP_EOL .
"\t" . 'Note that if option --autoapprove is specified, --autoapprove-label needs to' . PHP_EOL .
"\t" . 'be specified as well.' . PHP_EOL .
PHP_EOL .
"\t" . '--repo-owner=STRING Specify repository owner, can be an organization' . PHP_EOL .
"\t" . '--repo-name=STRING Specify name of the repository' . PHP_EOL .
"\t" . '--commit=STRING Specify the exact commit to scan (SHA)' . PHP_EOL .
"\t" . '--token=STRING The access-token to use to communicate with GitHub' . PHP_EOL .
"\t" . '--results-comments-sort=BOOL Sort issues found according to severity, from high ' . PHP_EOL .
"\t" . ' to low, before submitting to GitHub. Not sorted by default.' . PHP_EOL .
"\t" . '--review-comments-max=NUMBER Maximum number of inline comments to submit' . PHP_EOL .
"\t" . ' to GitHub in one review. If the number of ' . PHP_EOL .
"\t" . ' comments exceed this number, additional reviews ' . PHP_EOL .
"\t" . ' will be submitted.' . PHP_EOL .
"\t" . '--review-comments-total-max=NUMBER Maximum number of inline comments submitted to' . PHP_EOL .
"\t" . ' a single Pull-Request by the program -- includes' . PHP_EOL .
"\t" . ' comments from previous executions. A value of ' . PHP_EOL .
"\t" . ' \'0\' indicates no limit.' . PHP_EOL .
"\t" . '--review-comments-ignore=STRING Specify which result comments to ignore' . PHP_EOL .
"\t" . ' -- e.g. useful if one type of message is to be ignored' . PHP_EOL .
"\t" . ' rather than a whole PHPCS sniff. Should be a ' . PHP_EOL .
"\t" . ' whole string with items separated by \"|||\".' . PHP_EOL .
"\t" . '--dismiss-stale-reviews=BOOL Dismiss any reviews associated with Pull-Requests ' . PHP_EOL .
"\t" . ' that we process which have no active comments. ' . PHP_EOL .
"\t" . ' The Pull-Requests we process are those associated ' . PHP_EOL .
"\t" . ' with the commit specified.' . PHP_EOL .
"\t" . '--dismissed-reviews-repost-comments=BOOL When avoiding double-posting comments,' . PHP_EOL .
"\t" . ' do not take into consideration comments ' . PHP_EOL .
"\t" . ' posted against reviews that have now been ' . PHP_EOL .
"\t" . ' dismissed. Setting this to true entails ' . PHP_EOL .
"\t" . ' that comments from dismissed reviews will ' . PHP_EOL .
"\t" . ' be posted again, should the underlying issue ' . PHP_EOL .
"\t" . ' be detected during the run.' . PHP_EOL .
"\t" . '--dismissed-reviews-exclude-reviews-from-team=STRING With this parameter set, ' . PHP_EOL .
"\t" . ' comments that are part of reviews ' . PHP_EOL .
"\t" . ' dismissed by members of the teams specified, ' . PHP_EOL .
"\t" . ' would be taken into consideration when ' . PHP_EOL .
"\t" . ' avoiding double-posting; they would be ' . PHP_EOL .
"\t" . ' excluded. Note that this parameter ' . PHP_EOL .
"\t" . ' only works in conjunction with ' . PHP_EOL .
"\t" . ' --dismissed-reviews-repost-comments' . PHP_EOL .
"\t" . '--informational-url=STRING URL to documentation on what this bot does. Should ' . PHP_EOL .
"\t" . ' start with https:// or https:// ' . PHP_EOL .
PHP_EOL .
"\t" . '--post-generic-pr-support-comments=BOOL Whether to post generic comment to Pull-Requests ' . PHP_EOL .
"\t" . ' with support-related information for users. Will ' . PHP_EOL .
"\t" . ' be posted only once per Pull-Request. ' . PHP_EOL .
"\t" . '--post-generic-pr-support-comments-string=STRING String to use when posting support-comment. ' . PHP_EOL .
"\t" . '--post-generic-pr-support-comments-branches=ARRAY Only post support-comments to Pull-Requests ' . PHP_EOL .
"\t" . ' with the target branches specified. The ' . PHP_EOL .
"\t" . ' parameter can be a string with one value, or ' . PHP_EOL .
"\t" . ' comma separated. A single "any" value will ' . PHP_EOL .
"\t" . ' cause the message to be posted to any ' . PHP_EOL .
"\t" . ' branch.' . PHP_EOL .
"\t" . '--post-generic-pr-support-comments-repo-meta-match=ARRAY Only post generic support ' . PHP_EOL .
"\t" . ' messages when data from repo-meta API' . PHP_EOL .
"\t" . ' matches the criteria specified here. ' . PHP_EOL .
"\t" . ' See README.md for usage. ' . PHP_EOL .
PHP_EOL .
"\t" . '--set-support-level-label=BOOL Whether to attach support level labels to Pull-Requests. ' . PHP_EOL .
"\t" . ' Will fetch information on support levels from repo-meta API. ' . PHP_EOL .
"\t" . '--set-support-level-label-prefix=STRING Prefix to use for support level labels. Should be longer than five letters.' . PHP_EOL .
"\t" . '--repo-meta-api-base-url=STRING Base URL to repo-meta API, containing support level and other ' . PHP_EOL .
"\t" . ' information. ' . PHP_EOL .
"\t" . '--repo-meta-api-user-id=STRING Authentication detail for the repo-meta API. ' . PHP_EOL .
"\t" . '--repo-meta-api-access-token=STRING Access token for the repo-meta API. ' . PHP_EOL .
PHP_EOL .
"\t" . '--phpcs=BOOL Whether to run PHPCS (true/false)' . PHP_EOL .
"\t" . '--phpcs-path=FILE Full path to PHPCS script' . PHP_EOL .
"\t" . '--phpcs-standard=STRING Specify which PHPCS standard to use' . PHP_EOL .
"\t" . '--phpcs-severity=NUMBER Specify severity for PHPCS' . PHP_EOL .
"\t" . '--phpcs-sniffs-include=ARRAY Specify which sniffs to include when PHPCS scanning, ' . PHP_EOL .
"\t" . ' should be an array with items separated by commas. ' . PHP_EOL .
"\t" . '--phpcs-sniffs-exclude=ARRAY Specify which sniffs to exclude from PHPCS scanning, ' . PHP_EOL .
"\t" . ' should be an array with items separated by commas. ' . PHP_EOL .
"\t" . '--phpcs-runtime-set=STRING Specify --runtime-set values passed on to PHPCS' . PHP_EOL .
"\t" . ' -- expected to be a comma-separated value string of ' . PHP_EOL .
"\t" . ' key-value pairs.' . PHP_EOL .
"\t" . ' For example: --phpcs-runtime-set="foo1 bar1, foo2,bar2"' . PHP_EOL .
"\t" . '--phpcs-skip-scanning-via-labels-allowed=BOOL Whether to allow users to skip ' . PHP_EOL .
"\t" . ' PHPCS scanning of Pull-Requests ' . PHP_EOL .
"\t" . ' via labels attached to them. ' . PHP_EOL .
"\t" . ' The labels should be named "skip-phpcs-scan".' . PHP_EOL .
"\t" . '--phpcs-skip-folders=STRING Specify folders relative to root of the git repository in which ' . PHP_EOL .
"\t" . ' files are not to be scanned using PHPCS. Values are comma' . PHP_EOL .
"\t" . ' separated' . PHP_EOL .
"\t" . '--phpcs-skip-folders-in-repo-options-file=BOOL Allows folders that are not to be PHPCS ' . PHP_EOL .
"\t" . ' scanned to be specified in file in root of ' . PHP_EOL .
"\t" . ' repository (.vipgoci_phpcs_skip_folders).' . PHP_EOL .
"\t" . ' Folders should be separated by newlines.' . PHP_EOL .
PHP_EOL .
"\t" . '--autoapprove=BOOL Whether to auto-approve Pull-Requests' . PHP_EOL .
"\t" . ' altering only files of certain types or ' . PHP_EOL .
"\t" . ' already approved files. ' . PHP_EOL .
"\t" . '--autoapprove-filetypes=STRING Specify what file-types can be auto-' . PHP_EOL .
"\t" . ' approved. PHP files cannot be specified.' . PHP_EOL .
"\t" . '--autoapprove-php-nonfunctional-changes=BOOL For autoapprovals, also consider ' . PHP_EOL .
"\t" . ' PHP files approved that contain ' . PHP_EOL .
"\t" . ' non-functional changes, such as ' . PHP_EOL .
"\t" . ' whitespacing and comments alterations. ' . PHP_EOL .
"\t" . '--autoapprove-label=STRING String to use for labels when auto-approving' . PHP_EOL .
"\t" . '--php-path=FILE Full path to PHP, if not specified the' . PHP_EOL .
"\t" . ' default in $PATH will be used instead' . PHP_EOL .
"\t" . '--svg-checks=BOOL Enable or disable SVG checks, both' . PHP_EOL .
"\t" . ' auto-approval of SVG files and problem' . PHP_EOL .
"\t" . ' checking of these files. Note that if' . PHP_EOL .
"\t" . ' auto-approvals are turned off globally, no' . PHP_EOL .
"\t" . ' auto-approval is performed for SVG files.' . PHP_EOL .
"\t" . '--svg-scanner-path=FILE Path to SVG scanning tool. Should return' . PHP_EOL .
"\t" . ' similar output as PHPCS. ' . PHP_EOL .
"\t" . '--hashes-api=BOOL Whether to do hashes-to-hashes API verfication ' . PHP_EOL .
"\t" . ' with individual PHP files found to be altered ' . PHP_EOL .
"\t" . ' in the branch specified' . PHP_EOL .
"\t" . '--hashes-api-url=STRING URL to hashes-to-hashes HTTP API root' . PHP_EOL .
"\t" . ' -- note that it should not include any specific' . PHP_EOL .
"\t" . ' paths to individual parts of the API.' . PHP_EOL .
PHP_EOL .
"\t" . '--hashes-oauth-token=STRING, --hashes-oauth-token-secret=STRING, ' . PHP_EOL .
"\t" . '--hashes-oauth-consumer-key=STRING, --hashes-oauth-consumer-secret=STRING ' . PHP_EOL .
"\t" . ' OAuth 1.0 token, token secret, consumer key and ' . PHP_EOL .
"\t" . ' consumer secret needed for hashes-to-hashes HTTP requests' . PHP_EOL .
"\t" . ' All required for hashes-to-hashes requests.' . PHP_EOL .
PHP_EOL .
"\t" . '--irc-api-url=STRING URL to IRC API to send alerts' . PHP_EOL .
"\t" . '--irc-api-token=STRING Access-token to use to communicate with the IRC ' . PHP_EOL .
"\t" . ' API' . PHP_EOL .
"\t" . '--irc-api-bot=STRING Name for the bot which is supposed to send the IRC ' .PHP_EOL .
"\t" . ' messages.' . PHP_EOL .
"\t" . '--irc-api-room=STRING Name for the chatroom to which the IRC messages should ' . PHP_EOL .
"\t" . ' be sent. ' . PHP_EOL .
"\t" . '--branches-ignore=STRING,... What branches to ignore -- useful to make sure' . PHP_EOL .
"\t" . ' some branches never get scanned. Separate branches' . PHP_EOL .
"\t" . ' with commas' . PHP_EOL .
"\t" . '--local-git-repo=FILE The local git repository to use for direct access to code' . PHP_EOL .
"\t" . '--dry-run=BOOL If set to true, will not make any changes to any data' . PHP_EOL .
"\t" . ' on GitHub -- no comments will be submitted, etc.' . PHP_EOL .
"\t" . '--output=FILE Where to save output made from running PHPCS' . PHP_EOL .
PHP_EOL .
"\t" . '--lint=BOOL Whether to do PHP linting (true/false)' . PHP_EOL .
"\t" . '--lint-skip-folders=STRING Specify folders relative to root of the git repository in which ' . PHP_EOL .
"\t" . ' files should not be PHP linted. Values are comma separated.' . PHP_EOL .
"\t" . '--lint-skip-folders-in-repo-options-file=BOOL Allows folders that are not to be PHP Linted ' . PHP_EOL .
"\t" . ' to be specified in file in root of repository ' . PHP_EOL .
"\t" . ' (.vipgoci_lint_skip_folders). Folders should be ' . PHP_EOL .
"\t" . ' separated by newlines.' . PHP_EOL .
PHP_EOL .
"\t" . '--help Displays this message' . PHP_EOL .
"\t" . '--env-options=STRING Specifies configuration options to be read from environmental ' . PHP_EOL .
"\t" . ' variables -- any variable can be specified. For instance, with ' . PHP_EOL .
"\t" . ' --env-options="repo-owner=U_ROWNER,output=U_FOUTPUT" specified ' . PHP_EOL .
"\t" . ' vip-go-ci will attempt to read the --repo-owner and --output ' . PHP_EOL .
"\t" . ' from the $U_ROWNER and $U_FOUTPUT environmental variables, ' . PHP_EOL .
"\t" . ' respectively. This is useful for environments, such as ' . PHP_EOL .
"\t" . ' TeamCity or GitHub Actions, where vital configuration. ' . PHP_EOL .
"\t" . ' are specified via environmental variables. ' . PHP_EOL .
"\t" . '--repo-options=BOOL Whether to allow configuring of --phpcs-severity ' . PHP_EOL .
"\t" . ' and --post-generic-pr-support-comments via options file' . PHP_EOL .
"\t" . ' ("' . VIPGOCI_OPTIONS_FILE_NAME . '") placed in root of the repository.' . PHP_EOL .
"\t" . '--repo-options-allowed=STRING Limits the options that can be set via repository options ' . PHP_EOL .
"\t" . ' configuration file. Values are separated by commas. ' . PHP_EOL .
PHP_EOL .
"\t" . '--debug-level=NUMBER Specify minimum debug-level of messages to print' . PHP_EOL .
"\t" . ' -- higher number indicates more detailed debugging-messages.' . PHP_EOL .
"\t" . ' Default is zero' . PHP_EOL;
exit( VIPGOCI_EXIT_USAGE_ERROR );
}
/*
* Process the --branches-ignore parameter,
* -- expected to be an array
*/
vipgoci_option_array_handle(
$options,
'branches-ignore',
array()
);
/*
* Process --phpcs-path -- expected to
* be a file
*/
vipgoci_option_file_handle(
$options,
'phpcs-path',
null
);
/*
* Process --phpcs-standard -- expected to be
* a string
*/
if ( empty( $options['phpcs-standard'] ) ) {
$options['phpcs-standard'] = array(
'WordPress-VIP-Go'
);
}
else {
vipgoci_option_array_handle(
$options,
'phpcs-standard',
array(),
array(),
',',
false
);
}
/*
* Process --phpcs-sniffs-include and --phpcs-sniffs-exclude
* -- both expected to be an array.
*/
if ( empty( $options['phpcs-sniffs-include'] ) ) {
$options['phpcs-sniffs-include'] = array();
}
else {
vipgoci_option_array_handle(
$options,
'phpcs-sniffs-include',
array(),
array(),
',',
false
);
}
if ( empty( $options['phpcs-sniffs-exclude'] ) ) {
$options['phpcs-sniffs-exclude'] = array();
}
else {
vipgoci_option_array_handle(
$options,
'phpcs-sniffs-exclude',
array(),
array(),
',',
false
);
}
/*
* Process --phpcs-runtime-set -- expected to be an
* array of values.
*/
if ( empty( $options['phpcs-runtime-set'] ) ) {
$options['phpcs-runtime-set'] = array();
}
else {
vipgoci_option_array_handle(
$options,
'phpcs-runtime-set',
array(),
array(),
','
);
foreach(
$options['phpcs-runtime-set'] as
$tmp_runtime_key =>
$tmp_runtime_set
) {
$options
['phpcs-runtime-set']
[ $tmp_runtime_key ] =
explode( ' ', $tmp_runtime_set, 2 );
/*
* Catch any abnormalities with
* the --phpcs-runtime-set parameter, such
* as key/value being missing, or set to empty.
*/
if (
( count(
$options
['phpcs-runtime-set']
[ $tmp_runtime_key ]
) < 2 )
||
( empty( $options
['phpcs-runtime-set']
[ $tmp_runtime_key ]
[0]
) )
||
( empty( $options
['phpcs-runtime-set']
[ $tmp_runtime_key ]
[1]
) )
) {
vipgoci_sysexit(
'--phpcs-runtime-set is incorrectly formed; it should ' . PHP_EOL .
'be a comma separated string of keys and values.' . PHP_EOL .
'For instance: --phpcs-runtime-set="foo1 bar1,foo2 bar2"',
array(
$options['phpcs-runtime-set']
),
VIPGOCI_EXIT_USAGE_ERROR
);
}
}
}
vipgoci_option_skip_folder_handle(
$options,
'phpcs-skip-folders'
);
/*
* Process --review-comments-ignore -- expected
* to be an array (items separated by "|||").
* Then transform all of the messages to lower-case.
*/
vipgoci_option_array_handle(
$options,
'review-comments-ignore',
array(),
array(),
'|||'
);
if ( ! empty( $options[ 'review-comments-ignore' ] ) ) {
$options['review-comments-ignore'] = array_map(
'strtolower',
$options['review-comments-ignore']
);
}
/*
* Process --dismissed-reviews-exclude-reviews-from-team,
* expected to be a string.
*/
vipgoci_option_array_handle(
$options,
'dismissed-reviews-exclude-reviews-from-team',
array(),
array(),
','
);
/*
* Process --phpcs-severity -- expected to be
* an integer-value.
*/
vipgoci_option_integer_handle(
$options,
'phpcs-severity',
1,
array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
);
/*
* Process --php-path -- expected to be a file,
* default value is 'php' (then relies on $PATH)
*/
vipgoci_option_file_handle(
$options,
'php-path',
'php'
);
/*
* Process --hashes-api -- expected to be a boolean.
*/
vipgoci_option_bool_handle( $options, 'hashes-api', 'false' );
/*
* Process --svg-checks and --svg-scanner-path -- former expected
* to be a boolean, the latter a file-path.
*/
vipgoci_option_bool_handle( $options, 'svg-checks', 'false' );
vipgoci_option_file_handle(
$options,
'svg-scanner-path',
'invalid'
);
/*
* Process --hashes-api-url -- expected to
* be an URL to a webservice.
*/
if ( isset( $options['hashes-api-url'] ) ) {
$options['hashes-api-url'] = trim(
$options['hashes-api-url']
);
$options['hashes-api-url'] = rtrim(
$options['hashes-api-url'],
'/'
);
}
/*
* Process hashes-oauth arguments
*/
foreach( $hashes_oauth_arguments as $tmp_key ) {
if ( ! isset( $options[ $tmp_key ] ) ) {
continue;
}
$options[ $tmp_key ] = rtrim( trim(
$options[ $tmp_key ]
) );
}
/*
* Ask for the hashes-oauth-* arguments
* to be considered as sensitive options
* when cleaning options for printing.
*/
vipgoci_options_sensitive_clean(
null,
$hashes_oauth_arguments
);
/*
* Handle --local-git-repo parameter
*/
$options['local-git-repo'] = rtrim(
$options['local-git-repo'],
'/'
);
vipgoci_gitrepo_ok(
$options['commit'],
$options['local-git-repo']
);
/*
* Handle optional --debug-level parameter
*/
vipgoci_option_integer_handle(
$options,
'debug-level',
0,
array( 0, 1, 2 )
);
// Set the value to global
$vipgoci_debug_level = $options['debug-level'];
/*
* Maximum number of inline comments posted to
* Github with one review -- from 5 to 100.
*/
vipgoci_option_integer_handle(
$options,
'review-comments-max',
10,
range( 5, 100, 1 )
);
/*
* Overall maximum number of inline comments
* posted to GitHub Pull-Request Reviews -- from
* 0 to 500. 0 means unlimited.
*/
vipgoci_option_integer_handle(
$options,
'review-comments-total-max',
200,
range( 0, 500, 1 )
);
/*
* Handle optional --informational-url --
* URL to information on what this bot does.
*/
vipgoci_option_url_handle(
$options,
'informational-url',
null
);
/*
* Handle boolean parameters
*/
vipgoci_option_bool_handle( $options, 'dry-run', 'false' );
vipgoci_option_bool_handle( $options, 'phpcs', 'true' );
vipgoci_option_bool_handle( $options, 'phpcs-skip-folders-in-repo-options-file', 'false' );
vipgoci_option_bool_handle( $options, 'phpcs-skip-scanning-via-labels-allowed', 'false' );
vipgoci_option_bool_handle( $options, 'repo-options', 'false' );
vipgoci_option_bool_handle( $options, 'lint', 'true' );
vipgoci_option_bool_handle( $options, 'lint-skip-folders-in-repo-options-file', 'false' );
vipgoci_option_bool_handle( $options, 'dismiss-stale-reviews', 'false' );
vipgoci_option_bool_handle( $options, 'dismissed-reviews-repost-comments', 'true' );
vipgoci_option_bool_handle( $options, 'results-comments-sort', false );
if (
( false === $options['lint'] ) &&
( false === $options['phpcs'] )
) {
vipgoci_sysexit(
'Both --lint and --phpcs set to false, nothing to do!',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
/* This variable is not configurable, is internal only */
$options['phpcs-standard-file'] = false;
/*
* Should we auto-approve Pull-Requests when
* only altering certain file-types?
*/
vipgoci_option_bool_handle( $options, 'autoapprove', 'false' );
vipgoci_option_bool_handle( $options, 'autoapprove-php-nonfunctional-changes', 'false' );
vipgoci_option_array_handle(
$options,
'autoapprove-filetypes',
array(),
'php'
);
/*
* Handle parameters that enable posting of support-comments
* to Pull-Requests.
*/
vipgoci_option_bool_handle( $options, 'post-generic-pr-support-comments', 'false' );
vipgoci_option_bool_handle( $options, 'post-generic-pr-support-comments-on-drafts', 'false' );
if ( ! empty( $options['post-generic-pr-support-comments-string'] ) ) {
$options['post-generic-pr-support-comments-string'] =
trim(
$options['post-generic-pr-support-comments-string']
);
}
vipgoci_option_array_handle(
$options,
'post-generic-pr-support-comments-branches',
array()
);
vipgoci_option_array_handle(
$options,
'post-generic-pr-support-comments-repo-meta-match',
array()
);
$tmp_repo_meta_match = array();
for(
$i = 0;
$i < count(
$options['post-generic-pr-support-comments-repo-meta-match']
);
$i++
) {
$options['post-generic-pr-support-comments-repo-meta-match'][ $i ] =
explode(
'=',
$options['post-generic-pr-support-comments-repo-meta-match'][ $i ],
2 // Max one '='; any extra will be preserve
);
if ( count( $options['post-generic-pr-support-comments-repo-meta-match'][ $i ] ) != 2 ) {
continue;
}
/*
* Convert "true" strings to true boolean variable,
* same for "false" and "null".
*/
if ( ! isset(
$tmp_repo_meta_match[
$options['post-generic-pr-support-comments-repo-meta-match'][ $i ][0]
]
) ) {
$tmp_repo_meta_match[
$options['post-generic-pr-support-comments-repo-meta-match'][ $i ][0]
] = array();
}
$tmp_repo_meta_match[
$options['post-generic-pr-support-comments-repo-meta-match'][ $i ][0]
][] = vipgoci_convert_string_to_type(
$options['post-generic-pr-support-comments-repo-meta-match'][ $i ][1]
);
}
$options['post-generic-pr-support-comments-repo-meta-match'] =
$tmp_repo_meta_match;
unset(
$tmp_repo_meta_match
);
/*
* Handle option for setting support
* labels. Handle prefix too.
*/
vipgoci_option_bool_handle( $options, 'set-support-level-label', 'false' );
if (
( isset( $options['set-support-level-label-prefix'] ) ) &&
( strlen( $options['set-support-level-label-prefix'] ) > 5 )
) {
$options['set-support-level-label-prefix'] = trim(
$options['set-support-level-label-prefix']
);
}
else {
$options['set-support-level-label-prefix'] = null;
}
/*
* Handle options for repo-meta API.
*/
if ( isset( $options['repo-meta-api-base-url' ] ) ) {
vipgoci_option_url_handle( $options, 'repo-meta-api-base-url', null );
}
if ( isset( $options['repo-meta-api-user-id'] ) ) {
vipgoci_option_integer_handle( $options, 'repo-meta-api-user-id', 0 );
}
else {
$options['repo-meta-api-user-id'] = null;
}
if ( isset(
$options['repo-meta-api-access-token']
) ) {
$options['repo-meta-api-access-token'] = trim(
$options['repo-meta-api-access-token']
);
}
else {
$options['repo-meta-api-access-token'] = null;
}
vipgoci_options_sensitive_clean(
null,
array(
'repo-meta-api-access-token'
)
);
/*
* Handle IRC API parameters
*/
$irc_params_defined = 0;
foreach( array(
'irc-api-url',
'irc-api-token',
'irc-api-bot',
'irc-api-room'
) as $irc_api_param ) {
if ( isset( $options[ $irc_api_param ] ) ) {
$options[ $irc_api_param ] = trim(
$options[ $irc_api_param ]
);
$options[ $irc_api_param ] = rtrim(
$options[ $irc_api_param ]
);
$irc_params_defined++;
}
}
if ( isset( $options['irc-api-url'] ) ) {
vipgoci_option_url_handle(
$options,
'irc-api-url',
null
);
}
if (
( $irc_params_defined > 0 ) &&
( $irc_params_defined !== 4 )
) {
vipgoci_sysexit(
'Some IRC API parameters defined but not all; all must be defined to be useful',
array(
),
VIPGOCI_EXIT_USAGE_ERROR
);
}
unset( $irc_params_defined );
/*
* Make sure the IRC API token
* will be removed from output
* of options.
*/
vipgoci_options_sensitive_clean(
null,
array(
'irc-api-token',
)
);
/*
* Handle settings for the pixel API.
*/
if ( isset( $options['pixel-api-url'] ) ) {
vipgoci_option_url_handle(
$options,
'pixel-api-url',
null