-
Notifications
You must be signed in to change notification settings - Fork 26
Add script to resolve semver for WP_VERSION env
#207
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
base: main
Are you sure you want to change the base?
Conversation
|
Seems like some bug with my logic. It should resolve the latest minor version. |
Co-authored-by: Pascal Birchler <pascalb@google.com>
Co-authored-by: Pascal Birchler <pascalb@google.com>
| if ( 1 === $version_count ) { | ||
| $constraint = "^$wp_version_env"; // Get the latest minor version. | ||
| } elseif ( 2 === $version_count ) { | ||
| $constraint = "~$wp_version_env.0"; // Get the latest patch version. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added .0 as patch version due to semver package API behavior:
The ~ operator is best explained by example: ~1.2 is equivalent to >=1.2 <2.0.0, while ~1.2.3 is equivalent to >=1.2.3 <1.3.0. Ref: https://getcomposer.org/doc/articles/versions.md#tilde-version-range-
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a PHP-based script to resolve semantic versions for the WP_VERSION environment variable using the composer/semver package. The new resolver downloads a JSON mapping of WordPress versions, uses semver constraints to match version patterns (e.g., "5" → "5.9.2", "6.0" → "6.0.2"), and replaces the previous bash+jq-based implementation.
Key changes:
- Implements semver-based version resolution with support for major, minor, and patch version matching
- Replaces API call with cached JSON file approach for version lookup
- Adds comprehensive test coverage for various version input formats
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| utils/wp-version-resolver.php | New script implementing semver-based WordPress version resolution with JSON caching |
| tests/tests/TestWPVersionResolverTest.php | Unit tests covering 15 different version resolution scenarios |
| phpcs.xml.dist | Adds PHPCS exclusions for the new utility script |
| composer.json | Adds composer/semver dependency (^3.4) |
| bin/run-behat-tests | Integrates new PHP resolver, removes old bash+jq implementation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| */ | ||
| #[DataProvider( 'data_wp_version_resolver' )] // phpcs:ignore PHPCompatibility.Attributes.NewAttributes.PHPUnitAttributeFound | ||
| public function test_wp_version_resolver( $env, $expected ): void { | ||
| if ( $env ) { | ||
| putenv( "WP_VERSION=$env" ); | ||
| } | ||
|
|
||
| $output = exec( 'php ' . dirname( dirname( __DIR__ ) ) . '/utils/wp-version-resolver.php' ); | ||
|
|
||
| // Reset the environment variable. | ||
| putenv( 'WP_VERSION' ); | ||
|
|
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test doesn't verify the actual error handling behavior when the JSON file doesn't exist initially. While the setup creates the file, consider adding a test case that verifies the script correctly downloads the JSON file when it's missing.
| curl_exec( $ch ); | ||
| if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0. | ||
| // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated | ||
| curl_close( $ch ); | ||
| } | ||
| fclose( $fp ); |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of curl_exec() is not checked. If the curl request fails, the script will continue and potentially read an empty or invalid JSON file. Consider checking the return value and handling failure cases appropriately.
| curl_exec( $ch ); | |
| if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0. | |
| // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated | |
| curl_close( $ch ); | |
| } | |
| fclose( $fp ); | |
| $result = curl_exec( $ch ); | |
| if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0. | |
| // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated | |
| curl_close( $ch ); | |
| } | |
| fclose( $fp ); | |
| if ( $result === false ) { | |
| // Remove incomplete file if it exists. | |
| if ( file_exists( $wp_versions_file_path ) ) { | |
| unlink( $wp_versions_file_path ); | |
| } | |
| fwrite( STDERR, "Error: Failed to download WP versions JSON from " . WP_VERSIONS_JSON_URL . "\n" ); | |
| exit( 1 ); | |
| } |
| if ( ! file_exists( $wp_versions_file_path ) ) { | ||
| $ch = curl_init( WP_VERSIONS_JSON_URL ); | ||
| $fp = fopen( $wp_versions_file_path, 'w' ); | ||
|
|
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of curl_init() is not checked. If curl fails to initialize, $ch will be false, causing subsequent curl_setopt() calls to fail. Consider checking if $ch is valid before using it.
| if ( $ch === false ) { | |
| if ( $fp ) { | |
| fclose( $fp ); | |
| } | |
| echo "Error: Failed to initialize cURL to fetch WP versions.\n"; | |
| exit( 1 ); | |
| } |
| } | ||
|
|
||
| $wp_versions_json = json_decode( file_get_contents( $wp_versions_file_path ), true ); | ||
|
|
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of json_decode() is not checked. If the JSON file is invalid or corrupted, this will result in null, causing errors in subsequent operations. Consider checking if decoding was successful and handle the error case appropriately.
| if ( !is_array( $wp_versions_json ) ) { | |
| echo "Error: Failed to decode WP versions JSON file.\n"; | |
| echo $wp_version_env; | |
| exit( 1 ); | |
| } |
| } | ||
|
|
||
| $wp_version = end( $wp_satisfied_versions ); | ||
| echo $wp_version ? : $wp_version_env; |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Missing space before the ternary operator. The expression should be $wp_version ? $wp_version : $wp_version_env with proper spacing for readability.
| echo $wp_version ? : $wp_version_env; | |
| echo $wp_version ? $wp_version : $wp_version_env; |
| use WP_CLI\Tests\TestCase; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
|
|
||
| class TestWPVersionResolverTest extends TestCase { |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class name has a redundant "Test" suffix. The class is already named TestWPVersionResolverTest, which is redundant. Consider renaming to TestWPVersionResolver or WPVersionResolverTest.
| class TestWPVersionResolverTest extends TestCase { | |
| class WPVersionResolverTest extends TestCase { |
| array( '6.5.0.0', '6.5' ), // Return the latest version. | ||
| array( '6.5.2.0', '6.5.2' ), // Return the latest version. |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment incorrectly states "Return the latest version" for versions with 4 segments. The actual behavior based on the constraint logic is to return as-is (constraint = $wp_version_env at line 54). The comment should say "Does not match standard semver. Return as normalized version." or similar.
| array( '6.5.0.0', '6.5' ), // Return the latest version. | |
| array( '6.5.2.0', '6.5.2' ), // Return the latest version. | |
| array( '6.5.0.0', '6.5' ), // Does not match standard semver. Return as normalized version. | |
| array( '6.5.2.0', '6.5.2' ), // Does not match standard semver. Return as normalized version. |
| if ( empty( $wp_version_env ) || 'latest' === $wp_version_env ) { | ||
| $wp_version = array_search( 'latest', $wp_versions_json, true ); | ||
|
|
||
| if ( empty( $wp_version ) ) { | ||
| $wp_version = $wp_version_env; | ||
| } | ||
|
|
||
| echo $wp_version; | ||
| exit( 0 ); | ||
| } |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script doesn't handle the trunk value for WP_VERSION, which was previously supported according to the README documentation. The script should either handle trunk as a special case (similar to latest) or the documentation should be updated to reflect this breaking change.
|
|
||
| if ( ! file_exists( $wp_versions_file_path ) ) { | ||
| $ch = curl_init( WP_VERSIONS_JSON_URL ); | ||
| $fp = fopen( $wp_versions_file_path, 'w' ); |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of fopen() is not checked. If the file cannot be created (e.g., due to permissions), this will cause a fatal error when passed to curl_setopt(). Consider checking if $fp is valid before proceeding.
| $fp = fopen( $wp_versions_file_path, 'w' ); | |
| $fp = fopen( $wp_versions_file_path, 'w' ); | |
| if ( false === $fp ) { | |
| echo "Error: Unable to open file for writing: $wp_versions_file_path" . PHP_EOL; | |
| exit( 1 ); | |
| } |
| $output = exec( 'php ' . dirname( dirname( __DIR__ ) ) . '/utils/wp-version-resolver.php' ); | ||
|
|
||
| // Reset the environment variable. | ||
| putenv( 'WP_VERSION' ); |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The environment variable is not properly unset. Using putenv('WP_VERSION') without a value may not work correctly on all systems. Consider using putenv('WP_VERSION=') with an empty string or checking if the variable needs to be restored to its previous value.
| putenv( 'WP_VERSION' ); | |
| putenv( 'WP_VERSION=' ); |
Summary
This PR aims to add a script which helps to determine the semver for passed
WP_VERSIONenv variable. To do so correctly, this script usescomposer/semverpackage.Example cases:
Versions data
Fixes: #51