Skip to content

Commit

Permalink
ci: skip test for invalid PRs (#2013)
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 authored Oct 26, 2024
1 parent 74ed063 commit c7f9675
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: "CI"

on:
push:
branches:
Expand All @@ -11,7 +12,7 @@ on:
- "docs/**"
- "README.md"
- "LICENSE"
pull_request:
workflow_call:

jobs:
build:
Expand Down Expand Up @@ -43,3 +44,7 @@ jobs:

- name: Test Site
run: bash tools/test.sh

check-commit:
needs: build
uses: ./.github/workflows/commitlint.yml
3 changes: 2 additions & 1 deletion .github/workflows/commitlint.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Lint Commit Messages
on: pull_request

on: workflow_call

jobs:
commitlint:
Expand Down
17 changes: 14 additions & 3 deletions .github/workflows/pr-filter.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
name: Block Invalid PR
name: PR Filter

on:
pull_request_target:
types: [opened, reopened, edited]
types: [opened, reopened]

jobs:
check-template:
runs-on: ubuntu-latest
permissions:
pull-requests: write

steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Check PR Content
id: intercept
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
result-encoding: string
script: |
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
31 changes: 14 additions & 17 deletions .github/workflows/scripts/pr-filter.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
function noTypes(markdown) {
if (/## Type of change/.test(markdown) && /- \[x\]/i.test(markdown)) {
return false;
}
return true;
function hasTypes(markdown) {
return /## Type of change/.test(markdown) && /-\s*\[x\]/i.test(markdown);
}

function noDescription(markdown) {
function hasDescription(markdown) {
return (
/## Description/.test(markdown) === false ||
/## Description\s*\n\s*## \w+/.test(markdown) ||
/## Description\s*\n\s*$/.test(markdown)
/## Description/.test(markdown) &&
!/## Description\s*\n\s*(##|\s*$)/.test(markdown)
);
}

module.exports = async ({ github, context }) => {
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 markdown = body.replace(/<!--[\s\S]*?-->/g, '');
const action = context.payload.action;

const isValid =
pr.labels.length > 0 || // PR create by Dependabot would have labels
(markdown !== '' && hasTypes(markdown) && hasDescription(markdown));

if (body === '' || noTypes(markdown) || noDescription(markdown)) {
if (!isValid) {
await github.rest.pulls.update({
...context.repo,
pull_number: pr.number,
Expand All @@ -34,7 +29,9 @@ module.exports = async ({ github, context }) => {
await github.rest.issues.createComment({
...context.repo,
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;
};

0 comments on commit c7f9675

Please sign in to comment.