Skip to content

Commit

Permalink
chore: changeset validating github action (#1500)
Browse files Browse the repository at this point in the history
* chore: changeset validating github action

* test: Create metal-birds-share.md

* style: lint

---------

Co-authored-by: Steven Spriggs <[email protected]>
  • Loading branch information
bennypowers and zeroedin authored May 12, 2024
1 parent 434b8c8 commit cb7b0e3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-birds-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rhds/elements": major
---
DELETE THIS FILE BEFORE MERGING
chore: changeset validating github action
25 changes: 25 additions & 0 deletions .github/workflows/validate-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Validate PRs

on:
pull_request:
types:
- opened
- reopened
- synchronize
- edited

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm i semantic-release @changesets/read --prefer-offline
- uses: actions/github-script@v7
with:
script: |
const { validate } = await import('${{ github.workspace }}/scripts/validate-prs.js');
await validate({ context });
67 changes: 67 additions & 0 deletions scripts/validate-prs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { default as read } from '@changesets/read';
import semanticRelease from 'semantic-release';

/** @typedef {'major'|'minor'|'patch'} ReleaseType */

/**
* named capture group 1 `commitType`:
* > Either `feat`, `fix`, `chore`, `docs`, or `style`
* **ANY** (_>= 0x_)
* named capture group 2 `bang`:
* > `!`
*/
const COMMIT_TYPE_RE = /(?<commitType>feat|fix|chore|docs|style).*(?<bang>!)/;

async function getReleaseType(title, mergeType) {
if (mergeType === 'rebase') {
const result = await semanticRelease({
dryRun: true,
branches: ['main'],
}) ?? {};
/** @type {ReleaseType} */
const type = result?.nextRelease?.type;
return type;
} else {
const { commitType, bang } = title.match(COMMIT_TYPE_RE)?.groups ?? {};
if (bang) {
return 'major';
} else {
switch (commitType) {
case 'feat': return 'minor';
case 'fix': return 'patch';
}
}
}
}

export async function validate({ context }) {
const { base, title, auto_merge: autoMerge } = context.payload.pull_request;

if (base.ref.startsWith('staging/')) {
return true;
}

const type = await getReleaseType(title, autoMerge?.merge_method) ?? null;
const sets = await read(process.cwd());

/** @type {ReleaseType} */
const release = sets.reduce((greatest, type) => {
switch (greatest) {
case null:
case 'major':
return greatest;
case 'minor':
return type === 'major' ? type : greatest;
case 'patch':
return (type === 'major' || type === 'minor') ? type : greatest;
}
}, null);

if (!release && type?.match(/m(aj|in)or/)) {
throw new Error(`PR conventional commit title has type (${type}) but no changesets were detected.`);
}

if (type !== release) {
throw new Error(`PR conventional commit title type (${type}) does not match release type (${release}).`);
}
}

0 comments on commit cb7b0e3

Please sign in to comment.