Skip to content

Commit f7fc4b0

Browse files
authored
Make it easier for claude to run e2e tests (#2)
1 parent 7f407be commit f7fc4b0

4 files changed

Lines changed: 324 additions & 93 deletions

File tree

.claude/CLAUDE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Claude Scripts
2+
3+
`run-e2e-tests.sh` - Idempotent script that installs gh, gets a cached GitHub App token, and runs e2e tests

.claude/get_github_app_token.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#!/usr/bin/env -S uv run
2+
# /// script
3+
# requires-python = ">=3.11"
4+
# dependencies = [
5+
# "PyJWT",
6+
# "requests",
7+
# "cryptography",
8+
# ]
9+
# ///
10+
"""
11+
GitHub App Token Generator with caching
12+
13+
Generates an installation access token from GitHub App credentials in environment.
14+
Caches tokens and reuses them until they expire (with 5-minute buffer).
15+
16+
Environment variables required:
17+
- GH_APP_ID: GitHub App ID
18+
- GH_APP_PRIVATE_KEY_PEM_B64: Base64-encoded private key
19+
20+
Usage:
21+
# Run with uv (automatically installs dependencies)
22+
uv run get_github_app_token.py
23+
24+
# Save to file
25+
uv run get_github_app_token.py > token.txt
26+
27+
# Use in shell
28+
export GITHUB_TOKEN=$(uv run get_github_app_token.py)
29+
"""
30+
31+
import base64
32+
import json
33+
import os
34+
import sys
35+
import time
36+
from datetime import datetime, timezone
37+
from pathlib import Path
38+
39+
import jwt
40+
import requests
41+
42+
# Cache file location
43+
CACHE_FILE = Path("/tmp/gh_app_token_cache.json")
44+
# Buffer time before expiration (5 minutes)
45+
EXPIRATION_BUFFER_SECONDS = 300
46+
47+
48+
def get_cached_token():
49+
"""Get cached token if it exists and is still valid."""
50+
if not CACHE_FILE.exists():
51+
return None
52+
53+
try:
54+
with open(CACHE_FILE, 'r') as f:
55+
cache_data = json.load(f)
56+
57+
token = cache_data.get('token')
58+
expires_at_str = cache_data.get('expires_at')
59+
60+
if not token or not expires_at_str:
61+
return None
62+
63+
# Parse expiration time (GitHub returns ISO 8601 format)
64+
expires_at = datetime.fromisoformat(expires_at_str.replace('Z', '+00:00'))
65+
now = datetime.now(timezone.utc)
66+
67+
# Check if token is still valid (with buffer)
68+
time_until_expiry = (expires_at - now).total_seconds()
69+
70+
if time_until_expiry > EXPIRATION_BUFFER_SECONDS:
71+
print(f"Using cached token (expires in {int(time_until_expiry/60)} minutes)", file=sys.stderr)
72+
return token
73+
else:
74+
print("Cached token expired or expiring soon, generating new token", file=sys.stderr)
75+
return None
76+
77+
except (json.JSONDecodeError, ValueError, KeyError) as e:
78+
print(f"Error reading cache: {e}, generating new token", file=sys.stderr)
79+
return None
80+
81+
82+
def save_token_to_cache(token, expires_at):
83+
"""Save token and expiration to cache file."""
84+
cache_data = {
85+
'token': token,
86+
'expires_at': expires_at,
87+
'cached_at': datetime.now(timezone.utc).isoformat()
88+
}
89+
90+
try:
91+
with open(CACHE_FILE, 'w') as f:
92+
json.dump(cache_data, f)
93+
print("Token cached successfully", file=sys.stderr)
94+
except Exception as e:
95+
print(f"Warning: Failed to cache token: {e}", file=sys.stderr)
96+
97+
98+
def generate_installation_token():
99+
"""Generate a new installation access token for the GitHub App."""
100+
# Get credentials from environment
101+
app_id = os.getenv("GH_APP_ID")
102+
private_key_b64 = os.getenv("GH_APP_PRIVATE_KEY_PEM_B64")
103+
104+
if not all([app_id, private_key_b64]):
105+
print("Error: Missing GH_APP_ID or GH_APP_PRIVATE_KEY_PEM_B64", file=sys.stderr)
106+
sys.exit(1)
107+
108+
# Decode private key
109+
private_key = base64.b64decode(private_key_b64).decode('utf-8')
110+
111+
# Generate JWT
112+
now = int(time.time())
113+
payload = {
114+
"iat": now - 60,
115+
"exp": now + (10 * 60),
116+
"iss": app_id,
117+
}
118+
jwt_token = jwt.encode(payload, private_key, algorithm="RS256")
119+
120+
# Get installations
121+
headers = {
122+
"Authorization": f"Bearer {jwt_token}",
123+
"Accept": "application/vnd.github+json",
124+
"X-GitHub-Api-Version": "2022-11-28",
125+
}
126+
127+
response = requests.get("https://api.github.com/app/installations", headers=headers)
128+
response.raise_for_status()
129+
installations = response.json()
130+
131+
if not installations:
132+
print("Error: No installations found for this GitHub App", file=sys.stderr)
133+
sys.exit(1)
134+
135+
# Create installation token
136+
installation_id = installations[0]['id']
137+
url = f"https://api.github.com/app/installations/{installation_id}/access_tokens"
138+
response = requests.post(url, headers=headers)
139+
response.raise_for_status()
140+
141+
token_info = response.json()
142+
143+
# Save to cache
144+
save_token_to_cache(token_info['token'], token_info['expires_at'])
145+
146+
return token_info['token']
147+
148+
149+
def get_token():
150+
"""Get a valid token, either from cache or by generating a new one."""
151+
# Try to get cached token first
152+
cached_token = get_cached_token()
153+
if cached_token:
154+
return cached_token
155+
156+
# Generate new token if cache miss or expired
157+
print("Generating new GitHub App token", file=sys.stderr)
158+
return generate_installation_token()
159+
160+
161+
if __name__ == "__main__":
162+
try:
163+
token = get_token()
164+
print(token)
165+
except Exception as e:
166+
print(f"Error: {e}", file=sys.stderr)
167+
sys.exit(1)

.claude/run-e2e-tests.sh

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#!/bin/bash
2+
# Idempotent script to install gh CLI, acquire token, and run e2e tests
3+
set -e
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
PROJECT_ROOT="$SCRIPT_DIR/.."
7+
TOKEN_FILE="/tmp/gh_token.txt"
8+
9+
log_info() {
10+
echo "[INFO] $1"
11+
}
12+
13+
log_warn() {
14+
echo "[WARN] $1"
15+
}
16+
17+
log_error() {
18+
echo "[ERROR] $1" >&2
19+
}
20+
21+
# 1. Install gh CLI if not present
22+
install_gh_cli() {
23+
if command -v gh &> /dev/null; then
24+
local version=$(gh --version | head -n1)
25+
log_info "gh CLI already installed: $version"
26+
return 0
27+
fi
28+
29+
log_info "Installing gh CLI..."
30+
31+
local arch=$(uname -m)
32+
if [[ "$arch" != "x86_64" ]]; then
33+
log_error "Unsupported architecture: $arch (only x86_64 is supported)"
34+
return 1
35+
fi
36+
37+
# Get latest release URL
38+
local download_url=$(curl -s https://api.github.com/repos/cli/cli/releases/latest | \
39+
grep "browser_download_url.*linux_amd64.tar.gz\"" | \
40+
cut -d '"' -f 4)
41+
42+
if [[ -z "$download_url" ]]; then
43+
log_error "Failed to get gh CLI download URL"
44+
return 1
45+
fi
46+
47+
log_info "Downloading from: $download_url"
48+
49+
# Download and install
50+
cd /tmp
51+
curl -L -o gh_linux_amd64.tar.gz "$download_url"
52+
tar -xzf gh_linux_amd64.tar.gz
53+
sudo cp gh_*/bin/gh /usr/local/bin/
54+
sudo chmod +x /usr/local/bin/gh
55+
rm -rf gh_*
56+
57+
local version=$(gh --version | head -n1)
58+
log_info "Successfully installed gh CLI: $version"
59+
}
60+
61+
# 2. Acquire GitHub App token
62+
acquire_token() {
63+
log_info "Acquiring GitHub App token..."
64+
65+
# Check if required environment variables are set
66+
if [[ -z "$GH_APP_ID" ]] || [[ -z "$GH_APP_PRIVATE_KEY_PEM_B64" ]]; then
67+
log_error "Missing required environment variables: GH_APP_ID and/or GH_APP_PRIVATE_KEY_PEM_B64"
68+
return 1
69+
fi
70+
71+
# Generate token using the Python script
72+
if ! uv run "$SCRIPT_DIR/get_github_app_token.py" > "$TOKEN_FILE" 2>/dev/null; then
73+
log_error "Failed to generate GitHub App token"
74+
return 1
75+
fi
76+
77+
local token_preview=$(cat "$TOKEN_FILE" | head -c 10)
78+
log_info "Successfully acquired token: ${token_preview}..."
79+
}
80+
81+
# 3. Setup git config and gh auth
82+
setup_git_and_gh() {
83+
log_info "Setting up git configuration..."
84+
85+
# Save current git config state
86+
ORIGINAL_GPGSIGN=$(git config --global --get commit.gpgsign || echo "not-set")
87+
88+
# Disable commit signing for tests
89+
git config --global commit.gpgsign false
90+
91+
# Setup gh authentication (gh uses GH_TOKEN)
92+
export GH_TOKEN="$(cat "$TOKEN_FILE")"
93+
94+
log_info "Configuring gh auth..."
95+
gh auth setup-git
96+
97+
log_info "Git and gh authentication configured"
98+
}
99+
100+
# 4. Restore git config
101+
restore_git_config() {
102+
log_info "Restoring git configuration..."
103+
104+
if [[ "$ORIGINAL_GPGSIGN" == "not-set" ]]; then
105+
git config --global --unset commit.gpgsign || true
106+
else
107+
git config --global commit.gpgsign "$ORIGINAL_GPGSIGN"
108+
fi
109+
110+
log_info "Git configuration restored"
111+
}
112+
113+
# 5. Run e2e tests
114+
run_e2e_tests() {
115+
log_info "Running e2e tests..."
116+
117+
# GH_TOKEN is already exported from setup_git_and_gh, no need to re-export
118+
119+
# Run the tests (timeout should be handled externally)
120+
if bash "$PROJECT_ROOT/tests/test_e2e.sh"; then
121+
log_info "✅ E2E tests completed successfully!"
122+
return 0
123+
else
124+
local exit_code=$?
125+
log_error "❌ E2E tests failed with exit code: $exit_code"
126+
return $exit_code
127+
fi
128+
}
129+
130+
# Main execution
131+
main() {
132+
log_info "Starting E2E test setup and execution..."
133+
log_info "Project root: $PROJECT_ROOT"
134+
135+
# Trap to ensure cleanup happens
136+
trap restore_git_config EXIT
137+
138+
# Execute steps
139+
install_gh_cli || exit 1
140+
acquire_token || exit 1
141+
setup_git_and_gh || exit 1
142+
143+
# Run tests (allow failure to be handled by exit code)
144+
if run_e2e_tests; then
145+
log_info "🎉 All steps completed successfully!"
146+
exit 0
147+
else
148+
log_error "Tests failed, but cleanup will still occur"
149+
exit 1
150+
fi
151+
}
152+
153+
# Run main function
154+
main "$@"

0 commit comments

Comments
 (0)