forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-validation.php
167 lines (144 loc) · 3.5 KB
/
file-validation.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
<?php
/**
* Validation to determine wether vip-go-ci should scan a
* particular file or not.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Determines if the file specified can be scanned by vip-go-ci.
*
* @param string $temp_file_name Temporary file name.
* @param string $file_name File name.
* @param string $commit_id Commit-ID of current commit.
* @param int $max_lines Maximum number of lines allowed.
*
* @return array Array entry with issues.
* [
* 'issues' =>
* [
* 'max-lines' => [ $file_name ],
* ],
* 'total' => 1
* ]
*/
function vipgoci_validate(
string $temp_file_name,
string $file_name,
string $commit_id,
int $max_lines
): array {
$validation_result = array( 'total' => 0 );
if ( false === vipgoci_is_number_of_lines_valid(
$temp_file_name,
$file_name,
$commit_id,
$max_lines
) ) {
$validation_result['issues'][ VIPGOCI_VALIDATION_MAXIMUM_LINES ] = array( $file_name );
$validation_result['total'] = count( $validation_result['issues'] );
}
return $validation_result;
}
/**
* Verify that file does not exceed limit set. Caches results.
*
* @param string $temp_file_name Temporary file name.
* @param string $file_name File name.
* @param string $commit_id Commit-ID of current commit.
* @param int $max_lines Maximum number of lines.
*
* @return bool True if within limit, else false.
*/
function vipgoci_is_number_of_lines_valid(
string $temp_file_name,
string $file_name,
string $commit_id,
int $max_lines
): bool {
/*
* Check if results are cached.
*/
$cache_key = array(
__FUNCTION__,
$file_name,
$commit_id,
);
// Check for cached value.
$is_number_of_lines_valid = vipgoci_cache(
$cache_key
);
vipgoci_log(
'Validating number of lines' .
vipgoci_cached_indication_str( $is_number_of_lines_valid ),
array(
'file_name' => $file_name,
'cached_result' => ( false === $is_number_of_lines_valid ) ? null : $is_number_of_lines_valid,
)
);
if ( false !== $is_number_of_lines_valid ) {
/*
* Cached value found, should be string. Convert
* 'true' or 'false' string to boolean type.
*/
return vipgoci_convert_string_to_type(
$is_number_of_lines_valid
);
}
/*
* Get file length (number of lines).
*/
$cmd = sprintf(
'wc -l %s | awk \'{print $1;}\'',
escapeshellcmd( $temp_file_name )
);
$output_2 = '';
$output_res_code = -255;
$output = vipgoci_runtime_measure_exec_with_retry(
$cmd,
array( 0 ),
$output_2,
$output_res_code,
'file_validation',
true
);
$output_invalid = ( ( null === $output ) || ( ! is_numeric( $output ) ) );
vipgoci_log(
$output_invalid ?
'Unable to validate number of lines, unable to execute utility or invalid output' :
'Ran utility to validate number of lines',
array(
'file_name' => $file_name,
'cmd' => $cmd,
'output' => $output,
),
$output_invalid ? 0 : 2
);
if ( true === $output_invalid ) {
/*
* Failed to execute or invalid output. Assume file
* can be scanned, do not cache results.
*/
return true;
}
// Sanitize string.
$output = vipgoci_sanitize_string(
$output
);
// Check string value.
$is_number_of_lines_valid = ( $output < $max_lines );
// Cache results.
vipgoci_cache(
$cache_key,
( true === $is_number_of_lines_valid ) ? 'true' : 'false'
);
vipgoci_log(
'Validated number of lines',
array(
'file_name' => $file_name,
'output' => $output,
)
);
return $is_number_of_lines_valid;
}