Skip to content

Commit

Permalink
chore(release): try changesets instead of changelog (#10138)
Browse files Browse the repository at this point in the history
The newish changelog workflow has personally saved me a lot of time and
is half the reason we were able to release so many times on a moment's
notice last week. But it's prone to merge conflicts, both in this repo
and in the release tooling (most cherry picks from the main branch to
the next branch had merge conflicts because of the changelog) so it's
not going to scale out much more. But it's proved it's usefulness.

I tried Changesets proper—seemed like the obvious choice and @ahaywood
even independently came up with the idea—but it's overkill. I don't want
to have to associate a semver with a PR (we use milestones for that). I
also don't want to have to pick which packages we're versioning. Right
now it's always all of them. And without the tooling or without using
git, there's no obvious way to tie a changeset back to the PR that made
it. (Maybe one of the changeset changelog packages do this, but config.)
All I really want is what we're doing right now without the merge
conflicts, so I spiked on a small script to generate a minimal
changeset:

```
yarn changesets 10116
yarn changesets #10116
```

The idea is that we'll do what we've been doing, but just via that
command. It'll write a file to `.changesets/three-random-words.md`, and
we'll put our release notes for the PR there. The release tooling can
aggregate them into release notes at the time of release.

The main con is that we can't see all the unreleased changes in linear
order in one file. But tradeoffs. It's definitely worth not having to
put up with merge conflicts.
  • Loading branch information
jtoar authored and ahaywood committed Mar 8, 2024
1 parent f8b45da commit 7961108
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 87 deletions.
10 changes: 0 additions & 10 deletions .github/actions/check_changelog/action.yml

This file was deleted.

68 changes: 0 additions & 68 deletions .github/actions/check_changelog/check_changelog.mjs

This file was deleted.

10 changes: 10 additions & 0 deletions .github/actions/check_changesets/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Check changesets
description: Checks if changeset was added to the PR

runs:
using: node20
main: check_changesets.mjs

inputs:
labels:
required: true
47 changes: 47 additions & 0 deletions .github/actions/check_changesets/check_changesets.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getInput } from '@actions/core'
import { exec, getExecOutput } from '@actions/exec'
import github from '@actions/github'

async function main() {
// If the PR has the "changesets-ok" label, just pass.
const { labels } = JSON.parse(getInput('labels'))
const hasChangesetsOkLabel = labels.some((label) => label.name === 'changesets-ok')
if (hasChangesetsOkLabel) {
console.log('Skipping check because of the "changesets-ok" label')
return
}

// Check if the PR adds a changeset.
await exec('git fetch origin main', [], { silent: true })
const { stdout } = await getExecOutput('git diff origin/main --name-only', [], { silent: true })
const changedFiles = stdout.toString().trim().split('\n').filter(Boolean)
const addedChangeset = changedFiles.some((file) => file.startsWith('.changesets/'))
if (addedChangeset) {
// Empty space here (and in subsequent `console.log`s) for formatting in the action.
console.log(
[
'',
"Added a changeset",
].join('\n')
)

return
}

const pr = github.context.payload.pull_request
console.log(
[
'',
'📝 Consider adding a changeset',
'==============================',
'',
'If this is a user-facing PR (a feature or a fix), it should probably have a changeset.',
`Run \`yarn changesets ${pr.number}\` to create a changeset for this PR.`,
"If it doesn't need one (it's a chore), you can add the 'changesets-ok' label.",
].join('\n')
)

process.exitCode = 1
}

main()
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "check_changelog",
"name": "check_changesets",
"private": true,
"dependencies": {
"@actions/core": "1.10.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ __metadata:
languageName: node
linkType: hard

"check_changelog@workspace:.":
"check_changesets@workspace:.":
version: 0.0.0-use.local
resolution: "check_changelog@workspace:."
resolution: "check_changesets@workspace:."
dependencies:
"@actions/core": "npm:1.10.1"
"@actions/exec": "npm:1.1.1"
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/check-changelog.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 📝 Check CHANGELOG.md
name: 📝 Check changesets

on:
pull_request:
Expand All @@ -11,8 +11,8 @@ concurrency:
cancel-in-progress: true

jobs:
check-changelog:
name: 📝 Check CHANGELOG.md
check-changesets:
name: 📝 Check changesets
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -25,9 +25,9 @@ jobs:
node-version: 20

- run: yarn install
working-directory: ./.github/actions/check_changelog
working-directory: ./.github/actions/check_changesets

- name: Check CHANGELOG.md
uses: ./.github/actions/check_changelog
- name: Check changesets
uses: ./.github/actions/check_changesets
with:
labels: '{ "labels": ${{ toJSON(github.event.pull_request.labels) }} }'
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"build:pack": "nx run-many -t build:pack",
"build:test-project": "node ./tasks/test-project/test-project",
"build:watch": "lerna run build:watch --parallel; tsc --build",
"changesets": "tsx ./tasks/changesets/changesets.mts",
"check": "node ./tasks/check/check.mjs",
"clean:prisma": "rimraf node_modules/.prisma/client && node node_modules/@prisma/client/scripts/postinstall.js",
"e2e": "node ./tasks/run-e2e",
Expand Down Expand Up @@ -89,6 +90,7 @@
"execa": "5.1.1",
"fast-glob": "3.3.2",
"fs-extra": "11.2.0",
"human-id": "^4.1.1",
"jest": "29.7.0",
"jscodeshift": "0.15.0",
"lerna": "8.0.2",
Expand Down
50 changes: 50 additions & 0 deletions tasks/changesets/changesets.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { chalk, fs } from 'zx'

import {
getChangesetFilePath,
getPlaceholder,
resolveArgv,
shouldShowHelp,
showHelp,
} from './changesetsHelpers.mjs'

async function main() {
if (shouldShowHelp()) {
showHelp()
return
}

const { prNumber } = resolveArgv()

const changesetFilePath = getChangesetFilePath(prNumber)
const placeholder = await getPlaceholder(prNumber)
await fs.outputFile(changesetFilePath, placeholder)
console.log(
[
`📝 Created a changeset at ${chalk.magenta(changesetFilePath)}`,
" Commit it when you're done and push your branch up to GitHub. Thank you! 🙏",
].join('\n')
)
}

try {
await main()
} catch (error) {
console.error(`${error.message}\n`)
showHelp()
process.exitCode = 1
}

// Test suite
//
// - should be the placeholder at ./placeholder.md
// - yarn changesets
//
// - should be the title and body of PR 10075
// - yarn changesets 10075
// - yarn changesets '#10075'
//
// - should throw and show help
// - yarn changesets abcd
// - yarn changesets 10075000
// - yarn changesets https://github.com/redwoodjs/redwood/pull/10075
Loading

0 comments on commit 7961108

Please sign in to comment.