Skip to content

Commit e35db57

Browse files
authored
fix: LinkBot fork permission issue (#1363)
Signed-off-by: Mounil <[email protected]>
1 parent 8050bb2 commit e35db57

File tree

3 files changed

+84
-24
lines changed

3 files changed

+84
-24
lines changed
Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
module.exports = async ({ github, context }) => {
2-
const body = context.payload.pull_request.body || "";
3-
const regex = /\bFixes\s*:?\s*(#\d+)(\s*,\s*#\d+)*/i;
2+
let prNumber;
3+
try {
4+
const isDryRun = process.env.DRY_RUN === 'true';
5+
prNumber = parseInt(process.env.PR_NUMBER) || context.payload.pull_request.number;
6+
7+
console.log(`Processing PR #${prNumber} (Dry run: ${isDryRun})`);
8+
9+
// For workflow_dispatch, we need to fetch PR details
10+
let prData;
11+
if (context.payload.pull_request) {
12+
prData = context.payload.pull_request;
13+
} else {
14+
// workflow_dispatch case - fetch PR data
15+
const prResponse = await github.rest.pulls.get({
16+
owner: context.repo.owner,
17+
repo: context.repo.repo,
18+
pull_number: prNumber,
19+
});
20+
prData = prResponse.data;
21+
}
422

5-
const comments = await github.rest.issues.listComments({
6-
owner: context.repo.owner,
7-
repo: context.repo.repo,
8-
issue_number: context.payload.pull_request.number,
9-
});
23+
const body = prData.body || "";
24+
const regex = /\bFixes\s*:?\s*(#\d+)(\s*,\s*#\d+)*/i;
1025

11-
const alreadyCommented = comments.data.some(comment =>
12-
comment.body.includes("this is LinkBot")
13-
);
14-
15-
if (alreadyCommented) {
16-
return;
17-
}
18-
19-
if (!regex.test(body)) {
20-
await github.rest.issues.createComment({
26+
const comments = await github.rest.issues.listComments({
2127
owner: context.repo.owner,
2228
repo: context.repo.repo,
23-
issue_number: context.payload.pull_request.number,
24-
body: [
25-
`Hi @${context.payload.pull_request.user.login}, this is **LinkBot** 👋`,
29+
issue_number: prNumber,
30+
});
31+
32+
const alreadyCommented = comments.data.some(comment =>
33+
comment.body.includes("this is LinkBot")
34+
);
35+
36+
if (alreadyCommented) {
37+
console.log('LinkBot already commented on this PR');
38+
return;
39+
}
40+
41+
if (!regex.test(body)) {
42+
const commentBody = [
43+
`Hi @${prData.user.login}, this is **LinkBot** 👋`,
2644
``,
2745
`Linking pull requests to issues helps us significantly with reviewing pull requests and keeping the repository healthy.`,
2846
``,
@@ -38,8 +56,30 @@ module.exports = async ({ github, context }) => {
3856
`[docs/sdk_developers/creating_issues.md](https://github.com/${context.repo.owner}/${context.repo.repo}/blob/main/docs/sdk_developers/creating_issues.md)`,
3957
``,
4058
`Thanks!`
41-
].join('\n')
42-
});
59+
].join('\n');
60+
61+
if (isDryRun) {
62+
console.log('DRY RUN: Would post the following comment:');
63+
console.log('---');
64+
console.log(commentBody);
65+
console.log('---');
66+
} else {
67+
await github.rest.issues.createComment({
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
issue_number: prNumber,
71+
body: commentBody,
72+
});
73+
console.log('LinkBot comment posted successfully');
74+
}
75+
} else {
76+
console.log('PR has linked issue - no comment needed');
77+
}
78+
} catch (error) {
79+
console.error('Error processing PR:', error);
80+
console.error('PR number:', prNumber);
81+
console.error('Repository:', `${context.repo.owner}/${context.repo.repo}`);
82+
throw error;
4383
}
4484
};
4585

.github/workflows/bot-pr-missing-linked-issue.yml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1+
# This workflow checks PRs for linked issues and posts a reminder comment if missing.
2+
# Uses pull_request_target to enable write permissions for fork PRs while checking out
3+
# only the base branch code (ref: main) to avoid executing untrusted fork code.
14
name: PR Missing Linked Issue Reminder
25

36
on:
4-
pull_request:
7+
pull_request_target:
58
types: [opened, edited, reopened]
9+
workflow_dispatch:
10+
inputs:
11+
pr_number:
12+
description: 'PR number to check'
13+
required: true
14+
type: number
15+
dry_run:
16+
description: 'Dry run (only log, no comments)'
17+
required: false
18+
type: boolean
19+
default: true
620

721
permissions:
822
pull-requests: write
923
contents: read
24+
issues: write
1025

1126
jobs:
1227
check-linked-issue:
1328
runs-on: ubuntu-latest
1429

1530
concurrency:
16-
group: bot-pr-missing-linked-issue-${{ github.event.pull_request.number }}
31+
group: bot-pr-missing-linked-issue-${{ github.event.pull_request.number || github.event.inputs.pr_number }}
1732
cancel-in-progress: true
1833

1934
steps:
@@ -24,10 +39,14 @@ jobs:
2439

2540
- name: Checkout repository
2641
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
42+
with:
43+
ref: main
2744

2845
- name: Check PR body for linked issue
2946
env:
3047
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48+
DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
49+
PR_NUMBER: ${{ github.event.pull_request.number || github.event.inputs.pr_number }}
3150
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
3251
with:
3352
script: |

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
157157
- Workflow does not contain permissions for `pr-check-test-files` and `pr-check-codecov`
158158
- Fixed `cron-check-broken-links.yml` string parsing issue in context input `dry_run` (#1235)
159159
- Flaky tests by disabling TLS in mock Hedera nodes in `mock_server.py`
160+
- Fixed LinkBot permission issue for fork PRs by changing trigger to pull_request_target and adding proper permissions.
160161

161162
### Breaking Change
162163

0 commit comments

Comments
 (0)