|
10 | 10 | """ |
11 | 11 | GitHub App Token Generator with caching |
12 | 12 |
|
13 | | -Generates an installation access token from GitHub App credentials. |
| 13 | +Generates an installation access token from GitHub App credentials in environment. |
14 | 14 | Caches tokens and reuses them until they expire (with 5-minute buffer). |
15 | 15 |
|
16 | 16 | Environment variables required: |
17 | 17 | - GH_APP_ID: GitHub App ID |
18 | | -
|
19 | | -Files required: |
20 | | -- .gh-app-private-key.pem: PEM private key |
| 18 | +- GH_APP_PRIVATE_KEY: PEM private key |
21 | 19 |
|
22 | 20 | Usage: |
23 | 21 | # Run with uv (automatically installs dependencies) |
|
42 | 40 |
|
43 | 41 | # Cache file location |
44 | 42 | CACHE_FILE = Path("/tmp/gh_app_token_cache.json") |
45 | | -PRIVATE_KEY_FILE = Path(__file__).resolve().parents[1] / ".gh-app-private-key.pem" |
46 | 43 | # Buffer time before expiration (5 minutes) |
47 | 44 | EXPIRATION_BUFFER_SECONDS = 300 |
48 | 45 |
|
@@ -99,18 +96,14 @@ def save_token_to_cache(token, expires_at): |
99 | 96 |
|
100 | 97 | def generate_installation_token(): |
101 | 98 | """Generate a new installation access token for the GitHub App.""" |
| 99 | + # Get credentials from environment |
102 | 100 | app_id = os.getenv("GH_APP_ID") |
| 101 | + private_key = os.getenv("GH_APP_PRIVATE_KEY") |
103 | 102 |
|
104 | | - if not app_id: |
105 | | - print("Error: Missing GH_APP_ID", file=sys.stderr) |
106 | | - sys.exit(1) |
107 | | - |
108 | | - if not PRIVATE_KEY_FILE.exists(): |
109 | | - print(f"Error: Missing private key file: {PRIVATE_KEY_FILE}", file=sys.stderr) |
| 103 | + if not all([app_id, private_key]): |
| 104 | + print("Error: Missing GH_APP_ID or GH_APP_PRIVATE_KEY", file=sys.stderr) |
110 | 105 | sys.exit(1) |
111 | 106 |
|
112 | | - private_key = PRIVATE_KEY_FILE.read_text() |
113 | | - |
114 | 107 | # Generate JWT |
115 | 108 | now = int(time.time()) |
116 | 109 | payload = { |
|
0 commit comments