Skip to content

Commit 5930c54

Browse files
committed
use comment id
1 parent c34cff7 commit 5930c54

4 files changed

Lines changed: 41 additions & 25 deletions

File tree

.ci/magician/cmd/reassign_reviewer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func execReassignReviewer(prNumber, newPrimaryReviewer string, gh GithubClient)
9191

9292
if currentReviewer == "" {
9393
fmt.Println("No reviewer comment found, creating one")
94-
_, err := gh.PostComment(prNumber, comment)
94+
_, err = gh.PostComment(prNumber, comment)
9595
if err != nil {
9696
return err
9797
}

.ci/magician/cmd/templates/vcr/post_replay.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
{{range .NotRunGATests}}{{. | printf "- %s\n"}}{{end}}
1212
{{end}}
1313
{{end}}
14+
## Test report
15+
1416
#### Tests analytics
1517
Total tests: {{add (add (len .ReplayingResult.PassedTests) (len .ReplayingResult.SkippedTests)) (len .ReplayingResult.FailedTests) }}
1618
Passed tests: {{len .ReplayingResult.PassedTests}}

.ci/magician/cmd/test_terraform_vcr.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"text/template"
1212

13+
"strconv"
14+
1315
"github.com/spf13/cobra"
1416

1517
"magician/exec"
@@ -231,7 +233,7 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
231233
return fmt.Errorf("error uploading replaying logs: %w", err)
232234
}
233235

234-
if hasPanics, err := handlePanics(prNumber, buildID, buildStatusTargetURL, mmCommitSha, replayingResult, vcr.Replaying, gh); err != nil {
236+
if hasPanics, err := handlePanics(prNumber, buildID, buildStatusTargetURL, mmCommitSha, replayingResult, vcr.Replaying, gh, rnr); err != nil {
235237
return fmt.Errorf("error handling panics: %w", err)
236238
} else if hasPanics {
237239
return nil
@@ -260,7 +262,7 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
260262
if err != nil {
261263
return fmt.Errorf("error formatting post replay comment: %w", err)
262264
}
263-
if err := appendVCRResultToDiffComment(prNumber, comment, gh); err != nil {
265+
if err := appendVCRResultToDiffComment(prNumber, comment, gh, rnr); err != nil {
264266
return fmt.Errorf("error appending comment: %w", err)
265267
}
266268
if len(replayingResult.FailedTests) > 0 {
@@ -291,7 +293,7 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
291293
return fmt.Errorf("error uploading recording logs: %w", err)
292294
}
293295

294-
if hasPanics, err := handlePanics(prNumber, buildID, buildStatusTargetURL, mmCommitSha, recordingResult, vcr.Recording, gh); err != nil {
296+
if hasPanics, err := handlePanics(prNumber, buildID, buildStatusTargetURL, mmCommitSha, recordingResult, vcr.Recording, gh, rnr); err != nil {
295297
return fmt.Errorf("error handling panics: %w", err)
296298
} else if hasPanics {
297299
return nil
@@ -341,7 +343,7 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
341343
if err != nil {
342344
return fmt.Errorf("error formatting record replay comment: %w", err)
343345
}
344-
if err := appendVCRResultToDiffComment(prNumber, recordReplayComment, gh); err != nil {
346+
if err := appendVCRResultToDiffComment(prNumber, recordReplayComment, gh, rnr); err != nil {
345347
return fmt.Errorf("error appending comment: %w", err)
346348
}
347349
}
@@ -492,12 +494,12 @@ func runReplaying(runFullVCR bool, version provider.Version, services map[string
492494
return result, testDirs, replayingErr
493495
}
494496

495-
func handlePanics(prNumber, buildID, buildStatusTargetURL, mmCommitSha string, result vcr.Result, mode vcr.Mode, gh GithubClient) (bool, error) {
497+
func handlePanics(prNumber, buildID, buildStatusTargetURL, mmCommitSha string, result vcr.Result, mode vcr.Mode, gh GithubClient, rnr ExecRunner) (bool, error) {
496498
if len(result.Panics) > 0 {
497499
comment := color("red", fmt.Sprintf("The provider crashed while running the VCR tests in %s mode\n", mode.Upper()))
498500
comment += fmt.Sprintf(`Please fix it to complete your PR.
499501
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())
500-
if err := appendVCRResultToDiffComment(prNumber, comment, gh); err != nil {
502+
if err := appendVCRResultToDiffComment(prNumber, comment, gh, rnr); err != nil {
501503
return true, fmt.Errorf("error appending comment: %v", err)
502504
}
503505
if err := gh.PostBuildStatus(prNumber, "VCR-test", "failure", buildStatusTargetURL, mmCommitSha); err != nil {
@@ -508,17 +510,23 @@ View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/head
508510
return false, nil
509511
}
510512

511-
func appendVCRResultToDiffComment(prNumber string, content string, gh GithubClient) error {
513+
func appendVCRResultToDiffComment(prNumber string, content string, gh GithubClient, rnr ExecRunner) error {
512514
comments, err := gh.GetPullRequestComments(prNumber)
513515
if err != nil {
514516
return fmt.Errorf("error getting PR comments: %w", err)
515517
}
516518

517519
var diffComment *github.PullRequestComment
518-
for _, c := range comments {
519-
if strings.Contains(c.Body, "## Diff report") || strings.Contains(c.Body, "Hi there, I'm the Modular magician") {
520-
diffComment = &c
521-
break
520+
521+
// Try to find by ID from file
522+
if idStr, err := rnr.ReadFile("diff_comment_id.txt"); err == nil {
523+
if id, err := strconv.Atoi(strings.TrimSpace(idStr)); err == nil {
524+
for _, c := range comments {
525+
if c.ID == id {
526+
diffComment = &c
527+
break
528+
}
529+
}
522530
}
523531
}
524532

.ci/magician/cmd/test_terraform_vcr_test.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -581,41 +581,47 @@ func TestRecordReplay(t *testing.T) {
581581
}
582582
}
583583

584-
func TestAppendVCRResultToDiffComment_Exists(t *testing.T) {
584+
func TestAppendVCRResultToDiffComment_NotExists(t *testing.T) {
585585
gh := &mockGithub{
586586
calledMethods: make(map[string][][]any),
587587
pullRequestComments: []github.PullRequestComment{
588588
{
589589
ID: 456,
590-
Body: "## Diff report\nsome diffs",
590+
Body: "Some other comment",
591591
},
592592
},
593593
}
594594

595-
err := appendVCRResultToDiffComment("123", "VCR Results", gh)
595+
rnr := &mockRunner{}
596+
err := appendVCRResultToDiffComment("123", "VCR Results", gh, rnr)
596597

597598
assert.NoError(t, err)
598-
assert.Len(t, gh.calledMethods["UpdateComment"], 1)
599-
assert.Equal(t, "123", gh.calledMethods["UpdateComment"][0][0])
600-
assert.Contains(t, gh.calledMethods["UpdateComment"][0][1].(string), "VCR Results")
601-
assert.Equal(t, 456, gh.calledMethods["UpdateComment"][0][2])
599+
assert.Len(t, gh.calledMethods["PostComment"], 1)
600+
assert.Equal(t, "123", gh.calledMethods["PostComment"][0][0])
601+
assert.Equal(t, "VCR Results", gh.calledMethods["PostComment"][0][1])
602602
}
603603

604-
func TestAppendVCRResultToDiffComment_NotExists(t *testing.T) {
604+
func TestAppendVCRResultToDiffComment_UseFileID(t *testing.T) {
605605
gh := &mockGithub{
606606
calledMethods: make(map[string][][]any),
607607
pullRequestComments: []github.PullRequestComment{
608608
{
609609
ID: 456,
610-
Body: "Some other comment",
610+
Body: "Some comment",
611611
},
612612
},
613613
}
614+
rnr := &mockRunner{
615+
fileContents: map[string]string{
616+
"diff_comment_id.txt": "456",
617+
},
618+
}
614619

615-
err := appendVCRResultToDiffComment("123", "VCR Results", gh)
620+
err := appendVCRResultToDiffComment("123", "VCR Results", gh, rnr)
616621

617622
assert.NoError(t, err)
618-
assert.Len(t, gh.calledMethods["PostComment"], 1)
619-
assert.Equal(t, "123", gh.calledMethods["PostComment"][0][0])
620-
assert.Equal(t, "VCR Results", gh.calledMethods["PostComment"][0][1])
623+
assert.Len(t, gh.calledMethods["UpdateComment"], 1)
624+
assert.Equal(t, "123", gh.calledMethods["UpdateComment"][0][0])
625+
assert.Contains(t, gh.calledMethods["UpdateComment"][0][1].(string), "VCR Results")
626+
assert.Equal(t, 456, gh.calledMethods["UpdateComment"][0][2])
621627
}

0 commit comments

Comments
 (0)