Skip to content

Commit

Permalink
- When retrieving a ZIP from a private repository, use authentication…
Browse files Browse the repository at this point in the history
…, if provided

- Fix the GitLab failover behavior (used to bail-out on private GitLab repositories without trying to use a token if one was provided)
  • Loading branch information
drzraf committed Oct 6, 2022
1 parent 511aa12 commit 4e53a5d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Package_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ public function install( $args, $assoc_args ) {
'filename' => $temp,
'insecure' => $insecure,
];
$response = Utils\http_request( 'GET', $package_name, null, [], $options );
$gitlab_token = getenv( 'GITLAB_TOKEN' ); // Use GITLAB_TOKEN if available to avoid authorization failures or rate-limiting.
$headers = $gitlab_token && strpos( $package_name, '://gitlab.com/' ) !== false ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
$response = Utils\http_request( 'GET', $package_name, null, $headers, $options );
if ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) {
@unlink( $temp ); // @codingStandardsIgnoreLine
WP_CLI::error( sprintf( "Couldn't download package from '%s' (HTTP code %d).", $package_name, $response->status_code ) );
Expand Down Expand Up @@ -1175,17 +1177,17 @@ private function check_gitlab_package_name( $package_name, $version = '', $insec

$options = [ 'insecure' => $insecure ];

$response = Utils\http_request( 'GET', $raw_content_public_url, null /*data*/, [], $options );
if ( $response->status_code < 200 || $response->status_code >= 300 ) {
$gitlab_token = getenv( 'GITLAB_TOKEN' ); // Use GITLAB_TOKEN if available to avoid authorization failures or rate-limiting.
$response = Utils\http_request( 'GET', $raw_content_public_url, null /*data*/, [], $options );
if ( ! $gitlab_token && ( $response->status_code < 200 || $response->status_code >= 300 ) ) {
// Could not get composer.json. Possibly private so warn and return best guess from input (always xxx/xxx).
WP_CLI::warning( sprintf( "Couldn't download composer.json file from '%s' (HTTP code %d). Presuming package name is '%s'.", $raw_content_public_url, $response->status_code, $package_name ) );
return $package_name;
}

if ( strpos( $response->headers['content-type'], 'text/html' ) === 0 ) {
$gitlab_token = getenv( 'GITLAB_TOKEN' ); // Use GITLAB_TOKEN if available to avoid authorization failures or rate-limiting.
$headers = $gitlab_token ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
$response = Utils\http_request( 'GET', $raw_content_private_url, null /*data*/, $headers, $options );
$headers = $gitlab_token ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
$response = Utils\http_request( 'GET', $raw_content_private_url, null /*data*/, $headers, $options );

if ( $response->status_code < 200 || $response->status_code >= 300 ) {
// Could not get composer.json. Possibly private so warn and return best guess from input (always xxx/xxx).
Expand Down

0 comments on commit 4e53a5d

Please sign in to comment.