Skip to content

Commit c5633be

Browse files
committed
fix(ci): move conditionals from yaml to code
1 parent 039ebde commit c5633be

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

.github/scripts/constants.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = Object.freeze({
2+
/** @type {string} */
3+
"PR_ACTION": process.env.PR_ACTION || "",
4+
/** @type {string} */
5+
"PR_AUTHOR": process.env.PR_AUTHOR || "",
6+
/** @type {string} */
7+
"PR_BODY": process.env.PR_BODY || "",
8+
/** @type {string} */
9+
"PR_TITLE": process.env.PR_TITLE || "",
10+
/** @type {number} */
11+
"PR_NUMBER": process.env.PR_NUMBER || 0,
12+
/** @type {boolean} */
13+
"PR_IS_MERGED": process.env.PR_IS_MERGED || false,
14+
/** @type {string[]} */
15+
"IGNORE_AUTHORS": ["dependabot[bot]", "markdownify[bot]"],
16+
/** @type {string} */
17+
"BLOCK_LABEL": "do-not-merge",
18+
/** @type {string} */
19+
"BLOCK_REASON_LABEL": "need-issue",
20+
});
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1+
const {
2+
PR_ACTION,
3+
PR_AUTHOR,
4+
PR_BODY,
5+
PR_NUMBER,
6+
IGNORE_AUTHORS,
7+
BLOCK_LABEL,
8+
BLOCK_REASON_LABEL
9+
} = require("./constants")
10+
111
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;
12+
core.debug(`Number: ${PR_BODY}`);
13+
core.debug(`Action: ${PR_ACTION}`);
14+
core.debug(`Author: ${PR_AUTHOR}`);
15+
core.debug(`Body: ${PR_BODY}`);
616

7-
const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/;
17+
if (IGNORE_AUTHORS.includes(PR_AUTHOR)) {
18+
return core.notice("Author in IGNORE_AUTHORS list; skipping...")
19+
}
820

9-
const isMatch = RELATED_ISSUE_REGEX.exec(prBody);
21+
if (PR_ACTION != "opened") {
22+
return core.notice("Only newly open PRs are labelled to avoid spam; skipping")
23+
}
24+
25+
const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/;
26+
const isMatch = RELATED_ISSUE_REGEX.exec(PR_BODY);
1027
if (isMatch == null) {
1128
core.info(`No related issue found, maybe the author didn't use the template but there is one.`)
1229

@@ -15,14 +32,14 @@ module.exports = async ({github, context, core}) => {
1532
owner: context.repo.owner,
1633
repo: context.repo.repo,
1734
body: msg,
18-
issue_number: prNumber,
35+
issue_number: PR_NUMBER,
1936
});
2037

2138
return await github.rest.issues.addLabels({
22-
issue_number: prNumber,
39+
issue_number: PR_NUMBER,
2340
owner: context.repo.owner,
2441
repo: context.repo.repo,
25-
labels: [blockLabel, blockReasonLabel]
42+
labels: [BLOCK_LABEL, BLOCK_REASON_LABEL]
2643
})
2744
}
2845
}

.github/workflows/on_opened_pr.yml

+6-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ on:
66
types:
77
- completed
88

9-
env:
10-
BLOCK_LABEL: "do-not-merge"
11-
BLOCK_REASON_LABEL: "need-issue"
12-
IGNORE_AUTHORS: '["dependabot[bot]", "markdownify[bot]"]'
139

1410
jobs:
1511
get_pr_details:
@@ -21,18 +17,20 @@ jobs:
2117
token: ${{ secrets.GITHUB_TOKEN }}
2218
check_related_issue:
2319
needs: get_pr_details
24-
# Maintenance: Refactor condition to the correct env syntax later
25-
if: |
26-
needs.get_pr_details.outputs.prAction == 'opened'
27-
&& contains(fromJson('["dependabot[bot]", "markdownify[bot]"]'), needs.get_pr_details.outputs.prAuthor) != true
2820
runs-on: ubuntu-latest
2921
steps:
22+
- name: Debug outputs
23+
run: echo "Outputs ${{ toJSON(needs.get_pr_details.outputs) }}"
3024
- uses: actions/checkout@v3
3125
- name: "Ensure related issue is present"
26+
if: contains('["opened", "edited"]', needs.get_pr_details.outputs.prAction)
27+
if: needs.get_pr_details.outputs.prAction == 'opened' || needs.get_pr_details.outputs.prAction == 'edited'
3228
uses: actions/github-script@v6
3329
env:
3430
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
3531
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
32+
PR_ACTION: ${{ needs.get_pr_details.outputs.prAction }}
33+
PR_AUTHOR: ${{ needs.get_pr_details.outputs.prAuthor }}
3634
with:
3735
github-token: ${{ secrets.GITHUB_TOKEN }}
3836
script: |

0 commit comments

Comments
 (0)