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 64999f6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 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,15 +1177,15 @@ private function check_gitlab_package_name( $package_name, $version = '', $insec

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

$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 ( $response->status_code < 200 || $response->status_code >= 300 ) {
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 );

Expand Down

0 comments on commit 64999f6

Please sign in to comment.