Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPScan API: Ensure comments are only submitted for changed add-ons #327

Merged
merged 30 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f2bbd23
Draft logic to ensure comments are only submitted for changed add-ons.
gudmdharalds Nov 3, 2022
122beb9
WP CS fixes.
gudmdharalds Nov 3, 2022
173c75e
Rename function.
gudmdharalds Nov 3, 2022
7651c5c
Move add-on matching logic to a specific function
gudmdharalds Nov 3, 2022
b236cbb
Simplify parameters to function
gudmdharalds Nov 3, 2022
14d4c93
Adding function vipgoci_wpcore_misc_get_addons_not_altered()
gudmdharalds Nov 3, 2022
bca0e46
Fix bug, return new array.
gudmdharalds Nov 4, 2022
b6114be
Test for vipgoci_wpcore_misc_get_addons_not_altered() function.
gudmdharalds Nov 4, 2022
dca42df
Update test to reflect new function parameters.
gudmdharalds Nov 4, 2022
4434d06
Simplify logic and fix bug.
gudmdharalds Nov 4, 2022
bd2c232
Update test to reflect changed parameters
gudmdharalds Nov 4, 2022
a7b7068
Add private array.
gudmdharalds Nov 4, 2022
e2c525a
Rewrite test as function is changed.
gudmdharalds Nov 7, 2022
9d69e5d
Update comment.
gudmdharalds Nov 8, 2022
6bfba7f
Rename function
gudmdharalds Nov 8, 2022
73b0518
WP CS
gudmdharalds Nov 8, 2022
b33794c
Add test for vipgoci_wpscan_get_altered_addons_data_and_slugs()
gudmdharalds Nov 8, 2022
e36485a
Use config values.
gudmdharalds Nov 9, 2022
cf9dfc8
Removing static data.
gudmdharalds Nov 9, 2022
1c71b7f
Moving code around.
gudmdharalds Nov 9, 2022
dc12847
Updating comment.
gudmdharalds Nov 9, 2022
d5a2e29
Update comment.
gudmdharalds Nov 9, 2022
332ff7b
Avoid GitHub HTTP functions.
gudmdharalds Nov 10, 2022
8fc7c48
Add new option for test.
gudmdharalds Nov 10, 2022
21c6eb7
Merge branch 'trunk' into fix/wpscan-api-addon-notification
gudmdharalds Nov 10, 2022
c752d94
Update unittests.ini.dist
gudmdharalds Nov 10, 2022
db78396
Update unittests.ini.dist
gudmdharalds Nov 10, 2022
95f1dd3
Add comment.
gudmdharalds Nov 10, 2022
702e9a3
Merge branch 'fix/wpscan-api-addon-notification' of github.com:Automa…
gudmdharalds Nov 10, 2022
77d49c6
Use test values from options
gudmdharalds Nov 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
552 changes: 552 additions & 0 deletions tests/integration/WpscanGetAlteredAddonsDataAndSlugsTest.php

Large diffs are not rendered by default.

383 changes: 184 additions & 199 deletions tests/integration/WpscanScanDirsAlteredTest.php

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions tests/integration/WpscanScanFindAddonDirsAlteredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ final class WpscanScanFindAddonDirsAlteredTest extends TestCase {
'repo-owner' => null,
);

/**
* Variable for skipped files.
*
* @var $commit_skipped_files
*/
private array $commit_skipped_files = array();

/**
* Setup function. Require files, prepare repository, etc.
*
Expand Down Expand Up @@ -105,6 +112,26 @@ protected function tearDown(): void {
unset( $this->options );
}

/**
* Get files affected by commit by pull request.
*
* @return array
*/
private function getFilesAffectedByCommitByPR() :array {
return vipgoci_github_files_affected_by_commit(
$this->options,
$this->options['commit'],
$this->commit_skipped_files,
true,
true,
true,
array(
'skip_folders' => $this->options['wpscan-api-skip-folders'],
),
false
);
}

/**
* Test when addons are added to pull request.
*
Expand Down Expand Up @@ -149,11 +176,10 @@ public function testFindDirsAlteredAddonAdded(): void {

vipgoci_unittests_output_unsuppress();

$commit_skipped_files = array();

$results_actual = vipgoci_wpscan_find_addon_dirs_altered(
$this->options,
$commit_skipped_files
$this->commit_skipped_files,
$this->getFilesAffectedByCommitByPR()
);

$results_expected = array(
Expand Down Expand Up @@ -214,11 +240,10 @@ public function testFindDirsAlteredAddonUpdated(): void {

vipgoci_unittests_output_unsuppress();

$commit_skipped_files = array();

$results_actual = vipgoci_wpscan_find_addon_dirs_altered(
$this->options,
$commit_skipped_files
$this->commit_skipped_files,
$this->getFilesAffectedByCommitByPR()
);

$results_expected = array(
Expand Down Expand Up @@ -276,11 +301,10 @@ public function testFindDirsAlteredAddonRemoved(): void {

vipgoci_unittests_output_unsuppress();

$commit_skipped_files = array();

$results_actual = vipgoci_wpscan_find_addon_dirs_altered(
$this->options,
$commit_skipped_files
$this->commit_skipped_files,
$this->getFilesAffectedByCommitByPR()
);

$this->assertSame(
Expand Down
35 changes: 31 additions & 4 deletions tests/integration/WpscanScanSaveForSubmissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ final class WpscanScanSaveForSubmissionTest extends TestCase {
'repo-owner' => null,
);

/**
* Variable for skipped files.
*
* @var $commit_skipped_files
*/
private array $commit_skipped_files = array();

/**
* Variable for problematic addons.
*
Expand Down Expand Up @@ -243,6 +250,26 @@ private function prLabelRemove() :void {
);
}

/**
* Get files affected by commit by pull request.
*
* @return array
*/
private function getFilesAffectedByCommitByPR() :array {
return vipgoci_github_files_affected_by_commit(
$this->options,
$this->options['commit'],
$this->commit_skipped_files,
true,
true,
true,
array(
'skip_folders' => $this->options['wpscan-api-skip-folders'],
),
false
);
}

/**
* Test function when a 'skip-wpscan' label is associated with
* pull request, so results should not be added.
Expand Down Expand Up @@ -282,7 +309,6 @@ public function testSaveForSubmissionWithLabel(): void {

$commit_issues_submit = array();
$commit_issues_stats = array();
$commit_skipped_files = array();

$commit_issues_submit[ $this->options['wpscan-pr-1-number'] ] = array();
$commit_issues_stats[ $this->options['wpscan-pr-1-number'] ] = array(
Expand All @@ -300,7 +326,8 @@ public function testSaveForSubmissionWithLabel(): void {
$this->options,
$commit_issues_submit,
$commit_issues_stats,
$commit_skipped_files,
$this->commit_skipped_files,
$this->getFilesAffectedByCommitByPR(),
$this->problematic_addons_found
);

Expand Down Expand Up @@ -364,7 +391,6 @@ public function testSaveForSubmissionNoLabel(): void {

$commit_issues_submit = array();
$commit_issues_stats = array();
$commit_skipped_files = array();

$commit_issues_submit[ $this->options['wpscan-pr-1-number'] ] = array();
$commit_issues_stats[ $this->options['wpscan-pr-1-number'] ] = array(
Expand All @@ -376,7 +402,8 @@ public function testSaveForSubmissionNoLabel(): void {
$this->options,
$commit_issues_submit,
$commit_issues_stats,
$commit_skipped_files,
$this->commit_skipped_files,
$this->getFilesAffectedByCommitByPR(),
$this->problematic_addons_found
);

Expand Down
96 changes: 96 additions & 0 deletions tests/unit/WpCoreMiscGetAddonsNotAlteredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Test vipgoci_wpcore_misc_get_addons_not_altered() function.
*
* @package Automattic/vip-go-ci
*/

declare(strict_types=1);

namespace Vipgoci\Tests\Unit;

use PHPUnit\Framework\TestCase;

/**
* Class that implements the testing.
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
final class WpCoreMiscGetAddonsNotAlteredTest extends TestCase {
/**
* Setup function. Require file.
*
* @return void
*/
protected function setUp() :void {
require_once __DIR__ . '/../../wp-core-misc.php';
require_once __DIR__ . '/../../misc.php';
}

/**
* Tests common usage of the function.
*
* @covers ::vipgoci_wpcore_misc_get_addons_not_altered
*
* @return void
*/
public function testGetAddonsNotAltered(): void {
$options = array(
'wpscan-api-paths' => array(
'plugins',
'themes',
'client-mu-plugins',
),
);

$known_addons = array(
'client-mu-plugins/my-plugin/plugin.php',
'client-mu-plugins/my-plugin/path1/test.php',
'client-mu-plugins/my-plugin/path1/test2.php',
'client-mu-plugins/my-plugin2/plugin.php',
'plugins/hello/hello.php',
'plugins/hello2/hello2.php',
'plugins/hello3/hello3.php',
'themes/theme1/hello/hello.php',
'themes/theme1/style.css',
'themes/theme2/hello/hello.php',
'themes/theme2/style.css',
'themes/theme3/hello/hello.php',
'themes/theme3/style.css',
);

$files_affected_by_commit_by_pr = array(
'all' => array(
'client-mu-plugins/my-plugin/plugin.php',
'client-mu-plugins/my-plugin/path1/test.php',
'client-mu-plugins/my-plugin/path1/test2.php',
'plugins/hello/hello.php',
'plugins/hello/docs/file.php',
'plugins/hello/docs/file.txt',
'plugins/hello2/hello2.php',
'testing/test-plugin.php',
'themes/theme1/hello/hello.php',
'themes/theme2/style.css',
),
);

$addons_not_altered = vipgoci_wpcore_misc_get_addons_not_altered(
$options,
$known_addons,
$files_affected_by_commit_by_pr
);

$this->assertSame(
array(
'client-mu-plugins/my-plugin2/plugin.php',
'plugins/hello3/hello3.php',
'themes/theme1/style.css',
'themes/theme2/hello/hello.php',
'themes/theme3/hello/hello.php',
'themes/theme3/style.css',
),
$addons_not_altered
);
}
}
1 change: 1 addition & 0 deletions unittests.ini.dist
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ lint-php2-version=8.1
plugin-slug=classic-editor
theme-slug=twentytwenty
wpscan-pr-1-commit-id=343d83dda119ef782ad66ad1db9e7f80b0630cc4
wpscan-pr-1-branch-ref=master
wpscan-pr-1-dirs-scan=plugins,mu-plugins,themes
wpscan-pr-1-number=52
wpscan-pr-1-dirs-altered=plugins/hello,plugins/not-a-plugin,themes/twentytwentyone
Expand Down
62 changes: 62 additions & 0 deletions wp-core-misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,65 @@ function vipgoci_wpcore_misc_get_addon_data_and_slugs_for_directory(
return $addons_found;
}

/**
* Returns a list of WordPress add-ons found in $known_addons that
* cannot be associated with changes in pull requests. Attempts to
* associate each changed file with an add-on, and returns
* those that cannot be associated.
*
* @param array $options Options array for the program.
* @param array $known_addons Array of paths to known add-ons (relative to repository base).
* @param array $files_affected_by_commit_by_pr Files affected by commit by pull request (relative to repository base).
*
* @return Array Paths to add-ons that could not be associated with changed files.
*/
function vipgoci_wpcore_misc_get_addons_not_altered(
array $options,
array $known_addons,
array $files_affected_by_commit_by_pr
) :array {
$addons_matched = array();

$changed_files = $files_affected_by_commit_by_pr['all'];

$known_addon_base_paths = array();

foreach ( $known_addons as $addon_path ) {
$known_addon_base_paths[ dirname( $addon_path ) ] = $addon_path;
}

foreach ( $changed_files as $changed_file ) {
if ( in_array( $changed_file, $known_addons, true ) ) {
$addons_matched[ $changed_file ] = $changed_file;
continue;
}

$changed_file_dirname = $changed_file;

do {
$changed_file_dirname = dirname( $changed_file_dirname );

if ( in_array(
$changed_file_dirname,
$options['wpscan-api-paths'],
true
) ) {
break;
}

if ( isset( $known_addon_base_paths[ $changed_file_dirname ] ) ) {
$addons_matched[ $changed_file ] = $known_addon_base_paths[ $changed_file_dirname ];

break;
}
} while ( str_contains( $changed_file_dirname, '/' ) );
}

return array_values(
array_diff(
$known_addons,
array_values( $addons_matched )
)
);
}

Loading