Skip to content

Commit 3f0c33d

Browse files
committed
chore(ci): move error prone env to code as constants
1 parent c5633be commit 3f0c33d

6 files changed

+64
-30
lines changed

.github/scripts/constants.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
module.exports = Object.freeze({
22
/** @type {string} */
3+
// Values: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
34
"PR_ACTION": process.env.PR_ACTION || "",
5+
46
/** @type {string} */
57
"PR_AUTHOR": process.env.PR_AUTHOR || "",
8+
69
/** @type {string} */
710
"PR_BODY": process.env.PR_BODY || "",
11+
812
/** @type {string} */
913
"PR_TITLE": process.env.PR_TITLE || "",
14+
1015
/** @type {number} */
1116
"PR_NUMBER": process.env.PR_NUMBER || 0,
17+
1218
/** @type {boolean} */
1319
"PR_IS_MERGED": process.env.PR_IS_MERGED || false,
14-
/** @type {string[]} */
15-
"IGNORE_AUTHORS": ["dependabot[bot]", "markdownify[bot]"],
20+
21+
/** @type {string} */
22+
"LABEL_BLOCK": "do-not-merge",
23+
1624
/** @type {string} */
17-
"BLOCK_LABEL": "do-not-merge",
25+
"LABEL_BLOCK_REASON": "need-issue",
26+
1827
/** @type {string} */
19-
"BLOCK_REASON_LABEL": "need-issue",
28+
"LABEL_PENDING_RELEASE": "pending-release",
29+
30+
/** @type {string} */
31+
"HANDLE_MAINTAINERS_TEAM": "@awslabs/aws-lambda-powertools-python",
32+
33+
/** @type {string[]} */
34+
"IGNORE_AUTHORS": ["dependabot[bot]", "markdownify[bot]"],
2035
});

.github/scripts/label_missing_related_issue.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const {
44
PR_BODY,
55
PR_NUMBER,
66
IGNORE_AUTHORS,
7-
BLOCK_LABEL,
8-
BLOCK_REASON_LABEL
7+
LABEL_BLOCK,
8+
LABEL_BLOCK_REASON
99
} = require("./constants")
1010

1111
module.exports = async ({github, context, core}) => {
@@ -39,7 +39,7 @@ module.exports = async ({github, context, core}) => {
3939
issue_number: PR_NUMBER,
4040
owner: context.repo.owner,
4141
repo: context.repo.repo,
42-
labels: [BLOCK_LABEL, BLOCK_REASON_LABEL]
42+
labels: [LABEL_BLOCK, LABEL_BLOCK_REASON]
4343
})
4444
}
4545
}

.github/scripts/label_pr_based_on_title.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
module.exports = async ({github, context, core}) => {
2-
const pr_number = process.env.PR_NUMBER
3-
const pr_title = process.env.PR_TITLE
1+
const { PR_NUMBER, PR_TITLE } = require("./constants")
42

5-
console.log(pr_title)
3+
module.exports = async ({github, context, core}) => {
4+
core.debug(PR_NUMBER);
5+
core.debug(PR_TITLE);
66

77
const FEAT_REGEX = /feat(\((.+)\))?(\:.+)/
88
const BUG_REGEX = /(fix|bug)(\((.+)\))?(\:.+)/
@@ -20,14 +20,15 @@ module.exports = async ({github, context, core}) => {
2020
"deprecated": DEPRECATED_REGEX,
2121
}
2222

23+
// Maintenance: We should keep track of modified PRs in case their titles change
2324
for (const label in labels) {
2425
const matcher = new RegExp(labels[label])
25-
const isMatch = matcher.exec(pr_title)
26+
const isMatch = matcher.exec(PR_TITLE)
2627
if (isMatch != null) {
27-
console.info(`Auto-labeling PR ${pr_number} with ${label}`)
28+
core.info(`Auto-labeling PR ${PR_NUMBER} with ${label}`)
2829

2930
await github.rest.issues.addLabels({
30-
issue_number: pr_number,
31+
issue_number: PR_NUMBER,
3132
owner: context.repo.owner,
3233
repo: context.repo.repo,
3334
labels: [label]
@@ -36,4 +37,6 @@ module.exports = async ({github, context, core}) => {
3637
break
3738
}
3839
}
40+
41+
return core.notice(`PR ${PR_NUMBER} title '${PR_TITLE}' doesn't follow semantic titles; skipping...`)
3942
}
+27-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1+
const {
2+
PR_AUTHOR,
3+
PR_BODY,
4+
PR_NUMBER,
5+
IGNORE_AUTHORS,
6+
LABEL_PENDING_RELEASE,
7+
HANDLE_MAINTAINERS_TEAM,
8+
PR_IS_MERGED,
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 releaseLabel = process.env.RELEASE_LABEL;
5-
const maintainersTeam = process.env.MAINTAINERS_TEAM;
6-
const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/;
12+
core.debug(PR_BODY);
13+
core.debug(PR_IS_MERGED);
14+
15+
if (IGNORE_AUTHORS.includes(PR_AUTHOR)) {
16+
return core.notice("Author in IGNORE_AUTHORS list; skipping...")
17+
}
718

8-
core.info(prBody);
9-
const isMatch = RELATED_ISSUE_REGEX.exec(prBody);
19+
if (!PR_IS_MERGED) {
20+
return core.notice("Only merged PRs to avoid spam; skipping")
21+
}
22+
23+
24+
const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/;
25+
const isMatch = RELATED_ISSUE_REGEX.exec(PR_BODY);
1026
if (!isMatch) {
11-
core.setFailed(`Unable to find related issue for PR number ${prNumber}.\n\n Body details: ${prBody}`);
27+
core.setFailed(`Unable to find related issue for PR number ${PR_NUMBER}.\n\n Body details: ${PR_BODY}`);
1228
return await github.rest.issues.createComment({
1329
owner: context.repo.owner,
1430
repo: context.repo.repo,
15-
body: `${maintainersTeam} No related issues found. Please ensure '${releaseLabel}' label is applied before releasing.`,
16-
issue_number: prNumber,
31+
body: `${HANDLE_MAINTAINERS_TEAM} No related issues found. Please ensure '${LABEL_PENDING_RELEASE}' label is applied before releasing.`,
32+
issue_number: PR_NUMBER,
1733
});
1834
}
1935

@@ -24,6 +40,6 @@ module.exports = async ({github, context, core}) => {
2440
issue_number: relatedIssueNumber,
2541
owner: context.repo.owner,
2642
repo: context.repo.repo,
27-
labels: [releaseLabel]
43+
labels: [LABEL_PENDING_RELEASE]
2844
})
2945
}

.github/workflows/on_merged_pr.yml

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

9-
env:
10-
RELEASE_LABEL: "pending-release"
11-
MAINTAINERS_TEAM: "@awslabs/aws-lambda-powertools-python"
12-
139
jobs:
1410
get_pr_details:
1511
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
@@ -26,12 +22,16 @@ jobs:
2622
&& needs.get_pr_details.outputs.prIsMerged == true
2723
runs-on: ubuntu-latest
2824
steps:
25+
- name: Debug outputs
26+
run: echo "Outputs ${{ toJSON(needs.get_pr_details.outputs) }}"
2927
- uses: actions/checkout@v3
3028
- name: "Label PR related issue for release"
3129
uses: actions/github-script@v6
3230
env:
3331
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
3432
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
33+
PR_IS_MERGED: ${{ needs.get_pr_details.outputs.prIsMerged }}
34+
PR_AUTHOR: ${{ needs.get_pr_details.outputs.prAuthor }}
3535
with:
3636
github-token: ${{ secrets.GITHUB_TOKEN }}
3737
script: |

.github/workflows/on_opened_pr.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: On PR open
1+
name: On new PR
22

33
on:
44
workflow_run:

0 commit comments

Comments
 (0)