yiqingxiong 8bbd3ab4ec
Some checks failed
CI All / test-custom-executables (pull_request) Failing after 26s
CI All / test-base-action (pull_request) Failing after 1m37s
CI All / ci (pull_request) Failing after 2m0s
CI All / test-settings (pull_request) Failing after 2m38s
CI All / test-mcp-servers (pull_request) Failing after 2m48s
CI / test (pull_request) Failing after 1m14s
CI / prettier (pull_request) Failing after 1m0s
CI / typecheck (pull_request) Failing after 43s
Test Claude Code Action / test-inline-prompt (pull_request) Failing after 41s
CI All / test-structured-output (pull_request) Failing after 3m8s
Test Custom Executables / test-custom-executables (pull_request) Failing after 11s
Test Claude Code Action / test-prompt-file (pull_request) Failing after 41s
PR Review / review (pull_request) Failing after 1m23s
Test Settings Feature / test-settings-inline-allow (pull_request) Failing after 47s
Test Settings Feature / test-settings-inline-deny (pull_request) Failing after 49s
Test MCP Servers / test-mcp-integration (pull_request) Failing after 59s
Test Settings Feature / test-settings-file-allow (pull_request) Failing after 39s
Test MCP Servers / test-mcp-config-flag (pull_request) Failing after 1m18s
Test Settings Feature / test-settings-file-deny (pull_request) Failing after 30s
Test Structured Outputs / Test Basic Type Conversions (pull_request) Failing after 31s
Test Structured Outputs / Test Arrays and Objects (pull_request) Failing after 31s
Test Structured Outputs / Test Edge Cases (pull_request) Failing after 28s
Test Structured Outputs / Test Output Name Sanitization (pull_request) Failing after 28s
Test Structured Outputs / Test Execution File Format (pull_request) Failing after 32s
Test Structured Outputs / Summary (pull_request) Failing after 1s
fix run id in gitea
2026-01-31 14:49:12 +08:00

92 lines
2.5 KiB
TypeScript

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<string | undefined> {
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<void> {
// 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`);
}
}