forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ap-svg-files.php
212 lines (184 loc) · 4.18 KB
/
ap-svg-files.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
<?php
/*
* Process any SVG files that may be part of the PRs.
*
* Should there be any SVG files in the PRs, these
* files can be auto-approved as long as no PHPCS
* issues are found in them. The logic is that if there
* are any such issues, these have to be looked into
* manually, but if not, theses kind of files should be
* safe to be deployed.
*
* Note that the --skip-folders argument is ignored
* in this function.
*/
function vipgoci_ap_svg_files(
$options,
&$auto_approved_files_arr
) {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'ap_svg_files' );
vipgoci_log(
'Doing auto-approval scanning for SVG files',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'commit_id' => $options['commit'],
)
);
$prs_implicated = vipgoci_github_prs_implicated(
$options['repo-owner'],
$options['repo-name'],
$options['commit'],
$options['token'],
$options['branches-ignore']
);
foreach ( $prs_implicated as $pr_item ) {
$pr_diff = vipgoci_github_diffs_fetch(
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$pr_item->base->sha,
$options['commit'],
true, // include renamed files
true, // include removed files
true // include permission changes
);
foreach ( $pr_diff as
$pr_diff_file_name => $pr_diff_contents
) {
$pr_diff_file_extension = vipgoci_file_extension(
$pr_diff_file_name
);
/*
* If not a SVG file, do not do anything.
*/
if (
'svg' !==
$pr_diff_file_extension
) {
continue;
}
/*
* If the file is already in the array
* of approved files, do not do anything.
*/
if ( isset(
$auto_approved_files_arr[
$pr_diff_file_name
]
) ) {
continue;
}
/*
* No patch found for file, so likely
* there were only changes in file-name,
* permissions, removal or other -- we
* can auto-approve SVG files in such cases.
*/
if ( null === $pr_diff_contents ) {
vipgoci_log(
'Adding SVG file to list of approved ' .
'files, as no material changes ' .
'were being done, only renaming, ' .
'permission changes, or removal. ',
array(
'file_name' =>
$pr_diff_file_name,
)
);
$auto_approved_files_arr[
$pr_diff_file_name
] = 'ap-svg-files';
}
/*
* Scan the SVG file, get the results.
*/
$tmp_scan_results = vipgoci_svg_scan_single_file(
$options,
$pr_diff_file_name
);
$file_issues_arr_master =
$tmp_scan_results['file_issues_arr_master'];
/*
* Check for failure
*/
if (
( ! isset(
$file_issues_arr_master['totals']
) )
||
( ! isset(
$file_issues_arr_master['totals']['errors']
) )
||
( ! isset(
$file_issues_arr_master['totals']['warnings']
) )
) {
vipgoci_log(
'Not adding SVG file to list of ' .
'approved files as a failure occurred',
array(
'file_name' =>
$pr_diff_file_name,
'file_issues_arr_master' =>
$file_issues_arr_master,
),
0,
true // log to IRC
);
}
/*
* If no issues were found, we
* can approve this file.
*/
else if (
( 0 ===
$file_issues_arr_master['totals']['errors']
)
&&
( 0 ===
$file_issues_arr_master['totals']['warnings']
)
) {
vipgoci_log(
'Adding SVG file to list of approved ' .
'files, as no PHPCS-issues ' .
'were found',
array(
'file_name' =>
$pr_diff_file_name,
)
);
$auto_approved_files_arr[
$pr_diff_file_name
] = 'ap-svg-files';
}
else {
vipgoci_log(
'Not adding SVG file to list of ' .
'approved files as issues ' .
'were found',
array(
'file_name' =>
$pr_diff_file_name,
'file_issues_arr_master' =>
$file_issues_arr_master,
)
);
}
}
}
/*
* Reduce memory-usage as possible
*/
unset( $tmp_scan_results );
unset( $prs_implicated );
unset( $pr_diff );
unset( $pr_item );
unset( $pr_diff_file_extension );
unset( $pr_diff_file_name );
unset( $file_issues_arr_master );
gc_collect_cycles();
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'ap_svg_files' );
}