11module . exports = async ( { github, context } ) => {
2- const body = context . payload . pull_request . body || "" ;
3- const regex = / \b F i x e s \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 = / \b F i x e s \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
0 commit comments