Skip to content

Commit 7a1382c

Browse files
committed
chore(ci): refactor inline gh-script to file
1 parent 74356dc commit 7a1382c

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = async ({github, context, core}) => {
2+
const prBody = process.env.PR_BODY;
3+
const prNumber = process.env.PR_NUMBER;
4+
const blockLabel = process.env.BLOCK_LABEL;
5+
const blockReasonLabel = process.env.BLOCK_REASON_LABEL;
6+
7+
const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/;
8+
9+
const isMatch = RELATED_ISSUE_REGEX.exec(prBody);
10+
if (isMatch == null) {
11+
core.info(`No related issue found, maybe the author didn't use the template but there is one.`)
12+
13+
let msg = "No related issues found. Please ensure there is an open issue related to this change to avoid significant delays or closure.";
14+
await github.rest.issues.createComment({
15+
owner: context.repo.owner,
16+
repo: context.repo.repo,
17+
body: msg,
18+
issue_number: prNumber,
19+
});
20+
21+
return await github.rest.issues.addLabels({
22+
issue_number: prNumber,
23+
owner: context.repo.owner,
24+
repo: context.repo.repo,
25+
labels: [blockLabel, blockReasonLabel]
26+
})
27+
}
28+
}

.github/workflows/on_opened_pr.yml

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: On PR opened
1+
name: On PR open
22

33
on:
44
workflow_run:
@@ -9,8 +9,8 @@ on:
99
env:
1010
BLOCK_LABEL: "do-not-merge"
1111
BLOCK_REASON_LABEL: "need-issue"
12+
IGNORE_AUTHORS: '["dependabot[bot]", "markdownify[bot]"]'
1213

13-
# TODO: include markdownify author too
1414
jobs:
1515
get_pr_details:
1616
if: ${{ github.event.workflow_run.conclusion == 'success' }}
@@ -22,41 +22,19 @@ jobs:
2222
check_related_issue:
2323
needs: get_pr_details
2424
if: >
25-
${{ needs.get_pr_details.outputs.prAuthor != 'dependabot[bot]' &&
26-
needs.get_pr_details.outputs.prAction == 'opened'
25+
${{ needs.get_pr_details.outputs.prAction == 'opened' &&
26+
contains(fromJson(env.IGNORE_AUTHORS), needs.get_pr_details.outputs.prAuthor) != true
2727
}}
2828
runs-on: ubuntu-latest
2929
steps:
30+
- uses: actions/checkout@v3
3031
- name: "Ensure related issue is present"
3132
uses: actions/github-script@v6
33+
env:
34+
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
35+
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
3236
with:
3337
github-token: ${{ secrets.GITHUB_TOKEN }}
34-
# Maintenance: convert into a standalone JS like post_release.js
3538
script: |
36-
const prBody = "${{ needs.get_pr_details.outputs.prBody }}";
37-
const prNumber = ${{ needs.get_pr_details.outputs.prNumber }};
38-
const blockLabel = process.env.BLOCK_LABEL;
39-
const blockReasonLabel = process.env.BLOCK_REASON_LABEL;
40-
41-
const RELATED_ISSUE_REGEX = /Issue number:.+(\d)/
42-
43-
const matcher = new RegExp(RELATED_ISSUE_REGEX)
44-
const isMatch = matcher.exec(prBody)
45-
if (isMatch == null) {
46-
console.info(`No related issue found, maybe the author didn't use the template but there is one.`)
47-
48-
let msg = "No related issues found. Please ensure there is an open issue related to this change to avoid significant delays or closure.";
49-
await github.rest.issues.createComment({
50-
owner: context.repo.owner,
51-
repo: context.repo.repo,
52-
body: msg,
53-
issue_number: prNumber,
54-
});
55-
56-
await github.rest.issues.addLabels({
57-
issue_number: prNumber,
58-
owner: context.repo.owner,
59-
repo: context.repo.repo,
60-
labels: [blockLabel, blockReasonLabel]
61-
})
62-
}
39+
const script = require('.github/scripts/label_missing_related_issue.js')
40+
await script({github, context, core})

0 commit comments

Comments
 (0)