Skip to content

Commit

Permalink
ci: block invalid pull requests (cotes2020#2010)
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 authored Oct 25, 2024
1 parent d4f7f39 commit 74ed063
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/pr-filter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Block Invalid PR

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

jobs:
check-template:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Check PR Content
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const script = require('.github/workflows/scripts/pr-filter.js');
await script({ github, context });
40 changes: 40 additions & 0 deletions .github/workflows/scripts/pr-filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function noTypes(markdown) {
if (/## Type of change/.test(markdown) && /- \[x\]/i.test(markdown)) {
return false;
}
return true;
}

function noDescription(markdown) {
return (
/## Description/.test(markdown) === false ||
/## Description\s*\n\s*## \w+/.test(markdown) ||
/## Description\s*\n\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, '');

if (body === '' || noTypes(markdown) || noDescription(markdown)) {
await github.rest.pulls.update({
...context.repo,
pull_number: pr.number,
state: 'closed'
});

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."
});
}
};

0 comments on commit 74ed063

Please sign in to comment.