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