Skip to content

Commit 85bc25b

Browse files
committed
chore(ci): add on_merge_pr workflow to notify releases
Signed-off-by: heitorlessa <[email protected]>
1 parent 6ec1486 commit 85bc25b

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

.github/scripts/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = Object.freeze({
2525
"LABEL_BLOCK_REASON": "need-issue",
2626

2727
// /** @type {string} */
28-
// "LABEL_PENDING_RELEASE": "pending-release",
28+
"LABEL_PENDING_RELEASE": "pending-release",
2929

3030
/** @type {string} */
3131
"HANDLE_MAINTAINERS_TEAM": "@awslabs/aws-lambda-powertools-net",
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
11+
module.exports = async ({github, context, core}) => {
12+
if (IGNORE_AUTHORS.includes(PR_AUTHOR)) {
13+
return core.notice("Author in IGNORE_AUTHORS list; skipping...")
14+
}
15+
16+
if (PR_IS_MERGED == "false") {
17+
return core.notice("Only merged PRs to avoid spam; skipping")
18+
}
19+
20+
const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/;
21+
22+
const isMatch = RELATED_ISSUE_REGEX.exec(PR_BODY);
23+
24+
try {
25+
if (!isMatch) {
26+
core.setFailed(`Unable to find related issue for PR number ${PR_NUMBER}.\n\n Body details: ${PR_BODY}`);
27+
return await github.rest.issues.createComment({
28+
owner: context.repo.owner,
29+
repo: context.repo.repo,
30+
body: `${HANDLE_MAINTAINERS_TEAM} No related issues found. Please ensure '${LABEL_PENDING_RELEASE}' label is applied before releasing.`,
31+
issue_number: PR_NUMBER,
32+
});
33+
}
34+
} catch (error) {
35+
core.setFailed(`Unable to create comment on PR number ${PR_NUMBER}.\n\n Error details: ${error}`);
36+
throw new Error(error);
37+
}
38+
39+
const { groups: {issue} } = isMatch
40+
41+
try {
42+
core.info(`Auto-labeling related issue ${issue} for release`)
43+
await github.rest.issues.addLabels({
44+
issue_number: issue,
45+
owner: context.repo.owner,
46+
repo: context.repo.repo,
47+
labels: [LABEL_PENDING_RELEASE]
48+
})
49+
} catch (error) {
50+
core.setFailed(`Is this issue number (${issue}) valid? Perhaps a discussion?`);
51+
throw new Error(error);
52+
}
53+
}

.github/workflows/on_merged_pr.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: On PR merge
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Record PR details"]
6+
types:
7+
- completed
8+
9+
jobs:
10+
get_pr_details:
11+
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
12+
uses: ./.github/workflows/reusable_export_pr_details.yml
13+
with:
14+
record_pr_workflow_id: ${{ github.event.workflow_run.id }}
15+
secrets:
16+
token: ${{ secrets.GITHUB_TOKEN }}
17+
release_label_on_merge:
18+
needs: get_pr_details
19+
runs-on: ubuntu-latest
20+
if: needs.get_pr_details.outputs.prIsMerged == 'true'
21+
steps:
22+
- uses: actions/checkout@v3
23+
- name: "Label PR related issue for release"
24+
uses: actions/github-script@v6
25+
env:
26+
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
27+
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
28+
PR_IS_MERGED: ${{ needs.get_pr_details.outputs.prIsMerged }}
29+
PR_AUTHOR: ${{ needs.get_pr_details.outputs.prAuthor }}
30+
with:
31+
github-token: ${{ secrets.GITHUB_TOKEN }}
32+
script: |
33+
const script = require('.github/scripts/label_related_issue.js')
34+
await script({github, context, core})

0 commit comments

Comments
 (0)