Skip to content

Commit

Permalink
chore: add breaking change pr check ci (#9050)
Browse files Browse the repository at this point in the history
* chore: add breaking change pr check ci

* apply review

* apply review
  • Loading branch information
yeonjuan committed May 26, 2024
1 parent 8470ba5 commit 212d01c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/actions/breaking-pr-check/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Validate Breaking Change PR
description: Validate breaking change PR title and description

runs:
using: node20
main: index.js
66 changes: 66 additions & 0 deletions .github/actions/breaking-pr-check/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const core = require('@actions/core');
const github = require('@actions/github');

function raiseError(message) {
throw new Error(message);
}

async function getPullRequest() {
const client = github.getOctokit(process.env.GITHUB_TOKEN);

const pr = github.context.payload.pull_request;
if (!pr) {
throw new Error(
"This action can only be invoked in `pull_request_target` or `pull_request` events. Otherwise the pull request can't be inferred.",
);
}

const owner = pr.base.user.login;
const repo = pr.base.repo.name;

const { data } = await client.rest.pulls.get({
owner,
repo,
pull_number: pr.number,
});

return data;
}

function checkTitle(title) {
if (/^[a-z]+(\([a-z-]+\))?!: /.test(title)) {
raiseError(
`Do not use exclamation mark ('!') to indicate breaking change in the PR Title.`,
);
}
}

function checkDescription(body, labels) {
if (!labels.some(label => label.name === 'breaking change')) {
return;
}
const [firstLine, secondLine] = body.split(/\r?\n/);

if (!firstLine || !/^BREAKING CHANGE:/.test(firstLine)) {
raiseError(
`Breaking change PR body should start with "BREAKING CHANGE:". See https://typescript-eslint.io/maintenance/releases#2-merging-breaking-changes.`,
);
}
if (!secondLine) {
raiseError(
`The description of breaking change is missing. See https://typescript-eslint.io/maintenance/releases#2-merging-breaking-changes.`,
);
}
}

async function run() {
const pullRequest = await getPullRequest();
try {
checkTitle(pullRequest.title);
checkDescription(pullRequest.body, pullRequest.labels);
} catch (e) {
core.setFailed(e.message);
}
}

run();
21 changes: 21 additions & 0 deletions .github/workflows/semantic-breaking-change-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Semantic Breaking Change PR

on:
pull_request_target:
types:
- opened
- edited
- synchronize
- labeled
- unlabeled

jobs:
main:
name: Validate Breaking Change PR
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare-install
- uses: ./.github/actions/breaking-pr-check
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit 212d01c

Please sign in to comment.