import { GITHUB_API_URL, USE_GITEA_API } from "./config"; /** * Resolves the Gitea run ID from the run number. * In Gitea, the environment variable GITHUB_RUN_ID is not available, * so we need to fetch it via the API using GITEA_RUN_NUMBER. * * @param token - GitHub/Gitea token for authentication * @param owner - Repository owner * @param repo - Repository name * @returns The resolved run ID, or undefined if not found */ export async function resolveGiteaRunId( token: string, owner: string, repo: string, ): Promise { if (!USE_GITEA_API) { return undefined; } const runNumber = process.env.GITEA_RUN_NUMBER; if (!runNumber) { console.log("GITEA_RUN_NUMBER not set, cannot resolve Gitea run ID"); return undefined; } try { // Call Gitea API to get action runs // GET /api/v1/repos/{owner}/{repo}/actions/runs const url = `${GITHUB_API_URL}/repos/${owner}/${repo}/actions/runs`; const response = await fetch(url, { headers: { Authorization: `token ${token}`, Accept: "application/json", }, }); if (!response.ok) { console.error( `Failed to fetch Gitea action runs: ${response.status} ${response.statusText}`, ); return undefined; } const data = (await response.json()) as { workflow_runs?: Array<{ id: number; run_number: number }>; }; // Find the run with matching run_number const runs = data.workflow_runs || []; const targetRunNumber = parseInt(runNumber, 10); const matchingRun = runs.find((run) => run.run_number === targetRunNumber); if (matchingRun) { console.log( `Resolved Gitea run ID: ${matchingRun.id} from run number: ${runNumber}`, ); return String(matchingRun.id); } console.log( `Could not find Gitea run with run_number: ${runNumber} in ${runs.length} runs`, ); return undefined; } catch (error) { console.error("Error resolving Gitea run ID:", error); return undefined; } } /** * Sets up the GITHUB_RUN_ID environment variable for Gitea. * This should be called early in the action execution. */ export async function setupGiteaRunId( token: string, owner: string, repo: string, ): Promise { // Only proceed if we're in Gitea and don't already have a run ID if (!USE_GITEA_API || process.env.GITHUB_RUN_ID) { return; } const runId = await resolveGiteaRunId(token, owner, repo); if (runId) { process.env.GITHUB_RUN_ID = runId; console.log(`Set GITHUB_RUN_ID to ${runId} from Gitea API`); } }