Skip to content

Commit 89b2566

Browse files
committed
Check github-token on enterprise
On GitHub enterprise we may get a Personal Access token or a default token generated by GitHub that maybe not have the permissions required to access public GitHub, so we check and add it only if it can authenticate with the public GitHub API
1 parent dcec1cf commit 89b2566

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ Disable coverage for these reasons:
465465
- Specify the GitHub token to use for authentication.
466466
- Accepts a `string`.
467467
- By default, `GITHUB_TOKEN` secret provided by GitHub Actions is used.
468+
- For GitHub Enterprise users, it is recommended to use a Personal Access Token (PAT).
468469

469470
### Outputs
470471

@@ -797,8 +798,6 @@ restore-keys: ${{ runner.os }}-composer-${{ matrix.prefer }}-
797798

798799
By default, setup-php uses the `GITHUB_TOKEN` secret that is generated for each workflow run. In case you want to use a Personal Access Token (PAT) instead, you can set the `github-token` input.
799800

800-
The `COMPOSER_TOKEN` and `GITHUB_TOKEN` environment variables have been deprecated in favor of the `github-token` input and will be removed in the next major version.
801-
802801
```yaml
803802
- name: Setup PHP
804803
uses: shivammathur/setup-php@v2
@@ -807,6 +806,10 @@ The `COMPOSER_TOKEN` and `GITHUB_TOKEN` environment variables have been deprecat
807806
github-token: ${{ secrets.YOUR_PAT_TOKEN }}
808807
```
809808

809+
The `COMPOSER_TOKEN` and `GITHUB_TOKEN` environment variables have been deprecated in favor of the `github-token` input and will be removed in the next major version.
810+
811+
For GitHub Enterprise users, the `github-token` input does not default to the `GITHUB_TOKEN` secret. Therefore, it's recommended to set the `github-token` input to a Personal Access Token (PAT).
812+
810813
### Private Packagist Authentication
811814

812815
If you use Private Packagist for your private composer dependencies, you can set the `PACKAGIST_TOKEN` environment variable to authenticate.

src/scripts/tools/add_tools.ps1

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ Function Update-AuthJson {
6161
Set-Content -Path $composer_home\auth.json -Value ($existing | ConvertTo-Json -Depth 5)
6262
}
6363

64+
function Test-GitHubPublicAccess {
65+
param(
66+
[Parameter(Mandatory=$true)]
67+
[string]$Token
68+
)
69+
try {
70+
Invoke-RestMethod -Uri 'https://api.github.com/' -Headers @{ Authorization = "token $Token" } -ErrorAction Stop | Out-Null
71+
return $true
72+
} catch {
73+
return $false
74+
}
75+
}
76+
6477
# Function to setup authentication in composer.
6578
Function Set-ComposerAuth() {
6679
if(Test-Path env:COMPOSER_AUTH_JSON) {
@@ -74,9 +87,15 @@ Function Set-ComposerAuth() {
7487
if(Test-Path env:PACKAGIST_TOKEN) {
7588
$composer_auth += '"http-basic": {"repo.packagist.com": { "username": "token", "password": "' + $env:PACKAGIST_TOKEN + '"}}'
7689
}
90+
$write_token = $true
7791
$token = if ($env:COMPOSER_TOKEN) { $env:COMPOSER_TOKEN } else { $env:GITHUB_TOKEN }
7892
if ($token) {
79-
$composer_auth += '"github-oauth": {"github.com": "' + $token + '"}'
93+
if ($env:GITHUB_SERVER_URL -ne "https://github.com" -and -not(Test-GitHubPublicAccess $token)) {
94+
$write_token = $false
95+
}
96+
if($write_token) {
97+
$composer_auth += '"github-oauth": {"github.com": "' + $token + '"}'
98+
}
8099
}
81100
if($composer_auth.length) {
82101
Update-AuthJson $composer_auth

src/scripts/tools/add_tools.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ update_auth_json() {
6565
printf '%s' "$merged" > "$composer_home/auth.json"
6666
}
6767

68+
# Function to check if public GitHub token authentication is possible.
69+
can_access_public_github() {
70+
curl --fail -s -H "Authorization: token $1" 'https://api.github.com/' >/dev/null 2>&1
71+
}
72+
6873
# Function to setup authentication in composer.
6974
set_composer_auth() {
7075
if [ -n "$COMPOSER_AUTH_JSON" ]; then
@@ -78,8 +83,15 @@ set_composer_auth() {
7883
if [ -n "$PACKAGIST_TOKEN" ]; then
7984
composer_auth+=( '"http-basic": {"repo.packagist.com": { "username": "token", "password": "'"$PACKAGIST_TOKEN"'"}}' )
8085
fi
81-
if [ -n "${COMPOSER_TOKEN:-$GITHUB_TOKEN}" ]; then
82-
composer_auth+=( '"github-oauth": {"github.com": "'"${COMPOSER_TOKEN:-$GITHUB_TOKEN}"'"}' )
86+
token="${COMPOSER_TOKEN:-$GITHUB_TOKEN}"
87+
if [ -n "$token" ]; then
88+
write_token=true
89+
if [ "$GITHUB_SERVER_URL" != "https://github.com" ]; then
90+
can_access_public_github "$token" || write_token=false
91+
fi
92+
if [ "$write_token" = 'true' ]; then
93+
composer_auth+=( '"github-oauth": {"github.com": "'"$token"'"}' )
94+
fi
8395
fi
8496
if ((${#composer_auth[@]})); then
8597
update_auth_json "${composer_auth[@]}"

0 commit comments

Comments
 (0)