Skip to content

Commit 12f4376

Browse files
committed
Add option skip-large-files-limit
Default is set to 15000
1 parent c01757d commit 12f4376

File tree

8 files changed

+675
-222
lines changed

8 files changed

+675
-222
lines changed

file-validation.php

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
<?php
2-
declare(strict_types=1);
2+
declare( strict_types=1 );
33

44
/**
55
* Validation applied to verify wether the bot should scan or skip the file specified
66
*/
77

88
/**
9-
* @param $temp_file_name
10-
* @param $file_name
9+
* @param string $temp_file_name
10+
* @param string $file_name
1111
* @param string $commit_id
12+
* @param int $max_lines
1213
*
1314
* Validates if the file is valid to be scanned by vip-go-ci
1415
*
1516
* @return array
16-
* [
17-
* 'issues' =>
18-
* [
19-
* 'max-lines' => [$file_name],
20-
* ],
21-
* 'total' => 1
22-
* ]
17+
* [
18+
* 'issues' =>
19+
* [
20+
* 'max-lines' => [$file_name],
21+
* ],
22+
* 'total' => 1
23+
* ]
2324
* In a oop we could simply inject a service for caching
2425
* Vars and set values in the constructor
2526
*/
26-
function vipgoci_validate( string $temp_file_name, string $file_name, string $commit_id ): array
27-
{
27+
function vipgoci_validate( string $temp_file_name, string $file_name, string $commit_id, int $max_lines ): array {
2828
$validation_result = array( 'total' => 0 );
2929

30-
if ( false === vipgoci_is_number_of_lines_valid( $temp_file_name, $file_name, $commit_id ) ) {
31-
$validation_result[ 'issues' ][ VIPGOCI_VALIDATION_MAXIMUM_LINES ] = [ $file_name ];
32-
$validation_result[ 'total' ] = count( $validation_result[ 'issues' ] );
30+
if ( false === vipgoci_is_number_of_lines_valid( $temp_file_name, $file_name, $commit_id, $max_lines ) ) {
31+
$validation_result['issues'][ VIPGOCI_VALIDATION_MAXIMUM_LINES ] = [ $file_name ];
32+
$validation_result['total'] = count( $validation_result['issues'] );
3333
}
3434

3535
return $validation_result;
@@ -39,17 +39,17 @@ function vipgoci_validate( string $temp_file_name, string $file_name, string $co
3939
* @param string $temp_file_name
4040
* @param string $file_name
4141
* @param string $commit_id
42+
* @param int $max_lines
4243
*
4344
* @return bool
4445
*/
45-
function vipgoci_is_number_of_lines_valid( string $temp_file_name, string $file_name, string $commit_id ): bool
46-
{
46+
function vipgoci_is_number_of_lines_valid( string $temp_file_name, string $file_name, string $commit_id, int $max_lines ): bool {
4747
/**
4848
* Verifies if number of lines validation are in the cache
4949
* If so, returns the value
5050
*/
5151

52-
$cache_key = vipgoci_cache_get_is_number_of_lines_valid_key( $file_name, $commit_id );
52+
$cache_key = vipgoci_cache_get_is_number_of_lines_valid_key( $file_name, $commit_id );
5353
$is_number_of_lines_valid = vipgoci_cache_get_is_number_of_lines_valid( $cache_key );
5454
if ( ! is_null( $is_number_of_lines_valid ) ) {
5555
return $is_number_of_lines_valid;
@@ -72,7 +72,7 @@ function vipgoci_is_number_of_lines_valid( string $temp_file_name, string $file_
7272
array( 'file_name' => $file_name, 'cmd' => $cmd, 'output' => $output )
7373
);
7474

75-
$is_number_of_lines_valid = vipgoci_verify_number_of_lines_output( $output );
75+
$is_number_of_lines_valid = vipgoci_verify_number_of_lines_output( $output, $max_lines );
7676

7777
vipgoci_cache_set_is_number_of_lines_valid( $cache_key, $is_number_of_lines_valid );
7878

@@ -85,8 +85,7 @@ function vipgoci_is_number_of_lines_valid( string $temp_file_name, string $file_
8585
*
8686
* Sets cache for converted is_number_of_lines_valid
8787
*/
88-
function vipgoci_cache_set_is_number_of_lines_valid( array $cache_key, bool $is_number_of_lines_valid ): void
89-
{
88+
function vipgoci_cache_set_is_number_of_lines_valid( array $cache_key, bool $is_number_of_lines_valid ): void {
9089
vipgoci_cache(
9190
$cache_key,
9291
$is_number_of_lines_valid === true ? 'true' : 'false'
@@ -95,14 +94,12 @@ function vipgoci_cache_set_is_number_of_lines_valid( array $cache_key, bool $is_
9594

9695
/**
9796
* @param string $output
97+
* @param int $max_lines
9898
*
9999
* @return bool
100100
*/
101-
function vipgoci_verify_number_of_lines_output( string $output ): bool
102-
{
103-
return is_numeric( $output )
104-
? $output < VIPGOCI_VALIDATION_MAXIMUM_LINES_LIMIT
105-
: false;
101+
function vipgoci_verify_number_of_lines_output( string $output, int $max_lines ): bool {
102+
return is_numeric( $output ) && $output < $max_lines;
106103
}
107104

108105
/**
@@ -113,11 +110,10 @@ function vipgoci_verify_number_of_lines_output( string $output ): bool
113110
*
114111
* @return bool|null
115112
*/
116-
function vipgoci_cache_get_is_number_of_lines_valid( array $cache_key ): ?bool
117-
{
113+
function vipgoci_cache_get_is_number_of_lines_valid( array $cache_key ): ?bool {
118114
$cached_value = vipgoci_cache( $cache_key );
119115

120-
return false !== $cached_value? 'true' === $cached_value : null;
116+
return false !== $cached_value ? 'true' === $cached_value : null;
121117
}
122118

123119
/**
@@ -127,7 +123,6 @@ function vipgoci_cache_get_is_number_of_lines_valid( array $cache_key ): ?bool
127123
*
128124
* @return array
129125
*/
130-
function vipgoci_cache_get_is_number_of_lines_valid_key( string $file_name, string $commit_id ): array
131-
{
126+
function vipgoci_cache_get_is_number_of_lines_valid_key( string $file_name, string $commit_id ): array {
132127
return array( __FUNCTION__, $file_name, $commit_id );
133128
}

lint-scan.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,12 @@ function vipgoci_lint_scan_commit(
286286
* and if it's not valid, the scans skips it
287287
*/
288288
if ( true === $options['skip-large-files'] ) {
289-
$validation = vipgoci_validate( $temp_file_name, $filename, $commit_id );
289+
$validation = vipgoci_validate(
290+
$temp_file_name,
291+
$filename,
292+
$commit_id,
293+
$options[ 'skip-large-files-limit' ]
294+
);
290295
if ( 0 !== $validation[ 'total' ] ) {
291296
unlink( $temp_file_name );
292297

main.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ function vipgoci_options_recognized() {
225225
'branches-ignore:',
226226
'local-git-repo:',
227227
'skip-large-files:',
228+
'skip-large-files-limit:',
228229

229230
/*
230231
* Environmental & repo configuration
@@ -822,10 +823,11 @@ function vipgoci_run() {
822823
range( 0, 500, 1 )
823824
);
824825

826+
vipgoci_option_integer_handle( $options, 'skip-large-files-limit', 15000 );
827+
825828
/*
826829
* Handle boolean parameters
827830
*/
828-
829831
vipgoci_option_bool_handle( $options, 'skip-draft-prs', 'false' );
830832

831833
vipgoci_option_bool_handle( $options, 'phpcs', 'true' );

phpcs-scan.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,17 @@ function vipgoci_phpcs_scan_single_file(
212212
$file_contents
213213
);
214214

215-
$validation = vipgoci_validate( $temp_file_name, $file_name, $options['commit'] );
216-
217215
/*
218216
* Skips the phpcs scan when the validation contains any issue
219217
*/
220218
if ( true === $options['skip-large-files'] ) {
219+
$validation = vipgoci_validate(
220+
$temp_file_name,
221+
$file_name,
222+
$options['commit'],
223+
$options['skip-large-files-limit']
224+
);
225+
221226
if ( 0 !== $validation[ 'total' ] ) {
222227
$skipped = array(
223228
'file_issues_arr_master' => array(),
@@ -316,7 +321,7 @@ function vipgoci_phpcs_scan_single_file(
316321
'file_issues_arr_master' => $file_issues_arr_master,
317322
'file_issues_str' => $file_issues_str,
318323
'temp_file_name' => $temp_file_name,
319-
'validation' => $validation
324+
'validation' => $validation??[]
320325
);
321326
}
322327

@@ -569,7 +574,7 @@ function vipgoci_phpcs_scan_commit(
569574
$file_name
570575
);
571576

572-
if ( 0 !== $tmp_scanning_results[ 'validation' ][ 'total' ] ) {
577+
if ( true === $options[ 'skip-large-files' ] && 0 !== $tmp_scanning_results[ 'validation' ][ 'total' ] ) {
573578
vipgoci_log(
574579
VIPGOCI_SKIPPED_FILES,
575580
array(

0 commit comments

Comments
 (0)