Skip to content

Commit dbf422b

Browse files
Hyperkid123claude
andcommitted
feat(auth): per-platform git identity via includeIf
Split GIT_USER_NAME/EMAIL into GH_USER_NAME/EMAIL and GL_USER_NAME/EMAIL so each platform gets its own commit identity and GPG signing key. Uses git includeIf hasconfig:remote.*.url to auto-select the right identity based on the repo's remote URL. No manual per-repo config needed. RHCLOUD-47113 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d1022a2 commit dbf422b

5 files changed

Lines changed: 84 additions & 44 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ RUN mkdir -p /home/botuser/.config/containers /home/botuser/.local/share/contain
158158
> /home/botuser/.config/containers/registries.conf
159159

160160

161-
# Git config (identity is set at runtime via GIT_USER_NAME/GIT_USER_EMAIL env vars)
161+
# Git config (per-platform identity is set at runtime via includeIf)
162162
RUN git config --global http.https://gitlab.cee.redhat.com.sslVerify false \
163163
&& git config --global gpg.format openpgp \
164164
&& git config --global commit.gpgsign true

bot/run.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,50 @@ def _resolve_path(p: str) -> str:
3131

3232

3333
def setup_git(script_dir: Path) -> None:
34-
"""Generate a .gitconfig with identity and optional GPG signing.
34+
"""Generate per-platform .gitconfig files with includeIf.
3535
36-
Reads from env: GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GPG_SIGNING_KEY.
37-
Sets GIT_CONFIG_GLOBAL so all repos use this config.
36+
Reads GH_USER_NAME/GH_USER_EMAIL and GL_USER_NAME/GL_USER_EMAIL
37+
from env. Each platform gets its own identity and signing key.
38+
Falls back to GIT_AUTHOR_NAME/GIT_AUTHOR_EMAIL for single-identity
39+
local dev. Sets GIT_CONFIG_GLOBAL so all repos use this config.
3840
"""
39-
name = os.environ.get("GIT_AUTHOR_NAME")
40-
email = os.environ.get("GIT_AUTHOR_EMAIL")
41+
gh_name = os.environ.get("GH_USER_NAME") or os.environ.get("GIT_AUTHOR_NAME")
42+
gh_email = os.environ.get("GH_USER_EMAIL") or os.environ.get("GIT_AUTHOR_EMAIL")
43+
gl_name = os.environ.get("GL_USER_NAME") or os.environ.get("GIT_AUTHOR_NAME")
44+
gl_email = os.environ.get("GL_USER_EMAIL") or os.environ.get("GIT_AUTHOR_EMAIL")
4145

42-
if not name and not email:
46+
if not gh_name and not gl_name:
4347
return
4448

4549
config_path = script_dir / ".gitconfig"
50+
gh_config = script_dir / ".gitconfig-gh"
51+
gl_config = script_dir / ".gitconfig-gl"
52+
53+
for path, name, email, key_env in [
54+
(gh_config, gh_name, gh_email, "GH_GPG_SIGNING_KEY"),
55+
(gl_config, gl_name, gl_email, "GL_GPG_SIGNING_KEY"),
56+
]:
57+
lines = ["# Auto-generated by bot/run.py", "[user]"]
58+
if name:
59+
lines.append(f"\tname = {name}")
60+
if email:
61+
lines.append(f"\temail = {email}")
62+
signing_key = os.environ.get(key_env) or os.environ.get("GPG_SIGNING_KEY")
63+
if signing_key:
64+
lines.append(f"\tsigningkey = {signing_key}")
65+
path.write_text("\n".join(lines) + "\n")
66+
4667
lines = [
4768
"# Auto-generated by bot/run.py — do not edit manually",
48-
"[user]",
69+
'[includeIf "hasconfig:remote.*.url:https://github.com/**"]',
70+
f"\tpath = {gh_config}",
71+
'[includeIf "hasconfig:remote.*.url:https://gitlab.cee.redhat.com/**"]',
72+
f"\tpath = {gl_config}",
73+
"[commit]",
74+
"\tgpgsign = true",
75+
"[gpg]",
76+
"\tformat = openpgp",
4977
]
50-
if name:
51-
lines.append(f"\tname = {name}")
52-
if email:
53-
lines.append(f"\temail = {email}")
54-
55-
signing_key = os.environ.get("GPG_SIGNING_KEY")
56-
if signing_key:
57-
lines += [
58-
f"\tsigningkey = {signing_key}",
59-
"[commit]",
60-
"\tgpgsign = true",
61-
"[gpg]",
62-
"\tformat = openpgp",
63-
]
6478

6579
config_path.write_text("\n".join(lines) + "\n")
6680
os.environ["GIT_CONFIG_GLOBAL"] = str(config_path)

deploy/template.yaml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ parameters:
2121
value: hcc-ai-bot
2222
- name: BOT_REPLICAS
2323
value: "1"
24-
- name: GIT_USER_NAME
24+
- name: GH_USER_NAME
2525
required: true
26-
- name: GIT_USER_EMAIL
26+
- name: GH_USER_EMAIL
2727
required: true
2828
- name: GH_USERNAME
2929
required: true
30-
- name: GITLAB_USERNAME
30+
- name: GL_USER_NAME
31+
required: true
32+
- name: GL_USER_EMAIL
33+
required: true
34+
- name: GL_USERNAME
3135
required: true
3236
objects:
3337

@@ -265,15 +269,19 @@ objects:
265269
value: "60"
266270
- name: GOOGLE_APPLICATION_CREDENTIALS
267271
value: /home/botuser/sa-key.json
268-
# Git identity
269-
- name: GIT_USER_NAME
270-
value: ${GIT_USER_NAME}
271-
- name: GIT_USER_EMAIL
272-
value: ${GIT_USER_EMAIL}
272+
# Git identity — per-platform (name, email, login)
273+
- name: GH_USER_NAME
274+
value: ${GH_USER_NAME}
275+
- name: GH_USER_EMAIL
276+
value: ${GH_USER_EMAIL}
273277
- name: GH_USERNAME
274278
value: ${GH_USERNAME}
275-
- name: GITLAB_USERNAME
276-
value: ${GITLAB_USERNAME}
279+
- name: GL_USER_NAME
280+
value: ${GL_USER_NAME}
281+
- name: GL_USER_EMAIL
282+
value: ${GL_USER_EMAIL}
283+
- name: GL_USERNAME
284+
value: ${GL_USERNAME}
277285
# Proxy — HTTP(S)_PROXY for git and HTTP clients
278286
- name: PROXY_HOST
279287
value: devbot-proxy

docker-compose.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ services:
6868
- GPG_PRIVATE_KEY_B64
6969
- GH_TOKEN
7070
- GITLAB_TOKEN
71+
- GH_USER_NAME
72+
- GH_USER_EMAIL
7173
- GH_USERNAME
72-
- GITLAB_USERNAME
73-
- GIT_USER_NAME
74-
- GIT_USER_EMAIL
74+
- GL_USER_NAME
75+
- GL_USER_EMAIL
76+
- GL_USERNAME
7577
- GOOGLE_SA_KEY_B64
7678
- SSO_USERNAME
7779
- SSO_PASSWORD

entrypoint.sh

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ decode_or_raw() {
2828
}
2929

3030
# Git credential helpers for HTTPS auth (replaces SSH keys)
31-
# GitHub: gh CLI acts as credential helper
31+
# GitHub: gh CLI acts as credential helper (set up below)
3232
# GitLab: custom helper script injects token
3333
if [ -n "${GITLAB_TOKEN:-}" ]; then
3434
cat > /home/botuser/.git-credential-gitlab <<CREDEOF
3535
#!/bin/bash
36-
echo "username=${GITLAB_USERNAME}"
36+
echo "username=${GL_USERNAME}"
3737
echo "password=${GITLAB_TOKEN}"
3838
CREDEOF
3939
chmod 700 /home/botuser/.git-credential-gitlab
@@ -49,16 +49,32 @@ EOF
4949
unset SSO_USERNAME SSO_PASSWORD
5050
fi
5151

52-
# Git identity from env vars
53-
git config --global user.name "${GIT_USER_NAME}"
54-
git config --global user.email "${GIT_USER_EMAIL}"
55-
56-
# Import GPG key for commit signing
52+
# Import GPG keys for commit signing (may contain keys for both platforms)
5753
if [ -n "${GPG_PRIVATE_KEY_B64:-}" ]; then
5854
gpg --batch --import <(decode_or_raw "$GPG_PRIVATE_KEY_B64") 2>/dev/null
5955
fi
60-
export GPG_SIGNING_KEY="$(gpg --list-secret-keys --keyid-format long 2>/dev/null | grep ed25519 | head -1 | awk '{print $2}' | cut -d/ -f2)"
61-
git config --global user.signingkey "$GPG_SIGNING_KEY"
56+
57+
# Per-platform git identity via includeIf (git 2.36+)
58+
# Each platform gets its own name, email, and GPG signing key.
59+
GH_GPG_KEY="$(gpg --list-secret-keys --keyid-format long "${GH_USER_EMAIL}" 2>/dev/null | grep -oP '(?<=/)[A-F0-9]{16}' | head -1)"
60+
GL_GPG_KEY="$(gpg --list-secret-keys --keyid-format long "${GL_USER_EMAIL}" 2>/dev/null | grep -oP '(?<=/)[A-F0-9]{16}' | head -1)"
61+
62+
cat > /home/botuser/.gitconfig-gh <<EOF
63+
[user]
64+
name = ${GH_USER_NAME}
65+
email = ${GH_USER_EMAIL}
66+
signingkey = ${GH_GPG_KEY}
67+
EOF
68+
69+
cat > /home/botuser/.gitconfig-gl <<EOF
70+
[user]
71+
name = ${GL_USER_NAME}
72+
email = ${GL_USER_EMAIL}
73+
signingkey = ${GL_GPG_KEY}
74+
EOF
75+
76+
git config --global 'includeIf.hasconfig:remote.*.url:https://github.com/**.path' /home/botuser/.gitconfig-gh
77+
git config --global 'includeIf.hasconfig:remote.*.url:https://gitlab.cee.redhat.com/**.path' /home/botuser/.gitconfig-gl
6278

6379
# Decode GCP service account key
6480
if [ -n "${GOOGLE_SA_KEY_B64:-}" ]; then

0 commit comments

Comments
 (0)