Skip to content

Commit c7f9675

Browse files
authored
ci: skip test for invalid PRs (cotes2020#2013)
1 parent 74ed063 commit c7f9675

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

.github/workflows/ci.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
name: "CI"
2+
23
on:
34
push:
45
branches:
@@ -11,7 +12,7 @@ on:
1112
- "docs/**"
1213
- "README.md"
1314
- "LICENSE"
14-
pull_request:
15+
workflow_call:
1516

1617
jobs:
1718
build:
@@ -43,3 +44,7 @@ jobs:
4344

4445
- name: Test Site
4546
run: bash tools/test.sh
47+
48+
check-commit:
49+
needs: build
50+
uses: ./.github/workflows/commitlint.yml

.github/workflows/commitlint.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
name: Lint Commit Messages
2-
on: pull_request
2+
3+
on: workflow_call
34

45
jobs:
56
commitlint:

.github/workflows/pr-filter.yml

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
name: Block Invalid PR
1+
name: PR Filter
22

33
on:
44
pull_request_target:
5-
types: [opened, reopened, edited]
5+
types: [opened, reopened]
66

77
jobs:
88
check-template:
99
runs-on: ubuntu-latest
1010
permissions:
1111
pull-requests: write
12+
1213
steps:
1314
- name: Checkout Code
1415
uses: actions/checkout@v4
1516

1617
- name: Check PR Content
18+
id: intercept
1719
uses: actions/github-script@v7
1820
with:
1921
github-token: ${{ secrets.GITHUB_TOKEN }}
22+
result-encoding: string
2023
script: |
2124
const script = require('.github/workflows/scripts/pr-filter.js');
22-
await script({ github, context });
25+
return await script({ github, context });
26+
27+
- name: Abort due to invalid PR
28+
if: ${{ steps.intercept.outputs.result != 'true' }}
29+
run: exit 1
30+
31+
test:
32+
needs: check-template
33+
uses: ./.github/workflows/ci.yml
+14-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
function noTypes(markdown) {
2-
if (/## Type of change/.test(markdown) && /- \[x\]/i.test(markdown)) {
3-
return false;
4-
}
5-
return true;
1+
function hasTypes(markdown) {
2+
return /## Type of change/.test(markdown) && /-\s*\[x\]/i.test(markdown);
63
}
74

8-
function noDescription(markdown) {
5+
function hasDescription(markdown) {
96
return (
10-
/## Description/.test(markdown) === false ||
11-
/## Description\s*\n\s*## \w+/.test(markdown) ||
12-
/## Description\s*\n\s*$/.test(markdown)
7+
/## Description/.test(markdown) &&
8+
!/## Description\s*\n\s*(##|\s*$)/.test(markdown)
139
);
1410
}
1511

1612
module.exports = async ({ github, context }) => {
1713
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-
2414
const body = pr.body === null ? '' : pr.body.trim();
2515
const markdown = body.replace(/<!--[\s\S]*?-->/g, '');
16+
const action = context.payload.action;
17+
18+
const isValid =
19+
pr.labels.length > 0 || // PR create by Dependabot would have labels
20+
(markdown !== '' && hasTypes(markdown) && hasDescription(markdown));
2621

27-
if (body === '' || noTypes(markdown) || noDescription(markdown)) {
22+
if (!isValid) {
2823
await github.rest.pulls.update({
2924
...context.repo,
3025
pull_number: pr.number,
@@ -34,7 +29,9 @@ module.exports = async ({ github, context }) => {
3429
await github.rest.issues.createComment({
3530
...context.repo,
3631
issue_number: pr.number,
37-
body: "Oops, it seems you've submitted an invalid pull request. No worries, we'll close it for you."
32+
body: `Oops, it seems you've ${action} an invalid pull request. No worries, we'll close it for you.`
3833
});
3934
}
35+
36+
return isValid;
4037
};

0 commit comments

Comments
 (0)