Skip to content

Commit 24216e1

Browse files
committed
Merge remote-tracking branch 'upstream/master'
# Conflicts: # .github/workflows/pr-filter.yml # .github/workflows/scripts/pr-filter.js
2 parents 81b6b71 + eaf5e57 commit 24216e1

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

.github/workflows/pr-filter.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: PR Filter
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, reopened]
6+
7+
jobs:
8+
check-template:
9+
if: github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
10+
runs-on: ubuntu-latest
11+
permissions:
12+
pull-requests: write
13+
14+
steps:
15+
- name: Checkout Code
16+
uses: actions/checkout@v4
17+
18+
- name: Check PR Content
19+
id: intercept
20+
uses: actions/github-script@v7
21+
with:
22+
github-token: ${{ secrets.GITHUB_TOKEN }}
23+
script: |
24+
const { default: filter } = await import('${{ github.workspace }}/.github/workflows/scripts/pr-filter.js');
25+
await filter({ github, context, core });
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function hasTypes(markdown) {
2+
return /## Type of change/.test(markdown) && /-\s\[x\]/i.test(markdown);
3+
}
4+
5+
function hasDescription(markdown) {
6+
return (
7+
/## Description/.test(markdown) &&
8+
!/## Description\s*\n\s*(##|\s*$)/.test(markdown)
9+
);
10+
}
11+
12+
export default async ({ github, context, core }) => {
13+
const pr = context.payload.pull_request;
14+
const body = pr.body === null ? '' : pr.body;
15+
const markdown = body.replace(/<!--[\s\S]*?-->/g, '');
16+
const action = context.payload.action;
17+
18+
const isValid =
19+
markdown !== '' && hasTypes(markdown) && hasDescription(markdown);
20+
21+
if (!isValid) {
22+
await github.rest.pulls.update({
23+
...context.repo,
24+
pull_number: pr.number,
25+
state: 'closed'
26+
});
27+
28+
await github.rest.issues.createComment({
29+
...context.repo,
30+
issue_number: pr.number,
31+
body: `Oops, it seems you've ${action} an invalid pull request. No worries, we'll close it for you.`
32+
});
33+
34+
core.setFailed('PR content does not meet template requirements.');
35+
}
36+
};

0 commit comments

Comments
 (0)