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: add breaking change pr check ci #9050

Merged
merged 3 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (/^[a-z]+(\([a-z]+\))?!: /.test(title)) {
if (/^[a-z]+(\([a-z-]+\))?!: /.test(title)) {

Without the dash, this regexp won't catch feat(eslint-plugin)!: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I fixed it cde7ec4

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);
}
Comment on lines +58 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd personally rather something like const error = checkTitle(...) || checkDescription(...). But this is fine for an internal script. If it does end up getting changed much later, we can always refactor. 🙂

}

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
auvred marked this conversation as resolved.
Show resolved Hide resolved

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 }}