|
| 1 | +#!/usr/bin/env bun |
| 2 | +/** |
| 3 | + * Determines the HTML URL for a GitHub Actions job within the current run. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * bun run scripts/determine-job-url.ts --pattern "Benchmark agent / model / eval" |
| 7 | + */ |
| 8 | + |
| 9 | +import process from "node:process"; |
| 10 | + |
| 11 | +import { request as octokitRequest } from "@octokit/request"; |
| 12 | +import type { Endpoints } from "@octokit/types"; |
| 13 | + |
| 14 | +type ListWorkflowJobsResponse = |
| 15 | + Endpoints["GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs"]["response"]["data"]; |
| 16 | +type WorkflowJob = NonNullable<ListWorkflowJobsResponse["jobs"]>[number]; |
| 17 | + |
| 18 | +function usage(): void { |
| 19 | + console.error( |
| 20 | + "Usage: bun run scripts/determine-job-url.ts --pattern \"<job name substring>\"", |
| 21 | + ); |
| 22 | + console.error(""); |
| 23 | + console.error("Looks up the job URL from the current workflow run."); |
| 24 | +} |
| 25 | + |
| 26 | +function parseArgs(argv: string[]): { pattern: string } { |
| 27 | + let pattern: string | undefined; |
| 28 | + |
| 29 | + for (let index = 0; index < argv.length; index += 1) { |
| 30 | + const arg = argv[index]; |
| 31 | + if (arg === "--pattern") { |
| 32 | + pattern = argv[index + 1]; |
| 33 | + index += 1; |
| 34 | + } else { |
| 35 | + console.error(`Unknown argument: ${arg}`); |
| 36 | + usage(); |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (!pattern) { |
| 42 | + usage(); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + |
| 46 | + return { pattern }; |
| 47 | +} |
| 48 | + |
| 49 | +async function fetchJobs( |
| 50 | + owner: string, |
| 51 | + repo: string, |
| 52 | + runId: number, |
| 53 | +): Promise<WorkflowJob[]> { |
| 54 | + const token = process.env.GITHUB_TOKEN?.trim(); |
| 55 | + if (!token) { |
| 56 | + throw new Error("GITHUB_TOKEN is required to call the GitHub API."); |
| 57 | + } |
| 58 | + |
| 59 | + const request = octokitRequest.defaults({ |
| 60 | + headers: { |
| 61 | + authorization: `Bearer ${token}`, |
| 62 | + "user-agent": "opencode-bench/job-url", |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + const jobs: WorkflowJob[] = []; |
| 67 | + const perPage = 100; |
| 68 | + let page = 1; |
| 69 | + |
| 70 | + // GitHub caps pagination at 100 items per page. Loop until a page returns fewer rows. |
| 71 | + while (true) { |
| 72 | + const response = await request( |
| 73 | + "GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs", |
| 74 | + { |
| 75 | + owner, |
| 76 | + repo, |
| 77 | + run_id: runId, |
| 78 | + per_page: perPage, |
| 79 | + page, |
| 80 | + }, |
| 81 | + ); |
| 82 | + |
| 83 | + const data = response.data as ListWorkflowJobsResponse; |
| 84 | + const batch = data.jobs ?? []; |
| 85 | + jobs.push(...batch); |
| 86 | + |
| 87 | + if (batch.length < perPage) { |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + page += 1; |
| 92 | + } |
| 93 | + |
| 94 | + return jobs; |
| 95 | +} |
| 96 | + |
| 97 | +async function main(): Promise<void> { |
| 98 | + const repoSlug = process.env.GITHUB_REPOSITORY; |
| 99 | + const runIdRaw = process.env.GITHUB_RUN_ID; |
| 100 | + |
| 101 | + if (!repoSlug || !runIdRaw) { |
| 102 | + throw new Error( |
| 103 | + "GITHUB_REPOSITORY and GITHUB_RUN_ID must be defined in the environment.", |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + const runId = Number(runIdRaw); |
| 108 | + if (!Number.isFinite(runId)) { |
| 109 | + throw new Error(`Invalid GITHUB_RUN_ID value: ${runIdRaw}`); |
| 110 | + } |
| 111 | + |
| 112 | + const [owner, repo] = repoSlug.split("/", 2); |
| 113 | + if (!owner || !repo) { |
| 114 | + throw new Error(`Invalid GITHUB_REPOSITORY value: ${repoSlug}`); |
| 115 | + } |
| 116 | + |
| 117 | + const { pattern } = parseArgs(process.argv.slice(2)); |
| 118 | + const jobs = await fetchJobs(owner, repo, runId); |
| 119 | + |
| 120 | + if (jobs.length === 0) { |
| 121 | + throw new Error("No jobs were returned for the current workflow run."); |
| 122 | + } |
| 123 | + |
| 124 | + const match = jobs.find((job) => job.name?.includes(pattern)); |
| 125 | + |
| 126 | + if (!match) { |
| 127 | + console.error( |
| 128 | + `Failed to find a job whose name contains "${pattern}". Available jobs:`, |
| 129 | + ); |
| 130 | + for (const job of jobs) { |
| 131 | + console.error(`- ${job.name} [status=${job.status}]`); |
| 132 | + } |
| 133 | + process.exit(1); |
| 134 | + } |
| 135 | + |
| 136 | + if (!match.html_url) { |
| 137 | + throw new Error(`Job ${match.id} is missing an html_url field.`); |
| 138 | + } |
| 139 | + |
| 140 | + process.stdout.write(`${match.html_url}\n`); |
| 141 | +} |
| 142 | + |
| 143 | +if (import.meta.main) { |
| 144 | + main().catch((error) => { |
| 145 | + console.error( |
| 146 | + error instanceof Error ? error.message : String(error), |
| 147 | + ); |
| 148 | + process.exit(1); |
| 149 | + }); |
| 150 | +} |
0 commit comments