From 1287db680d3da87f5f5e6fa869d198aeede663ed Mon Sep 17 00:00:00 2001 From: Jordan Ribbink <17958158+jribbink@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:38:17 -0700 Subject: [PATCH] Prevent major version bumps (#1991) --- .github/scripts/prevent-major-bumps.js | 60 ++++++++++++++++++++++++++ .github/workflows/integrate.yml | 15 +++++++ 2 files changed, 75 insertions(+) create mode 100644 .github/scripts/prevent-major-bumps.js diff --git a/.github/scripts/prevent-major-bumps.js b/.github/scripts/prevent-major-bumps.js new file mode 100644 index 000000000..0836ddad3 --- /dev/null +++ b/.github/scripts/prevent-major-bumps.js @@ -0,0 +1,60 @@ +/** + * DO NOT REMOVE ME UNLESS YOU KNOW WHAT YOU'RE DOING!! + */ + +const {execSync} = require("child_process") +const fs = require("fs") + +// Fetch the latest changes from the main branch +execSync("git fetch origin master") + +const packageJsons = execSync("git ls-files 'packages/*/package.json'") + .toString() + .split("\n") + .filter(Boolean) + +// Assert that the package.json files exist +if (packageJsons.length === 0) { + console.error("Error: No package.json files found.") + process.exit(1) // Fail the CI +} + +for (const packageJson of packageJsons) { + // Get the current version from package.json + const newPackageJson = JSON.parse(fs.readFileSync(packageJson, "utf8")) + const newPackageName = newPackageJson.name + const newVersion = newPackageJson.version + + // Get the version from the main branch (or latest release) + const prevPackageJson = JSON.parse( + execSync(`git show origin/master:${packageJson}`).toString() + ) + const prevPackageName = prevPackageJson.name + const prevVersion = prevPackageJson.version + + // Assert that the package names match + if (newPackageName !== prevPackageName) { + console.error( + `Error: Package name mismatch for ${newPackageName} (${prevPackageName} -> ${newPackageName}).` + ) + process.exit(1) // Fail the CI + } + + // Extract major, minor, and patch numbers + const newMajor = parseInt(newVersion.split(".")[0]) + const prevMajor = parseInt(prevVersion.split(".")[0]) + + // Check if it's a major version bump + if (newMajor > prevMajor) { + console.error( + `Error: Major version bump detected for ${newPackageName} (${prevVersion} -> ${newVersion}).` + ) + process.exit(1) // Fail the CI + } + + console.log( + `Version bump allowed for ${newPackageName}: ${prevVersion} -> ${newVersion}` + ) +} + +process.exit(0) // Allow the CI to pass diff --git a/.github/workflows/integrate.yml b/.github/workflows/integrate.yml index cafe53565..f0c62eaa4 100644 --- a/.github/workflows/integrate.yml +++ b/.github/workflows/integrate.yml @@ -11,4 +11,19 @@ jobs: - uses: actions/setup-node@v1 with: node-version: 18.x + + # This step is VERY important. Changesets has a bug where peer dependencies will cause major version bumps + # This script will prevent that from happening. You must manually edit any changesets PRs which have this issue + # to correct the version bump. + # + # If you wish to allow a major version bump, you will have to override this check, but be VERY careful with it. + # + # See: + # https://github.com/changesets/changesets/pull/1132 + # https://github.com/changesets/changesets/issues/1011 + # https://github.com/changesets/changesets/issues/960 + # https://github.com/changesets/changesets/issues/822 + - name: Prevent Major Version Bumps + run: node ./.github/scripts/prevent-major-bumps.js + - run: make ci