Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/build-guard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions scripts/__tests__/version-scripts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
bumpComponent,
} = require("../bump-version");
const { parsePushRefs } = require("../pre-push-bump");
const { validateComponentVersions } = require("../check-pr-versions");

const tempDirs: string[] = [];

Expand Down Expand Up @@ -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);
});
});
169 changes: 169 additions & 0 deletions scripts/check-pr-versions.js
Original file line number Diff line number Diff line change
@@ -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<string, { baseVersion: string | null, headVersion: string, tagExists: boolean }>} 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,
};
Loading