forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-scan.php
383 lines (307 loc) · 6.98 KB
/
lint-scan.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
<?php
/*
* Execute PHP linter, get results and
* return them to caller as an array of
* lines.
*/
function vipgoci_lint_do_scan(
$php_path,
$temp_file_name
) {
/*
* Prepare command to use, make sure
* to grab all the output, also
* the output to STDERR.
*
* Further, make sure PHP error-reporting is set to
* E_ALL & ~E_DEPRECATED via configuration-option.
*/
$cmd = sprintf(
'( %s -d %s -d %s -d %s -l %s 2>&1 )',
escapeshellcmd( $php_path ),
escapeshellarg( 'error_reporting=24575' ),
escapeshellarg( 'error_log=null' ),
escapeshellarg( 'display_errors=off' ),
escapeshellarg( $temp_file_name )
);
$file_issues_arr = array();
/*
* Execute linter, grab issues in array,
* measure how long time it took
*/
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'php_lint_cli' );
exec( $cmd, $file_issues_arr );
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'php_lint_cli' );
vipgoci_log(
'PHP linting execution details',
array(
'cmd' => $cmd,
'file_issues_arr' => $file_issues_arr,
),
2
);
return $file_issues_arr;
}
/*
* Parse array of results, extract the problems
* and return as a well-structed array.
*/
function vipgoci_lint_get_issues(
$file_name,
$temp_file_name,
$file_issues_arr
) {
$file_issues_arr_new = array();
// Loop through everything we got from the command
foreach( $file_issues_arr as $index => $message ) {
if (
( 0 === strpos(
$message,
'No syntax errors detected'
) )
) {
// Skip non-errors we do not care about
continue;
}
/*
* Catch any syntax-error problems
*/
if (
( false !== strpos( $message, ' on line ' ) ) &&
( false !== strpos( $message, 'PHP Parse error:' ) )
) {
/*
* Get rid of 'PHP Parse...' which is not helpful
* for users when seen on GitHub
*/
$message = str_replace(
'PHP Parse error:',
'',
$message
);
/*
* Figure out on what line the problem is
*/
$pos = strpos(
$message,
' on line '
) + strlen( ' on line ' );
$file_line = substr(
$message,
$pos,
strlen( $message ) - $pos
);
unset( $pos );
unset( $pos2 );
/*
* Get rid of name of the file, and
* the rest of the message, too.
*/
$pos3 = strpos( $message, ' in ' . $temp_file_name );
$message = substr( $message, 0, $pos3 );
$message = ltrim( rtrim( $message ) );
$file_issues_arr_new[ $file_line ][] = array(
'message' => $message,
'level' => 'ERROR'
);
}
}
return $file_issues_arr_new;
}
/**
* Run PHP lint on all files in a path
*/
function vipgoci_lint_scan_commit(
$options,
&$commit_issues_submit,
&$commit_issues_stats
) {
$repo_owner = $options['repo-owner'];
$repo_name = $options['repo-name'];
$commit_id = $options['commit'];
$github_token = $options['token'];
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'lint_scan_commit' );
vipgoci_log(
'About to lint PHP-files',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
)
);
// Ask for information about the commit
$commit_info = vipgoci_github_fetch_commit_info(
$repo_owner,
$repo_name,
$commit_id,
$github_token,
array(
'file_extensions'
=> array( 'php' ),
'status'
=> array( 'added', 'modified' ),
)
);
// Fetch list of files that exist in the commit
$commit_tree = vipgoci_gitrepo_fetch_tree(
$options,
$commit_id,
array(
'file_extensions'
=> array( 'php' ),
'skip_folders'
=> $options['skip-folders'],
)
);
// Ask for all Pull-Requests that this commit is part of
$prs_implicated = vipgoci_github_prs_implicated(
$repo_owner,
$repo_name,
$commit_id,
$github_token,
$options['branches-ignore']
);
/*
* Lint every PHP file existing in the commit
*/
foreach( $commit_tree as $filename ) {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'lint_scan_single_file' );
$file_contents = vipgoci_gitrepo_fetch_committed_file(
$repo_owner,
$repo_name,
$github_token,
$commit_id,
$filename,
$options['local-git-repo']
);
// Save the file-contents in a temporary-file
$temp_file_name = vipgoci_save_temp_file(
'lint-scan-',
null,
$file_contents
);
/*
* Keep statistics of what we do.
*/
vipgoci_stats_per_file(
$options,
$filename,
'linted'
);
/*
* Actually lint the file
*/
vipgoci_log(
'About to PHP-lint file',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
'filename' => $filename,
'temp_file_name' => $temp_file_name,
)
);
$file_issues_arr_raw = vipgoci_lint_do_scan(
$options['php-path'],
$temp_file_name
);
/* Get rid of temporary file */
unlink( $temp_file_name );
/*
* Process the results, get them in an array format
*/
$file_issues_arr = vipgoci_lint_get_issues(
$filename,
$temp_file_name,
$file_issues_arr_raw
);
vipgoci_log(
'Linting issues details',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
'filename' => $filename,
'temp_file_name' => $temp_file_name,
'file_issues_arr' => $file_issues_arr,
'file_issues_arr_raw' => $file_issues_arr_raw,
),
2
);
/* Get rid of the raw version of issues */
unset( $file_issues_arr_raw );
// If there are no new issues, just leave it at that
if ( empty( $file_issues_arr ) ) {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'lint_scan_single_file' );
continue;
}
/*
* Process results of linting
* for each Pull-Request -- actually
* queue issues for submission.
*/
foreach( $prs_implicated as $pr_item ) {
vipgoci_log(
'Linting issues found',
array(
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
'commit_id' => $commit_id,
'filename'
=> $filename,
'pr_number'
=> $pr_item->number,
'file_issues_arr'
=> $file_issues_arr,
),
2
);
/*
* Loop through array of lines in which
* issues exist.
*/
foreach (
$file_issues_arr as
$file_issue_line => $file_issue_values
) {
/*
* Loop through each issue for the particular
* line.
*/
foreach (
$file_issue_values as
$file_issue_val_item
) {
$commit_issues_submit[
$pr_item->number
][] = array(
'type' => VIPGOCI_STATS_LINT,
'file_name' =>
$filename,
'file_line' => intval(
$file_issue_line
),
'issue' =>
$file_issue_val_item
);
$commit_issues_stats[
$pr_item->number
]['error']++;
}
}
}
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'lint_scan_single_file' );
}
/*
* Reduce memory-usage, as possible.
*/
unset( $file_contents );
unset( $file_issues_arr );
unset( $file_issues_arr_raw );
unset( $prs_implicated );
unset( $file_issue_values );
unset( $commit_tree );
unset( $commit_info );
gc_collect_cycles();
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'lint_scan_commit' );
}