Skip to content

Commit 1b1d0e6

Browse files
committed
@ author and reviewers
1 parent dd10660 commit 1b1d0e6

5 files changed

Lines changed: 72 additions & 0 deletions

File tree

.ci/magician/cmd/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
type GithubClient interface {
2525
GetPullRequest(prNumber string) (github.PullRequest, error)
26+
GetPullRequestAuthor(prNumber string) (string, error)
2627
GetPullRequests(state, base, sort, direction string) ([]github.PullRequest, error)
2728
GetPullRequestRequestedReviewers(prNumber string) ([]github.User, error)
2829
GetPullRequestPreviousReviewers(prNumber string) ([]github.User, error)

.ci/magician/cmd/mock_github_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func (m *mockGithub) GetPullRequest(prNumber string) (github.PullRequest, error)
3838
return m.pullRequest, nil
3939
}
4040

41+
func (m *mockGithub) GetPullRequestAuthor(prNumber string) (string, error) {
42+
m.calledMethods["GetPullRequestAuthor"] = append(m.calledMethods["GetPullRequestAuthor"], []any{prNumber})
43+
return m.pullRequest.User.Login, nil
44+
}
45+
4146
func (m *mockGithub) GetPullRequests(state, base, sort, direction string) ([]github.PullRequest, error) {
4247
m.calledMethods["GetPullRequests"] = append(m.calledMethods["GetPullRequests"], []any{state, base, sort, direction})
4348
return []github.PullRequest{m.pullRequest}, nil

.ci/magician/cmd/test_terraform_vcr.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
263263
if err != nil {
264264
return fmt.Errorf("error formatting post replay comment: %w", err)
265265
}
266+
if len(replayingResult.FailedTests) == 0 {
267+
mentionStr := getMentions(prNumber, gh)
268+
if mentionStr != "" {
269+
comment = fmt.Sprintf("%s\n\n%s VCR tests complete for %s!", comment, mentionStr, mmCommitSha)
270+
}
271+
}
266272
if err := appendVCRResultToDiffComment(prNumber, comment, gh, rnr); err != nil {
267273
return fmt.Errorf("error appending comment: %w", err)
268274
}
@@ -345,6 +351,10 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
345351
if err != nil {
346352
return fmt.Errorf("error formatting record replay comment: %w", err)
347353
}
354+
mentionStr := getMentions(prNumber, gh)
355+
if mentionStr != "" {
356+
recordReplayComment = fmt.Sprintf("%s\n\n%s VCR tests complete for %s!", recordReplayComment, mentionStr, mmCommitSha)
357+
}
348358
if err := appendVCRResultToDiffComment(prNumber, recordReplayComment, gh, rnr); err != nil {
349359
return fmt.Errorf("error appending comment: %w", err)
350360
}
@@ -501,6 +511,10 @@ func handlePanics(prNumber, buildID, buildStatusTargetURL, mmCommitSha string, r
501511
comment := color("red", fmt.Sprintf("The provider crashed while running the VCR tests in %s mode\n", mode.Upper()))
502512
comment += fmt.Sprintf(`Please fix it to complete your PR.
503513
View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-%s/artifacts/%s/build-log/%s_test.log)`, prNumber, buildID, mode.Lower())
514+
mentionStr := getMentions(prNumber, gh)
515+
if mentionStr != "" {
516+
comment = fmt.Sprintf("%s\n\n%s VCR tests complete for %s!", comment, mentionStr, mmCommitSha)
517+
}
504518
if err := appendVCRResultToDiffComment(prNumber, comment, gh, rnr); err != nil {
505519
return true, fmt.Errorf("error appending comment: %v", err)
506520
}
@@ -512,6 +526,35 @@ View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/head
512526
return false, nil
513527
}
514528

529+
func getMentions(prNumber string, gh GithubClient) string {
530+
author := ""
531+
reviewerMentions := ""
532+
if authorName, err := gh.GetPullRequestAuthor(prNumber); err == nil {
533+
author = "@" + authorName
534+
}
535+
if reviewers, err := gh.GetPullRequestRequestedReviewers(prNumber); err == nil {
536+
var mentions []string
537+
for _, r := range reviewers {
538+
mentions = append(mentions, "@"+r.Login)
539+
}
540+
if len(mentions) > 0 {
541+
reviewerMentions = strings.Join(mentions, ", ")
542+
}
543+
}
544+
545+
mentionStr := ""
546+
if author != "" {
547+
mentionStr += author
548+
}
549+
if reviewerMentions != "" {
550+
if mentionStr != "" {
551+
mentionStr += ", "
552+
}
553+
mentionStr += reviewerMentions
554+
}
555+
return mentionStr
556+
}
557+
515558
// appendVCRResultToDiffComment appends content to the existing diff report comment
516559
// identified by the ID in /workspace/diff_comment_id.txt.
517560
// If the file is missing or the comment cannot be fetched, it falls back to posting a new comment.

.ci/magician/cmd/test_terraform_vcr_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,13 @@ func TestRecordReplay(t *testing.T) {
551551
func TestAppendVCRResultToDiffComment_NotExists(t *testing.T) {
552552
gh := &mockGithub{
553553
calledMethods: make(map[string][][]any),
554+
pullRequest: github.PullRequest{
555+
User: github.User{Login: "author1"},
556+
},
557+
requestedReviewers: []github.User{
558+
{Login: "reviewer1"},
559+
{Login: "reviewer2"},
560+
},
554561
pullRequestComments: []github.PullRequestComment{
555562
{
556563
ID: 456,
@@ -571,6 +578,13 @@ func TestAppendVCRResultToDiffComment_NotExists(t *testing.T) {
571578
func TestAppendVCRResultToDiffComment_UseFileID(t *testing.T) {
572579
gh := &mockGithub{
573580
calledMethods: make(map[string][][]any),
581+
pullRequest: github.PullRequest{
582+
User: github.User{Login: "author1"},
583+
},
584+
requestedReviewers: []github.User{
585+
{Login: "reviewer1"},
586+
{Login: "reviewer2"},
587+
},
574588
pullRequestComments: []github.PullRequestComment{
575589
{
576590
ID: 456,

.ci/magician/github/get.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ func (c *Client) GetPullRequest(prNumber string) (PullRequest, error) {
7070
return convertGHPullRequest(pr), nil
7171
}
7272

73+
// GetPullRequestAuthor fetches the author of a pull request
74+
func (c *Client) GetPullRequestAuthor(prNumber string) (string, error) {
75+
pr, err := c.GetPullRequest(prNumber)
76+
if err != nil {
77+
return "", err
78+
}
79+
return pr.User.Login, nil
80+
}
81+
7382
// GetPullRequests fetches multiple pull requests
7483
func (c *Client) GetPullRequests(state, base, sort, direction string) ([]PullRequest, error) {
7584
opts := &gh.PullRequestListOptions{

0 commit comments

Comments
 (0)