@@ -69,6 +69,10 @@ Link to latest failed build: ${linkToBuild}
69
69
Commit: ${commit}
70
70
71
71
${failedTests}
72
+ `
73
+ prCommentTemplate = `@${prAuthor} some tests are failing on main after these changes.
74
+ Details: ${issueLink}
75
+ Please take a look when you get a chance. Thanks!
72
76
`
73
77
)
74
78
@@ -227,6 +231,13 @@ func (c *Client) CommentOnIssue(ctx context.Context, r report.Report, issue *git
227
231
c .handleBadResponses (response )
228
232
}
229
233
234
+ // Also comment on the PR with a link to this comment
235
+ if prNumber > 0 && issueComment != nil && issueComment .HTMLURL != nil {
236
+ if prAuthor := c .GetPRAuthor (ctx , prNumber ); prAuthor != "" {
237
+ _ = c .CommentOnPR (ctx , prNumber , prAuthor , * issueComment .HTMLURL )
238
+ }
239
+ }
240
+
230
241
return issueComment
231
242
}
232
243
@@ -315,6 +326,83 @@ func (c *Client) getCommitMessage(ctx context.Context) string {
315
326
return ""
316
327
}
317
328
329
+ // GetPRAuthor fetches the author of a pull request
330
+ func (c * Client ) GetPRAuthor (ctx context.Context , prNumber int ) string {
331
+ if prNumber <= 0 {
332
+ return ""
333
+ }
334
+
335
+ pr , response , err := c .client .PullRequests .Get (
336
+ ctx ,
337
+ c .envVariables [githubOwner ],
338
+ c .envVariables [githubRepository ],
339
+ prNumber ,
340
+ )
341
+ if err != nil {
342
+ c .logger .Warn ("Failed to get PR details from GitHub API" ,
343
+ zap .Int ("pr_number" , prNumber ),
344
+ zap .Error (err ),
345
+ )
346
+ return ""
347
+ }
348
+
349
+ if response .StatusCode != http .StatusOK {
350
+ c .logger .Warn ("Unexpected response when fetching PR" ,
351
+ zap .Int ("status_code" , response .StatusCode ),
352
+ zap .Int ("pr_number" , prNumber ),
353
+ )
354
+ return ""
355
+ }
356
+
357
+ if pr .User != nil && pr .User .Login != nil {
358
+ return * pr .User .Login
359
+ }
360
+
361
+ return ""
362
+ }
363
+
364
+ // CommentOnPR adds a comment to a pull request to notify the author about failing tests
365
+ func (c * Client ) CommentOnPR (ctx context.Context , prNumber int , prAuthor string , issueURL string ) * github.IssueComment {
366
+ if prNumber <= 0 || prAuthor == "" {
367
+ c .logger .Warn ("Cannot comment on PR: missing PR number or author" ,
368
+ zap .Int ("pr_number" , prNumber ),
369
+ zap .String ("pr_author" , prAuthor ),
370
+ )
371
+ return nil
372
+ }
373
+
374
+ body := os .Expand (prCommentTemplate , func (param string ) string {
375
+ return prTemplateHelper (param , prAuthor , issueURL )
376
+ })
377
+
378
+ prComment , response , err := c .client .Issues .CreateComment (
379
+ ctx ,
380
+ c .envVariables [githubOwner ],
381
+ c .envVariables [githubRepository ],
382
+ prNumber ,
383
+ & github.IssueComment {
384
+ Body : & body ,
385
+ },
386
+ )
387
+ if err != nil {
388
+ c .logger .Warn ("Failed to comment on PR" ,
389
+ zap .Int ("pr_number" , prNumber ),
390
+ zap .Error (err ),
391
+ )
392
+ return nil
393
+ }
394
+
395
+ if response .StatusCode != http .StatusCreated {
396
+ c .logger .Warn ("Unexpected response when commenting on PR" ,
397
+ zap .Int ("status_code" , response .StatusCode ),
398
+ zap .Int ("pr_number" , prNumber ),
399
+ )
400
+ return nil
401
+ }
402
+
403
+ return prComment
404
+ }
405
+
318
406
func (c * Client ) extractPRNumberFromCommitMessage (commitMsg string ) int {
319
407
// Only consider the first line of the commit message.
320
408
firstLine := strings .SplitN (commitMsg , "\n " , 2 )[0 ]
@@ -377,9 +465,27 @@ func (c *Client) CreateIssue(ctx context.Context, r report.Report) *github.Issue
377
465
c .handleBadResponses (response )
378
466
}
379
467
468
+ // After creating the issue, also comment on the PR with a link to the created issue
469
+ if prNumber > 0 && issue != nil && issue .HTMLURL != nil {
470
+ if prAuthor := c .GetPRAuthor (ctx , prNumber ); prAuthor != "" {
471
+ _ = c .CommentOnPR (ctx , prNumber , prAuthor , * issue .HTMLURL )
472
+ }
473
+ }
474
+
380
475
return issue
381
476
}
382
477
478
+ func prTemplateHelper (param string , prAuthor string , issueURL string ) string {
479
+ switch param {
480
+ case "prAuthor" :
481
+ return prAuthor
482
+ case "issueLink" :
483
+ return issueURL
484
+ default :
485
+ return ""
486
+ }
487
+ }
488
+
383
489
func (c * Client ) handleBadResponses (response * github.Response ) {
384
490
body , _ := io .ReadAll (response .Body )
385
491
c .logger .Fatal (
0 commit comments