File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change 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 });
Original file line number Diff line number Diff line change 1+ function hasTypes ( markdown ) {
2+ return / # # T y p e o f c h a n g e / . test ( markdown ) && / - \s \[ x \] / i. test ( markdown ) ;
3+ }
4+
5+ function hasDescription ( markdown ) {
6+ return (
7+ / # # D e s c r i p t i o n / . test ( markdown ) &&
8+ ! / # # D e s c r i p t i o n \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+ } ;
You can’t perform that action at this time.
0 commit comments