diff --git a/README.md b/README.md index 38280e372..dca7baaad 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,6 @@ Here is an example auto approval by `vip-go-ci`: `vip-go-ci` has support for a number of other features in addition to the above, such as: * Publishing a GitHub build status. * Automatic submission of support messages to pull requests. -* Auto approval of files via Hashes API. * Dismissing stale reviews. * Logging messages to a IRC gateway. @@ -495,7 +494,7 @@ With this setting, any PHP files having only whitespacing changes or updating to Note also that if set up to auto-approve, and the Hases API feature (see below) is configured, it will be utilised. -If _all_ files altered by a pull request have been found to be auto-approvable -- for example, by the non-functional changes or Hashes API --, the whole pull request will be approved automatically. +If _all_ files altered by a pull request have been found to be auto-approvable -- for example, by the non-functional changes mechanism --, the whole pull request will be approved automatically. The following Autoapprovals-related options can be configured via repository config-file: @@ -509,38 +508,6 @@ For instance: {"autoapprove": false, "autoapprove-php-nonfunctional-changes": false} ``` -### Hashes API - -This feature is useful when you want to automatically approve pull requests containing PHP or JavaScript files that are already known to be good and are approved already, so no manual reviewing is needed. To make use of this feature, you will need a database of files already approved. You will also have to be using the auto-approvals feature. - -The feature can be activated using the `--hashes-api` parameter and by specifying a HTTP API endpoint. For instance: - -> ./vip-go-ci.php --autoapprove=true --hashes-api=true --hashes-api-url=https://myservice.mycompany.is/wp-json/viphash/ - -Configured this way, `vip-go-ci` will make HTTP API requests for any PHP or JavaScript file it sees being altered in pull requests it scans. The HTTP API requests would look like this: - -> https://myservice.mycompany.is/wp-json/viphash/v1/hashes/id/[HASH] - -where `[HASH]` is a SHA1 hash of a particular PHP or JavaScript file, after it all comments and whitespaces have been removed from them. `vip-go-ci` expects a JSON result like this from the HTTP API: - -``` -[{"status":"true"},{"status":"true"}] -``` - -The JSON result can contain other fields, but they are not used. Note that a single "false" status is enough to make sure a file is considered _not_ approved. - -An open-source tool to label files as approved or non-approved is available [here](https://github.com/Automattic/vip-hash/). It requires a HTTP API service that `vip-go-ci` communicates with as well. - -The following Hashes-API related option can be configured via repository config-file: - -#### Option `--hashes-api` - -Specifies if to check for approved files in Hashes-API. For instance: - -``` -{"hashes-api":true} -``` - ### Ignore certain branches Sometimes users do not want specific branches scanned for any issues -- they want them effectively to be ignored. To do this, you can use the `--branches-ignore` option. @@ -762,20 +729,15 @@ Start with preparing the `unittests.ini` file: Alter any options in the file as needed to match the setup of your system. Note that in some cases, you may have to use different PHP versions for PHPCS or the SVG scanner, than `vip-go-ci` itself. -Note that some tests will require a GitHub token to submit POST/PUT requests to GitHub in order to complete, need access to the hashes-to-hashes database, or to a repo-meta API. +#### Test suite secrets file + +Note that some tests will require a GitHub token to submit POST/PUT requests to GitHub in order to complete, and some will need access to a repo-meta API. To skip these tests, simply place an empty `unittests-secrets.ini` file in the root directory of `vip-go-ci` and skip the rest of this section. To enable the testing of these, you need to set up a `unittests-secrets.ini` file in the root directory of `vip-go-ci`. This file should include the following fields: ``` -[auto-approvals-secrets] -hashes-api-url= -hashes-oauth-consumer-key= -hashes-oauth-consumer-secret= -hashes-oauth-token= -hashes-oauth-token-secret= - [git-secrets] github-token= ; Personal access token from GitHub team-slug= ; Team slug to test if present, is a string. diff --git a/ap-hashes-api.php b/ap-hashes-api.php deleted file mode 100644 index 7475f0928..000000000 --- a/ap-hashes-api.php +++ /dev/null @@ -1,371 +0,0 @@ - $file_path, - - 'file_extension' - => $file_info_extension, - - 'file_extensions_approvable' - => $file_extensions_approvable, - ) - ); - - vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'hashes_api_scan_file' ); - - return null; - } - - vipgoci_log( - 'Checking if file is already approved in ' . - 'hashes-to-hashes API', - array( - 'repo_owner' => $options['repo-owner'], - 'repo_name' => $options['repo-name'], - 'commit' => $options['commit'], - 'file_path' => $file_path, - ) - ); - - /* - * Try to read file from disk, then - * get rid of whitespaces in the file - * and calculate SHA1 hash from the whole. - */ - - $file_contents = vipgoci_gitrepo_fetch_committed_file( - $options['repo-owner'], - $options['repo-name'], - $options['token'], - $options['commit'], - $file_path, - $options['local-git-repo'] - ); - - if ( false === $file_contents ) { - vipgoci_log( - 'Unable to read file', - array( - 'file_path' => $file_path, - ) - ); - - vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'hashes_api_scan_file' ); - - return null; - } - - vipgoci_log( - 'Saving file from git-repository into temporary file ' . - 'in order to strip any whitespacing from it', - array( - 'file_path' => $file_path, - ), - 2 - ); - - $file_temp_path = vipgoci_save_temp_file( - $file_path, - null, - $file_contents - ); - - $file_contents_stripped = php_strip_whitespace( - $file_temp_path - ); - - $file_sha1 = sha1( $file_contents_stripped ); - - unlink( $file_temp_path ); - unset( $file_contents ); - unset( $file_contents_stripped ); - - /* - * Ask the API for information about - * the specific hash we calculated. - */ - - vipgoci_log( - 'Asking hashes-to-hashes HTTP API if hash of file is ' . - 'known', - array( - 'file_path' => $file_path, - 'file_sha1' => $file_sha1, - ) - ); - - $hashes_to_hashes_url = - $options['hashes-api-url'] . - '/v1/hashes/id/' . - rawurlencode( $file_sha1 ); - - /* - * Not really asking GitHub here, - * but we can re-use the function - * for this purpose. - */ - - $file_hashes_info = - vipgoci_http_api_fetch_url( - $hashes_to_hashes_url, - array( - 'oauth_consumer_key' => $options['hashes-oauth-consumer-key'], - 'oauth_consumer_secret' => $options['hashes-oauth-consumer-secret'], - 'oauth_token' => $options['hashes-oauth-token'], - 'oauth_token_secret' => $options['hashes-oauth-token-secret'], - ) - ); - - /* - * Try to parse, and check for errors. - */ - - if ( false !== $file_hashes_info ) { - $file_hashes_info = json_decode( - $file_hashes_info, - true - ); - } - - if ( - ( false === $file_hashes_info ) || - ( null === $file_hashes_info ) || - ( isset( $file_hashes_info['data']['status'] ) ) - ) { - vipgoci_log( - 'Unable to get information from ' . - 'hashes-to-hashes HTTP API', - array( - 'hashes_to_hashes_url' => $hashes_to_hashes_url, - 'file_path' => $file_path, - 'http_reply' => $file_hashes_info, - ), - 0, - true // Log to IRC. - ); - - vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'hashes_api_scan_file' ); - - return null; - } - - $file_approved = null; - - /* - * Only approve file if all info-items show - * the file to be approved. - */ - foreach ( $file_hashes_info as $file_hash_info ) { - if ( ! isset( $file_hash_info['status'] ) ) { - $file_approved = false; - } - - if ( - ( 'false' === $file_hash_info['status'] ) || - ( false === $file_hash_info['status'] ) - ) { - $file_approved = false; - } elseif ( - ( 'true' === $file_hash_info['status'] ) || - ( true === $file_hash_info['status'] ) - ) { - /* - * Only update approval-flag if we have not - * seen any other approvals, and if we have - * not seen any rejections. - */ - if ( null === $file_approved ) { - $file_approved = true; - } - } - } - - /* - * If no approval is seen, assume it is not - * approved at all. - */ - - if ( null === $file_approved ) { - $file_approved = false; - } - - vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'hashes_api_scan_file' ); - - return $file_approved; -} - -/** - * Scan a particular commit, look for altered - * files in the pull request we are associated with - * and for each of these files, check if they - * are approved in the hashes-to-hashes API. - * - * @param array $options Options needed. - * @param array $commit_issues_submit Results of scanning (reference). - * @param array $commit_issues_stats Result statistics, focussed on hashes-to-hashes API (reference). - * @param array $auto_approved_files_arr Auto approved files array. - * - * @return void - */ -function vipgoci_ap_hashes_api_scan_commit( - array $options, - array &$commit_issues_submit, - array &$commit_issues_stats, - array &$auto_approved_files_arr -) :void { - vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'hashes_api_scan' ); - - vipgoci_log( - 'Scanning altered or new files affected by pull request(s) ' . - 'using hashes-to-hashes API', - array( - 'repo_owner' => $options['repo-owner'], - 'repo_name' => $options['repo-name'], - 'commit_id' => $options['commit'], - 'hashes-api' => $options['hashes-api'], - 'hashes-api-url' => $options['hashes-api-url'], - ) - ); - - $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 ) { - /* - * Do not auto-approve renamed, - * removed or permission-changed files, - * even if they might be auto-approved: the - * reason is that renaming might be harmful to - * stability, there could be removal of vital - * files, and permission changes might be dangerous. - */ - $pr_diff = vipgoci_git_diffs_fetch( - $options['local-git-repo'], - $options['repo-owner'], - $options['repo-name'], - $options['token'], - $pr_item->base->sha, - $options['commit'], - false, // Exclude renamed files. - false, // Exclude removed files. - false // Exclude permission changes. - ); - - foreach ( $pr_diff['files'] as - $pr_diff_file_name => $pr_diff_contents - ) { - /* - * If it is already approved, - * do not do anything. - */ - if ( isset( - $auto_approved_files_arr[ $pr_diff_file_name ] - ) ) { - continue; - } - - /* - * Check if the hashes-to-hashes database - * recognises this file, and check its - * status. - */ - $approval_status = vipgoci_ap_hashes_api_file_approved( - $options, - $pr_diff_file_name - ); - - /* - * Add the file to a list of approved files - * of these affected by the pull request. - */ - if ( true === $approval_status ) { - vipgoci_log( - 'File is approved in ' . - 'hashes-to-hashes API', - array( - 'file_name' => $pr_diff_file_name, - ) - ); - - $auto_approved_files_arr[ $pr_diff_file_name ] - = 'autoapprove-hashes-to-hashes'; - } elseif ( false === $approval_status ) { - vipgoci_log( - 'File is not approved in ' . - 'hashes-to-hashes API', - array( - 'file_name' => $pr_diff_file_name, - ) - ); - } elseif ( null === $approval_status ) { - vipgoci_log( - 'Could not determine if file is approved ' . - 'in hashes-to-hashes API', - array( - 'file_name' => $pr_diff_file_name, - ) - ); - } - } - } - - vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'hashes_api_scan' ); -} - diff --git a/auto-approval.php b/auto-approval.php index 84f87535a..d4eae7b79 100644 --- a/auto-approval.php +++ b/auto-approval.php @@ -95,116 +95,6 @@ function vipgoci_auto_approval_non_approval( ); } - /* - * Loop through approved PHP and JS files, - * adding comment for each about it - * being approved in the hashes-to-hashes API. - */ - foreach ( - $auto_approved_files_arr as - $approved_file => $approved_file_system - ) { - /* - * Make sure that the file was - * really altered in the pull request; - * this is to avoid any errors when - * submitting inline comments. - */ - if ( in_array( - $approved_file, - $pr_files_changed, - true - ) === false ) { - vipgoci_log( - 'Not adding auto-approved in hashes-api ' . - 'database to results for file, as it ' . - 'was not altered by the pull request', - array( - 'pr_number' => $pr_item->number, - 'file_name' => $approved_file, - 'pr_files_changed' => $pr_files_changed, - ) - ); - - continue; - } - - if ( - 'autoapprove-hashes-to-hashes' !== - $approved_file_system - ) { - /* - * If not autoapproved by hashes-to-hashes, - * do not comment on it. Only PHP and JS files - * are auto-approved by hashes-to-hashes. - */ - continue; - } - - $results['issues'][ (int) $pr_item->number ][] = array( - 'type' => VIPGOCI_STATS_HASHES_API, - 'file_name' => $approved_file, - 'file_line' => 1, - 'issue' => array( - 'message' => VIPGOCI_FILE_IS_APPROVED_MSG, - 'source' => 'VipgociInternal.Info.ApprovedHashesToHashesAPI', - 'severity' => 1, - 'fixable' => false, - 'type' => 'INFO', - 'line' => 1, - 'column' => 1, - 'level' => 'INFO', - ), - ); - - $results['stats'][ VIPGOCI_STATS_HASHES_API ][ (int) $pr_item->number ]['info']++; - } - - /* - * Remove any 'file is approved in ...' comments, - * but only for files that are no longer approved. - */ - vipgoci_log( - 'Removing any comments indicating a file is approved ' . - 'for files that are not approved anymore', - array( - 'pr_number' => $pr_item->number, - ) - ); - - $pr_comments = vipgoci_github_pr_reviews_comments_get_by_pr( - $options, - $pr_item->number, - array( - 'login' => 'myself', - ) - ); - - foreach ( $pr_comments as $pr_comment_item ) { - /* - * Skip approved files. - */ - if ( isset( - $auto_approved_files_arr[ $pr_comment_item->path ] - ) ) { - continue; - } - - /* - * If we find the 'approved in hashes-to-hashes ...' - * message, we can safely remove the comment. - */ - if ( false !== strpos( - $pr_comment_item->body, - VIPGOCI_FILE_IS_APPROVED_MSG - ) ) { - vipgoci_github_pr_reviews_comments_delete( - $options, - $pr_comment_item->id - ); - } - } - /* * Get any approving reviews for the pull request * submitted by us. Then dismiss them. @@ -333,13 +223,12 @@ function vipgoci_autoapproval_do_approve( $options['commit'], 'Auto-approved pull request #' . (int) $pr_item->number . ' as it ' . - 'contains only auto-approvable files' . - ' -- either pre-approved files' . + 'contains only auto-approvable files -- ' . ( true === $options['autoapprove-php-nonfunctional-changes'] ? - ', non-functional changes to PHP files ' : '' + 'non-functional changes to PHP files or ' : '' ) . - ' _or_ file-types that are ' . + 'file-types that are ' . 'auto-approvable (' . implode( ', ', @@ -405,46 +294,6 @@ function( $type ) { ); } - /* - * Remove any comments indicating that a file is - * approved -- we want to get rid of these, as they - * are useless to reviewers at this point. The PR is - * approved anyway. - */ - vipgoci_log( - 'Removing any previously submitted comments ' . - 'indicating that a particular file ' . - 'is approved as the whole ' . - 'pull request is approved', - array( - 'pr_number' => $pr_item->number, - ) - ); - - $pr_comments = vipgoci_github_pr_reviews_comments_get_by_pr( - $options, - $pr_item->number, - array( - 'login' => 'myself', - ) - ); - - foreach ( $pr_comments as $pr_comment_item ) { - /* - * If we find the 'approved in hashes-to-hashes ...' - * message, we can safely remove the comment. - */ - if ( false !== strpos( - $pr_comment_item->body, - rtrim( VIPGOCI_FILE_IS_APPROVED_MSG, '.' ) - ) ) { - vipgoci_github_pr_reviews_comments_delete( - $options, - $pr_comment_item->id - ); - } - } - vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'vipgoci_autoapproval_do_approve' ); } @@ -650,21 +499,6 @@ function vipgoci_auto_approval_process( ); } - /* - * Do scanning of all altered files, using - * the hashes-to-hashes database API, collecting - * which files can be auto-approved. - */ - - if ( true === $options['hashes-api'] ) { - vipgoci_ap_hashes_api_scan_commit( - $options, - $results['issues'], - $results['stats'][ VIPGOCI_STATS_HASHES_API ], - $auto_approved_files_arr - ); - } - // If set to true, any SVG files without issues is auto-approved. if ( true === $options['svg-checks'] ) { vipgoci_ap_svg_files( @@ -682,7 +516,7 @@ function vipgoci_auto_approval_process( /* * Remove issues from $results for files - * that are approved in hashes-to-hashes API. + * that are approved. */ vipgoci_results_approved_files_comments_remove( $options, diff --git a/defines.php b/defines.php index 5a721154a..74397975a 100644 --- a/defines.php +++ b/defines.php @@ -44,8 +44,6 @@ /* * Various messages. */ -define( 'VIPGOCI_FILE_IS_APPROVED_MSG', 'File is approved in review database (hashes-to-hashes).' ); - define( 'VIPGOCI_REVIEW_COMMENTS_TOTAL_MAX', 'Total number of active review comments per ' . @@ -161,14 +159,12 @@ define( 'VIPGOCI_STATS_PHPCS', 'phpcs' ); define( 'VIPGOCI_STATS_LINT', 'lint' ); -define( 'VIPGOCI_STATS_HASHES_API', 'hashes-api' ); /* * Define auto-approval types */ define( 'VIPGOCI_APPROVAL_AUTOAPPROVE', 'auto-approval' ); -define( 'VIPGOCI_APPROVAL_HASHES_API', 'hashes-api' ); /* * Defines for vipgoci_runtime_measure() function. diff --git a/log.php b/log.php index eeca3efea..a50a6b870 100644 --- a/log.php +++ b/log.php @@ -115,7 +115,6 @@ function vipgoci_sysexit( ( vipgoci_unittests_check_indication_for_test_id( 'MainRunScanSkipExecutionTest' ) ) || ( vipgoci_unittests_check_indication_for_test_id( 'MainRunScanMaxExecTimeTest' ) ) || ( vipgoci_unittests_check_indication_for_test_id( 'MainRunInitGithubTokenOptionTest' ) ) || - ( vipgoci_unittests_check_indication_for_test_id( 'MainRunInitOptionsAutoapproveHashesOverlapTest' ) ) || ( vipgoci_unittests_check_indication_for_test_id( 'OtherWebServicesIrcApiFilterIgnorableStringsTest' ) ) || ( vipgoci_unittests_check_indication_for_test_id( 'GitRepoRepoOkTest' ) ) ) diff --git a/main.php b/main.php index 5909c3344..7d8d9aa2e 100755 --- a/main.php +++ b/main.php @@ -139,21 +139,6 @@ function vipgoci_help_print() :void { "\t" . ' whitespacing and comment changes.' . PHP_EOL . "\t" . '--autoapprove-label=STRING String to use for labels when auto-approving.' . PHP_EOL . PHP_EOL . - 'Hashes API configuration:' . PHP_EOL . - "\t" . '--hashes-api=BOOL Whether to do hashes-to-hashes API verfication with' . PHP_EOL . - "\t" . ' individual PHP files found to be altered in' . PHP_EOL . - "\t" . ' scanned pull requests.' . PHP_EOL . - "\t" . '--hashes-api-url=STRING URL to hashes-to-hashes HTTP API root' . PHP_EOL . - "\t" . ' -- note that it should not include any specific' . PHP_EOL . - "\t" . ' paths to individual parts of the API.' . PHP_EOL . - "\t" . '--hashes-oauth-token=STRING,' . PHP_EOL . - "\t" . '--hashes-oauth-token-secret=STRING,' . PHP_EOL . - "\t" . '--hashes-oauth-consumer-key=STRING,' . PHP_EOL . - "\t" . '--hashes-oauth-consumer-secret=STRING' . PHP_EOL . - "\t" . ' OAuth 1.0 token, token secret, consumer key and' . PHP_EOL . - "\t" . ' consumer secret needed for hashes-to-hashes HTTP requests.' . PHP_EOL . - "\t" . ' All required for hashes-to-hashes requests.' . PHP_EOL . - PHP_EOL . 'GitHub reviews & generic comments configuration:' . PHP_EOL . "\t" . '--report-no-issues-found=BOOL Post message indicating no issues were found during scanning.' . PHP_EOL . "\t" . ' Enabled by default.' . PHP_EOL . @@ -322,16 +307,6 @@ function vipgoci_options_recognized() :array { 'autoapprove-php-nonfunctional-changes:', 'autoapprove-label:', - /* - * Hashes API configuration - */ - 'hashes-api:', - 'hashes-api-url:', - 'hashes-oauth-token:', - 'hashes-oauth-token-secret:', - 'hashes-oauth-consumer-key:', - 'hashes-oauth-consumer-secret:', - /* * GitHub reviews & generic comments configuration */ @@ -829,107 +804,6 @@ function vipgoci_run_init_options_autoapprove( array &$options ) :void { } } -/** - * Process hashes-to-hashes options. - * - * @param array $options Array of options. - * @param array $hashes_oauth_arguments OAuth 1.0a options for hashes-to-hashes API. - * - * @return void - * - * @codeCoverageIgnore - */ -function vipgoci_run_init_options_hashes_options( - array &$options, - array $hashes_oauth_arguments -) :void { - /* - * Process --hashes-api -- expected to be a boolean. - */ - vipgoci_option_bool_handle( $options, 'hashes-api', 'false' ); - - /* - * Process --hashes-api-url -- expected to - * be an URL to a webservice. - */ - vipgoci_option_url_handle( $options, 'hashes-api-url', null ); - - $options['hashes-api-url'] = trim( - $options['hashes-api-url'], - '/' - ); - - /* - * Sanity check: Can only use --hashes-api=true with a URL - * configured. - */ - if ( - ( true === $options['hashes-api'] ) && - ( empty( $options['hashes-api-url'] ) ) - ) { - vipgoci_sysexit( - 'Cannot run with --hashes-api set to true and without --hashes-api-url set', - array(), - VIPGOCI_EXIT_USAGE_ERROR - ); - } - - /* - * Process hashes-oauth arguments - */ - foreach ( $hashes_oauth_arguments as $tmp_key ) { - if ( ! isset( $options[ $tmp_key ] ) ) { - vipgoci_sysexit( - 'Asking to use --hashes-api-url without --hashes-oauth-* parameters, but that is not possible, as authorization is needed for hashes-to-hashes API', - array(), - VIPGOCI_EXIT_USAGE_ERROR - ); - } - - $options[ $tmp_key ] = trim( - $options[ $tmp_key ] - ); - - unset( $tmp_key ); - } - - /* - * Ask for the hashes-oauth-* arguments - * to be considered as sensitive options - * when cleaning options for printing. - */ - vipgoci_options_sensitive_clean( - null, - $hashes_oauth_arguments - ); -} - -/** - * Sanity-checks for hashes-to-hashes options. - * - * @param array $options Array of options. - * - * @return void - */ -function vipgoci_run_init_options_autoapprove_hashes_overlap( - array &$options -) :void { - /* - * Do sanity-checking with --autoapprove parameter - * and --hashes-api-url parameter. - */ - if ( - ( isset( $options['hashes-api-url'] ) ) && - ( false === $options['autoapprove'] ) - ) { - vipgoci_sysexit( - 'Asking to use --hashes-api-url without --autoapproval set to true, but for hashes-to-hashes functionality to be useful, --autoapprove must be enabled. Otherwise the functionality will not really be used', - array(), - VIPGOCI_EXIT_USAGE_ERROR - ); - } -} - /** * Set options relating to GitHub reviews * @@ -2027,11 +1901,6 @@ function vipgoci_run_init_options_repo_options( array &$options ):void { 'valid_values' => array( true, false ), ), - 'hashes-api' => array( - 'type' => 'boolean', - 'valid_values' => array( true, false ), - ), - 'lint-modified-files-only' => array( 'type' => 'boolean', 'valid_values' => array( true, false ), @@ -2154,14 +2023,6 @@ function vipgoci_run_init_options( array &$options, array $options_recognized ):void { - $hashes_oauth_arguments = - array( - 'hashes-oauth-token', - 'hashes-oauth-token-secret', - 'hashes-oauth-consumer-key', - 'hashes-oauth-consumer-secret', - ); - /* * Handle --enforce-https-urls absolutely first, * as that is used in processing parameters expecting @@ -2230,12 +2091,6 @@ function vipgoci_run_init_options( // Process autoapprove options. vipgoci_run_init_options_autoapprove( $options ); - // Process hashes-to-hashes options. - vipgoci_run_init_options_hashes_options( - $options, - $hashes_oauth_arguments - ); - // Set SVG options. vipgoci_run_init_options_svg( $options ); @@ -2266,12 +2121,6 @@ function vipgoci_run_init_options( // Check --output option. vipgoci_run_init_options_output( $options ); - /* - * Process autoapprove and hashes-to-hashes - * options that overlap. - */ - vipgoci_run_init_options_autoapprove_hashes_overlap( $options ); - /* * Handle --repo-options and related parameters. */ @@ -2942,7 +2791,7 @@ function vipgoci_run_scan( * approvable. * * Will also remove from $results any files - * auto-approved in hashes-to-hashes API. + * auto-approved. */ vipgoci_auto_approval_process( $options, @@ -3157,7 +3006,6 @@ function vipgoci_run_init_vars() :array { 'stats' => array( VIPGOCI_STATS_PHPCS => null, VIPGOCI_STATS_LINT => null, - VIPGOCI_STATS_HASHES_API => null, ), ); diff --git a/reports.php b/reports.php index f28166382..72d1ef403 100644 --- a/reports.php +++ b/reports.php @@ -381,7 +381,6 @@ function vipgoci_report_create_scan_details_auto_approve_configuration( array( 'autoapprove' => 'Auto-approvals enabled', 'autoapprove-php-nonfunctional-changes' => 'Non-functional changes auto-approved', - 'hashes-api' => 'Auto-approval DB enabled', ) as $key => $value ) { if ( @@ -846,7 +845,6 @@ function vipgoci_report_submit_pr_review_from_results( ) :void { $stats_types_to_process = array( VIPGOCI_STATS_PHPCS, - VIPGOCI_STATS_HASHES_API, ); vipgoci_log( diff --git a/requires.php b/requires.php index dc8344e5c..729658203 100644 --- a/requires.php +++ b/requires.php @@ -11,7 +11,6 @@ declare(strict_types=1); require_once __DIR__ . '/ap-file-types.php'; -require_once __DIR__ . '/ap-hashes-api.php'; require_once __DIR__ . '/ap-nonfunctional-changes.php'; require_once __DIR__ . '/ap-svg-files.php'; require_once __DIR__ . '/auto-approval.php'; diff --git a/statistics.php b/statistics.php index 04bac9b33..a4d7172aa 100644 --- a/statistics.php +++ b/statistics.php @@ -44,7 +44,6 @@ function vipgoci_stats_init( array( VIPGOCI_STATS_PHPCS, VIPGOCI_STATS_LINT, - VIPGOCI_STATS_HASHES_API, ) as $stats_type ) { diff --git a/tests/integration/ApHashesApiFileApprovedTest.php b/tests/integration/ApHashesApiFileApprovedTest.php deleted file mode 100644 index 6ea5b2ddd..000000000 --- a/tests/integration/ApHashesApiFileApprovedTest.php +++ /dev/null @@ -1,199 +0,0 @@ - null, - 'repo-name' => null, - 'github-repo-url' => null, - 'git-path' => null, - ); - - var $options_auto_approvals = array( - 'commit-test-ap-hashes-file-approved-1' => null, - ); - - protected function setUp(): void { - vipgoci_unittests_get_config_values( - 'git', - $this->options_git - ); - - vipgoci_unittests_get_config_values( - 'auto-approvals', - $this->options_auto_approvals - ); - $this->options = array_merge( - $this->options_git, - $this->options_auto_approvals - ); - - $this->options[ 'github-token' ] = - vipgoci_unittests_get_config_value( - 'git-secrets', - 'github-token', - true // Fetch from secrets file - ); - - $this->options['token'] = - $this->options['github-token']; - - unset( $this->options['github-token'] ); - - $this->options['branches-ignore'] = array(); - - foreach ( - array( - 'hashes-api-url', - 'hashes-oauth-token', - 'hashes-oauth-token-secret', - 'hashes-oauth-consumer-key', - 'hashes-oauth-consumer-secret', - ) as $option_secret_key - ) { - $this->options[ $option_secret_key ] = - vipgoci_unittests_get_config_value( - 'auto-approvals-secrets', - $option_secret_key, - true // Fetch from secrets file - ); - } - - $this->options['commit'] = - $this->options['commit-test-ap-hashes-file-approved-1']; - - $this->options['local-git-repo'] = - vipgoci_unittests_setup_git_repo( - $this->options - ); - } - - protected function tearDown(): void { - if ( false !== $this->options['local-git-repo'] ) { - vipgoci_unittests_remove_git_repo( - $this->options['local-git-repo'] - ); - } - - $this->options = null; - $this->options_auto_approvals = null; - $this->options_git = null; - } - - /** - * @covers ::vipgoci_ap_hashes_api_file_approved - */ - public function testApHashesApiFileApproved1() { - $options_test = vipgoci_unittests_options_test( - $this->options, - array( 'github-token', 'token' ), - $this - ); - - if ( -1 === $options_test ) { - return; - } - - vipgoci_unittests_output_suppress(); - - if ( false === $this->options['local-git-repo'] ) { - $this->markTestSkipped( - 'Could not set up git repository: ' . - vipgoci_unittests_output_get() - ); - - return; - } - - $file_status = vipgoci_ap_hashes_api_file_approved( - $this->options, - 'approved-1.php' - ); - - vipgoci_unittests_output_unsuppress(); - - $this->assertTrue( - $file_status - ); - } - - /** - * @covers ::vipgoci_ap_hashes_api_file_approved - */ - public function testApHashesApiFileApproved2() { - $options_test = vipgoci_unittests_options_test( - $this->options, - array( 'github-token', 'token' ), - $this - ); - - if ( -1 === $options_test ) { - return; - } - - vipgoci_unittests_output_suppress(); - - if ( false === $this->options['local-git-repo'] ) { - $this->markTestSkipped( - 'Could not set up git repository: ' . - vipgoci_unittests_output_get() - ); - - return; - } - - $file_status = vipgoci_ap_hashes_api_file_approved( - $this->options, - 'not-approved-1.php' - ); - - vipgoci_unittests_output_unsuppress(); - - $this->assertFalse( - $file_status - ); - } - - /** - * @covers ::vipgoci_ap_hashes_api_file_approved - */ - public function testApHashesApiFileApproved3() { - $options_test = vipgoci_unittests_options_test( - $this->options, - array( 'github-token', 'token' ), - $this - ); - - if ( -1 === $options_test ) { - return; - } - - vipgoci_unittests_output_suppress(); - - if ( false === $this->options['local-git-repo'] ) { - $this->markTestSkipped( - 'Could not set up git repository: ' . - vipgoci_unittests_output_get() - ); - - return; - } - - // Invalid config - $this->options['hashes-api-url'] .= "////"; - - $file_status = vipgoci_ap_hashes_api_file_approved( - $this->options, - 'not-approved-1.php' - ); - - vipgoci_unittests_output_unsuppress(); - - $this->assertNull( - $file_status - ); - } -} diff --git a/tests/integration/ApHashesApiScanCommitTest.php b/tests/integration/ApHashesApiScanCommitTest.php deleted file mode 100644 index 244efe716..000000000 --- a/tests/integration/ApHashesApiScanCommitTest.php +++ /dev/null @@ -1,135 +0,0 @@ - null, - 'repo-name' => null, - 'github-repo-url' => null, - 'git-path' => null, - ); - - var $options_auto_approvals = array( - 'commit-test-hashes-api-scan-commit' => null, - ); - - protected function setUp(): void { - vipgoci_unittests_get_config_values( - 'git', - $this->options_git - ); - - vipgoci_unittests_get_config_values( - 'auto-approvals', - $this->options_auto_approvals - ); - $this->options = array_merge( - $this->options_git, - $this->options_auto_approvals - ); - - $this->options['hashes-api'] = true; - - $this->options[ 'github-token' ] = - vipgoci_unittests_get_config_value( - 'git-secrets', - 'github-token', - true // Fetch from secrets file - ); - - $this->options['token'] = - $this->options['github-token']; - - unset( $this->options['github-token'] ); - - $this->options['branches-ignore'] = array(); - - foreach ( - array( - 'hashes-api-url', - 'hashes-oauth-token', - 'hashes-oauth-token-secret', - 'hashes-oauth-consumer-key', - 'hashes-oauth-consumer-secret', - ) as $option_secret_key - ) { - $this->options[ $option_secret_key ] = - vipgoci_unittests_get_config_value( - 'auto-approvals-secrets', - $option_secret_key, - true // Fetch from secrets file - ); - } - - $this->options['commit'] = - $this->options['commit-test-hashes-api-scan-commit']; - - $this->options['local-git-repo'] = - vipgoci_unittests_setup_git_repo( - $this->options - ); - - $this->options['skip-draft-prs'] = false; - } - - protected function tearDown(): void { - if ( false !== $this->options['local-git-repo'] ) { - vipgoci_unittests_remove_git_repo( - $this->options['local-git-repo'] - ); - } - - $this->options = null; - $this->options_auto_approvals = null; - $this->options_git = null; - } - - /** - * @covers ::vipgoci_ap_hashes_api_scan_commit - */ - public function testApHashesApiScanCommitTest1() { - $options_test = vipgoci_unittests_options_test( - $this->options, - array( ), - $this - ); - - if ( -1 === $options_test ) { - return; - } - - vipgoci_unittests_output_suppress(); - - if ( false === $this->options['local-git-repo'] ) { - $this->markTestSkipped( - 'Could not set up git repository: ' . - vipgoci_unittests_output_get() - ); - - return; - } - - $commit_issues_submit = array(); - $commit_issues_stats = array(); - $auto_approved_files_arr = array(); - - vipgoci_ap_hashes_api_scan_commit( - $this->options, - $commit_issues_submit, - $commit_issues_stats, - $auto_approved_files_arr - ); - - vipgoci_unittests_output_unsuppress(); - - $this->assertSame( - array( - 'auto-approvable-6.php' => 'autoapprove-hashes-to-hashes', - ), - $auto_approved_files_arr - ); - } -} diff --git a/tests/integration/ApAutoApprovalTest.php b/tests/integration/AutoApprovalScanCommitTest.php similarity index 76% rename from tests/integration/ApAutoApprovalTest.php rename to tests/integration/AutoApprovalScanCommitTest.php index 46e0a3b79..96b1c04dd 100644 --- a/tests/integration/ApAutoApprovalTest.php +++ b/tests/integration/AutoApprovalScanCommitTest.php @@ -1,23 +1,49 @@ options_git = array( - 'git-path' => null, - 'github-repo-url' => null, - 'repo-owner' => null, - 'repo-name' => null, + 'git-path' => null, + 'github-repo-url' => null, + 'repo-owner' => null, + 'repo-name' => null, ); $this->options_auto_approvals = array( - 'pr-test-ap-auto-approval-1' => null, - 'commit-test-ap-auto-approval-1' => null, + 'pr-test-ap-auto-approval-1' => null, + 'commit-test-ap-auto-approval-1' => null, ); vipgoci_unittests_get_config_values( @@ -39,7 +65,7 @@ protected function setUp(): void { vipgoci_unittests_get_config_value( 'git-secrets', 'github-token', - true // Fetch from secrets file + true // Fetch from secrets file. ); $this->options['token'] = @@ -57,16 +83,16 @@ protected function setUp(): void { $this->options['autoapprove-label'] = 'Autoapproved Pull-Request'; - // Not used in this test, but needs to be defined + // Not used in this test, but needs to be defined. $this->options['autoapprove-filetypes'] = array( 'css', 'txt', 'json', - 'md' + 'md', ); - // Same, not used, but needs to be defined + // Same, not used, but needs to be defined. $this->options['autoapprove-php-nonfunctional-changes'] = false; $this->options['skip-draft-prs'] = false; @@ -79,6 +105,12 @@ protected function setUp(): void { $this->cleanup_prs(); } + /** + * Tear down function. Unset variables, remove + * local git repo if set up. + * + * @return void + */ protected function tearDown(): void { if ( false !== $this->options['local-git-repo'] ) { vipgoci_unittests_remove_git_repo( @@ -88,15 +120,21 @@ protected function tearDown(): void { $this->cleanup_prs(); - $this->options_git = null; - $this->options_auto_approvals = null; - $this->options = null; + unset( $this->options_git ); + unset( $this->options_auto_approvals ); + unset( $this->options ); } - private function cleanup_prs() { + /** + * Dismiss any previously submitted reviews + * to pull requests used in these tests. + * + * @return void + */ + private function cleanup_prs() :void { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -123,7 +161,7 @@ private function cleanup_prs() { (int) $this->options['pr-test-ap-auto-approval-1'] ) { printf( - 'Warning: Got unexpected Pull-Request item; pr-number=%d, expected-pr-number=%d, pr_item=%s', + 'Warning: Got unexpected pull request; pr-number=%d, expected-pr-number=%d, pr_item=%s', (int) $pr_item->number, (int) $this->options['pr-test-ap-auto-approval-1'], print_r( $pr_item, true ) @@ -132,9 +170,7 @@ private function cleanup_prs() { $this->safe_to_run = false; continue; - } - - else { + } else { if ( null === $this->safe_to_run ) { $this->safe_to_run = true; } @@ -151,10 +187,10 @@ private function cleanup_prs() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); - foreach( $pr_item_reviews as $pr_item_review ) { + foreach ( $pr_item_reviews as $pr_item_review ) { vipgoci_github_pr_review_dismiss( $this->options['repo-owner'], $this->options['repo-name'], @@ -169,14 +205,19 @@ private function cleanup_prs() { } } - private function pr_get_labels() { + /** + * Get labels for pull request used in these tests. + * + * @return mixed + */ + private function pr_get_labels() :mixed { $github_url = VIPGOCI_GITHUB_BASE_URL . '/' . 'repos/' . rawurlencode( $this->options['repo-owner'] ) . '/' . rawurlencode( $this->options['repo-name'] ) . '/' . 'issues/' . - rawurlencode( $this->options['pr-test-ap-auto-approval-1'] ) . '/' . + rawurlencode( (string) $this->options['pr-test-ap-auto-approval-1'] ) . '/' . 'labels'; $data = vipgoci_http_api_fetch_url( @@ -186,7 +227,7 @@ private function pr_get_labels() { $data = json_decode( $data ); - foreach( $data as $data_item ) { + foreach ( $data as $data_item ) { if ( $data_item->name === $this->options['autoapprove-label'] @@ -198,66 +239,16 @@ private function pr_get_labels() { return false; } - /** - * Test which PRs we get; make sure these - * are only the relevant ones. Mimics behaviour - * found in vipgoci_auto_approval_scan_commit(). - * - * @covers ::vipgoci_auto_approval - */ - - public function testAutoApproval1() { - $options_test = vipgoci_unittests_options_test( - $this->options, - array( ), - $this - ); - - if ( -1 === $options_test ) { - return; - } - - if ( false === $this->safe_to_run ) { - $this->markTestSkipped( - 'Test not safe to run due to earlier warnings' - ); - } - - vipgoci_unittests_output_suppress(); - - $prs_implicated = vipgoci_github_prs_implicated( - $this->options['repo-owner'], - $this->options['repo-name'], - $this->options['commit'], - $this->options['token'], - $this->options['branches-ignore'] - ); - - vipgoci_unittests_output_unsuppress(); - - $this->assertSame( - 1, - count( $prs_implicated ) - ); - - foreach ( $prs_implicated as $pr_item ) { - $this->assertSame( - $this->options['pr-test-ap-auto-approval-1'], - $pr_item->number - ); - } - } - /** * Test auto-approvals for PR that should * auto-appove. - - * @covers ::vipgoci_auto_approval + * + * @covers ::vipgoci_auto_approval_scan_commit */ - public function testAutoApproval2() { + public function testAutoApproval1() { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -286,11 +277,11 @@ public function testAutoApproval2() { } $auto_approved_files_arr = array( - 'file-1.php' => 'autoapprove-hashes-to-hashes', - 'file-2.css' => 'autoapprove-filetypes', - 'file-3.txt' => 'autoapprove-filetypes', + 'file-1.php' => 'autoapprove-approved-php-file', // Not a value used generally, only for testing. + 'file-2.css' => 'autoapprove-filetypes', + 'file-3.txt' => 'autoapprove-filetypes', 'file-4.json' => 'autoapprove-filetypes', - 'README.md' => 'autoapprove-filetypes', + 'README.md' => 'autoapprove-filetypes', ); $results = array(); @@ -328,6 +319,7 @@ public function testAutoApproval2() { ); foreach ( $prs_implicated as $pr_item ) { + // Ensure the correct pull request was retrieved. $this->assertSame( $this->options['pr-test-ap-auto-approval-1'], $pr_item->number @@ -344,7 +336,7 @@ public function testAutoApproval2() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); @@ -354,7 +346,7 @@ public function testAutoApproval2() { count( $pr_item_reviews ) ); - foreach( $pr_item_reviews as $pr_item_review ) { + foreach ( $pr_item_reviews as $pr_item_review ) { $this->assertSame( 'APPROVED', $pr_item_review->state @@ -373,13 +365,13 @@ public function testAutoApproval2() { /** * Test auto-approvals for PR that should * not auto-appove. - - * @covers ::vipgoci_auto_approval + * + * @covers ::vipgoci_auto_approval_scan_commit */ - public function testAutoApproval3() { + public function testAutoApproval2() { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -408,11 +400,11 @@ public function testAutoApproval3() { } $auto_approved_files_arr = array( - // note: file-1.php is NOT approved - 'file-2.css' => 'autoapprove-filetypes', - 'file-3.txt' => 'autoapprove-filetypes', + // Note: file-1.php is NOT approved. + 'file-2.css' => 'autoapprove-filetypes', + 'file-3.txt' => 'autoapprove-filetypes', 'file-4.json' => 'autoapprove-filetypes', - 'README.md' => 'autoapprove-filetypes', + 'README.md' => 'autoapprove-filetypes', ); $results = array(); @@ -450,6 +442,7 @@ public function testAutoApproval3() { ); foreach ( $prs_implicated as $pr_item ) { + // Ensure the correct pull request was retrieved. $this->assertSame( $this->options['pr-test-ap-auto-approval-1'], $pr_item->number @@ -466,7 +459,7 @@ public function testAutoApproval3() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); @@ -490,12 +483,12 @@ public function testAutoApproval3() { * not auto-appove, but should leave a comment * about one PHP file that is approved. * - * @covers ::vipgoci_auto_approval + * @covers ::vipgoci_auto_approval_scan_commit */ - public function testAutoApproval4() { + public function testAutoApproval3() { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -524,23 +517,17 @@ public function testAutoApproval4() { } $auto_approved_files_arr = array( - // note: file-1.php is approved, but - // some of the other files are not - 'file-1.php' => 'autoapprove-hashes-to-hashes', + // Note: file-1.php is approved, but + // some of the other files are not. + 'file-1.php' => 'autoapprove-approved-php-file', // Not a value used generally, only for testing. 'file-2.css' => 'autoapprove-filetypes', 'file-3.txt' => 'autoapprove-filetypes', - // file-4.json is not approved - 'README.md' => 'autoapprove-filetypes', + // file-4.json is not approved. + 'README.md' => 'autoapprove-filetypes', ); $results = array( - 'stats' => array( - VIPGOCI_STATS_HASHES_API => array( - $this->options['pr-test-ap-auto-approval-1'] => array( - 'info' => 0 - ) - ) - ) + 'stats' => array(), ); vipgoci_unittests_output_suppress(); @@ -567,6 +554,7 @@ public function testAutoApproval4() { ); foreach ( $prs_implicated as $pr_item ) { + // Ensure the correct pull request was retrieved. $this->assertSame( $this->options['pr-test-ap-auto-approval-1'], $pr_item->number @@ -583,7 +571,7 @@ public function testAutoApproval4() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); @@ -602,19 +590,10 @@ public function testAutoApproval4() { ); $this->assertSame( - 1, - $results['stats'] - [ VIPGOCI_STATS_HASHES_API ] - [ $this->options['pr-test-ap-auto-approval-1'] ] - [ 'info' ] - ); - - $this->assertSame( - 'file-1.php', - $results['issues'] - [ $this->options['pr-test-ap-auto-approval-1'] ] - [ 0 ] - ['file_name'] + array( + 'stats' => array(), + ), + $results ); } @@ -623,12 +602,12 @@ public function testAutoApproval4() { * sure we do not re-approve already approved * Pull-Requests. Make sure this really works. * - * @covers ::vipgoci_auto_approval + * @covers ::vipgoci_auto_approval_scan_commit */ - public function testAutoApproval5() { + public function testAutoApproval4() { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -670,7 +649,7 @@ public function testAutoApproval5() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); @@ -685,21 +664,15 @@ public function testAutoApproval5() { */ $auto_approved_files_arr = array( - // all files in the PR are approvable - 'file-1.php' => 'autoapprove-hashes-to-hashes', - 'file-2.css' => 'autoapprove-filetypes', - 'file-3.txt' => 'autoapprove-filetypes', + // All files in the PR are approvable. + 'file-1.php' => 'autoapprove-approved-php-file', // Not a value used generally, only for testing. + 'file-2.css' => 'autoapprove-filetypes', + 'file-3.txt' => 'autoapprove-filetypes', 'file-4.json' => 'autoapprove-filetypes', ); - + $results = array( - 'stats' => array( - VIPGOCI_STATS_HASHES_API => array( - $this->options['pr-test-ap-auto-approval-1'] => array( - 'info' => 0 - ) - ) - ) + 'stats' => array(), ); vipgoci_unittests_output_suppress(); @@ -725,7 +698,7 @@ public function testAutoApproval5() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); diff --git a/tests/integration/MainRunScanTotalCommentsMaxWarningPostTest.php b/tests/integration/MainRunScanTotalCommentsMaxWarningPostTest.php index 4cfe4059e..17056c904 100644 --- a/tests/integration/MainRunScanTotalCommentsMaxWarningPostTest.php +++ b/tests/integration/MainRunScanTotalCommentsMaxWarningPostTest.php @@ -66,7 +66,7 @@ protected function setUp(): void { $this->options['commit'] = $this->options['commit-test-run-scan-total-comments-max-warning-post']; - $this->options['pr_number'] = $this->options['pr-test-run-scan-total-comments-max-warning-post']; + $this->options['pr_number'] = (int) $this->options['pr-test-run-scan-total-comments-max-warning-post']; if ( ! empty( $this->options['github-token'] ) ) { $this->options['token'] = $this->options['github-token']; diff --git a/tests/integration/MiscApprovedFilesCommentsRemoveTest.php b/tests/integration/MiscApprovedFilesCommentsRemoveTest.php index da4ec42fe..68eb9575f 100644 --- a/tests/integration/MiscApprovedFilesCommentsRemoveTest.php +++ b/tests/integration/MiscApprovedFilesCommentsRemoveTest.php @@ -1,31 +1,57 @@ 'autoapprove-hashes-to-hashes', + 'bla-10.php' => 'autoapprove-approved-php-file', // Not a value used generally, only for testing. ); - $results_altered = $results; - vipgoci_unittests_output_suppress(); - + vipgoci_results_approved_files_comments_remove( array(), $results_altered, @@ -35,8 +61,8 @@ public function testRemoveCommentFromResults() { vipgoci_unittests_output_unsuppress(); $this->assertSame( - $results_desired, - json_encode( $results_altered ) + $results_expected, + $results_altered ); } } diff --git a/tests/unit/MainRunInitOptionsAutoapproveHashesOverlapTest.php b/tests/unit/MainRunInitOptionsAutoapproveHashesOverlapTest.php deleted file mode 100644 index e84227fc8..000000000 --- a/tests/unit/MainRunInitOptionsAutoapproveHashesOverlapTest.php +++ /dev/null @@ -1,105 +0,0 @@ -options = array( - 'hashes-api-url' => 'https://127.0.0.1:1234', - ); - } - - /** - * Clear variables and remove indication. - */ - protected function tearDown(): void { - vipgoci_unittests_remove_indication_for_test_id( 'MainRunInitOptionsAutoapproveHashesOverlapTest' ); - - unset( $this->options ); - } - - /** - * Check if correct message is printed, emulating exit. - * - * @covers ::vipgoci_run_init_options_autoapprove_hashes_overlap - */ - public function testAutoapproveOverLap() :void { - $this->options['autoapprove'] = false; - - ob_start(); - - vipgoci_run_init_options_autoapprove_hashes_overlap( - $this->options - ); - - $printed_data = ob_get_contents(); - - ob_end_clean(); - - /* - * Check if expected string was printed. - */ - $printed_data_found = strpos( - $printed_data, - 'Asking to use --hashes-api-url without --autoapproval set to true, but for hashes-to-hashes functionality to be useful, --autoapprove must be enabled. Otherwise the functionality will not really be used' - ); - - $this->assertNotFalse( - $printed_data_found - ); - } - - - /** - * Check if no message is printed, emulating non-exit. - * - * @covers ::vipgoci_run_init_options_autoapprove_hashes_overlap - */ - public function testAutoapproveNotOverLap() :void { - $this->options['autoapprove'] = true; - - ob_start(); - - vipgoci_run_init_options_autoapprove_hashes_overlap( - $this->options - ); - - $printed_data = ob_get_contents(); - - ob_end_clean(); - - /* - * Check if expected string was printed. - */ - $printed_data_found = strpos( - $printed_data, - 'Asking to use --hashes-api-url without --autoapproval set to true, but for hashes-to-hashes functionality to be useful, --autoapprove must be enabled. Otherwise the functionality will not really be used' - ); - - $this->assertFalse( - $printed_data_found - ); - } -} diff --git a/tests/unit/MainRunInitOptionsHashesOptionsTest.php b/tests/unit/MainRunInitOptionsHashesOptionsTest.php deleted file mode 100644 index 86c1d62e3..000000000 --- a/tests/unit/MainRunInitOptionsHashesOptionsTest.php +++ /dev/null @@ -1,102 +0,0 @@ -options = array(); - } - - /** - * Clear variable. - */ - protected function tearDown() :void { - unset( $this->options ); - } - - /** - * Check if hashes-to-hashes default options are correctly - * parsed and provided. - * - * @covers ::vipgoci_run_init_options_hashes_options - */ - public function testRunInitOptionsHashesOptionsDefault() :void { - $this->options = array( - 'hashes-api' => null, - 'hashes-api-url' => 'https://api.test.local/v1/api', - 'hashes-oauth-param1' => ' value1 ', - 'hashes-oauth-param2' => ' value2 ', - ); - - $this->hashes_oauth_arguments = array( - 'hashes-oauth-param1', - 'hashes-oauth-param2', - ); - - vipgoci_run_init_options_hashes_options( - $this->options, - $this->hashes_oauth_arguments - ); - - $this->assertSame( - array( - 'hashes-api' => false, - 'hashes-api-url' => 'https://api.test.local/v1/api', - 'hashes-oauth-param1' => 'value1', - 'hashes-oauth-param2' => 'value2', - ), - $this->options - ); - } - - /** - * Check if hashes-to-hashes options are correctly parsed. - * - * @covers ::vipgoci_run_init_options_hashes_options - */ - public function testRunInitOptionsHashesOptionsCustom() :void { - $this->options = array( - 'hashes-api' => 'true', - 'hashes-api-url' => 'https://api.test.local/v1/api/', - 'hashes-oauth-param1' => ' value1 ', - 'hashes-oauth-param2' => ' value2 ', - ); - - $this->hashes_oauth_arguments = array( - 'hashes-oauth-param1', - 'hashes-oauth-param2', - ); - - vipgoci_run_init_options_hashes_options( - $this->options, - $this->hashes_oauth_arguments - ); - - $this->assertSame( - array( - 'hashes-api' => true, - 'hashes-api-url' => 'https://api.test.local/v1/api', - 'hashes-oauth-param1' => 'value1', - 'hashes-oauth-param2' => 'value2', - ), - $this->options - ); - } -} diff --git a/tests/unit/OptionsReadRepositoryConfigFileTest.php b/tests/unit/OptionsReadRepositoryConfigFileTest.php index e326c9b59..44bb51251 100644 --- a/tests/unit/OptionsReadRepositoryConfigFileTest.php +++ b/tests/unit/OptionsReadRepositoryConfigFileTest.php @@ -98,7 +98,7 @@ private function getOverritableMock(): array { 'valid_values' => null, ), - 'hashes-api' => array( + 'test-api' => array( 'type' => 'boolean', 'valid_values' => array( true, false ), ), @@ -129,6 +129,6 @@ private function getOverritableMock(): array { * @return array */ private function getOptionsMock(): array { - return json_decode( '{"repo-owner":"any","repo-name":"any","commit":"any","token":"any","local-git-repo":"any","phpcs-path":"any","phpcs":true,"enforce-https-urls":false,"repo-options":true,"env-options":[],"branches-ignore":[],"phpcs-standard":["WordPress-VIP-Go"],"phpcs-sniffs-include":[],"phpcs-sniffs-exclude":[],"phpcs-runtime-set":[],"phpcs-skip-folders":[],"review-comments-ignore":[],"dismissed-reviews-exclude-reviews-from-team":[],"phpcs-severity":1,"php-path":"php","hashes-api":false,"svg-checks":false,"svg-scanner-path":"invalid","debug-level":0,"max-exec-time":0,"review-comments-max":10,"review-comments-total-max":200,"skip-large-files-limit":15000,"skip-draft-prs":false,"phpcs-skip-folders-in-repo-options-file":false,"phpcs-skip-scanning-via-labels-allowed":false,"lint":true,"lint-modified-files-only":true,"skip-large-files":true,"lint-skip-folders-in-repo-options-file":false,"dismiss-stale-reviews":false,"dismissed-reviews-repost-comments":true,"review-comments-sort":false,"review-comments-include-severity":false,"phpcs-standard-file":false,"skip-execution":false,"autoapprove":false,"autoapprove-php-nonfunctional-changes":false,"autoapprove-filetypes":[],"post-generic-pr-support-comments":false,"post-generic-pr-support-comments-on-drafts":false,"post-generic-pr-support-comments-string":null,"post-generic-pr-support-comments-skip-if-label-exists":null,"post-generic-pr-support-comments-branches":[],"post-generic-pr-support-comments-repo-meta-match":[],"set-support-level-label":false,"set-support-level-label-prefix":null,"set-support-level-field":null,"repo-meta-api-user-id":null,"repo-meta-api-access-token":null,"lint-skip-folders":[],"repo-options-allowed":["skip-execution","skip-draft-prs","review-comments-sort","review-comments-include-severity","phpcs","phpcs-severity","post-generic-pr-support-comments","phpcs-sniffs-include","phpcs-sniffs-exclude","hashes-api","svg-checks","autoapprove","autoapprove-php-nonfunctional-changes","lint-modified-files-only"],"autoapprove-label":false}', true ); + return json_decode( '{"repo-owner":"any","repo-name":"any","commit":"any","token":"any","local-git-repo":"any","phpcs-path":"any","phpcs":true,"enforce-https-urls":false,"repo-options":true,"env-options":[],"branches-ignore":[],"phpcs-standard":["WordPress-VIP-Go"],"phpcs-sniffs-include":[],"phpcs-sniffs-exclude":[],"phpcs-runtime-set":[],"phpcs-skip-folders":[],"review-comments-ignore":[],"dismissed-reviews-exclude-reviews-from-team":[],"phpcs-severity":1,"php-path":"php","test-api":false,"svg-checks":false,"svg-scanner-path":"invalid","debug-level":0,"max-exec-time":0,"review-comments-max":10,"review-comments-total-max":200,"skip-large-files-limit":15000,"skip-draft-prs":false,"phpcs-skip-folders-in-repo-options-file":false,"phpcs-skip-scanning-via-labels-allowed":false,"lint":true,"lint-modified-files-only":true,"skip-large-files":true,"lint-skip-folders-in-repo-options-file":false,"dismiss-stale-reviews":false,"dismissed-reviews-repost-comments":true,"review-comments-sort":false,"review-comments-include-severity":false,"phpcs-standard-file":false,"skip-execution":false,"autoapprove":false,"autoapprove-php-nonfunctional-changes":false,"autoapprove-filetypes":[],"post-generic-pr-support-comments":false,"post-generic-pr-support-comments-on-drafts":false,"post-generic-pr-support-comments-string":null,"post-generic-pr-support-comments-skip-if-label-exists":null,"post-generic-pr-support-comments-branches":[],"post-generic-pr-support-comments-repo-meta-match":[],"set-support-level-label":false,"set-support-level-label-prefix":null,"set-support-level-field":null,"repo-meta-api-user-id":null,"repo-meta-api-access-token":null,"lint-skip-folders":[],"repo-options-allowed":["skip-execution","skip-draft-prs","review-comments-sort","review-comments-include-severity","phpcs","phpcs-severity","post-generic-pr-support-comments","phpcs-sniffs-include","phpcs-sniffs-exclude","test-api","svg-checks","autoapprove","autoapprove-php-nonfunctional-changes","lint-modified-files-only"],"autoapprove-label":false}', true ); } } diff --git a/tests/unit/ReportCreateScanDetailsAutoApproveConfigurationTest.php b/tests/unit/ReportCreateScanDetailsAutoApproveConfigurationTest.php index f4a302370..3ba3959dd 100644 --- a/tests/unit/ReportCreateScanDetailsAutoApproveConfigurationTest.php +++ b/tests/unit/ReportCreateScanDetailsAutoApproveConfigurationTest.php @@ -72,13 +72,6 @@ public function testCreateDetails1(): void { ) ); - $this->assertFalse( - strpos( - $actual_output, - 'Auto-approval DB enabled' - ) - ); - $this->assertFalse( strpos( $actual_output, @@ -96,7 +89,6 @@ public function testCreateDetails2(): void { $this->options['autoapprove'] = true; $this->options['autoapprove-php-nonfunctional-changes'] = true; $this->options['autoapprove-filetypes'] = array( 'txt', 'ini' ); - $this->options['hashes-api'] = false; $actual_output = vipgoci_report_create_scan_details_auto_approve_configuration( $this->options @@ -133,13 +125,5 @@ public function testCreateDetails2(): void { '
  • txt
  • ini
  • ' ) ); - - $this->assertNotFalse( - strpos( - $actual_output, - '

    Auto-approval DB enabled:' . PHP_EOL . - 'false

    ' - ) - ); } } diff --git a/tests/unit/ResultsFilterIgnorableTest.php b/tests/unit/ResultsFilterIgnorableTest.php index 8c2888831..b2b826d01 100644 --- a/tests/unit/ResultsFilterIgnorableTest.php +++ b/tests/unit/ResultsFilterIgnorableTest.php @@ -42,10 +42,10 @@ public function testFilterIgnorable1() { $options['review-comments-ignore'] ); - $results_desired = '{"issues":{"32":[{"type":"phpcs","file_name":"bla-8.php","file_line":3,"issue":{"message":"All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found \'mysql_query\'.","source":"WordPress.Security.EscapeOutput.OutputNotEscaped","severity":5,"fixable":false,"type":"ERROR","line":3,"column":6,"level":"ERROR"}},{"type":"phpcs","file_name":"bla-8.php","file_line":3,"issue":{"message":"Extension \'mysql_\' is deprecated since PHP 5.5 and removed since PHP 7.0; Use mysqli instead","source":"PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved","severity":5,"fixable":false,"type":"ERROR","line":3,"column":6,"level":"ERROR"}}]},"stats":{"phpcs":{"32":{"error":2,"warning":0,"info":0}},"lint":{"32":{"error":0,"warning":0,"info":0}},"hashes-api":{"32":{"error":0,"warning":0,"info":0}}}}'; + $results_expected = '{"issues":{"32":[{"type":"phpcs","file_name":"bla-8.php","file_line":3,"issue":{"message":"All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found \'mysql_query\'.","source":"WordPress.Security.EscapeOutput.OutputNotEscaped","severity":5,"fixable":false,"type":"ERROR","line":3,"column":6,"level":"ERROR"}},{"type":"phpcs","file_name":"bla-8.php","file_line":3,"issue":{"message":"Extension \'mysql_\' is deprecated since PHP 5.5 and removed since PHP 7.0; Use mysqli instead","source":"PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved","severity":5,"fixable":false,"type":"ERROR","line":3,"column":6,"level":"ERROR"}}]},"stats":{"phpcs":{"32":{"error":2,"warning":0,"info":0}},"lint":{"32":{"error":0,"warning":0,"info":0}},"test-api":{"32":{"error":0,"warning":0,"info":0}}}}'; $results_altered = json_decode( - '{"issues":{"32":[{"type":"phpcs","file_name":"bla-10.php","file_line":7,"issue":{"message":"json_encode() is discouraged. Use wp_json_encode() instead.","source":"WordPress.WP.AlternativeFunctions.json_encode_json_encode","severity":5,"fixable":false,"type":"WARNING","line":7,"column":6,"level":"WARNING"}},{"type":"phpcs","file_name":"bla-8.php","file_line":3,"issue":{"message":"All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found \'mysql_query\'.","source":"WordPress.Security.EscapeOutput.OutputNotEscaped","severity":5,"fixable":false,"type":"ERROR","line":3,"column":6,"level":"ERROR"}},{"type":"phpcs","file_name":"bla-8.php","file_line":3,"issue":{"message":"Extension \'mysql_\' is deprecated since PHP 5.5 and removed since PHP 7.0; Use mysqli instead","source":"PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved","severity":5,"fixable":false,"type":"ERROR","line":3,"column":6,"level":"ERROR"}}]},"stats":{"phpcs":{"32":{"error":2,"warning":1,"info":0}},"lint":{"32":{"error":0,"warning":0,"info":0}},"hashes-api":{"32":{"error":0,"warning":0,"info":0}}}}', + '{"issues":{"32":[{"type":"phpcs","file_name":"bla-10.php","file_line":7,"issue":{"message":"json_encode() is discouraged. Use wp_json_encode() instead.","source":"WordPress.WP.AlternativeFunctions.json_encode_json_encode","severity":5,"fixable":false,"type":"WARNING","line":7,"column":6,"level":"WARNING"}},{"type":"phpcs","file_name":"bla-8.php","file_line":3,"issue":{"message":"All output should be run through an escaping function (see the Security sections in the WordPress Developer Handbooks), found \'mysql_query\'.","source":"WordPress.Security.EscapeOutput.OutputNotEscaped","severity":5,"fixable":false,"type":"ERROR","line":3,"column":6,"level":"ERROR"}},{"type":"phpcs","file_name":"bla-8.php","file_line":3,"issue":{"message":"Extension \'mysql_\' is deprecated since PHP 5.5 and removed since PHP 7.0; Use mysqli instead","source":"PHPCompatibility.Extensions.RemovedExtensions.mysql_DeprecatedRemoved","severity":5,"fixable":false,"type":"ERROR","line":3,"column":6,"level":"ERROR"}}]},"stats":{"phpcs":{"32":{"error":2,"warning":1,"info":0}},"lint":{"32":{"error":0,"warning":0,"info":0}},"test-api":{"32":{"error":0,"warning":0,"info":0}}}}', true ); @@ -59,7 +59,7 @@ public function testFilterIgnorable1() { vipgoci_unittests_output_unsuppress(); $this->assertSame( - $results_desired, + $results_expected, json_encode( $results_altered ) diff --git a/tests/unit/SkipFileTestFunctionality.php b/tests/unit/SkipFileTestFunctionality.php index 9e8be7dd0..5b872e520 100644 --- a/tests/unit/SkipFileTestFunctionality.php +++ b/tests/unit/SkipFileTestFunctionality.php @@ -487,7 +487,7 @@ private function getSkippedSingleFileCommentMock(): string { */ private function getSkippedFilesCommentMock(): string { $mock = - '**hashes-api**-scanning skipped + '**test-api**-scanning skipped *** **skipped-files** @@ -510,7 +510,7 @@ private function getSkippedFilesCommentMock(): string { */ private function getCommentMock(): string { $commentMock = - '**hashes-api**'; + '**test-api**'; return $commentMock; } diff --git a/tests/unit/StatsStatsInitTest.php b/tests/unit/StatsStatsInitTest.php index c71558158..d03b4bccc 100644 --- a/tests/unit/StatsStatsInitTest.php +++ b/tests/unit/StatsStatsInitTest.php @@ -27,9 +27,9 @@ public function testStatsInit() { vipgoci_stats_init( array( - 'phpcs' => true, - 'lint' => true, - 'hashes-api' => false + 'phpcs' => true, + 'lint' => true, + 'test-api' => false, ), array( $pr_item1, @@ -59,7 +59,7 @@ public function testStatsInit() { 'warning' => 0, 'info' => 0, ), - // no hashes-api; not supposed to initialize that + // no test-api; not supposed to initialize that. ), VIPGOCI_STATS_LINT => array( @@ -73,7 +73,7 @@ public function testStatsInit() { 'warning' => 0, 'info' => 0, ), - // no hashes-api; not supposed to initialize that + // no test-api; not supposed to initialize that. ), ), ); diff --git a/unittests.ini.dist b/unittests.ini.dist index e6899ac34..b2d448a07 100644 --- a/unittests.ini.dist +++ b/unittests.ini.dist @@ -22,7 +22,7 @@ phpcs-sniffs-existing=WordPressVIPMinimum.Functions.StripTags,WordPressVIPMinimu commit-test-phpcs-scan-commit-1=9882d7da670f8f45f3431e3c7da80a00d6af4b5e commit-test-phpcs-scan-commit-2=1c63fd89388e3f5563869ca5dc54ae7d4da9f1a4 commit-test-phpcs-scan-commit-3=ba678917a65855a3129ff4f95de3b2ede3d76f4b -commit-test-phpcs-scan-commit-4=4702a1416ee36510a3cc43c68e631be572acddfe +commit-test-phpcs-scan-commit-4=62adb9b6cd459c086fc10bdc98d57891fb9e55d4 commit-test-phpcs-scan-commit-5=dd60c66a7c1e27fc78c400f0c5160c9a80c4bd49 commit-test-phpcs-scan-commit-6=84985c25706e83dada8c40061e6f1496cfe3c522 @@ -54,10 +54,8 @@ commit-test-svg-files-1=44ed02088a503d84522f6e9b40eb0788e0935c24 commit-test-svg-files-2b=bc23e8530ad4881af447646c6e53f2acd1ef364d autoapprove-filetypes=txt,gif,jpg,png,md -commit-test-ap-hashes-file-approved-1=3dc1135e4bc1dbc5c5a57cdf45c2ba2bd5690b51 -commit-test-ap-auto-approval-1=a96b12cc806fef600818085cf26dad61321ddc81 -pr-test-ap-auto-approval-1=40 -commit-test-hashes-api-scan-commit=f8a50f557b25dca72978854eae97bd8ba07f0a55 +commit-test-ap-auto-approval-1=5a891b161ec88bd885cd09e3fd370399921508d8 +pr-test-ap-auto-approval-1=46 commit-test-ap-nonfunctionalchanges-1b=c4a3fe05f67f329cdf884392df29e55f5b8d4498