|
| 1 | +import { exec } from "child_process"; |
| 2 | + |
| 3 | +type Runner = (command: string) => Promise<string | undefined>; |
| 4 | + |
| 5 | +// Quiet git runner, same contract as git_context's: trimmed stdout or |
| 6 | +// undefined on any failure. A deploy must never fail because a release |
| 7 | +// version couldn't be proposed. |
| 8 | +const gitRunner = |
| 9 | + (workdir?: string): Runner => |
| 10 | + command => |
| 11 | + new Promise(resolve => { |
| 12 | + exec(command, { cwd: workdir }, (err, stdout) => { |
| 13 | + if (err) return resolve(undefined); |
| 14 | + const out = stdout?.toString().trim(); |
| 15 | + resolve(out || undefined); |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | +// Loose version shape: "starts like semver". Filters out non-version tags |
| 20 | +// (e.g. "nightly", "deploy-2026-07-01") when falling back to git describe; |
| 21 | +// explicit values (--release / FAABLE_RELEASE) are NEVER validated — the |
| 22 | +// platform treats release as free text. |
| 23 | +const looksLikeVersion = (v: string) => /^\d+\.\d+\.\d+/.test(v); |
| 24 | + |
| 25 | +const stripV = (tag: string) => tag.replace(/^v/, ""); |
| 26 | + |
| 27 | +/** |
| 28 | + * Propose the release version for a deploy (Sentry propose-version pattern): |
| 29 | + * explicit `--release` > `FAABLE_RELEASE` env > latest reachable git tag |
| 30 | + * (`v1.2.3` or `1.2.3`, e.g. the tag semantic-release created) > undefined. |
| 31 | + * When undefined the field is omitted from the deployment and the platform |
| 32 | + * injects no FAABLE_RELEASE — the app falls back to its own version source. |
| 33 | + * No SHA fallback: the commit already travels as `github_commit`. |
| 34 | + */ |
| 35 | +export const propose_release = async (opts?: { |
| 36 | + workdir?: string; |
| 37 | + explicit?: string; |
| 38 | + env?: Record<string, string | undefined>; |
| 39 | + run?: Runner; |
| 40 | +}): Promise<{ release: string; source: string } | undefined> => { |
| 41 | + const env = opts?.env ?? process.env; |
| 42 | + const run = opts?.run ?? gitRunner(opts?.workdir); |
| 43 | + |
| 44 | + if (opts?.explicit) return { release: opts.explicit, source: "--release" }; |
| 45 | + if (env.FAABLE_RELEASE) |
| 46 | + return { release: env.FAABLE_RELEASE, source: "FAABLE_RELEASE env" }; |
| 47 | + |
| 48 | + // Latest tag reachable from HEAD. Try version-shaped tags first so a |
| 49 | + // repo that also tags non-releases still resolves; retry unfiltered for |
| 50 | + // repos whose release tags carry no `v` prefix. |
| 51 | + for (const cmd of [ |
| 52 | + `git describe --tags --abbrev=0 --match "v[0-9]*"`, |
| 53 | + `git describe --tags --abbrev=0`, |
| 54 | + ]) { |
| 55 | + const tag = await run(cmd); |
| 56 | + if (!tag) continue; |
| 57 | + const version = stripV(tag); |
| 58 | + if (looksLikeVersion(version)) { |
| 59 | + return { release: version, source: `git tag ${tag}` }; |
| 60 | + } |
| 61 | + } |
| 62 | + return undefined; |
| 63 | +}; |
0 commit comments