Skip to content

Commit d768f44

Browse files
committed
Merge branch 'develop' into chore/maintainers-update
* develop: chore(ci): make export PR reusable
2 parents 8429c87 + 06965bb commit d768f44

9 files changed

+133
-104
lines changed
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module.exports = async ({github, context, core}) => {
22
const fs = require('fs');
33

4-
const workflowRunId = core.getInput('record_pr_workflow_id');
4+
const workflowRunId = process.env.WORKFLOW_ID;
5+
core.info(`Listing artifacts for workflow run ${workflowRunId}`);
6+
57
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
68
owner: context.repo.owner,
79
repo: context.repo.repo,
@@ -10,12 +12,15 @@ module.exports = async ({github, context, core}) => {
1012

1113
const matchArtifact = artifacts.data.artifacts.filter(artifact => artifact.name == "pr")[0];
1214

15+
core.info(`Downloading artifacts for workflow run ${workflowRunId}`);
1316
const artifact = await github.rest.actions.downloadArtifact({
1417
owner: context.repo.owner,
1518
repo: context.repo.repo,
1619
artifact_id: matchArtifact.id,
1720
archive_format: 'zip',
1821
});
1922

20-
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(artifact.data));
23+
core.info("Saving artifact found", artifact);
24+
25+
fs.writeFileSync('pr.zip', Buffer.from(artifact.data));
2126
}
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module.exports = async ({github, context, core}) => {
2+
const pr_number = process.env.PR_NUMBER
3+
const pr_title = process.env.PR_TITLE
4+
5+
console.log(pr_title)
6+
7+
const FEAT_REGEX = /feat(\((.+)\))?(\:.+)/
8+
const BUG_REGEX = /(fix|bug)(\((.+)\))?(\:.+)/
9+
const DOCS_REGEX = /(docs|doc)(\((.+)\))?(\:.+)/
10+
const CHORE_REGEX = /(chore)(\((.+)\))?(\:.+)/
11+
const DEPRECATED_REGEX = /(deprecated)(\((.+)\))?(\:.+)/
12+
const REFACTOR_REGEX = /(refactor)(\((.+)\))?(\:.+)/
13+
14+
const labels = {
15+
"feature": FEAT_REGEX,
16+
"bug": BUG_REGEX,
17+
"documentation": DOCS_REGEX,
18+
"internal": CHORE_REGEX,
19+
"enhancement": REFACTOR_REGEX,
20+
"deprecated": DEPRECATED_REGEX,
21+
}
22+
23+
for (const label in labels) {
24+
const matcher = new RegExp(labels[label])
25+
const isMatch = matcher.exec(pr_title)
26+
if (isMatch != null) {
27+
console.info(`Auto-labeling PR ${pr_number} with ${label}`)
28+
29+
await github.rest.issues.addLabels({
30+
issue_number: pr_number,
31+
owner: context.repo.owner,
32+
repo: context.repo.repo,
33+
labels: [label]
34+
})
35+
36+
break
37+
}
38+
}
39+
}

.github/scripts/label_related_issue.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
module.exports = async ({github, context, core}) => {
2-
core.info(process.env);
3-
const fs = require('fs');
4-
5-
const pr = JSON.parse(fs.readFileSync('./pr.txt', 'utf-8').trim());
6-
const prBody = pr.body;
7-
const prNumber = pr.number;
2+
const prBody = process.env.PR_BODY;
3+
const prNumber = process.env.PR_NUMBER;
84
const releaseLabel = process.env.RELEASE_LABEL;
9-
const maintainersTeam = process.env.MAINTAINERS_TEAM
10-
11-
return "Temporarily..."
12-
5+
const maintainersTeam = process.env.MAINTAINERS_TEAM;
136
const RELATED_ISSUE_REGEX = /Issue number:[^\d\r\n]+(?<issue>\d+)/;
147

8+
core.info(prBody);
159
const isMatch = RELATED_ISSUE_REGEX.exec(prBody);
1610
if (!isMatch) {
1711
core.setFailed(`Unable to find related issue for PR number ${prNumber}.\n\n Body details: ${prBody}`);

.github/workflows/label_pr_on_title.yml

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Label PR based on title
22

33
on:
44
workflow_run:
5-
workflows: ["Record PR number"]
5+
workflows: ["Record PR details"]
66
types:
77
- completed
88

@@ -20,46 +20,18 @@ jobs:
2020
needs: get_pr_details
2121
runs-on: ubuntu-latest
2222
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v3
2325
- name: "Label PR based on title"
2426
uses: actions/github-script@v6
27+
env:
28+
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
29+
PR_TITLE: ${{ needs.get_pr_details.outputs.prTitle }}
2530
with:
2631
github-token: ${{ secrets.GITHUB_TOKEN }}
2732
# This safely runs in our base repo, not on fork
2833
# thus allowing us to provide a write access token to label based on PR title
2934
# and label PR based on semantic title accordingly
3035
script: |
31-
const pr_number = ${{ needs.get_pr_details.outputs.prNumber }};
32-
const pr_title = "${{ needs.get_pr_details.outputs.prTitle }}";
33-
34-
const FEAT_REGEX = /feat(\((.+)\))?(\:.+)/
35-
const BUG_REGEX = /(fix|bug)(\((.+)\))?(\:.+)/
36-
const DOCS_REGEX = /(docs|doc)(\((.+)\))?(\:.+)/
37-
const CHORE_REGEX = /(chore)(\((.+)\))?(\:.+)/
38-
const DEPRECATED_REGEX = /(deprecated)(\((.+)\))?(\:.+)/
39-
const REFACTOR_REGEX = /(refactor)(\((.+)\))?(\:.+)/
40-
41-
const labels = {
42-
"feature": FEAT_REGEX,
43-
"bug": BUG_REGEX,
44-
"documentation": DOCS_REGEX,
45-
"internal": CHORE_REGEX,
46-
"enhancement": REFACTOR_REGEX,
47-
"deprecated": DEPRECATED_REGEX,
48-
}
49-
50-
for (const label in labels) {
51-
const matcher = new RegExp(labels[label])
52-
const isMatch = matcher.exec(pr_title)
53-
if (isMatch != null) {
54-
console.info(`Auto-labeling PR ${pr_number} with ${label}`)
55-
56-
await github.rest.issues.addLabels({
57-
issue_number: pr_number,
58-
owner: context.repo.owner,
59-
repo: context.repo.repo,
60-
labels: [label]
61-
})
62-
63-
break
64-
}
65-
}
36+
const script = require('.github/scripts/label_pr_based_on_title.js')
37+
await script({github, context, core})

.github/workflows/on_merged_pr.yml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Maintenance:
1+
name: On PR merge
22

33
on:
44
workflow_run:
5-
workflows: ["Record PR number"]
5+
workflows: ["Record PR details"]
66
types:
77
- completed
88

@@ -18,28 +18,20 @@ jobs:
1818
record_pr_workflow_id: ${{ github.event.workflow_run.id }}
1919
secrets:
2020
token: ${{ secrets.GITHUB_TOKEN }}
21-
skip_dependabot_prs:
22-
needs: get_pr_details
23-
if: >
24-
${{ needs.get_pr_details.outputs.prAuthor != 'dependabot[bot]' &&
25-
needs.get_pr_details.outputs.prAction == 'opened'
26-
}}
27-
runs-on: ubuntu-latest
2821
release_label_on_merge:
2922
needs: get_pr_details
30-
# if: ${{ needs.get_pr_details.outputs.prIsMerged == true }}
23+
# Maintenance: Conditional isn't working as expected
24+
if: |
25+
needs.get_pr_details.outputs.prAuthor != 'dependabot[bot]'
26+
&& needs.get_pr_details.outputs.prIsMerged == true
3127
runs-on: ubuntu-latest
3228
steps:
3329
- uses: actions/checkout@v3
3430
- name: "Label PR related issue for release"
3531
uses: actions/github-script@v6
3632
env:
3733
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
38-
PR_TITLE: ${{ needs.get_pr_details.outputs.prTitle }}
3934
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
40-
PR_AUTHOR: ${{ needs.get_pr_details.outputs.prAuthor }}
41-
PR_ACTION: ${{ needs.get_pr_details.outputs.prAction }}
42-
PR_MERGE: ${{ needs.get_pr_details.outputs.prIsMerged }}
4335
with:
4436
github-token: ${{ secrets.GITHUB_TOKEN }}
4537
script: |

.github/workflows/on_opened_pr.yml

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
name: On PR open
2+
13
on:
24
workflow_run:
3-
workflows: ["Record PR number"]
5+
workflows: ["Record PR details"]
46
types:
57
- completed
68

79
env:
810
BLOCK_LABEL: "do-not-merge"
911
BLOCK_REASON_LABEL: "need-issue"
12+
IGNORE_AUTHORS: '["dependabot[bot]", "markdownify[bot]"]'
1013

1114
# TODO: include markdownify author too
1215
jobs:
@@ -19,42 +22,20 @@ jobs:
1922
token: ${{ secrets.GITHUB_TOKEN }}
2023
check_related_issue:
2124
needs: get_pr_details
22-
if: >
23-
${{ needs.get_pr_details.outputs.prAuthor != 'dependabot[bot]' &&
24-
needs.get_pr_details.outputs.prAction == 'opened'
25-
}}
25+
# Maintenance: Refactor condition to the correct env syntax later
26+
if: |
27+
needs.get_pr_details.outputs.prAction == 'opened'
28+
&& contains(fromJson('["dependabot[bot]", "markdownify[bot]"]'), needs.get_pr_details.outputs.prAuthor) != true
2629
runs-on: ubuntu-latest
2730
steps:
31+
- uses: actions/checkout@v3
2832
- name: "Ensure related issue is present"
2933
uses: actions/github-script@v6
34+
env:
35+
PR_BODY: ${{ needs.get_pr_details.outputs.prBody }}
36+
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
3037
with:
3138
github-token: ${{ secrets.GITHUB_TOKEN }}
32-
# Maintenance: convert into a standalone JS like post_release.js
3339
script: |
34-
const prBody = "${{ needs.get_pr_details.outputs.prBody }}";
35-
const prNumber = ${{ needs.get_pr_details.outputs.prNumber }};
36-
const blockLabel = process.env.BLOCK_LABEL;
37-
const blockReasonLabel = process.env.BLOCK_REASON_LABEL;
38-
39-
const RELATED_ISSUE_REGEX = /Issue number:.+(\d)/
40-
41-
const matcher = new RegExp(RELATED_ISSUE_REGEX)
42-
const isMatch = matcher.exec(prBody)
43-
if (isMatch == null) {
44-
console.info(`No related issue found, maybe the author didn't use the template but there is one.`)
45-
46-
let msg = "No related issues found. Please ensure there is an open issue related to this change to avoid significant delays or closure.";
47-
await github.rest.issues.createComment({
48-
owner: context.repo.owner,
49-
repo: context.repo.repo,
50-
body: msg,
51-
issue_number: prNumber,
52-
});
53-
54-
await github.rest.issues.addLabels({
55-
issue_number: prNumber,
56-
owner: context.repo.owner,
57-
repo: context.repo.repo,
58-
labels: [blockLabel, blockReasonLabel]
59-
})
60-
}
40+
const script = require('.github/scripts/label_missing_related_issue.js')
41+
await script({github, context, core})

.github/workflows/record_pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Record PR number
1+
name: Record PR details
22

33
on:
44
pull_request:

.github/workflows/reusable_export_pr_details.yml

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
name: Export previously recorded PR
2+
13
on:
24
workflow_call:
35
inputs:
@@ -28,10 +30,11 @@ on:
2830
description: "Whether PR is merged"
2931
value: ${{ jobs.export_pr_details.outputs.prIsMerged }}
3032

31-
name: Export Pull Request details from fork
3233
jobs:
3334
export_pr_details:
3435
runs-on: ubuntu-latest
36+
env:
37+
FILENAME: pr.txt
3538
# Map the job outputs to step outputs
3639
outputs:
3740
prNumber: ${{ steps.prNumber.outputs.prNumber }}
@@ -45,6 +48,8 @@ jobs:
4548
uses: actions/checkout@v3
4649
- name: "Download previously saved PR"
4750
uses: actions/github-script@v6
51+
env:
52+
WORKFLOW_ID: ${{ inputs.record_pr_workflow_id }}
4853
# For security, we only download artifacts tied to the successful PR recording workflow
4954
with:
5055
github-token: ${{ secrets.token }}
@@ -54,10 +59,23 @@ jobs:
5459
# NodeJS standard library doesn't provide ZIP capabilities; use system `unzip` command instead
5560
- name: "Unzip PR artifact"
5661
run: unzip pr.zip
57-
- name: "Export PR details"
58-
uses: actions/github-script@v6
59-
with:
60-
github-token: ${{ secrets.token }}
61-
script: |
62-
const script = require('.github/scripts/export_pr_details.js')
63-
await script({github, context, core})
62+
# NOTE: We need separate steps for each mapped output and respective IDs
63+
# otherwise the parent caller won't see them regardless on how outputs are set.
64+
- name: "Export Pull Request Number"
65+
id: prNumber
66+
run: echo ::set-output name=prNumber::$(jq -c '.number' ${FILENAME})
67+
- name: "Export Pull Request Title"
68+
id: prTitle
69+
run: echo ::set-output name=prTitle::$(jq -c '.pull_request.title' ${FILENAME})
70+
- name: "Export Pull Request Body"
71+
id: prBody
72+
run: echo ::set-output name=prBody::$(jq -c '.pull_request.body' ${FILENAME})
73+
- name: "Export Pull Request Author"
74+
id: prAuthor
75+
run: echo ::set-output name=prAuthor::$(jq -c '.pull_request.user.login' ${FILENAME})
76+
- name: "Export Pull Request Action"
77+
id: prAction
78+
run: echo ::set-output name=prAction::$(jq -c '.action' ${FILENAME})
79+
- name: "Export Pull Request Merged status"
80+
id: prIsMerged
81+
run: echo ::set-output name=prIsMerged::$(jq -c '.pull_request.merged' ${FILENAME})

0 commit comments

Comments
 (0)