Skip to content

Commit

Permalink
Prevent major version bumps (#1991)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Oct 24, 2024
1 parent 1ef02e3 commit 1287db6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
60 changes: 60 additions & 0 deletions .github/scripts/prevent-major-bumps.js
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1287db6

Please sign in to comment.