Skip to content

Commit 569a486

Browse files
committed
ci: block invalid PRs
1 parent d4f7f39 commit 569a486

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

.github/workflows/pr-filter.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Block Invalid PRs
2+
3+
on:
4+
pull_request:
5+
types: [opened, edited]
6+
7+
permissions:
8+
pull-requests: write
9+
10+
jobs:
11+
check-template:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout Code
15+
uses: actions/checkout@v4
16+
17+
- name: Check PR Content
18+
uses: actions/github-script@v7
19+
with:
20+
github-token: ${{ secrets.GITHUB_TOKEN }}
21+
script: |
22+
const script = require('.github/workflows/scripts/pr-filter.js');
23+
await script({ github, context });
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function noTypes(body) {
2+
return (
3+
body.includes('[ ] Bug fix') &&
4+
body.includes('[ ] New feature') &&
5+
body.includes('[ ] Improvement') &&
6+
body.includes('[ ] Breaking change') &&
7+
body.includes('[ ] Documentation update')
8+
);
9+
}
10+
11+
function noDescription(rawMarkdown) {
12+
const commentRegex = /<!--[\s\S]*?-->/g;
13+
const markdown = rawMarkdown.replace(commentRegex, '');
14+
15+
const descriptionRegex = /## Description\s*([\s\S]*?)(##|$)/;
16+
const match = markdown.match(descriptionRegex);
17+
18+
if (match) {
19+
const descriptionContent = match[1].trim();
20+
return descriptionContent.length === 0;
21+
}
22+
return false;
23+
}
24+
25+
module.exports = async ({ github, context }) => {
26+
const pr = context.payload.pull_request;
27+
const body = pr.body === null ? '' : pr.body.trim();
28+
29+
if (body === '' || noTypes(body) || noDescription(body)) {
30+
await github.rest.pulls.update({
31+
...context.repo,
32+
pull_number: pr.number,
33+
state: 'closed'
34+
});
35+
36+
await github.rest.issues.createComment({
37+
...context.repo,
38+
issue_number: pr.number,
39+
body: "Oops, it seems you've submitted an invalid Pull Request. No worries, we'll close it for you."
40+
});
41+
}
42+
};

0 commit comments

Comments
 (0)