ci: skip test for invalid PRs (#2013)

This commit is contained in:
Cotes Chung 2024-10-26 16:58:07 +08:00 committed by GitHub
parent 74ed06321c
commit c7f967529c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 22 deletions

View file

@ -1,4 +1,5 @@
name: "CI" name: "CI"
on: on:
push: push:
branches: branches:
@ -11,7 +12,7 @@ on:
- "docs/**" - "docs/**"
- "README.md" - "README.md"
- "LICENSE" - "LICENSE"
pull_request: workflow_call:
jobs: jobs:
build: build:
@ -43,3 +44,7 @@ jobs:
- name: Test Site - name: Test Site
run: bash tools/test.sh run: bash tools/test.sh
check-commit:
needs: build
uses: ./.github/workflows/commitlint.yml

View file

@ -1,5 +1,6 @@
name: Lint Commit Messages name: Lint Commit Messages
on: pull_request
on: workflow_call
jobs: jobs:
commitlint: commitlint:

View file

@ -1,22 +1,33 @@
name: Block Invalid PR name: PR Filter
on: on:
pull_request_target: pull_request_target:
types: [opened, reopened, edited] types: [opened, reopened]
jobs: jobs:
check-template: check-template:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions: permissions:
pull-requests: write pull-requests: write
steps: steps:
- name: Checkout Code - name: Checkout Code
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Check PR Content - name: Check PR Content
id: intercept
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: | script: |
const script = require('.github/workflows/scripts/pr-filter.js'); const script = require('.github/workflows/scripts/pr-filter.js');
await script({ github, context }); return await script({ github, context });
- name: Abort due to invalid PR
if: ${{ steps.intercept.outputs.result != 'true' }}
run: exit 1
test:
needs: check-template
uses: ./.github/workflows/ci.yml

View file

@ -1,30 +1,25 @@
function noTypes(markdown) { function hasTypes(markdown) {
if (/## Type of change/.test(markdown) && /- \[x\]/i.test(markdown)) { return /## Type of change/.test(markdown) && /-\s*\[x\]/i.test(markdown);
return false;
}
return true;
} }
function noDescription(markdown) { function hasDescription(markdown) {
return ( return (
/## Description/.test(markdown) === false || /## Description/.test(markdown) &&
/## Description\s*\n\s*## \w+/.test(markdown) || !/## Description\s*\n\s*(##|\s*$)/.test(markdown)
/## Description\s*\n\s*$/.test(markdown)
); );
} }
module.exports = async ({ github, context }) => { module.exports = async ({ github, context }) => {
const pr = context.payload.pull_request; const pr = context.payload.pull_request;
if (pr.labels.length > 0) {
// Skip if the PR is already labeled (typically created by a deps-bot.)
return;
}
const body = pr.body === null ? '' : pr.body.trim(); const body = pr.body === null ? '' : pr.body.trim();
const markdown = body.replace(/<!--[\s\S]*?-->/g, ''); const markdown = body.replace(/<!--[\s\S]*?-->/g, '');
const action = context.payload.action;
if (body === '' || noTypes(markdown) || noDescription(markdown)) { const isValid =
pr.labels.length > 0 || // PR create by Dependabot would have labels
(markdown !== '' && hasTypes(markdown) && hasDescription(markdown));
if (!isValid) {
await github.rest.pulls.update({ await github.rest.pulls.update({
...context.repo, ...context.repo,
pull_number: pr.number, pull_number: pr.number,
@ -34,7 +29,9 @@ module.exports = async ({ github, context }) => {
await github.rest.issues.createComment({ await github.rest.issues.createComment({
...context.repo, ...context.repo,
issue_number: pr.number, issue_number: pr.number,
body: "Oops, it seems you've submitted an invalid pull request. No worries, we'll close it for you." body: `Oops, it seems you've ${action} an invalid pull request. No worries, we'll close it for you.`
}); });
} }
return isValid;
}; };