Skip to content

Commit 6c6f43f

Browse files
geroplona-agent
andauthored
[dev] Automatically authorize gh (#20971)
Co-authored-by: Ona <[email protected]>
1 parent 7137b3a commit 6c6f43f

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"mounts": [
1313
"source=/usr/local/gitpod/config/,target=/usr/local/gitpod/config/,type=bind"
1414
],
15+
"onCreateCommand": "bash /workspace/gitpod/dev/setup-github-auth.sh",
1516
"remoteEnv": {
1617
"GIT_EDITOR": "code --wait",
1718
"KUBE_EDITOR": "code --wait"

.gitpod.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ tasks:
4747
leeway run components/local-app:install-cli
4848
leeway run components/local-app:cli-completion
4949
exit 0
50+
- name: Setup GitHub CLI Auth
51+
init: |
52+
bash /workspace/gitpod/dev/setup-github-auth.sh
53+
exit 0
5054
# This task takes care of configuring your workspace so it can manage and interact
5155
# with preview environments.
5256
- name: Preview environment configuration

dev/github-token.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
# GitHub Token Helper
4+
# Dynamically retrieves GitHub token from git credentials
5+
# Safe to source - will not error if git credentials are unavailable
6+
7+
# Only set GH_TOKEN if not already set and git credential is available
8+
if [ -z "$GH_TOKEN" ] && command -v git >/dev/null 2>&1; then
9+
# Attempt to get token from git credentials, suppress errors
10+
TOKEN=$(printf 'protocol=https\nhost=github.com\n' | git credential fill 2>/dev/null | awk -F= '/password/ {print $2}' 2>/dev/null)
11+
12+
# Only export if we got a non-empty token
13+
if [ -n "$TOKEN" ]; then
14+
export GH_TOKEN="$TOKEN"
15+
fi
16+
17+
unset TOKEN
18+
fi

dev/setup-github-auth.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# GitHub CLI Authentication Setup
4+
# Adds sourcing of GitHub token helper to shell profiles
5+
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
GITHUB_TOKEN_HELPER="$SCRIPT_DIR/github-token.sh"
8+
9+
{
10+
echo ""
11+
echo "# GitHub token helper"
12+
echo "source \"$GITHUB_TOKEN_HELPER\""
13+
} >> ~/.bashrc
14+
15+
{
16+
echo ""
17+
echo "# GitHub token helper"
18+
echo "source \"$GITHUB_TOKEN_HELPER\""
19+
} >> ~/.zshrc
20+
21+
source "$GITHUB_TOKEN_HELPER"
22+
23+
echo "✅ GitHub CLI configured"

0 commit comments

Comments
 (0)