Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,23 @@
* Define for vipgoci_git_diffs_fetch()
*/

define( 'VIPGOCI_GIT_DIFF_CALC_CHANGES', array ('+' => 'additions', '-' => 'deletions') );
define( 'VIPGOCI_GIT_DIFF_CALC_CHANGES', array ('+' => 'additions', '-' => 'deletions') );
define( 'VIPGOCI_GIT_DIFF_DATA_SOURCE_GIT_REPO', 'local-git-repo' );
define( 'VIPGOCI_GIT_DIFF_DATA_SOURCE_GITHUB_API', 'github-api' );



/**
* Define file number of lines limit
*/
define( 'VIPGOCI_SKIPPED_FILES', 'skipped-files' );

define( 'VIPGOCI_VALIDATION_MAXIMUM_LINES_LIMIT', 15000 );
Comment thread
ariskataoka marked this conversation as resolved.
define( 'VIPGOCI_VALIDATION_MAXIMUM_LINES', 'max-lines' );
define(
'VIPGOCI_VALIDATION',
[
VIPGOCI_VALIDATION_MAXIMUM_LINES
=> 'Maximum number of lines exceeded (%d)'
]
);
128 changes: 128 additions & 0 deletions file-validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
declare( strict_types=1 );

/**
* Validation applied to verify wether the bot should scan or skip the file specified
*/

/**
* @param string $temp_file_name
* @param string $file_name
* @param string $commit_id
* @param int $max_lines
*
* Validates if the file is valid to be scanned by vip-go-ci
*
* @return array
* [
* 'issues' =>
* [
* 'max-lines' => [$file_name],
* ],
* 'total' => 1
* ]
* In a oop we could simply inject a service for caching
* Vars and set values in the constructor
*/
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 ] = [ $file_name ];
$validation_result['total'] = count( $validation_result['issues'] );
}

return $validation_result;
}

/**
* @param string $temp_file_name
* @param string $file_name
* @param string $commit_id
* @param int $max_lines
*
* @return bool
*/
function vipgoci_is_number_of_lines_valid( string $temp_file_name, string $file_name, string $commit_id, int $max_lines ): bool {
/**
* Verifies if number of lines validation are in the cache
* If so, returns the value
*/

$cache_key = vipgoci_cache_get_is_number_of_lines_valid_key( $file_name, $commit_id );
$is_number_of_lines_valid = vipgoci_cache_get_is_number_of_lines_valid( $cache_key );
if ( ! is_null( $is_number_of_lines_valid ) ) {
return $is_number_of_lines_valid;
}

/**
* Calculates the file number of lines
* if it is > than the default limit (defined at defined)
* the bot won't scan it
*/
$cmd = sprintf( 'wc -l %s | awk \'{print $1;}\' 2>&1', escapeshellcmd( $temp_file_name ) );
vipgoci_log( 'Validating number of lines', array( 'file_name' => $file_name ) );

$output = vipgoci_sanitize_string(
vipgoci_runtime_measure_shell_exec( $cmd, 'file_validation' )
);

vipgoci_log(
'Validation number of lines ',
array( 'file_name' => $file_name, 'cmd' => $cmd, 'output' => $output )
);

$is_number_of_lines_valid = vipgoci_verify_number_of_lines_output( $output, $max_lines );

vipgoci_cache_set_is_number_of_lines_valid( $cache_key, $is_number_of_lines_valid );

return $is_number_of_lines_valid;
}

/**
* @param array $cache_key
* @param bool $is_number_of_lines_valid
*
* Sets cache for converted is_number_of_lines_valid
*/
function vipgoci_cache_set_is_number_of_lines_valid( array $cache_key, bool $is_number_of_lines_valid ): void {
vipgoci_cache(
$cache_key,
$is_number_of_lines_valid === true ? 'true' : 'false'
);
}

/**
* @param string $output
* @param int $max_lines
*
* @return bool
*/
function vipgoci_verify_number_of_lines_output( string $output, int $max_lines ): bool {
return is_numeric( $output ) && $output < $max_lines;
}

/**
* @param array $cache_key
*
* Verifies if there's cached validation result available
* returns it if it does, else returns null
*
* @return bool|null
*/
function vipgoci_cache_get_is_number_of_lines_valid( array $cache_key ): ?bool {
$cached_value = vipgoci_cache( $cache_key );

return false !== $cached_value ? 'true' === $cached_value : null;
}

/**
* @param string $file_name
* @param string $commit_id
* Builds the cache key for the number of lines validation
*
* @return array
*/
function vipgoci_cache_get_is_number_of_lines_valid_key( string $file_name, string $commit_id ): array {
return array( __FUNCTION__, $file_name, $commit_id );
}
14 changes: 12 additions & 2 deletions github-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,6 @@ function vipgoci_github_pr_review_submit(
}
}


/*
* If there are no issues to report to GitHub,
* do not continue processing the Pull-Request.
Expand All @@ -2602,7 +2601,8 @@ function vipgoci_github_pr_review_submit(
if (
( false === $github_errors ) &&
( false === $github_warnings ) &&
( false === $github_info )
( false === $github_info ) &&
empty( $results[ VIPGOCI_SKIPPED_FILES ][ $pr_number ] )
) {
continue;
}
Expand Down Expand Up @@ -2718,6 +2718,16 @@ function vipgoci_github_pr_review_submit(
}
}

/**
* Format skipped files message if it validation has issues
*/
if ( ! empty( $results[ VIPGOCI_SKIPPED_FILES ][ $pr_number ] ) ) {
$github_postfields[ 'body' ] .= vipgoci_get_skipped_files_message(
$results[ VIPGOCI_SKIPPED_FILES ][ $pr_number ],
$pr_number
);
}

/*
* If we have a informational-URL about
* the bot, append it along with a generic
Expand Down
29 changes: 28 additions & 1 deletion lint-scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ function vipgoci_lint_parse_results(
function vipgoci_lint_scan_commit(
$options,
&$commit_issues_submit,
&$commit_issues_stats
&$commit_issues_stats,
array &$commit_skipped_files
) {
$repo_owner = $options['repo-owner'];
$repo_name = $options['repo-name'];
Expand Down Expand Up @@ -258,6 +259,7 @@ function vipgoci_lint_scan_commit(

/*
* Lint every PHP file existing in the commit
* $commit_tree is an array of files for that commit
*/

foreach( $commit_tree as $filename ) {
Expand All @@ -279,6 +281,31 @@ function vipgoci_lint_scan_commit(
$file_contents
);

/**
* Validates the file
* and if it's not valid, the scans skips it
*/
if ( true === $options['skip-large-files'] ) {
$validation = vipgoci_validate(
$temp_file_name,
$filename,
$commit_id,
$options[ 'skip-large-files-limit' ]
);
if ( 0 !== $validation[ 'total' ] ) {
unlink( $temp_file_name );

vipgoci_set_prs_implicated_skipped_files( $prs_implicated, $commit_skipped_files, $validation );
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'lint_scan_single_file' );

continue;
}
}

/**
* The lint scan will only proceed if the file is valid
*
*/
/*
* Keep statistics of what we do.
*/
Expand Down
29 changes: 24 additions & 5 deletions main.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function vipgoci_help_print( $argv ) {
"\t" . ' that expect a URL are HTTPS and not HTTP. Default is true.' . PHP_EOL .
"\t" . '--skip-draft-prs=BOOL If true, skip scanning of all Pull-Requests that are in draft mode.' . PHP_EOL .
"\t" . ' Default is false.' . PHP_EOL .
"\t" . '--skip-large-files=true=BOOL If true, skip scanning files that have number of lines higher than the skip-large-files-limit value.' . PHP_EOL .
"\t" . ' Default is true.' . PHP_EOL .
"\t" . '--skip-large-files-limit=INTEGER Defines the maximum number of lines limit per file.' . PHP_EOL .
"\t" . ' Default is 15000 lines.' . PHP_EOL .
"\t" . '--branches-ignore=STRING,... What branches to ignore -- useful to make sure' . PHP_EOL .
"\t" . ' some branches never get scanned. Separate branches' . PHP_EOL .
"\t" . ' with commas.' . PHP_EOL .
Expand Down Expand Up @@ -224,6 +228,8 @@ function vipgoci_options_recognized() {
'skip-draft-prs:',
'branches-ignore:',
'local-git-repo:',
'skip-large-files:',
'skip-large-files-limit:',

/*
* Environmental & repo configuration
Expand Down Expand Up @@ -380,7 +386,15 @@ function vipgoci_exit_status( $results ) {
return VIPGOCI_EXIT_CODE_ISSUES;
}
}
}

if ( ! empty( $results['skipped-files'] ) ) {
foreach ( $results['skipped-files'] as $pr_number ) {
if( 0 < $pr_number[ 'total' ] ) {
// Results contains skipped files due issues, return non-zero
return VIPGOCI_EXIT_CODE_ISSUES;
}
}
}

return 0;
Expand Down Expand Up @@ -813,10 +827,11 @@ function vipgoci_run() {
range( 0, 500, 1 )
);

vipgoci_option_integer_handle( $options, 'skip-large-files-limit', 15000 );

/*
* Handle boolean parameters
*/

vipgoci_option_bool_handle( $options, 'skip-draft-prs', 'false' );

vipgoci_option_bool_handle( $options, 'phpcs', 'true' );
Expand All @@ -829,6 +844,8 @@ function vipgoci_run() {

vipgoci_option_bool_handle( $options, 'lint', 'true' );

vipgoci_option_bool_handle( $options, 'skip-large-files', 'true' );

vipgoci_option_bool_handle( $options, 'lint-skip-folders-in-repo-options-file', 'false' );

vipgoci_option_bool_handle( $options, 'dismiss-stale-reviews', 'false' );
Expand Down Expand Up @@ -1654,7 +1671,7 @@ function vipgoci_run() {
VIPGOCI_GITHUB_WEB_BASE_URL . '/' .
rawurlencode( $options['repo-owner'] ) . '/' .
rawurlencode( $options['repo-name'] ) . '/' .
'commit/' .
'commit/' .
rawurlencode( $options['commit'] )
);

Expand All @@ -1671,7 +1688,7 @@ function vipgoci_run() {
vipgoci_log(
'Starting up...',
array(
'options' => vipgoci_options_sensitive_clean(
'options' => vipgoci_options_sensitive_clean(
$options
)
)
Expand Down Expand Up @@ -1889,7 +1906,8 @@ function vipgoci_run() {
vipgoci_lint_scan_commit(
$options,
$results['issues'],
$results['stats'][ VIPGOCI_STATS_LINT ]
$results['stats'][ VIPGOCI_STATS_LINT ],
$results[ VIPGOCI_SKIPPED_FILES ]
);
}

Expand All @@ -1902,7 +1920,8 @@ function vipgoci_run() {
vipgoci_phpcs_scan_commit(
$options,
$results['issues'],
$results['stats'][ VIPGOCI_STATS_PHPCS ]
$results['stats'][ VIPGOCI_STATS_PHPCS ],
$results[ VIPGOCI_SKIPPED_FILES ]
);
}

Expand Down
Loading