Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions features/package-install.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,43 @@ Feature: Install WP-CLI packages
wp-cli/community-command
"""

Scenario: Warn with the package name when the GitHub API response cannot be decoded
Given an empty directory
And a mock-github-api.php file:
"""
<?php
// Answer GitHub API requests with a non-JSON body (as a proxy or captive portal page would).
WP_CLI::add_hook(
'http_request_options',
static function ( $options, $method, $url ) {
if ( 0 === strpos( $url, 'https://api.github.com/repos/' ) ) {
$options['transport'] = new class() implements \WpOrg\Requests\Transport {
public function request( $url, $headers = [], $data = [], $options = [] ) {
return "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nnot json";
}
public function request_multiple( $requests, $options ) {
return [];
}
public static function test( $capabilities = [] ) {
return true;
}
};
}
return $options;
}
);
"""

When I try `WP_CLI_REQUIRE=mock-github-api.php wp package install https://github.com/non-existent-git-user-asdfasdf/non-existent-git-repo-asdfasdf.git`
Then STDERR should contain:
"""
Warning: Couldn't fetch default branch for package 'non-existent-git-user-asdfasdf/non-existent-git-repo-asdfasdf' (failed to decode JSON response). Presuming default branch is 'master'.
"""
And STDERR should not contain:
"""
package '%s'
"""

Scenario: Try to install bad packages
Given an empty directory
And a package-dir/composer.json file:
Expand Down
7 changes: 6 additions & 1 deletion src/Package_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,12 @@ private function get_github_default_branch( $package_name, $insecure = false ) {
$package_data = json_decode( $response->body );

if ( JSON_ERROR_NONE !== json_last_error() ) {
WP_CLI::warning( "Couldn't fetch default branch for package '%s' (failed to decode JSON response). Presuming default branch is 'master'." );
WP_CLI::warning(
sprintf(
"Couldn't fetch default branch for package '%s' (failed to decode JSON response). Presuming default branch is 'master'.",
$package_name
)
);
return 'master';
}

Expand Down