Skip to content

Commit 5dadc43

Browse files
heitorlessarubenfonseca
authored andcommitted
fix(ci): on_label permissioning model & workflow execution
1 parent 8e90151 commit 5dadc43

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed
+45-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
const {
22
PR_NUMBER,
3+
PR_ACTION,
34
PR_AUTHOR,
45
IGNORE_AUTHORS,
56
} = require("./constants")
67

7-
module.exports = async ({github, context, core}) => {
8-
if (IGNORE_AUTHORS.includes(PR_AUTHOR)) {
9-
return core.notice("Author in IGNORE_AUTHORS list; skipping...")
10-
}
118

9+
const notifyAuthor = async ({
10+
gh_client,
11+
owner,
12+
repository,
13+
}) => {
1214
core.info(`Commenting on PR ${PR_NUMBER}`)
1315

1416
let msg = `
@@ -17,10 +19,46 @@ module.exports = async ({github, context, core}) => {
1719
Please consider breaking into smaller PRs to avoid significant review delays. Ignore if this PR has naturally grown to this size after reviews.
1820
`;
1921

20-
await github.rest.issues.createComment({
22+
try {
23+
await gh_client.rest.issues.createComment({
24+
owner: owner,
25+
repo: repository,
26+
body: msg,
27+
issue_number: PR_NUMBER,
28+
});
29+
} catch (error) {
30+
core.setFailed("Failed to notify PR author to split large PR");
31+
console.error(err);
32+
}
33+
}
34+
35+
module.exports = async ({github, context, core}) => {
36+
if (IGNORE_AUTHORS.includes(PR_AUTHOR)) {
37+
return core.notice("Author in IGNORE_AUTHORS list; skipping...")
38+
}
39+
40+
if (PR_ACTION != "labeled") {
41+
return core.notice("Only run on PRs labeling actions; skipping")
42+
}
43+
44+
45+
/** @type {string[]} */
46+
const labels = await github.rest.issues.listLabelsOnIssue({
2147
owner: context.repo.owner,
2248
repo: context.repo.repo,
23-
body: msg,
2449
issue_number: PR_NUMBER,
25-
});
50+
})
51+
52+
// Schema: https://docs.github.com/en/rest/issues/labels#list-labels-for-an-issue
53+
for (const label of labels) {
54+
core.info(`Label: ${label}`)
55+
if (label.name == "size/XXL") {
56+
await notifyAuthor({
57+
gh_client: github,
58+
owner: context.repo.owner,
59+
repository: context.repo.repo,
60+
})
61+
break;
62+
}
63+
}
2664
}

.github/workflows/on_label_added.yml

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
name: On Label added
22

33
on:
4-
pull_request:
4+
workflow_run:
5+
workflows: ["Record PR details"]
56
types:
6-
- labeled
7+
- completed
78

89
jobs:
10+
get_pr_details:
11+
if: ${{ 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+
workflow_origin: ${{ github.event.repository.full_name }}
16+
secrets:
17+
token: ${{ secrets.GITHUB_TOKEN }}
18+
919
split-large-pr:
10-
if: ${{ github.event.label.name == 'size/XXL' }}
20+
needs: get_pr_details
1121
runs-on: ubuntu-latest
1222
permissions:
1323
issues: write
@@ -17,8 +27,9 @@ jobs:
1727
- name: "Suggest split large Pull Request"
1828
uses: actions/github-script@v6
1929
env:
20-
PR_NUMBER: ${{ github.event.pull_request.number }}
21-
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
30+
PR_NUMBER: ${{ needs.get_pr_details.outputs.prNumber }}
31+
PR_ACTION: ${{ needs.get_pr_details.outputs.prAction }}
32+
PR_AUTHOR: ${{ needs.get_pr_details.outputs.prAuthor }}
2233
with:
2334
github-token: ${{ secrets.GITHUB_TOKEN }}
2435
script: |

0 commit comments

Comments
 (0)