Skip to content

Commit e99d897

Browse files
committed
fix(shell): bridge gh token to git credentials
1 parent f75efe1 commit e99d897

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, it } from "@effect/vitest"
2+
import { Effect } from "effect"
3+
4+
import { defaultTemplateConfig } from "@effect-template/lib/core/domain"
5+
import { renderEntrypoint } from "@effect-template/lib/core/templates-entrypoint"
6+
7+
describe("renderEntrypoint auth bridge", () => {
8+
it.effect("maps GH token fallback to git auth and sets git credential helper", () =>
9+
Effect.sync(() => {
10+
const entrypoint = renderEntrypoint({
11+
...defaultTemplateConfig,
12+
repoUrl: "https://github.com/org/repo.git",
13+
enableMcpPlaywright: false
14+
})
15+
16+
expect(entrypoint).toContain(
17+
"GIT_AUTH_TOKEN=\"${GIT_AUTH_TOKEN:-${GITHUB_TOKEN:-${GH_TOKEN:-}}}\""
18+
)
19+
expect(entrypoint).toContain("GITHUB_TOKEN=\"${GITHUB_TOKEN:-${GH_TOKEN:-}}\"")
20+
expect(entrypoint).toContain("if [[ -n \"$GH_TOKEN\" || -n \"$GITHUB_TOKEN\" ]]; then")
21+
expect(entrypoint).toContain(String.raw`printf "export GITHUB_TOKEN=%q\n" "$EFFECTIVE_GITHUB_TOKEN"`)
22+
expect(entrypoint).toContain(String.raw`printf "%s\n" "GITHUB_TOKEN=$EFFECTIVE_GITHUB_TOKEN" >> "$SSH_ENV_PATH"`)
23+
expect(entrypoint).toContain("GIT_CREDENTIAL_HELPER_PATH=\"/usr/local/bin/docker-git-credential-helper\"")
24+
expect(entrypoint).toContain("token=\"$GITHUB_TOKEN\"")
25+
expect(entrypoint).toContain("token=\"$GH_TOKEN\"")
26+
expect(entrypoint).toContain(String.raw`printf "%s\n" "password=$token"`)
27+
expect(entrypoint).toContain("git config --global credential.helper")
28+
}))
29+
})

packages/lib/src/core/templates-entrypoint/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ REPO_REF="\${REPO_REF:-}"
1010
FORK_REPO_URL="\${FORK_REPO_URL:-}"
1111
TARGET_DIR="\${TARGET_DIR:-${config.targetDir}}"
1212
GIT_AUTH_USER="\${GIT_AUTH_USER:-\${GITHUB_USER:-x-access-token}}"
13-
GIT_AUTH_TOKEN="\${GIT_AUTH_TOKEN:-\${GITHUB_TOKEN:-}}"
13+
GIT_AUTH_TOKEN="\${GIT_AUTH_TOKEN:-\${GITHUB_TOKEN:-\${GH_TOKEN:-}}}"
1414
GH_TOKEN="\${GH_TOKEN:-\${GIT_AUTH_TOKEN:-}}"
15+
GITHUB_TOKEN="\${GITHUB_TOKEN:-\${GH_TOKEN:-}}"
1516
GIT_USER_NAME="\${GIT_USER_NAME:-}"
1617
GIT_USER_EMAIL="\${GIT_USER_EMAIL:-}"
1718
CODEX_AUTO_UPDATE="\${CODEX_AUTO_UPDATE:-1}"

packages/lib/src/core/templates-entrypoint/git.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,54 @@
11
import type { TemplateConfig } from "../domain.js"
22

33
export const renderEntrypointGitConfig = (config: TemplateConfig): string =>
4-
String.raw`# 2) Ensure GH_TOKEN is available for SSH sessions if provided
5-
if [[ -n "$GH_TOKEN" ]]; then
6-
printf "export GH_TOKEN=%q\n" "$GH_TOKEN" > /etc/profile.d/gh-token.sh
4+
String.raw`# 2) Ensure GitHub auth vars are available for SSH sessions if provided
5+
if [[ -n "$GH_TOKEN" || -n "$GITHUB_TOKEN" ]]; then
6+
EFFECTIVE_GITHUB_TOKEN="$GITHUB_TOKEN"
7+
if [[ -z "$EFFECTIVE_GITHUB_TOKEN" ]]; then
8+
EFFECTIVE_GITHUB_TOKEN="$GH_TOKEN"
9+
fi
10+
11+
EFFECTIVE_GH_TOKEN="$GH_TOKEN"
12+
if [[ -z "$EFFECTIVE_GH_TOKEN" ]]; then
13+
EFFECTIVE_GH_TOKEN="$EFFECTIVE_GITHUB_TOKEN"
14+
fi
15+
16+
printf "export GH_TOKEN=%q\n" "$EFFECTIVE_GH_TOKEN" > /etc/profile.d/gh-token.sh
17+
printf "export GITHUB_TOKEN=%q\n" "$EFFECTIVE_GITHUB_TOKEN" >> /etc/profile.d/gh-token.sh
718
chmod 0644 /etc/profile.d/gh-token.sh
819
SSH_ENV_PATH="/home/${config.sshUser}/.ssh/environment"
9-
printf "%s\n" "GH_TOKEN=$GH_TOKEN" > "$SSH_ENV_PATH"
10-
if [[ -n "$GITHUB_TOKEN" ]]; then
11-
printf "%s\n" "GITHUB_TOKEN=$GITHUB_TOKEN" >> "$SSH_ENV_PATH"
12-
fi
20+
printf "%s\n" "GH_TOKEN=$EFFECTIVE_GH_TOKEN" > "$SSH_ENV_PATH"
21+
printf "%s\n" "GITHUB_TOKEN=$EFFECTIVE_GITHUB_TOKEN" >> "$SSH_ENV_PATH"
1322
chmod 600 "$SSH_ENV_PATH"
1423
chown 1000:1000 "$SSH_ENV_PATH" || true
1524
fi
1625
17-
# 3) Configure git identity for the dev user if provided
26+
# 3) Configure git credential helper for HTTPS remotes
27+
GIT_CREDENTIAL_HELPER_PATH="/usr/local/bin/docker-git-credential-helper"
28+
cat <<'EOF' > "$GIT_CREDENTIAL_HELPER_PATH"
29+
#!/usr/bin/env bash
30+
set -euo pipefail
31+
32+
if [[ "$#" -lt 1 || "$1" != "get" ]]; then
33+
exit 0
34+
fi
35+
36+
token="$GITHUB_TOKEN"
37+
if [[ -z "$token" ]]; then
38+
token="$GH_TOKEN"
39+
fi
40+
41+
if [[ -z "$token" ]]; then
42+
exit 0
43+
fi
44+
45+
printf "%s\n" "username=x-access-token"
46+
printf "%s\n" "password=$token"
47+
EOF
48+
chmod 0755 "$GIT_CREDENTIAL_HELPER_PATH"
49+
su - ${config.sshUser} -c "git config --global credential.helper '$GIT_CREDENTIAL_HELPER_PATH'"
50+
51+
# 4) Configure git identity for the dev user if provided
1852
if [[ -n "$GIT_USER_NAME" ]]; then
1953
SAFE_GIT_USER_NAME="$(printf "%q" "$GIT_USER_NAME")"
2054
su - ${config.sshUser} -c "git config --global user.name $SAFE_GIT_USER_NAME"

0 commit comments

Comments
 (0)