Skip to content

Commit 309eeb7

Browse files
committed
Restrict cherry-pick comments to owners and members
Signed-off-by: Minh Vu <vuhoangminh97@gmail.com>
1 parent abc8f3b commit 309eeb7

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

.github/scripts/add-labels-from-comment.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
module.exports = async ({ github, context, core }) => {
1818
const commentBody = context.payload.comment.body;
1919
const prNumber = context.payload.issue.number;
20+
const trustedAssociations = new Set(['OWNER', 'MEMBER']);
21+
const authorAssociation = context.payload.comment.author_association;
2022

2123
core.info(`Processing comment: ${commentBody}`);
2224

25+
if (!trustedAssociations.has(authorAssociation)) {
26+
core.warning(`Ignoring /cherry-pick comment from ${authorAssociation || 'unknown'} author association`);
27+
return { success: false, message: 'Unauthorized comment author' };
28+
}
29+
2330
// Parse comment for /cherry-pick branches
2431
const cherryPickPattern = /^\/cherry-pick\s+(.+)$/m;
2532
const match = commentBody.match(cherryPickPattern);
@@ -107,4 +114,3 @@ module.exports = async ({ github, context, core }) => {
107114
};
108115
}
109116
};
110-

.github/workflows/cherrypick.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ on:
2020
pull_request_target:
2121
types: [closed]
2222

23-
permissions:
24-
contents: write
25-
pull-requests: write
26-
issues: write
23+
permissions: {}
2724

2825
jobs:
2926
add-labels:
@@ -33,7 +30,12 @@ jobs:
3330
if: |
3431
github.event_name == 'issue_comment' &&
3532
github.event.issue.pull_request &&
36-
startsWith(github.event.comment.body, '/cherry-pick')
33+
startsWith(github.event.comment.body, '/cherry-pick') &&
34+
contains(fromJSON('["OWNER", "MEMBER"]'), github.event.comment.author_association)
35+
permissions:
36+
contents: write
37+
pull-requests: write
38+
issues: write
3739

3840
steps:
3941
- name: Checkout repository
@@ -62,6 +64,10 @@ jobs:
6264
github.event_name == 'pull_request_target' &&
6365
github.event.pull_request.merged == true &&
6466
contains(join(github.event.pull_request.labels.*.name, ','), 'cherry-pick/')
67+
permissions:
68+
contents: write
69+
pull-requests: write
70+
issues: write
6571

6672
steps:
6773
- name: Checkout base branch repository

0 commit comments

Comments
 (0)