Skip to content

Commit 85d9d9a

Browse files
authored
Merge pull request #218 from wp-cli/copilot/handle-more-git-providers
Handle additional git providers in `set_composer_auth_env_var()`
2 parents c28f0e4 + 4cfccd0 commit 85d9d9a

3 files changed

Lines changed: 413 additions & 1 deletion

File tree

features/package-auth.feature

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Feature: Composer authentication for various git providers
2+
3+
Scenario: GitHub OAuth token is set in COMPOSER_AUTH
4+
Given an empty directory
5+
When I run `GITHUB_TOKEN=ghp_test123456789 wp package path`
6+
Then STDOUT should not be empty
7+
And the return code should be 0
8+
9+
Scenario: GitLab OAuth token is set in COMPOSER_AUTH
10+
Given an empty directory
11+
When I run `GITLAB_OAUTH_TOKEN=glpat_test123456789 wp package path`
12+
Then STDOUT should not be empty
13+
And the return code should be 0
14+
15+
Scenario: GitLab personal access token is set in COMPOSER_AUTH
16+
Given an empty directory
17+
When I run `GITLAB_TOKEN=glpat_test123456789 wp package path`
18+
Then STDOUT should not be empty
19+
And the return code should be 0
20+
21+
Scenario: Bitbucket OAuth consumer is set in COMPOSER_AUTH
22+
Given an empty directory
23+
When I run `BITBUCKET_CONSUMER_KEY=test_key BITBUCKET_CONSUMER_SECRET=test_secret wp package path`
24+
Then STDOUT should not be empty
25+
And the return code should be 0
26+
27+
Scenario: HTTP Basic Auth is set in COMPOSER_AUTH
28+
Given an empty directory
29+
When I run `HTTP_BASIC_AUTH='{"repo.example.com":{"username":"user","password":"pass"}}' wp package path`
30+
Then STDOUT should not be empty
31+
And the return code should be 0
32+
33+
Scenario: Multiple auth providers can be used together
34+
Given an empty directory
35+
When I run `GITHUB_TOKEN=ghp_test123 GITLAB_TOKEN=glpat_test456 wp package path`
36+
Then STDOUT should not be empty
37+
And the return code should be 0
38+
39+
Scenario: Invalid HTTP_BASIC_AUTH JSON is ignored
40+
Given an empty directory
41+
When I run `HTTP_BASIC_AUTH='not-valid-json' wp package path`
42+
Then STDOUT should not be empty
43+
And the return code should be 0

src/Package_Command.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,11 +1500,54 @@ private function set_composer_auth_env_var() {
15001500
if ( empty( $composer_auth ) || ! is_array( $composer_auth ) ) {
15011501
$composer_auth = [];
15021502
}
1503+
1504+
// GitHub OAuth token.
15031505
$github_token = getenv( 'GITHUB_TOKEN' );
1504-
if ( ! isset( $composer_auth['github-oauth'] ) && is_string( $github_token ) ) {
1506+
if ( ! isset( $composer_auth['github-oauth'] ) && is_string( $github_token ) && '' !== $github_token ) {
15051507
$composer_auth['github-oauth'] = [ 'github.com' => $github_token ];
15061508
$changed = true;
15071509
}
1510+
1511+
// GitLab OAuth token.
1512+
$gitlab_oauth_token = getenv( 'GITLAB_OAUTH_TOKEN' );
1513+
if ( ! isset( $composer_auth['gitlab-oauth'] ) && is_string( $gitlab_oauth_token ) && '' !== $gitlab_oauth_token ) {
1514+
$composer_auth['gitlab-oauth'] = [ 'gitlab.com' => $gitlab_oauth_token ];
1515+
$changed = true;
1516+
}
1517+
1518+
// GitLab personal access token.
1519+
$gitlab_token = getenv( 'GITLAB_TOKEN' );
1520+
if ( ! isset( $composer_auth['gitlab-token'] ) && is_string( $gitlab_token ) && '' !== $gitlab_token ) {
1521+
$composer_auth['gitlab-token'] = [ 'gitlab.com' => $gitlab_token ];
1522+
$changed = true;
1523+
}
1524+
1525+
// Bitbucket OAuth consumer.
1526+
$bitbucket_key = getenv( 'BITBUCKET_CONSUMER_KEY' );
1527+
$bitbucket_secret = getenv( 'BITBUCKET_CONSUMER_SECRET' );
1528+
if ( ! isset( $composer_auth['bitbucket-oauth'] )
1529+
&& is_string( $bitbucket_key ) && '' !== $bitbucket_key
1530+
&& is_string( $bitbucket_secret ) && '' !== $bitbucket_secret
1531+
) {
1532+
$composer_auth['bitbucket-oauth'] = [
1533+
'bitbucket.org' => [
1534+
'consumer-key' => $bitbucket_key,
1535+
'consumer-secret' => $bitbucket_secret,
1536+
],
1537+
];
1538+
$changed = true;
1539+
}
1540+
1541+
// HTTP Basic Authentication.
1542+
$http_basic_auth = getenv( 'HTTP_BASIC_AUTH' );
1543+
if ( ! isset( $composer_auth['http-basic'] ) && is_string( $http_basic_auth ) && '' !== $http_basic_auth ) {
1544+
$http_basic_auth_decoded = json_decode( $http_basic_auth, true /*assoc*/ );
1545+
if ( is_array( $http_basic_auth_decoded ) ) {
1546+
$composer_auth['http-basic'] = $http_basic_auth_decoded;
1547+
$changed = true;
1548+
}
1549+
}
1550+
15081551
if ( $changed ) {
15091552
putenv( 'COMPOSER_AUTH=' . json_encode( $composer_auth ) );
15101553
}

0 commit comments

Comments
 (0)