From 5f081fc72ff04ac1c5ec287103338cbd18f6bccf Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 13:21:40 +0000 Subject: [PATCH 01/35] Remove ap-hashes-api.php from files required. --- requires.php | 1 - 1 file changed, 1 deletion(-) 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'; From f2c40160c63d16daf1728dcbcaeb1141340c29a7 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 13:44:52 +0000 Subject: [PATCH 02/35] Removing hashes-api support. --- auto-approval.php | 172 +--------------------------------------------- 1 file changed, 3 insertions(+), 169 deletions(-) diff --git a/auto-approval.php b/auto-approval.php index 84f87535a..219cdbf22 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( From f31377a9b1cb3f3b2d9f0958ed78ff82ddfecb85 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 13:47:41 +0000 Subject: [PATCH 03/35] Removing hashes-api from report. --- reports.php | 1 - 1 file changed, 1 deletion(-) diff --git a/reports.php b/reports.php index f28166382..8cf737d22 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 ( From a096b79b43288070095daf3d7388f87508d4eb16 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 13:52:14 +0000 Subject: [PATCH 04/35] Remove reference to hashes-api. --- ...teScanDetailsAutoApproveConfigurationTest.php | 16 ---------------- 1 file changed, 16 deletions(-) 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

    ' - ) - ); } } From 19eb2970bd97810c88b3a460b49307ad452ac756 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 13:59:53 +0000 Subject: [PATCH 05/35] Remove hashes-api support, including options and function calls. --- main.php | 153 +------------------------------------------------------ 1 file changed, 1 insertion(+), 152 deletions(-) diff --git a/main.php b/main.php index 5909c3344..4045ef296 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, From 8f60e1ca281e4013a0681259313997081c48ad64 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 14:01:14 +0000 Subject: [PATCH 06/35] Removing VIPGOCI_STATS_HASHES_API reference from statistics. --- main.php | 1 - 1 file changed, 1 deletion(-) diff --git a/main.php b/main.php index 4045ef296..7d8d9aa2e 100755 --- a/main.php +++ b/main.php @@ -3006,7 +3006,6 @@ function vipgoci_run_init_vars() :array { 'stats' => array( VIPGOCI_STATS_PHPCS => null, VIPGOCI_STATS_LINT => null, - VIPGOCI_STATS_HASHES_API => null, ), ); From a2e3648656ecc7916a4200ae4812459f762973f9 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 15:13:48 +0000 Subject: [PATCH 07/35] Remove VIPGOCI_STATS_HASHES_API from vipgoci_stats_init(). --- statistics.php | 1 - 1 file changed, 1 deletion(-) 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 ) { From 4a1b8b3294ef73361617c505ec89eae1e34b4aee Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 15:16:25 +0000 Subject: [PATCH 08/35] Remove VIPGOCI_APPROVAL_HASHES_API from vipgoci_report_submit_pr_generic_comment_from_results(). --- reports.php | 1 - 1 file changed, 1 deletion(-) diff --git a/reports.php b/reports.php index 8cf737d22..72d1ef403 100644 --- a/reports.php +++ b/reports.php @@ -845,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( From 9fea87241ec224b650c0334b47fcd00c6f65e9a9 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 15:18:56 +0000 Subject: [PATCH 09/35] Update comment. --- auto-approval.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auto-approval.php b/auto-approval.php index 219cdbf22..d4eae7b79 100644 --- a/auto-approval.php +++ b/auto-approval.php @@ -516,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, From 6228142903f1d425a60e531c393e8f288b71bd40 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 15:34:52 +0000 Subject: [PATCH 10/35] Update test; change hashes-api to test-api. --- tests/unit/StatsStatsInitTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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. ), ), ); From 391052c01371581a59bb9e1cde2dfddd3d0cb9ea Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 15:38:13 +0000 Subject: [PATCH 11/35] Update test; change hashes-api to test-api. --- tests/unit/SkipFileTestFunctionality.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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; } From 2a9c0dd228d50c3f221500f5ecacabdcd4184259 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 15:44:31 +0000 Subject: [PATCH 12/35] Update test; change hashes-api to test-api. --- tests/unit/ResultsFilterIgnorableTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/ResultsFilterIgnorableTest.php b/tests/unit/ResultsFilterIgnorableTest.php index 8c2888831..fe580f352 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_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}},"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 ); From 2bd1a6ddebef2e55edd66e6749ea5e2df285aa75 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 15:44:45 +0000 Subject: [PATCH 13/35] Remove reference to hashes-api related commit. --- unittests.ini.dist | 1 - 1 file changed, 1 deletion(-) diff --git a/unittests.ini.dist b/unittests.ini.dist index e6899ac34..8106e9f30 100644 --- a/unittests.ini.dist +++ b/unittests.ini.dist @@ -57,7 +57,6 @@ 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-nonfunctionalchanges-1b=c4a3fe05f67f329cdf884392df29e55f5b8d4498 From 3f540d39d326aa6b7c681264d3f35d0ec91fffa6 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 15:59:43 +0000 Subject: [PATCH 14/35] Removing hashes-api support. --- ap-hashes-api.php | 371 ---------------------------------------------- 1 file changed, 371 deletions(-) delete mode 100644 ap-hashes-api.php 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' ); -} - From 4befebdd6039be5a06b848dc1359f8f64fcfd6a6 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 16:02:19 +0000 Subject: [PATCH 15/35] Remove another reference to hashes-api related commit. --- unittests.ini.dist | 1 - 1 file changed, 1 deletion(-) diff --git a/unittests.ini.dist b/unittests.ini.dist index 8106e9f30..ce4d82005 100644 --- a/unittests.ini.dist +++ b/unittests.ini.dist @@ -54,7 +54,6 @@ 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 From f3c0fd21c3a3ac0b13b6e65ae3f9fbdb148b9ff2 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 16:08:29 +0000 Subject: [PATCH 16/35] Remove test for vipgoci_ap_hashes_api_scan_commit() --- .../integration/ApHashesApiScanCommitTest.php | 135 ------------------ 1 file changed, 135 deletions(-) delete mode 100644 tests/integration/ApHashesApiScanCommitTest.php 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 - ); - } -} From 0956128eedf7ea550c0eae5560a1de4a085d3b0d Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 16:14:01 +0000 Subject: [PATCH 17/35] Remove test for vipgoci_ap_hashes_api_file_approved() --- .../ApHashesApiFileApprovedTest.php | 199 ------------------ 1 file changed, 199 deletions(-) delete mode 100644 tests/integration/ApHashesApiFileApprovedTest.php 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 - ); - } -} From 571af60e4f27d67265e709a26b09b07e79b583af Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 16:19:05 +0000 Subject: [PATCH 18/35] Remove test for vipgoci_run_init_options_hashes_options(). --- .../MainRunInitOptionsHashesOptionsTest.php | 102 ------------------ 1 file changed, 102 deletions(-) delete mode 100644 tests/unit/MainRunInitOptionsHashesOptionsTest.php 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 - ); - } -} From 432116fe2d6abf4915ad7f25f1a52c743a917e5c Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 16:21:46 +0000 Subject: [PATCH 19/35] Update test; change hashes-api to test-api. --- tests/unit/OptionsReadRepositoryConfigFileTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 ); } } From 38bd09b86523804602347b6257664fa34c4811fc Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 16:24:11 +0000 Subject: [PATCH 20/35] Removing test for vipgoci_run_init_options_autoapprove_hashes_overlap() --- ...nitOptionsAutoapproveHashesOverlapTest.php | 105 ------------------ 1 file changed, 105 deletions(-) delete mode 100644 tests/unit/MainRunInitOptionsAutoapproveHashesOverlapTest.php 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 - ); - } -} From 3a5e1857434aadc0867e6f0619e9754220ecd9f8 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 16:24:30 +0000 Subject: [PATCH 21/35] Removing MainRunInitOptionsAutoapproveHashesOverlapTest as test is removed. --- log.php | 1 - 1 file changed, 1 deletion(-) 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' ) ) ) From 3c26cedddff9d5601cedd7b0b070f88142557c39 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 17:15:43 +0000 Subject: [PATCH 22/35] Remove hashes-api related defines. --- defines.php | 4 ---- 1 file changed, 4 deletions(-) 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. From 16437a53141056fc58cc0bc21f5d09b8e21f1d7a Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 27 Jun 2022 17:41:04 +0000 Subject: [PATCH 23/35] Removing references to hashes-api. --- README.md | 44 ++------------------------------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 38280e372..0dba9e203 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,13 @@ 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. +Note that some tests will require a GitHub token to submit POST/PUT requests to GitHub in order to complete, and some may 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. From 0ddd5ab574145c26d6d852ed5b7362a46ee10178 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Tue, 28 Jun 2022 11:48:55 +0000 Subject: [PATCH 24/35] Update test as hashes-api support is removed. --- tests/integration/ApAutoApprovalTest.php | 33 +++++------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/tests/integration/ApAutoApprovalTest.php b/tests/integration/ApAutoApprovalTest.php index 46e0a3b79..3d1b24040 100644 --- a/tests/integration/ApAutoApprovalTest.php +++ b/tests/integration/ApAutoApprovalTest.php @@ -534,13 +534,7 @@ public function testAutoApproval4() { ); $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(); @@ -602,19 +596,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 ); } @@ -693,13 +678,7 @@ public function testAutoApproval5() { ); $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(); From b6e37cf5a156eadcedb990f55d27d6aa768becde Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Tue, 28 Jun 2022 11:55:41 +0000 Subject: [PATCH 25/35] Cast to integer. --- .../integration/MainRunScanTotalCommentsMaxWarningPostTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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']; From e50130ce8ef2a38ec2b48441bcceb1843f6167f7 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Tue, 28 Jun 2022 11:56:37 +0000 Subject: [PATCH 26/35] Update commit ID and PR number due to changes to tests/integration/ApAutoApprovalTest.php --- unittests.ini.dist | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unittests.ini.dist b/unittests.ini.dist index ce4d82005..01ab07c5e 100644 --- a/unittests.ini.dist +++ b/unittests.ini.dist @@ -54,8 +54,8 @@ commit-test-svg-files-1=44ed02088a503d84522f6e9b40eb0788e0935c24 commit-test-svg-files-2b=bc23e8530ad4881af447646c6e53f2acd1ef364d autoapprove-filetypes=txt,gif,jpg,png,md -commit-test-ap-auto-approval-1=a96b12cc806fef600818085cf26dad61321ddc81 -pr-test-ap-auto-approval-1=40 +commit-test-ap-auto-approval-1=5a891b161ec88bd885cd09e3fd370399921508d8 +pr-test-ap-auto-approval-1=46 commit-test-ap-nonfunctionalchanges-1b=c4a3fe05f67f329cdf884392df29e55f5b8d4498 From e65ac26fc2865a69dfa40a3a4729de2d7310f850 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Tue, 28 Jun 2022 12:24:07 +0000 Subject: [PATCH 27/35] Update test; apply WP CS, remove hash-api. --- .../MiscApprovedFilesCommentsRemoveTest.php | 46 ++++++++++++++----- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/integration/MiscApprovedFilesCommentsRemoveTest.php b/tests/integration/MiscApprovedFilesCommentsRemoveTest.php index da4ec42fe..265b6d381 100644 --- a/tests/integration/MiscApprovedFilesCommentsRemoveTest.php +++ b/tests/integration/MiscApprovedFilesCommentsRemoveTest.php @@ -1,31 +1,55 @@ 'autoapprove-hashes-to-hashes', ); - $results_altered = $results; - vipgoci_unittests_output_suppress(); - + vipgoci_results_approved_files_comments_remove( array(), $results_altered, @@ -35,8 +59,8 @@ public function testRemoveCommentFromResults() { vipgoci_unittests_output_unsuppress(); $this->assertSame( - $results_desired, - json_encode( $results_altered ) + $results_expected, + $results_altered ); } } From 584f66162072d71145c4a95996291374cd98db71 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 4 Jul 2022 13:53:57 +0000 Subject: [PATCH 28/35] Update test; add comments, remove final hashes reference, update @covers, remove one duplicate test. --- tests/integration/ApAutoApprovalTest.php | 77 +++++------------------- 1 file changed, 15 insertions(+), 62 deletions(-) diff --git a/tests/integration/ApAutoApprovalTest.php b/tests/integration/ApAutoApprovalTest.php index 3d1b24040..825a357d8 100644 --- a/tests/integration/ApAutoApprovalTest.php +++ b/tests/integration/ApAutoApprovalTest.php @@ -198,63 +198,13 @@ 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( ), @@ -286,7 +236,7 @@ public function testAutoApproval2() { } $auto_approved_files_arr = array( - 'file-1.php' => 'autoapprove-hashes-to-hashes', + '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', @@ -328,6 +278,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 @@ -374,9 +325,9 @@ 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( ), @@ -450,6 +401,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 @@ -490,9 +442,9 @@ 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( ), @@ -526,7 +478,7 @@ 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', + '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 @@ -561,6 +513,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 @@ -608,9 +561,9 @@ 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( ), @@ -671,7 +624,7 @@ public function testAutoApproval5() { $auto_approved_files_arr = array( // all files in the PR are approvable - 'file-1.php' => 'autoapprove-hashes-to-hashes', + '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', From 835893179648685acd7a3b43239f5362411a5225 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 4 Jul 2022 14:23:17 +0000 Subject: [PATCH 29/35] Apply WP CS, add comments. Rename file. --- ...est.php => AutoApprovalScanCommitTest.php} | 151 +++++++++++------- 1 file changed, 96 insertions(+), 55 deletions(-) rename tests/integration/{ApAutoApprovalTest.php => AutoApprovalScanCommitTest.php} (81%) diff --git a/tests/integration/ApAutoApprovalTest.php b/tests/integration/AutoApprovalScanCommitTest.php similarity index 81% rename from tests/integration/ApAutoApprovalTest.php rename to tests/integration/AutoApprovalScanCommitTest.php index 825a357d8..09f504bde 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. + * + * @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. + * + * @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'] @@ -207,7 +248,7 @@ private function pr_get_labels() { public function testAutoApproval1() { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -236,11 +277,11 @@ public function testAutoApproval1() { } $auto_approved_files_arr = array( - '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-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(); @@ -295,7 +336,7 @@ public function testAutoApproval1() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); @@ -305,7 +346,7 @@ public function testAutoApproval1() { 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 @@ -324,13 +365,13 @@ public function testAutoApproval1() { /** * Test auto-approvals for PR that should * not auto-appove. - + * * @covers ::vipgoci_auto_approval_scan_commit */ public function testAutoApproval2() { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -359,11 +400,11 @@ public function testAutoApproval2() { } $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(); @@ -418,7 +459,7 @@ public function testAutoApproval2() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); @@ -447,7 +488,7 @@ public function testAutoApproval2() { public function testAutoApproval3() { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -476,13 +517,13 @@ public function testAutoApproval3() { } $auto_approved_files_arr = array( - // note: file-1.php is approved, but - // some of the other files are not + // 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( @@ -530,7 +571,7 @@ public function testAutoApproval3() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); @@ -566,7 +607,7 @@ public function testAutoApproval3() { public function testAutoApproval4() { $options_test = vipgoci_unittests_options_test( $this->options, - array( ), + array(), $this ); @@ -608,7 +649,7 @@ public function testAutoApproval4() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); @@ -623,13 +664,13 @@ public function testAutoApproval4() { */ $auto_approved_files_arr = array( - // 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', + // 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(), ); @@ -657,7 +698,7 @@ public function testAutoApproval4() { 'login' => 'myself', 'state' => array( 'APPROVED' ), ), - true // skip cache + true // Skip cache. ); vipgoci_unittests_output_unsuppress(); From 6f1dab05c2fc72c57e3c03ab47ef392116d12e3b Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 4 Jul 2022 14:24:50 +0000 Subject: [PATCH 30/35] Update PHPCS setting due to changes to pull request needed for #282. --- unittests.ini.dist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unittests.ini.dist b/unittests.ini.dist index 01ab07c5e..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 From 30c2a531ca2ca49f26247a0ef445f6b21591cc7c Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 4 Jul 2022 14:36:51 +0000 Subject: [PATCH 31/35] Update comments, change value. --- tests/integration/MiscApprovedFilesCommentsRemoveTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/MiscApprovedFilesCommentsRemoveTest.php b/tests/integration/MiscApprovedFilesCommentsRemoveTest.php index 265b6d381..68eb9575f 100644 --- a/tests/integration/MiscApprovedFilesCommentsRemoveTest.php +++ b/tests/integration/MiscApprovedFilesCommentsRemoveTest.php @@ -31,9 +31,11 @@ protected function setUp() :void { * Test function and compare result with expected results. * Expects results for one approved file to be removed. * + * @return void + * * @covers ::vipgoci_results_approved_files_comments_remove */ - public function testRemoveCommentFromResults() { + public function testRemoveCommentFromResults() :void { $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}}}}', true @@ -45,7 +47,7 @@ public function testRemoveCommentFromResults() { ); $auto_approved_files_arr = array( - 'bla-10.php' => 'autoapprove-hashes-to-hashes', + 'bla-10.php' => 'autoapprove-approved-php-file', // Not a value used generally, only for testing. ); vipgoci_unittests_output_suppress(); From 55218f8258a670bc6ebcff8a571c032f42344d4b Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 4 Jul 2022 14:47:03 +0000 Subject: [PATCH 32/35] Rename variable. --- tests/unit/ResultsFilterIgnorableTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/ResultsFilterIgnorableTest.php b/tests/unit/ResultsFilterIgnorableTest.php index fe580f352..b2b826d01 100644 --- a/tests/unit/ResultsFilterIgnorableTest.php +++ b/tests/unit/ResultsFilterIgnorableTest.php @@ -42,7 +42,7 @@ 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}},"test-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}},"test-api":{"32":{"error":0,"warning":0,"info":0}}}}', @@ -59,7 +59,7 @@ public function testFilterIgnorable1() { vipgoci_unittests_output_unsuppress(); $this->assertSame( - $results_desired, + $results_expected, json_encode( $results_altered ) From b61bb328ed725eb1cd25aebf207553dbe2c2f904 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 4 Jul 2022 14:47:31 +0000 Subject: [PATCH 33/35] Update comment. --- tests/integration/AutoApprovalScanCommitTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/AutoApprovalScanCommitTest.php b/tests/integration/AutoApprovalScanCommitTest.php index 09f504bde..da46fb356 100644 --- a/tests/integration/AutoApprovalScanCommitTest.php +++ b/tests/integration/AutoApprovalScanCommitTest.php @@ -27,7 +27,7 @@ final class AutoApprovalScanCommitTest extends TestCase { private mixed $safe_to_run = null; /** - * Setup function. Require files, etc. + * Setup function. Require files, set up variables, etc. * * @return void */ From 0eb57a81dcabac4ca637ccba8acb0e410d4fbaa6 Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 4 Jul 2022 14:48:10 +0000 Subject: [PATCH 34/35] Add header, refine text slightly. --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0dba9e203..dca7baaad 100644 --- a/README.md +++ b/README.md @@ -729,7 +729,9 @@ 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, and some may need access 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. From 66eeedd9c088dd5c97a94c457137a39c552fe7ef Mon Sep 17 00:00:00 2001 From: Gudmundur Haraldsson Date: Mon, 4 Jul 2022 14:52:05 +0000 Subject: [PATCH 35/35] Update comments. --- tests/integration/AutoApprovalScanCommitTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/AutoApprovalScanCommitTest.php b/tests/integration/AutoApprovalScanCommitTest.php index da46fb356..96b1c04dd 100644 --- a/tests/integration/AutoApprovalScanCommitTest.php +++ b/tests/integration/AutoApprovalScanCommitTest.php @@ -127,7 +127,7 @@ protected function tearDown(): void { /** * Dismiss any previously submitted reviews - * to pull requests. + * to pull requests used in these tests. * * @return void */ @@ -206,7 +206,7 @@ private function cleanup_prs() :void { } /** - * Get labels for pull request. + * Get labels for pull request used in these tests. * * @return mixed */