Skip to content

Commit 7c994b2

Browse files
Phlogistiqueclaude
andauthored
Set up GitHub App token for e2e tests (#1)
* feat: Add GitHub App token generator script Add Python script to generate GitHub App installation tokens from environment variables (GH_APP_ID and GH_APP_PRIVATE_KEY_PEM_B64). This is needed to run e2e tests with proper authentication. * refactor: Convert token generator to uv standalone script - Add uv inline script metadata with dependencies - Automatically installs PyJWT, requests, cryptography on uv run - Update usage documentation to reflect uv usage - Remove manual dependency installation requirement --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6eefa5e commit 7c994b2

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

get_github_app_token.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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
12+
13+
Generates an installation access token from GitHub App credentials in environment.
14+
15+
Environment variables required:
16+
- GH_APP_ID: GitHub App ID
17+
- GH_APP_PRIVATE_KEY_PEM_B64: Base64-encoded private key
18+
19+
Usage:
20+
# Run with uv (automatically installs dependencies)
21+
uv run get_github_app_token.py
22+
23+
# Save to file
24+
uv run get_github_app_token.py > token.txt
25+
26+
# Use in shell
27+
export GITHUB_TOKEN=$(uv run get_github_app_token.py)
28+
"""
29+
30+
import base64
31+
import os
32+
import sys
33+
import time
34+
35+
import jwt
36+
import requests
37+
38+
39+
def get_installation_token():
40+
"""Generate an installation access token for the GitHub App."""
41+
42+
# Get credentials from environment
43+
app_id = os.getenv("GH_APP_ID")
44+
private_key_b64 = os.getenv("GH_APP_PRIVATE_KEY_PEM_B64")
45+
46+
if not all([app_id, private_key_b64]):
47+
print("Error: Missing GH_APP_ID or GH_APP_PRIVATE_KEY_PEM_B64", file=sys.stderr)
48+
sys.exit(1)
49+
50+
# Decode private key
51+
private_key = base64.b64decode(private_key_b64).decode('utf-8')
52+
53+
# Generate JWT
54+
now = int(time.time())
55+
payload = {
56+
"iat": now - 60,
57+
"exp": now + (10 * 60),
58+
"iss": app_id,
59+
}
60+
jwt_token = jwt.encode(payload, private_key, algorithm="RS256")
61+
62+
# Get installations
63+
headers = {
64+
"Authorization": f"Bearer {jwt_token}",
65+
"Accept": "application/vnd.github+json",
66+
"X-GitHub-Api-Version": "2022-11-28",
67+
}
68+
69+
response = requests.get("https://api.github.com/app/installations", headers=headers)
70+
response.raise_for_status()
71+
installations = response.json()
72+
73+
if not installations:
74+
print("Error: No installations found for this GitHub App", file=sys.stderr)
75+
sys.exit(1)
76+
77+
# Create installation token
78+
installation_id = installations[0]['id']
79+
url = f"https://api.github.com/app/installations/{installation_id}/access_tokens"
80+
response = requests.post(url, headers=headers)
81+
response.raise_for_status()
82+
83+
token_info = response.json()
84+
return token_info['token']
85+
86+
87+
if __name__ == "__main__":
88+
try:
89+
token = get_installation_token()
90+
print(token)
91+
except Exception as e:
92+
print(f"Error: {e}", file=sys.stderr)
93+
sys.exit(1)

0 commit comments

Comments
 (0)