26 lines · 1.2 KBRaw
| 1 | const MAX_ID = 100; |
| 2 | |
| 3 | /** |
| 4 | * The run's ID, and so its Workflow instance ID (D2): repo, commit, config hash, and a variant that tells a pull |
| 5 | * request run (`pr12`) or a manual run (`m<time>`) apart from the push run of the same commit. A second start of the |
| 6 | * same commit, config and variant gets the same ID and starts nothing. Workflow IDs allow only letters, digits, - and _. |
| 7 | */ |
| 8 | export async function runIdFor(repo: string, sha: string, config: string, variant = ""): Promise<string> { |
| 9 | const hash = (await configHash(config)).slice(0, 12); |
| 10 | const suffix = `-${sha.slice(0, 12)}-${hash}${variant ? `-${variant}` : ""}`; |
| 11 | const safeRepo = repo.toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-+|-+$/g, "") || "repo"; |
| 12 | return `${safeRepo.slice(0, MAX_ID - suffix.length)}${suffix}`; |
| 13 | } |
| 14 | |
| 15 | export function jobRunnerName(runId: string, job: string): string { |
| 16 | return `${runId}/${job}`; |
| 17 | } |
| 18 | |
| 19 | export function approvalEvent(job: string): string { |
| 20 | return `approval-${job}`; |
| 21 | } |
| 22 | |
| 23 | export async function configHash(config: string): Promise<string> { |
| 24 | const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(config))); |
| 25 | return Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join(""); |
| 26 | } |