|
| 1 | +/** |
| 2 | + * Repository metadata read from the CI system the CLI runs in, so a Maestro |
| 3 | + * run can link back to its repository, branch, commit and pull request |
| 4 | + * without the user passing --repo-owner / --repo-name / --commit-sha. |
| 5 | + * |
| 6 | + * `repoUrl` is the repository's web URL on whatever host it lives on (GitHub, |
| 7 | + * GitLab, Bitbucket, Azure Repos, a self-hosted server); the dashboard builds |
| 8 | + * the branch / commit / PR links from it. `repoOwner` / `repoName` are only |
| 9 | + * filled in for github.com, because the API reads them as a GitHub repository |
| 10 | + * (status checks via the TestingBot GitHub App). |
| 11 | + * |
| 12 | + * Explicit CLI flags always win over anything detected here. |
| 13 | + */ |
| 14 | + |
| 15 | +import fs from 'node:fs'; |
| 16 | + |
| 17 | +export interface CiRepoMetadata { |
| 18 | + repoUrl?: string; |
| 19 | + repoOwner?: string; |
| 20 | + repoName?: string; |
| 21 | + commitSha?: string; |
| 22 | + branch?: string; |
| 23 | + pullRequestId?: string; |
| 24 | + pullRequestUrl?: string; |
| 25 | +} |
| 26 | + |
| 27 | +type Env = NodeJS.ProcessEnv; |
| 28 | + |
| 29 | +/** |
| 30 | + * Turns a clone URL into the repository's web URL: SSH (`git@host:o/r.git`, |
| 31 | + * `ssh://git@host/o/r`) becomes https, credentials and a trailing `.git` are |
| 32 | + * dropped. Returns undefined for anything that is not an http(s) URL after that. |
| 33 | + */ |
| 34 | +export function webRepoUrl(raw: string | undefined): string | undefined { |
| 35 | + if (!raw) return undefined; |
| 36 | + let value = raw.trim(); |
| 37 | + const scp = value.match(/^[^@\s/]+@([^:\s/]+):(.+)$/); |
| 38 | + if (scp) value = `https://${scp[1]}/${scp[2]}`; |
| 39 | + value = value.replace(/^ssh:\/\//, 'https://'); |
| 40 | + let url: URL; |
| 41 | + try { |
| 42 | + url = new URL(value); |
| 43 | + } catch { |
| 44 | + return undefined; |
| 45 | + } |
| 46 | + if (url.protocol === 'http:') url.protocol = 'https:'; |
| 47 | + if (url.protocol !== 'https:') return undefined; |
| 48 | + url.username = ''; |
| 49 | + url.password = ''; |
| 50 | + url.port = ''; |
| 51 | + url.search = ''; |
| 52 | + url.hash = ''; |
| 53 | + const path = url.pathname.replace(/\/+$/, '').replace(/\.git$/, ''); |
| 54 | + if (path === '' || path === '/') return undefined; |
| 55 | + return `${url.origin}${path}`; |
| 56 | +} |
| 57 | + |
| 58 | +/** `refs/heads/feature/x` → `feature/x`; other refs are returned as given. */ |
| 59 | +function stripHeads(ref: string | undefined): string | undefined { |
| 60 | + if (!ref) return undefined; |
| 61 | + return ref.replace(/^refs\/heads\//, ''); |
| 62 | +} |
| 63 | + |
| 64 | +function githubOwnerName( |
| 65 | + repoUrl: string | undefined, |
| 66 | +): Pick<CiRepoMetadata, 'repoOwner' | 'repoName'> { |
| 67 | + const m = repoUrl?.match(/^https:\/\/github\.com\/([^/]+)\/([^/]+)$/); |
| 68 | + return m ? { repoOwner: m[1], repoName: m[2] } : {}; |
| 69 | +} |
| 70 | + |
| 71 | +function readJson(path: string | undefined): Record<string, unknown> | null { |
| 72 | + if (!path) return null; |
| 73 | + try { |
| 74 | + return JSON.parse(fs.readFileSync(path, 'utf8')); |
| 75 | + } catch { |
| 76 | + return null; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function githubActions(env: Env): CiRepoMetadata { |
| 81 | + const server = env.GITHUB_SERVER_URL || 'https://github.com'; |
| 82 | + const repoUrl = env.GITHUB_REPOSITORY |
| 83 | + ? webRepoUrl(`${server}/${env.GITHUB_REPOSITORY}`) |
| 84 | + : undefined; |
| 85 | + // On pull_request events GITHUB_SHA is the throwaway merge commit; the |
| 86 | + // event payload carries the PR's head commit, which is what the user pushed. |
| 87 | + const event = readJson(env.GITHUB_EVENT_PATH); |
| 88 | + const pr = event?.pull_request as |
| 89 | + | { number?: number; head?: { sha?: string }; html_url?: string } |
| 90 | + | undefined; |
| 91 | + const prFromRef = env.GITHUB_REF?.match(/^refs\/pull\/(\d+)\//)?.[1]; |
| 92 | + return { |
| 93 | + repoUrl, |
| 94 | + ...githubOwnerName(repoUrl), |
| 95 | + commitSha: pr?.head?.sha || env.GITHUB_SHA, |
| 96 | + branch: env.GITHUB_HEAD_REF || env.GITHUB_REF_NAME, |
| 97 | + pullRequestId: pr?.number ? String(pr.number) : prFromRef, |
| 98 | + pullRequestUrl: pr?.html_url, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +function gitlab(env: Env): CiRepoMetadata { |
| 103 | + const repoUrl = webRepoUrl(env.CI_PROJECT_URL); |
| 104 | + return { |
| 105 | + repoUrl, |
| 106 | + commitSha: env.CI_COMMIT_SHA, |
| 107 | + branch: |
| 108 | + env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME || |
| 109 | + env.CI_COMMIT_BRANCH || |
| 110 | + env.CI_COMMIT_REF_NAME, |
| 111 | + pullRequestId: env.CI_MERGE_REQUEST_IID, |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +function bitbucket(env: Env): CiRepoMetadata { |
| 116 | + const repoUrl = webRepoUrl(env.BITBUCKET_GIT_HTTP_ORIGIN); |
| 117 | + return { |
| 118 | + repoUrl, |
| 119 | + commitSha: env.BITBUCKET_COMMIT, |
| 120 | + branch: env.BITBUCKET_BRANCH, |
| 121 | + pullRequestId: env.BITBUCKET_PR_ID, |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +function azurePipelines(env: Env): CiRepoMetadata { |
| 126 | + const repoUrl = webRepoUrl(env.BUILD_REPOSITORY_URI); |
| 127 | + // PR builds check out a merge commit; for GitHub-hosted repositories Azure |
| 128 | + // also exposes the PR's own head commit. Azure Repos PRs use the ID, GitHub |
| 129 | + // PRs the number. |
| 130 | + const isPr = !!env.SYSTEM_PULLREQUEST_PULLREQUESTID; |
| 131 | + const prId = |
| 132 | + env.BUILD_REPOSITORY_PROVIDER === 'GitHub' |
| 133 | + ? env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER |
| 134 | + : env.SYSTEM_PULLREQUEST_PULLREQUESTID; |
| 135 | + return { |
| 136 | + repoUrl, |
| 137 | + ...githubOwnerName(repoUrl), |
| 138 | + commitSha: env.SYSTEM_PULLREQUEST_SOURCECOMMITID || env.BUILD_SOURCEVERSION, |
| 139 | + branch: isPr |
| 140 | + ? stripHeads(env.SYSTEM_PULLREQUEST_SOURCEBRANCH) |
| 141 | + : stripHeads(env.BUILD_SOURCEBRANCH) || env.BUILD_SOURCEBRANCHNAME, |
| 142 | + pullRequestId: isPr ? prId : undefined, |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +function circleci(env: Env): CiRepoMetadata { |
| 147 | + const repoUrl = webRepoUrl(env.CIRCLE_REPOSITORY_URL); |
| 148 | + const prUrl = env.CIRCLE_PULL_REQUEST; |
| 149 | + return { |
| 150 | + repoUrl, |
| 151 | + ...githubOwnerName(repoUrl), |
| 152 | + commitSha: env.CIRCLE_SHA1, |
| 153 | + branch: env.CIRCLE_BRANCH, |
| 154 | + pullRequestId: prUrl?.match(/\/(\d+)$/)?.[1], |
| 155 | + pullRequestUrl: prUrl, |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +/** Detects the CI system from `env` and returns what it knows; {} outside CI. */ |
| 160 | +export function ciRepoMetadata(env: Env = process.env): CiRepoMetadata { |
| 161 | + let detected: CiRepoMetadata = {}; |
| 162 | + if (env.GITHUB_ACTIONS === 'true') detected = githubActions(env); |
| 163 | + else if (env.GITLAB_CI === 'true') detected = gitlab(env); |
| 164 | + else if (env.BITBUCKET_BUILD_NUMBER) detected = bitbucket(env); |
| 165 | + else if (env.TF_BUILD?.toLowerCase() === 'true') |
| 166 | + detected = azurePipelines(env); |
| 167 | + else if (env.CIRCLECI === 'true') detected = circleci(env); |
| 168 | + |
| 169 | + const result: CiRepoMetadata = {}; |
| 170 | + for (const [key, value] of Object.entries(detected)) { |
| 171 | + if (value) result[key as keyof CiRepoMetadata] = value; |
| 172 | + } |
| 173 | + return result; |
| 174 | +} |
0 commit comments