forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ap-svg-files.php
208 lines (188 loc) · 4.59 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
<?php
/**
* Auto-approve SVG files without issues.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* 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.
*
* @param array $options Options needed.
* @param array $auto_approved_files_arr Auto approved files array.
*/
function vipgoci_ap_svg_files(
array $options,
array &$auto_approved_files_arr
) :void {
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'],
$options['skip-draft-prs']
);
foreach ( $prs_implicated as $pr_item ) {
$pr_diff = vipgoci_git_diffs_fetch(
$options['local-git-repo'],
$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['files'] as
$pr_diff_file_name => $pr_diff_contents
) {
$pr_diff_file_extension = vipgoci_file_extension_get(
$pr_diff_file_name
);
/*
* If not a SVG file, do not do anything.
*/
if (
( null === $pr_diff_file_extension ) ||
( false === in_array(
$pr_diff_file_extension,
$options['svg-file-extensions'],
true
) )
) {
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';
} elseif ( 'removed' === $pr_diff['files_status'][ $pr_diff_file_name ] ) {
vipgoci_log(
'Adding SVG file to list of ' .
'approved files, as it was ' .
'removed',
array(
'file_name' =>
$pr_diff_file_name,
)
);
$auto_approved_files_arr[ $pr_diff_file_name ] =
'ap-svg-files';
continue;
}
/*
* 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.
);
} elseif (
( 0 ===
$file_issues_arr_master['totals']['errors']
)
&&
( 0 ===
$file_issues_arr_master['totals']['warnings']
)
) {
/*
* As no issues were found, we
* can approve this file.
*/
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,
)
);
}
}
}
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'ap_svg_files' );
}