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

Add check for restricted contributors #819

Merged
merged 8 commits into from
Dec 9, 2024
Merged
101 changes: 100 additions & 1 deletion includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@
*
* @param Check_Result $result The Check Result to amend.
* @param string $readme_file Readme file.
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function check_for_contributors( Check_Result $result, string $readme_file ) {
$regex = '/Contributors\s?:(?:\*\*|\s)?(.*?)\R/';
Expand All @@ -666,7 +668,7 @@

$usernames = explode( ',', $matches[1] );

$usernames = array_map( 'trim', $usernames );
$usernames = array_unique( array_map( 'trim', $usernames ) );

$valid = true;

Expand All @@ -692,6 +694,72 @@
'',
6
);

return;

Check warning on line 698 in includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php#L698

Added line #L698 was not covered by tests
}

$restricted_contributors = $this->get_restricted_contributors();

$disallowed_contributors = array_keys(
array_filter(
$restricted_contributors,
function ( $value ) {
return 'error' === strtolower( $value );
}
)
);

if ( ! empty( $disallowed_contributors ) ) {
$disallowed_usernames = array_intersect( $usernames, $disallowed_contributors );

if ( ! empty( $disallowed_usernames ) ) {
$this->add_result_error_for_file(
$result,
sprintf(

Check warning on line 718 in includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php#L716-L718

Added lines #L716 - L718 were not covered by tests
/* translators: 1: plugin header field, 2: usernames */
__( 'The "%1$s" header in the readme file contains restricted username(s). Found: %2$s', 'plugin-check' ),
'Contributors',
'"' . implode( '", "', $disallowed_usernames ) . '"'
),
'readme_restricted_contributors',
$readme_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/#readme-header-information',
7
);

Check warning on line 730 in includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php#L720-L730

Added lines #L720 - L730 were not covered by tests
}
}

$reserved_contributors = array_keys(
array_filter(
$restricted_contributors,
function ( $value ) {
return 'warning' === strtolower( $value );
}
)
);

if ( ! empty( $reserved_contributors ) ) {
$reserved_usernames = array_intersect( $usernames, $reserved_contributors );

if ( ! empty( $reserved_usernames ) ) {
$this->add_result_warning_for_file(
$result,
sprintf(
/* translators: 1: plugin header field, 2: usernames */
__( 'The "%1$s" header in the readme file contains reserved username(s). Found: %2$s', 'plugin-check' ),
'Contributors',
'"' . implode( '", "', $reserved_usernames ) . '"'
),
'readme_reserved_contributors',
$readme_file,
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/#readme-header-information',
6
);
}
}
}

Expand Down Expand Up @@ -762,6 +830,37 @@
return $ignored_warnings;
}

/**
* Returns restricted contributors.
*
* @since 1.4.0
*
* @return array Restricted contributors.
*/
private function get_restricted_contributors() {
$restricted_contributors = array(
'username' => 'error',
'your-name' => 'error',
'your-username' => 'error',
'your-wordpress-username' => 'error',
'your_wordpress_username' => 'error',
'yourusername' => 'error',
'yourwordpressusername' => 'error',
'wordpressdotorg' => 'warning',
ernilambar marked this conversation as resolved.
Show resolved Hide resolved
);

/**
* Filter the list of restricted contributors.
*
* @since 1.4.0
*
* @param array $restricted_contributors Array of restricted contributors with error type.
*/
$restricted_contributors = (array) apply_filters( 'wp_plugin_check_restricted_contributors', $restricted_contributors );

return $restricted_contributors;
}

/**
* Gets the description for the check.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== Plugin Name ===

Contributors: plugin-check
Contributors: plugin-check, username, wordpressdotorg
Requires at least: 6.0
Tested up to: 6.1
Requires PHP: 5.6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

=== Test Plugin Readme Errors Parser Warnings ===

Contributors: plugin check, wordpressdotorg
Contributors: plugin check, johndoe
Requires at least: 6.0
Tested up to: Latest
Requires PHP: 5.6
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ public function test_run_with_errors_invalid_name() {
$this->assertCount( 1, wp_list_filter( $errors['readme.txt'][0][0], array( 'code' => 'invalid_plugin_name' ) ) );
}

public function test_run_with_errors_restricted_contributors() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-errors-invalid-name/load.php' );
$check_result = new Check_Result( $check_context );

$readme_check->run( $check_result );

$errors = $check_result->get_errors();
$warnings = $check_result->get_warnings();

$this->assertNotEmpty( $errors );
$this->assertNotEmpty( $warnings );
$this->assertArrayHasKey( 'readme.txt', $errors );
$this->assertArrayHasKey( 'readme.txt', $warnings );

// Check for restricted contributors error.
$this->assertCount( 1, wp_list_filter( $errors['readme.txt'][0][0], array( 'code' => 'readme_restricted_contributors' ) ) );

// Check for reserved contributors warning.
$this->assertCount( 1, wp_list_filter( $warnings['readme.txt'][0][0], array( 'code' => 'readme_reserved_contributors' ) ) );
}

public function test_run_with_errors_empty_name() {
$readme_check = new Plugin_Readme_Check();
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-plugin-readme-errors-empty-name/load.php' );
Expand Down
Loading