Generate tokens before running action/play.ts (#296)

* Generate tokens before running `action/play.ts`

* Extract `ensureGitHubToken` utility

---------

Co-authored-by: pullfrog[bot] <226033991+pullfrog[bot]@users.noreply.github.com>
This commit is contained in:
Mateusz Burzyński
2026-02-13 20:09:54 +00:00
committed by pullfrog[bot]
parent 6f108237d4
commit 8037c118cc
3 changed files with 23 additions and 8 deletions
+18
View File
@@ -274,6 +274,24 @@ async function acquireTokenViaGitHubApp(opts?: AcquireTokenOptions): Promise<str
return await createInstallationToken(jwt, installationId, opts?.permissions);
}
/**
* Ensure a GitHub token is available in the environment.
*
* If neither `GITHUB_TOKEN` nor `GH_TOKEN` is set, attempts to acquire an
* installation token using `GITHUB_APP_ID` / `GITHUB_PRIVATE_KEY`.
*
* **Not intended for production use** — this is a convenience for local
* development and test harnesses where tokens aren't pre-provisioned.
*/
export async function ensureGitHubToken(): Promise<void> {
if (!process.env.GITHUB_TOKEN && !process.env.GH_TOKEN) {
if (process.env.GITHUB_APP_ID && process.env.GITHUB_PRIVATE_KEY) {
const token = await acquireNewToken();
process.env.GITHUB_TOKEN = token;
}
}
}
export async function acquireNewToken(opts?: AcquireTokenOptions): Promise<string> {
if (isOIDCAvailable()) {
return await retry(() => acquireTokenViaOIDC(opts), {