Skip to content

Commit 74ed063

Browse files
authored
ci: block invalid pull requests (cotes2020#2010)
1 parent d4f7f39 commit 74ed063

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

.github/workflows/pr-filter.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Block Invalid PR
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, reopened, edited]
6+
7+
jobs:
8+
check-template:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
pull-requests: write
12+
steps:
13+
- name: Checkout Code
14+
uses: actions/checkout@v4
15+
16+
- name: Check PR Content
17+
uses: actions/github-script@v7
18+
with:
19+
github-token: ${{ secrets.GITHUB_TOKEN }}
20+
script: |
21+
const script = require('.github/workflows/scripts/pr-filter.js');
22+
await script({ github, context });
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function noTypes(markdown) {
2+
if (/## Type of change/.test(markdown) && /- \[x\]/i.test(markdown)) {
3+
return false;
4+
}
5+
return true;
6+
}
7+
8+
function noDescription(markdown) {
9+
return (
10+
/## Description/.test(markdown) === false ||
11+
/## Description\s*\n\s*## \w+/.test(markdown) ||
12+
/## Description\s*\n\s*$/.test(markdown)
13+
);
14+
}
15+
16+
module.exports = async ({ github, context }) => {
17+
const pr = context.payload.pull_request;
18+
19+
if (pr.labels.length > 0) {
20+
// Skip if the PR is already labeled (typically created by a deps-bot.)
21+
return;
22+
}
23+
24+
const body = pr.body === null ? '' : pr.body.trim();
25+
const markdown = body.replace(/<!--[\s\S]*?-->/g, '');
26+
27+
if (body === '' || noTypes(markdown) || noDescription(markdown)) {
28+
await github.rest.pulls.update({
29+
...context.repo,
30+
pull_number: pr.number,
31+
state: 'closed'
32+
});
33+
34+
await github.rest.issues.createComment({
35+
...context.repo,
36+
issue_number: pr.number,
37+
body: "Oops, it seems you've submitted an invalid pull request. No worries, we'll close it for you."
38+
});
39+
}
40+
};

0 commit comments

Comments
 (0)