diff --git a/.github/workflows/build-guard.yml b/.github/workflows/build-guard.yml index 23e0d8ef..cf174762 100644 --- a/.github/workflows/build-guard.yml +++ b/.github/workflows/build-guard.yml @@ -170,6 +170,15 @@ jobs: echo "✓ All checked commit signatures verified successfully." echo "========================================================" + - name: Verify component version bumps + if: github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' && github.event.pull_request.user.login != 'dependabot[bot]' + env: + GITHUB_BASE_REF: ${{ github.base_ref }} + PR_ACTOR: ${{ github.event.pull_request.user.login || github.actor }} + PR_TITLE: ${{ github.event.pull_request.title }} + PR_BODY: ${{ github.event.pull_request.body }} + run: node scripts/check-pr-versions.js + - name: Validate Dependabot config run: | set -euo pipefail diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 31de00e3..8fd9414a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -159,6 +159,8 @@ jobs: INPUT_WEB: ${{ github.event.inputs.build_web || 'false' }} INPUT_MOBILE: ${{ github.event.inputs.build_mobile || 'false' }} INPUT_BACKUP: ${{ github.event.inputs.build_backup || 'false' }} + BEFORE_SHA: ${{ github.event.before || '' }} + AFTER_SHA: ${{ github.sha }} run: | set -euo pipefail @@ -216,8 +218,12 @@ jobs: DEPLOY_INFRA=true fi else - # push to main - COMMIT_MSG=$(git log -1 --format=%B) + # push to main — inspect all commits in the push/merge range + if [ -n "$BEFORE_SHA" ] && [ "$BEFORE_SHA" != "0000000000000000000000000000000000000000" ] && git cat-file -e "${BEFORE_SHA}^{commit}" 2>/dev/null; then + COMMIT_MSG=$(git log "${BEFORE_SHA}..${AFTER_SHA}" --format=%B) + else + COMMIT_MSG=$(git log -1 --format=%B) + fi if echo "$COMMIT_MSG" | grep -qiE '\[(skip release|manual-release)\]'; then echo "ℹ️ Release skipped due to [skip release] / [manual-release] marker." else diff --git a/scripts/__tests__/version-scripts.test.ts b/scripts/__tests__/version-scripts.test.ts index a45f4f7a..1382fa04 100644 --- a/scripts/__tests__/version-scripts.test.ts +++ b/scripts/__tests__/version-scripts.test.ts @@ -16,6 +16,7 @@ const { bumpComponent, } = require("../bump-version"); const { parsePushRefs } = require("../pre-push-bump"); +const { validateComponentVersions } = require("../check-pr-versions"); const tempDirs: string[] = []; @@ -194,4 +195,57 @@ describe("versioning scripts", () => { }, ]); }); + + it("blocks PRs when a changed component matches base version or existing tag", () => { + const baseState = { + web: { baseVersion: "4.7.4", headVersion: "4.7.4", tagExists: false }, + mobile: { baseVersion: "4.7.2", headVersion: "4.7.2", tagExists: false }, + backup: { baseVersion: "1.0.0", headVersion: "1.0.0", tagExists: false }, + }; + + const unbumped = validateComponentVersions({ + changedFiles: ["src/app/page.tsx"], + components: baseState, + baseRef: "origin/main", + }); + expect(unbumped.ok).toBe(false); + expect(unbumped.errors[0]).toContain("version is still 4.7.4"); + + const tagCollision = validateComponentVersions({ + changedFiles: ["src/app/page.tsx"], + components: { + ...baseState, + web: { baseVersion: "4.7.4", headVersion: "4.7.5", tagExists: true }, + }, + baseRef: "origin/main", + }); + expect(tagCollision.ok).toBe(false); + expect(tagCollision.errors[0]).toContain("web-v4.7.5"); + + const clean = validateComponentVersions({ + changedFiles: ["src/app/page.tsx"], + components: { + ...baseState, + web: { baseVersion: "4.7.4", headVersion: "4.7.5", tagExists: false }, + }, + baseRef: "origin/main", + }); + expect(clean.ok).toBe(true); + expect(clean.checked).toEqual(["web@4.7.5"]); + + const docsOnly = validateComponentVersions({ + changedFiles: ["docs/VERSIONING.md"], + components: baseState, + baseRef: "origin/main", + }); + expect(docsOnly.ok).toBe(true); + + const skipped = validateComponentVersions({ + changedFiles: ["src/app/page.tsx"], + components: baseState, + skipRelease: true, + baseRef: "origin/main", + }); + expect(skipped.ok).toBe(true); + }); }); diff --git a/scripts/check-pr-versions.js b/scripts/check-pr-versions.js new file mode 100644 index 00000000..50e2352e --- /dev/null +++ b/scripts/check-pr-versions.js @@ -0,0 +1,169 @@ +#!/usr/bin/env node + +const { execFileSync } = require("node:child_process"); +const path = require("node:path"); +const { + VALID_COMPONENTS, + getPrimaryVersionFile, + parseComponentVersionContent, + readComponentVersion, +} = require("./sync-version"); +const { isComponentPath } = require("./bump-version"); + +const ROOT_DIR = path.join(__dirname, ".."); +const SKIP_MARKER_REGEX = + /\[(skip release|manual-release|skip version check)\]/i; + +function tryGit(args, rootDir = ROOT_DIR) { + try { + return execFileSync("git", args, { + cwd: rootDir, + encoding: "utf8", + stdio: ["ignore", "pipe", "pipe"], + }).trim(); + } catch { + return null; + } +} + +/** + * Validates that every changed component has a bumped version not colliding with + * the base branch or an existing release tag. + * @param {object} params + * @param {string[]} params.changedFiles + * @param {Record} params.components + * @param {boolean} [params.skipRelease] + * @param {string} [params.baseRef] + * @returns {{ ok: boolean, errors: string[], checked: string[] }} + */ +function validateComponentVersions({ + changedFiles, + components, + skipRelease = false, + baseRef = "main", +}) { + if (skipRelease) { + return { ok: true, errors: [], checked: [] }; + } + + const errors = []; + const checked = []; + + for (const component of VALID_COMPONENTS) { + const changed = changedFiles.some((file) => + isComponentPath(component, file), + ); + if (!changed) continue; + + const info = components[component]; + if (!info || !info.baseVersion) { + continue; + } + + checked.push(`${component}@${info.headVersion}`); + + if (info.headVersion === info.baseVersion) { + errors.push( + `${component} files changed, but version is still ${info.headVersion} (matches ${baseRef}). ` + + `Pull ${baseRef} locally and run 'git push' so .husky/pre-push bumps it, or include [skip release] to batch.`, + ); + continue; + } + + if (info.tagExists) { + errors.push( + `${component} version ${info.headVersion} already has release tag ${component}-v${info.headVersion}. ` + + `Bump ${component} past ${info.headVersion} before merging.`, + ); + } + } + + return { ok: errors.length === 0, errors, checked }; +} + +function main() { + const actor = process.env.PR_ACTOR || process.env.GITHUB_ACTOR || ""; + if (actor === "dependabot[bot]") { + console.log("ℹ️ Skipping component version guard for dependabot[bot]."); + process.exit(0); + } + + const baseBranch = process.env.GITHUB_BASE_REF || "main"; + const remoteBase = tryGit(["rev-parse", "--verify", `origin/${baseBranch}`]) + ? `origin/${baseBranch}` + : baseBranch; + + if (!tryGit(["rev-parse", "--verify", remoteBase])) { + console.log( + `ℹ️ Base ref ${remoteBase} not found; skipping component version guard.`, + ); + process.exit(0); + } + + const prText = `${process.env.PR_TITLE || ""}\n${process.env.PR_BODY || ""}`; + const commitMessages = + tryGit(["log", `${remoteBase}..HEAD`, "--format=%B"]) || ""; + const skipRelease = + SKIP_MARKER_REGEX.test(prText) || SKIP_MARKER_REGEX.test(commitMessages); + + if (skipRelease) { + console.log( + "ℹ️ Skipping component version guard ([skip release] / [manual-release] marker found).", + ); + process.exit(0); + } + + const diffRaw = tryGit(["diff", "--name-only", `${remoteBase}...HEAD`]) || ""; + const changedFiles = diffRaw + .split(/\r?\n/) + .map((f) => f.trim()) + .filter(Boolean); + + const components = {}; + for (const component of VALID_COMPONENTS) { + const file = getPrimaryVersionFile(component); + const baseContent = tryGit(["show", `${remoteBase}:${file}`]); + const baseVersion = baseContent + ? parseComponentVersionContent(component, baseContent) + : null; + const headVersion = readComponentVersion(component, ROOT_DIR); + const tagExists = Boolean( + tryGit([ + "rev-parse", + "-q", + "--verify", + `refs/tags/${component}-v${headVersion}`, + ]), + ); + components[component] = { baseVersion, headVersion, tagExists }; + } + + const result = validateComponentVersions({ + changedFiles, + components, + skipRelease: false, + baseRef: remoteBase, + }); + + if (!result.ok) { + for (const err of result.errors) { + console.error(`::error::${err}`); + } + process.exit(1); + } + + if (result.checked.length > 0) { + console.log(`✓ Component versions verified: ${result.checked.join(", ")}`); + } else { + console.log("ℹ️ No release-tracked component files changed in this PR."); + } +} + +if (require.main === module) { + main(); +} + +module.exports = { + SKIP_MARKER_REGEX, + validateComponentVersions, +};