tldraw/scripts/update-pr-template.ts
David Sheldrick b9547c2e6b
[DX] PR labels revamp (#3112)
This PR switches up how PR labels are validated to allow for more
freeform label tweaking in the future. Basically **huppy will now only
check that your PR is labelled, it doesn't care how it's labelled**. I
also updated the PR template with a new labelling scheme that we can
tweak over time.

So before Huppy bot had to know about the specific set of allowed
labels, and now as long as the label exists you're allowed to add it.

So to add a new label to the PR template, just create the label and then
add an option for it in the .md file.

### Change Type

- [ ] `patch` — Bug fix
- [ ] `minor` — New feature
- [ ] `major` — Breaking change
- [ ] `dependencies` — Changes to package dependencies[^1]
- [ ] `documentation` — Changes to the documentation only[^2]
- [ ] `tests` — Changes to any test code only[^2]
- [x] `internal` — Any other changes that don't affect the published
package[^2]
- [ ] I don't know
2024-03-12 14:53:57 +00:00

70 lines
2 KiB
TypeScript

import { Octokit } from '@octokit/rest'
import { existsSync, readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import { REPO_ROOT } from './lib/file'
import { formatLabelOptionsForPRTemplate, getLabelNames } from './lib/labels'
const prTemplatePath = join(REPO_ROOT, '.github', 'pull_request_template.md')
const octo = new Octokit({})
async function updatePRTemplate(check: boolean) {
if (!existsSync(prTemplatePath)) {
console.error('❌ Could not find PR template at', prTemplatePath)
process.exit(1)
}
const prTemplate = readFileSync(prTemplatePath).toString()
const labelsPart = prTemplate.match(/(### Change Type(.|\s)*?\n)###/)?.[1]
if (!labelsPart) {
console.error(
'❌ Could not find the labels section of the pull request template! It should start with "### Change Type"'
)
process.exit(1)
}
const updated = prTemplate.replace(
labelsPart,
`### Change Type\n\n${formatLabelOptionsForPRTemplate()}\n\n`
)
if (check && updated !== prTemplate) {
console.error(
'❌ PR template labels section is out of date. Run `yarn update-pr-template` to fix it.'
)
console.error(
'💡 Were you trying to change the labels section manually? Update scripts/lib/labels.ts instead.'
)
process.exit(1)
}
// make sure all labels exist
const repoLabels = new Set(
(
await octo.issues.listLabelsForRepo({
owner: 'tldraw',
repo: 'tldraw',
per_page: 100,
})
).data.map((x) => x.name)
)
const missingLabels = getLabelNames().filter((x) => !repoLabels.has(x))
if (missingLabels.length > 0) {
console.error(
'❌ The following labels do not exist in the tldraw repo:',
missingLabels.map((l) => JSON.stringify(l)).join(', ')
)
console.error(
`Add them yourself or update scripts/lib/labels.ts and re-run \`yarn update-pr-template\` to remove them.`
)
process.exit(1)
}
if (!check) {
console.log('Writing template to', prTemplatePath)
writeFileSync(prTemplatePath, updated)
} else {
console.log('All good!')
}
}
updatePRTemplate(process.argv.includes('--check'))