Skip to content

Commit

Permalink
chore: changeset validating github action
Browse files Browse the repository at this point in the history
  • Loading branch information
bennypowers committed Mar 14, 2024
1 parent c25b726 commit bdaa734
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/validate-pr-base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Validate PRs

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

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/github-script@v7
with:
script: |
const { validate } = await import('${{ github.workspace }}/scripts/validate-prs.js');
await validate({ context });
43 changes: 43 additions & 0 deletions scripts/validate-prs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-env node */
import read from '@changesets/read';
import conventionalCommitsParser from 'conventional-commits-parser';

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

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

const { type } = conventionalCommitsParser.sync(title);

console.log({ type });

if (autoMerge?.merge_method === 'rebase') {
throw new Error('Validating auto-rebase PRs is not yet implemented');
// one approach could be to use `@commitlint/load` to read { from: baseBranch }
// then pipe that array as a stream to conventionalcommitsparser's transform stream
}

const sets = await read(process.cwd());

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) {
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 bdaa734

Please sign in to comment.