Skip to content

Commit 9417866

Browse files
committed
expand use-cases in gh_auth
1 parent daf03f8 commit 9417866

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

scripts/lib.sh

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,22 @@ requiredenvs() {
134134
}
135135

136136
gh_auth() {
137-
local fail=0
138-
if [[ "${CODER:-}" == "true" ]]; then
139-
if ! output=$(coder external-auth access-token github 2>&1); then
140-
log "ERROR: Could not authenticate with GitHub."
141-
log "$output"
142-
fail=1
137+
if [[ -z ${GITHUB_TOKEN:-} ]]; then
138+
if [[ -n ${GH_TOKEN:-} ]]; then
139+
export GITHUB_TOKEN=${GH_TOKEN}
140+
elif [[ ${CODER:-} == true ]]; then
141+
if ! output=$(coder external-auth access-token github 2>&1); then
142+
# TODO(maf): We could allow checking `gh auth token` here.
143+
log "${output}"
144+
error "Could not authenticate with GitHub using Coder external auth."
145+
else
146+
export GITHUB_TOKEN=${output}
147+
fi
148+
elif token="$(gh auth token --hostname github.com 2>/dev/null)"; then
149+
export GITHUB_TOKEN=${token}
143150
else
144-
GITHUB_TOKEN=$(coder external-auth access-token github)
145-
export GITHUB_TOKEN
151+
error "GitHub authentication is required to run this command, please set GITHUB_TOKEN or authenticate run 'gh auth login'."
146152
fi
147-
elif token="$(gh auth token --hostname github.com 2>/dev/null)"; then
148-
export GITHUB_TOKEN=$token
149-
else
150-
log "Please authenticate gh CLI by running 'gh auth login'"
151153
fi
152154
}
153155

0 commit comments

Comments
 (0)