Skip to content

Commit 04367e8

Browse files
rfayclaude
andauthored
fix: don't configure the external-auth URL as a Composer GitHub token (#180)
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 0ac89b2 commit 04367e8

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎drupal-contrib/template.tf‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,12 +717,33 @@ COMPOSE_EOF
717717
if [ -z "$${_COMPOSER_GITHUB_TOKEN}" ]; then
718718
_CODER_BIN=$(find /tmp -name "coder" -path "*/coder.*/*" -type f -executable 2>/dev/null | head -1)
719719
if [ -n "$${_CODER_BIN}" ]; then
720-
_COMPOSER_GITHUB_TOKEN=$("$${_CODER_BIN}" external-auth access-token github 2>/dev/null || true)
720+
# `coder external-auth access-token` prints the token to stdout and exits 0,
721+
# but when the user has no linked GitHub account it prints the *authentication
722+
# URL* to stdout and exits 1. The exit code must be honored: handing that URL
723+
# to Composer as an OAuth token makes every github.com dist download fail with
724+
# "Could not authenticate against github.com" and suppresses the anonymous
725+
# fallback, which is strictly worse than configuring no token at all.
726+
if _token=$("$${_CODER_BIN}" external-auth access-token github 2>/dev/null); then
727+
_COMPOSER_GITHUB_TOKEN="$_token"
728+
else
729+
log_setup "No linked GitHub account — using anonymous Composer downloads"
730+
fi
721731
fi
722732
fi
733+
# An expired or revoked token fails the same opaque way, so verify before use.
734+
if [ -n "$${_COMPOSER_GITHUB_TOKEN}" ] && \
735+
! curl -sSf --max-time 10 -o /dev/null -H "Authorization: Bearer $${_COMPOSER_GITHUB_TOKEN}" https://api.github.com/user 2>/dev/null; then
736+
log_setup "⚠ GitHub token rejected by api.github.com — using anonymous Composer downloads"
737+
_COMPOSER_GITHUB_TOKEN=""
738+
fi
723739
if [ -n "$${_COMPOSER_GITHUB_TOKEN}" ]; then
724740
log_setup "Configuring Composer GitHub OAuth..."
725741
ddev exec composer config --global github-oauth.github.com "$${_COMPOSER_GITHUB_TOKEN}" >> "$SETUP_LOG" 2>&1 || true
742+
else
743+
# Clear any token a previous run configured. Composer's global config lives in
744+
# a persistent DDEV volume, so a bad value keeps breaking every later start
745+
# even once this code stops writing one.
746+
ddev exec composer config --global --unset github-oauth.github.com >> "$SETUP_LOG" 2>&1 || true
726747
fi
727748
728749
# Run ddev poser: expands composer.json → composer.contrib.json (includes require-dev),

‎drupal-core/template.tf‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,12 +887,33 @@ WELCOME_STATIC
887887
if [ -z "$${_COMPOSER_GITHUB_TOKEN}" ]; then
888888
_CODER_BIN=$(find /tmp -name "coder" -path "*/coder.*/*" -type f -executable 2>/dev/null | head -1)
889889
if [ -n "$${_CODER_BIN}" ]; then
890-
_COMPOSER_GITHUB_TOKEN=$("$${_CODER_BIN}" external-auth access-token github 2>/dev/null || true)
890+
# `coder external-auth access-token` prints the token to stdout and exits 0,
891+
# but when the user has no linked GitHub account it prints the *authentication
892+
# URL* to stdout and exits 1. The exit code must be honored: handing that URL
893+
# to Composer as an OAuth token makes every github.com dist download fail with
894+
# "Could not authenticate against github.com" and suppresses the anonymous
895+
# fallback, which is strictly worse than configuring no token at all.
896+
if _token=$("$${_CODER_BIN}" external-auth access-token github 2>/dev/null); then
897+
_COMPOSER_GITHUB_TOKEN="$_token"
898+
else
899+
log_setup "No linked GitHub account — using anonymous Composer downloads"
900+
fi
891901
fi
892902
fi
903+
# An expired or revoked token fails the same opaque way, so verify before use.
904+
if [ -n "$${_COMPOSER_GITHUB_TOKEN}" ] && \
905+
! curl -sSf --max-time 10 -o /dev/null -H "Authorization: Bearer $${_COMPOSER_GITHUB_TOKEN}" https://api.github.com/user 2>/dev/null; then
906+
log_setup "⚠ GitHub token rejected by api.github.com — using anonymous Composer downloads"
907+
_COMPOSER_GITHUB_TOKEN=""
908+
fi
893909
if [ -n "$${_COMPOSER_GITHUB_TOKEN}" ]; then
894910
log_setup "Configuring Composer GitHub OAuth..."
895911
ddev exec composer config --global github-oauth.github.com "$${_COMPOSER_GITHUB_TOKEN}" >> "$SETUP_LOG" 2>&1 || true
912+
else
913+
# Clear any token a previous run configured. Composer's global config lives in
914+
# a persistent DDEV volume, so a bad value keeps breaking every later start
915+
# even once this code stops writing one.
916+
ddev exec composer config --global --unset github-oauth.github.com >> "$SETUP_LOG" 2>&1 || true
896917
fi
897918
898919
# Retry up to 3 times to handle transient codeload.github.com 400s.
@@ -1006,9 +1027,13 @@ WELCOME_STATIC
10061027
fi
10071028
fi # end SETUP_FAILED guard
10081029
1009-
# Step 6.5: Cache rebuild — ensures a clean state after any setup path
1010-
log_setup "Running cache rebuild..."
1011-
ddev drush cr >> "$SETUP_LOG" 2>&1 || true
1030+
# Step 6.5: Cache rebuild — ensures a clean state after any successful setup path.
1031+
# Skipped when setup failed: there is no installed site to rebuild, and running it
1032+
# anyway just appends a confusing "drush is not available" error to the log.
1033+
if [ "$SETUP_FAILED" != "true" ]; then
1034+
log_setup "Running cache rebuild..."
1035+
ddev drush cr >> "$SETUP_LOG" 2>&1 || true
1036+
fi
10121037
10131038
# Step 6.6: Set up phpunit.xml for running core tests
10141039
if [ ! -f "phpunit.xml" ] && [ -f "phpunit-ddev.xml" ]; then

0 commit comments

Comments
 (0)