-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.php
844 lines (745 loc) · 18.7 KB
/
main.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
<?php
define( 'VIPGOCI_INCLUDED', true );
/*
* Return array with options recognized.
*
* @codeCoverageIgnore
*/
function vipgocs_options_recognized(): array {
return array(
/*
* General configuration
*/
'vipgoci-path:',
'dry-run:',
'local-git-repo:',
'help',
/*
* PHPCS configuration
*/
'phpcs-path:',
'phpcs-php-path:',
'phpcs-standard:',
'phpcs-severity:',
'phpcs-runtime-set:',
'phpcs-sniffs-exclude:',
'phpcs-cachedb:', // PHPCS CacheDB
/*
* GitHub reviews configuration
*/
'review-comments-ignore:',
/*
* GitHub configuration
*/
'repo-owner:',
'repo-name:',
'token:',
'github-labels:',
'github-issue-title:',
'github-issue-body:',
'github-issue-body-file:',
'github-issue-assign:',
'github-issue-group-by:',
/*
* Zendesk DB
*/
'zendesk-db:',
'zendesk-access-username:', // For sanity-checking
);
}
/*
* Print help message.
*
* @codeCoverageIgnore
*/
function vipgocs_help( ) :void {
global $argv;
print 'Usage: ' . $argv[0] . PHP_EOL .
"\t" . 'Options --vipgoci-path, --local-git-repo, --phpcs-path, --phpcs-standard,' . PHP_EOL .
"\t" . ' --repo-owner, --repo-name, --token, --github-issue-title,' . PHP_EOL .
"\t" . ' --github-issue-body are mandatory parameters.' . PHP_EOL .
PHP_EOL .
"\t" . "Note that some parameters have a complementary '-file' parameter (see below)." . PHP_EOL .
PHP_EOL .
"\t" . 'General configuration:' . PHP_EOL .
"\t" . '--help Prints this message.' . PHP_EOL .
"\t" . '--vipgoci-path=STRING Path to were vip-go-ci lives, should be folder.' . PHP_EOL .
"\t" . '--dry-run=BOOL If set to true, will do scanning of code and then' . PHP_EOL .
"\t" . ' exit without submitting GitHub issues.' . PHP_EOL .
"\t" . '--local-git-repo=FILE The local git repository to use for direct access to code.' . PHP_EOL .
"\t" . '--skip-empty-files=BOOL Will not scan files that are empty or consist only of whitespace.' . PHP_EOL .
PHP_EOL .
"\t" . 'PHPCS configuration:' . PHP_EOL .
"\t" . '--phpcs-path=FILE Full path to PHPCS script.' . PHP_EOL .
"\t" . '--phpcs-php-path=FILE Full path to PHP used to run PHPCS. If not specified the default in' . PHP_EOL .
"\t" . ' $PATH will be used instead.' . 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-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-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-cachedb=FILE File to cache PHPCS results. In this DB file results' . PHP_EOL .
"\t" . ' are cached for all files scanned in case an identical' . PHP_EOL .
"\t" . ' file is later to be scanned; the cached results can then' . PHP_EOL .
"\t" . ' be re-used and the scanning itself skipped which can save' . PHP_EOL .
"\t" . ' time. Use with caution, see README.md.' . PHP_EOL .
PHP_EOL .
"\t" . 'GitHub reviews configuration:' . PHP_EOL .
"\t" . '--review-comments-ignore=ARRAY Array of issues to be filtered away before posting results to GitHub.' . PHP_EOL .
PHP_EOL .
"\t" . 'GitHub configuration:' . 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" . '--token=STRING The access-token to use to communicate with GitHub.' . PHP_EOL .
"\t" . '--github-labels=STRING Comma separated list of labels to attach to GitHub issues opened.' . PHP_EOL .
"\t" . '--github-issue-title=STRING Title prefix to use for GitHub issues created.' . PHP_EOL .
"\t" . '--github-issue-body=STRING Body for each created GitHub issue.' . PHP_EOL .
"\t" . ' The option supports tokens that will be replaced with values:' . PHP_EOL .
"\t" . ' * %error_msg%: Will be replaced with problems noted.' . PHP_EOL .
"\t" . ' * %branch_name%: Will be replaced with name of current branch.' . PHP_EOL .
"\t" . '--github-issue-body-file=FILE A file to read the content of --github-issue-body parameter' . PHP_EOL .
"\t" . ' instead of specifying the parameter itself.' . PHP_EOL .
"\t" . '--github-issue-assign=STRING Assign specified admins as collaborators for each created issue' . PHP_EOL .
"\t" . ' -- outside, direct, or all.' . PHP_EOL .
"\t" . '--github-issue-group-by=STRING How to group the issues found; either by "file" or "folder" --' . PHP_EOL .
"\t" . ' "folder" is default.' . PHP_EOL .
PHP_EOL .
"\t" . 'Zendesk configuration:' . PHP_EOL .
"\t" . '--zendesk-db=FILE File to store URLs to GitHub issues for later processing' . PHP_EOL .
"\t" . ' by the zendesk-tickets-create.php utility.' . PHP_EOL .
PHP_EOL;
}
/*
* Check if required options are in place,
* exit if not and display help message.
*
* Do the same if --help is specified.
*
* @codeCoverageIgnore
*/
function vipgocs_compatibility_scanner_check_required_options(
array &$options // $options is a pointer
) :void {
/*
* Check if any required options are missing.
*/
if (
( ! isset(
$options['vipgoci-path'],
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$options['github-issue-title'],
$options['local-git-repo'],
$options['phpcs-path'],
$options['phpcs-standard']
) )
||
(
( ! isset(
$options['github-issue-body-file']
) )
&&
( ! isset(
$options['github-issue-body']
) )
)
||
(
( isset( $options['help'] ) )
)
) {
if ( ! isset( $options['help'] ) ) {
echo 'Error: Essential parameter missing.' . PHP_EOL;
}
// Print help.
vipgocs_help( );
exit(253);
}
}
/*
* Initialize general options, related
* to vip-go-ci etc.
*
* @codeCoverageIgnore
*/
function vipgocs_compatibility_scanner_init_general(
array &$options // $options is a pointer
) :void {
/*
* Load vip-go-ci
*/
vipgocs_vipgoci_load(
$options,
'vipgoci-path'
);
/*
* Parse --dry-run parameter
*/
vipgoci_option_bool_handle(
$options,
'dry-run',
'false'
);
/*
* Parse --skip-empty-files
*/
vipgoci_option_bool_handle(
$options,
'skip-empty-files',
'true'
);
/*
* Get the HEAD commit
* from the local repository -- this
* would be the latest commit. Used
* by the PHPCS scanning function
* and elsewhere.
*/
$vipgoci_git_repo_head = vipgoci_gitrepo_get_head(
$options['local-git-repo']
);
$options['commit'] = $vipgoci_git_repo_head;
/*
* Check if commit-ID matches in length to the
* valid ones, exit if not.
*/
if ( strlen( $options['commit'] ) !== 40 ) {
vipgoci_sysexit(
'Invalid commit-ID from git repository',
array(
'commit' => $options['commit'],
)
);
}
}
/*
* Initialize PHPCS related options.
*
* @codeCoverageIgnore
*/
function vipgocs_compatibility_scanner_init_phpcs(
array &$options, // $options is a pointer
&$phpcscachedb_conn // $phpcscachedb_conn is a pointer
) :void {
/*
* Parse rest of options
*/
vipgoci_option_file_handle(
$options,
'phpcs-path',
null
);
vipgoci_option_file_handle(
$options,
'phpcs-php-path',
'php'
);
vipgoci_option_array_handle(
$options,
'phpcs-standard',
array(),
array(),
',',
false
);
vipgoci_option_phpcs_runtime_set(
$options,
'phpcs-runtime-set'
);
if ( empty( $options['phpcs-sniffs-exclude'] ) ) {
$options['phpcs-sniffs-exclude'] = array();
}
else {
vipgoci_option_array_handle(
$options,
'phpcs-sniffs-exclude',
array(),
array(),
',',
false
);
}
vipgoci_option_integer_handle(
$options,
'phpcs-severity',
1,
array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
);
if ( ! empty( $options['phpcs-cachedb'] ) ) {
$phpcscachedb_conn = vipgocs_phpcs_cachedb_db_open(
$options['phpcs-cachedb']
);
}
}
/*
* Initialize GitHub related options.
*
* @codeCoverageIgnore
*/
function vipgocs_compatibility_scanner_init_github(
array &$options // $options is a pointer
) :void {
/*
* Read the parameter's '-file' counterpart from file if
* specified.
*/
vipgocs_file_option_process_complementary(
$options,
'github-issue-body'
);
if ( strpos(
$options['github-issue-body'],
'%error_msg%'
) === false ) {
vipgoci_sysexit(
'--github-issue-body is missing %error_msg% string',
array(
'github-issue-body' => $options['github-issue-body'],
)
);
}
if ( empty( $options['github-labels'] ) ) {
$options['github-labels'] = array();
}
else {
vipgoci_option_array_handle(
$options,
'github-labels',
array(),
null,
',',
false
);
}
if ( isset( $options['github-issue-assign'] ) ) {
$options['github-issue-assign'] = trim(
$options['github-issue-assign']
);
if (
( 'all' !== $options['github-issue-assign'] ) &&
( 'direct' !== $options['github-issue-assign'] ) &&
( 'outside' !== $options['github-issue-assign'] )
) {
vipgoci_sysexit(
'--github-issue-assign is assigned invalid parameter',
array(
'github-issue-assign' => $options['github-issue-assign']
)
);
}
}
if ( ! isset( $options['github-issue-group-by'] ) ) {
$options['github-issue-group-by'] = 'folder';
}
else {
$options['github-issue-group-by'] = trim(
$options['github-issue-group-by']
);
if (
( 'file' !== $options['github-issue-group-by'] ) &&
( 'folder' !== $options['github-issue-group-by'] )
) {
vipgoci_sysexit(
'Invalid argument provided to option --github-issue-group-by; should be "file" or "folder".'
);
}
}
/*
* Add token to sensitive options list
*/
vipgoci_options_sensitive_clean(
null,
array(
'token',
)
);
/*
* Check if GitHub token is okay.
*/
$current_user_info = vipgoci_github_authenticated_user_get(
$options['token']
);
if (
( false === $current_user_info ) ||
( ! isset( $current_user_info->login ) ) ||
( empty( $current_user_info->login ) )
) {
vipgoci_sysexit(
'Unable to get information about token-holder user from GitHub',
array(
),
VIPGOCI_EXIT_GITHUB_PROBLEM
);
}
else {
vipgoci_log(
'Got information about token-holder user from GitHub',
array(
'login' => $current_user_info->login,
'html_url' => $current_user_info->html_url,
)
);
}
}
/*
* Initialize Zendesk options.
*
* @codeCoverageIgnore
*/
function vipgocs_compatibility_scanner_init_zendesk(
array &$options, // $options is a pointer
&$zendesk_db_conn // $zendesk_db_conn is a pointer
) :void {
if ( isset(
$options['zendesk-access-username'],
) ) {
echo 'The --zendesk-access-username, --zendesk-access-password, --zendesk-access-token, --zendesk-ticket-subject, --zendesk-ticket-body, etc. parameters are not supported by this utility anymore. You may want to start using the --zendesk-db parameter instead. See README.md for more information' . PHP_EOL;
exit( 253 );
}
if ( ! empty(
$options['zendesk-db']
) ) {
// Create/open Zendesk DB
$zendesk_db_conn = vipgocs_zendesk_db_open(
$options['zendesk-db']
);
}
}
/*
* Prepare to do actual work.
*
* @codeCoverageIgnore
*/
function vipgocs_compatibility_scanner_init(
array &$options, // $options is a pointer
int &$startup_time, // $startup_time is a pointer
&$zendesk_db_conn, // $zendesk_db_conn is a pointer
&$phpcscachedb_conn // $phpcscachedb_conn is a pointer
) :void {
$zendesk_db_conn = null;
echo 'Initializing...' . PHP_EOL;
/*
* Do some sanity checks on our
* environment.
*/
vipgocs_env_check();
/*
* Log startup time
*/
$startup_time = time();
/*
* Set up logging, etc.
*/
vipgocs_logging_setup();
/*
* Get option-values
*/
$options = getopt(
'',
vipgocs_options_recognized()
);
/*
* Check if all required options are specified.
*/
vipgocs_compatibility_scanner_check_required_options( $options );
/*
* Intialize general options,
* --dry-run, etc.
*/
vipgocs_compatibility_scanner_init_general( $options );
/*
* Initialize PHPCS options
*/
vipgocs_compatibility_scanner_init_phpcs( $options, $phpcscachedb_conn );
/*
* Initialize GitHub options.
*/
vipgocs_compatibility_scanner_init_github( $options );
/*
* GitHub reviews options
*/
vipgoci_option_array_handle(
$options,
'review-comments-ignore',
array(),
array(),
',',
false
);
/*
* Do not skip large files.
*/
$options['skip-large-files'] = false;
/*
* Intialize Zendesk options.
*/
vipgocs_compatibility_scanner_init_zendesk(
$options,
$zendesk_db_conn
);
}
/*
* Main invocation function.
*
* @codeCoverageIgnore
*/
function vipgocs_compatibility_scanner_run(
array &$options, // $options is a pointer
int &$startup_time, // $startup_time is a pointer
&$zendesk_db_conn, // zendesk_db_conn is a pointer
&$phpcscachedb_conn // $phpcscachedb_conn is a pointer
) :int {
/*
* Get options with sensitive items cleaned.
*/
$options_clean = vipgoci_options_sensitive_clean(
$options
);
/*
* Log our startup along with clean options.
*/
vipgoci_log(
'Starting up...',
$options_clean
);
unset( $options_clean );
sleep( 5 );
/*
* Verify if we can find this commit
* in GitHub, and also verify that
* repo exists.
*/
$commit_info = vipgoci_github_fetch_commit_info(
$options['repo-owner'],
$options['repo-name'],
$options['commit'],
$options['token']
);
if ( ! isset( $commit_info->sha ) ) {
vipgoci_sysexit(
'Unable to fetch information about commit from GitHub; wrong parameter specified?',
array(
'repo-owner' => $options['repo-owner'],
'repo-name' => $options['repo-name'],
'commit' => $options['commit'],
)
);
}
/*
* Main processing starts.
*/
vipgoci_log(
'Starting processing...'
);
/*
* Scan all relevant files in the Git repository,
* get results.
*/
$all_results = vipgocs_scan_files(
$options,
$phpcscachedb_conn
);
/*
* Get outside collaborators for the
* the repository who are admin, assign
* them to the issues created.
*/
if ( ! empty( $options['github-issue-assign'] ) ) {
$assignees = vipgocs_get_repo_collaborators_admins(
$options
);
}
else {
$assignees = array();
}
/*
* Get git branch being used.
*/
$git_branch = vipgoci_gitrepo_branch_current_get(
$options['local-git-repo']
);
if ( empty( $git_branch ) ) {
$git_branch = '[UNKNOWN]';
}
/*
* Process each file which as any issues
* tagged against it.
*/
$issue_statistics = vipgocs_open_issues(
$options,
$all_results,
$git_branch,
$options['github-labels'],
$assignees,
$options['dry-run']
);
/*
* If set to dry-run, exit here,
* closing Zendesk DB connection
* and PHPCS caching database connection.
* Clean the PHPCS database first.
*/
if ( true === $options['dry-run'] ) {
if ( ! empty(
$zendesk_db_conn
) ) {
vipgocs_zendesk_db_close(
$zendesk_db_conn
);
}
if ( ! empty( $phpcscachedb_conn ) ) {
vipgocs_phpcs_cachedb_db_vacuum(
$phpcscachedb_conn
);
vipgocs_phpcs_cachedb_db_close(
$phpcscachedb_conn
);
}
vipgoci_log(
sprintf(
'Dry-run complete. Would open GitHub %d issues, found PHPCS %d issues',
$issue_statistics[
'github_issues_opened'
],
$issue_statistics[
'phpcs_issues_found'
]
),
$issue_statistics
);
exit ( 0 );
}
/*
* Construct links to issues,
* use labels to search.
*/
$github_issues_links = array();
foreach(
$options['github-labels'] as $label_name
) {
$github_issues_links[] =
'https://github.com/' .
rawurlencode( $options['repo-owner'] ) . '/' .
rawurlencode( $options['repo-name'] ) . '/' .
'labels/' .
rawurlencode( $label_name );
}
if (
( ! empty( $zendesk_db_conn ) )
&&
( $issue_statistics['github_issues_opened'] > 0 )
) {
vipgoci_log(
'Logging created GitHub issues into Zendesk DB....',
array(
'zendesk-db' => $options['zendesk-db'],
)
);
/*
* Write GitHub issues URL to DB
*/
vipgocs_zendesk_db_write_github_issue(
$zendesk_db_conn,
$options['repo-owner'],
$options['repo-name'],
$github_issues_links[0]
);
}
else {
vipgoci_log(
'Note: Not logging GitHub ticket to Zendesk DB as not all requirements fulfilled'
);
}
/*
* Close PHPCacheDB connection if open.
* Clean the database first.
*/
if ( ! empty( $phpcscachedb_conn ) ) {
vipgocs_phpcs_cachedb_db_vacuum(
$phpcscachedb_conn
);
vipgocs_phpcs_cachedb_db_close(
$phpcscachedb_conn
);
}
/*
* Close connection to DB.
*/
if ( ! empty(
$zendesk_db_conn
) ) {
vipgocs_zendesk_db_close(
$zendesk_db_conn
);
}
/*
* Collect counter-information.
*/
$counter_report = vipgoci_counter_report(
VIPGOCI_COUNTERS_DUMP,
null,
null
);
/*
* Log information and return.
*/
vipgoci_log(
'Complete, shutting down...',
array(
'repo-owner' => $options['repo-owner'],
'repo-name' => $options['repo-name'],
'run_time_seconds' => time() - $startup_time,
'run_time_measurements' =>
vipgoci_runtime_measure(
VIPGOCI_RUNTIME_DUMP,
null
),
'counters_report' => $counter_report,
'github_api_rate_limit' =>
vipgoci_http_api_rate_limit_usage(),
)
);
/*
* Provide a URL to newly created issues.
*/
echo PHP_EOL;
echo 'Find newly created issues here: ' . PHP_EOL;
foreach( $github_issues_links as $github_issue_link ) {
echo "* " . $github_issue_link . PHP_EOL;
}
return 0;
}
if ( ( ! defined( 'VIPGOCS_UNIT_TESTING' ) ) || ( false === VIPGOCS_UNIT_TESTING ) ) {
/*
* Initialize variables used across
* the two main functions.
*/
$options = array();
$startup_time = 0;
$zendesk_db_conn = null;
$phpcscachedb_conn = null;
/*
* Initialize, get options, sanitize
* input, etc.
*/
vipgocs_compatibility_scanner_init(
$options,
$startup_time,
$zendesk_db_conn,
$phpcscachedb_conn
);
/*
* Main invocation function.
*/
$status = vipgocs_compatibility_scanner_run(
$options,
$startup_time,
$zendesk_db_conn,
$phpcscachedb_conn
);
exit( $status );
}