|
| 1 | +import type { Octokit } from "@octokit/core"; |
| 2 | + |
| 3 | +// standard github REST API headers |
| 4 | +const HEADERS = { |
| 5 | + accept: "application/vnd.github+json", |
| 6 | + "X-Github-Api-Version": "2026-03-10", |
| 7 | +} |
| 8 | + |
| 9 | +// metadata looks like: { name, value, created_at, updated_at ... } |
| 10 | +export async function getRepoVariableMetadata(client: Octokit, repoName: string, repoOwnerName: string, variableName: string): Promise<any> { |
| 11 | + const res = await client.request( |
| 12 | + "GET /repos/{owner}/{repo}/actions/variables/{name}", |
| 13 | + { owner: repoOwnerName, repo: repoName, name: variableName, headers: HEADERS}, |
| 14 | + ); |
| 15 | + return res.data; |
| 16 | +} |
| 17 | + |
| 18 | +export async function setRepoVariable(client: Octokit, repoName: string, repoOwnerName: string, variableName: string, value: string): Promise<void> { |
| 19 | + try { |
| 20 | + await client.request( |
| 21 | + "PATCH /repos/{owner}/{repo}/actions/variables/{name}", |
| 22 | + { owner: repoOwnerName, repo: repoName, name: variableName, value, headers: HEADERS}, |
| 23 | + ); |
| 24 | + } catch (err) { |
| 25 | + if (!isNotFoundError(err)) throw err; |
| 26 | + |
| 27 | + await client.request( |
| 28 | + "POST /repos/{owner}/{repo}/actions/variables", |
| 29 | + { owner: repoOwnerName, repo: repoName, name: variableName, value, headers: HEADERS}, |
| 30 | + ); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function isNotFoundError(err: unknown): boolean { |
| 35 | + return typeof err === "object" && err !== null && "status" in err && err.status === 404; |
| 36 | +} |
0 commit comments