Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: changeset validating github action #1500

Merged
merged 6 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });
59 changes: 59 additions & 0 deletions scripts/validate-prs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-env node */
import { default as read } from '@changesets/read';
import semanticRelease from 'semantic-release';

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

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(/(?<commitType>feat|fix|chore|docs|style).*(?<bang>!)/)?.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}).`);
}
}
Loading