Skip to content

Commit 6f284d1

Browse files
committed
fetch comment by ID
1 parent 1d6fa04 commit 6f284d1

4 files changed

Lines changed: 29 additions & 11 deletions

File tree

.ci/magician/cmd/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type GithubClient interface {
2727
GetPullRequestRequestedReviewers(prNumber string) ([]github.User, error)
2828
GetPullRequestPreviousReviewers(prNumber string) ([]github.User, error)
2929
GetPullRequestComments(prNumber string) ([]github.PullRequestComment, error)
30+
GetPullRequestComment(commentID int) (github.PullRequestComment, error)
3031
GetCommitMessage(owner, repo, sha string) (string, error)
3132
GetUserType(user string) github.UserType
3233
GetTeamMembers(organization, team string) ([]github.User, error)

.ci/magician/cmd/mock_github_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"errors"
20+
"fmt"
2021

2122
"magician/github"
2223
)
@@ -62,6 +63,16 @@ func (m *mockGithub) GetPullRequestComments(prNumber string) ([]github.PullReque
6263
return m.pullRequestComments, nil
6364
}
6465

66+
func (m *mockGithub) GetPullRequestComment(commentID int) (github.PullRequestComment, error) {
67+
m.calledMethods["GetPullRequestComment"] = append(m.calledMethods["GetPullRequestComment"], []any{commentID})
68+
for _, c := range m.pullRequestComments {
69+
if c.ID == commentID {
70+
return c, nil
71+
}
72+
}
73+
return github.PullRequestComment{}, fmt.Errorf("comment not found")
74+
}
75+
6576
func (m *mockGithub) GetCommitMessage(owner, repo, sha string) (string, error) {
6677
m.calledMethods["GetCommitMessage"] = append(m.calledMethods["GetCommitMessage"], []any{owner, repo, sha})
6778
return m.commitMessage, nil

.ci/magician/cmd/test_terraform_vcr.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -512,22 +512,19 @@ View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/head
512512
return false, nil
513513
}
514514

515+
// appendVCRResultToDiffComment appends content to the existing diff report comment
516+
// identified by the ID in /workspace/diff_comment_id.txt.
517+
// If the file is missing or the comment cannot be fetched, it falls back to posting a new comment.
515518
func appendVCRResultToDiffComment(prNumber string, content string, gh GithubClient, rnr ExecRunner) error {
516-
comments, err := gh.GetPullRequestComments(prNumber)
517-
if err != nil {
518-
return fmt.Errorf("error getting PR comments: %w", err)
519-
}
520-
521519
var diffComment *github.PullRequestComment
522520

523521
// Try to find by ID from file
524522
if idStr, err := rnr.ReadFile("/workspace/diff_comment_id.txt"); err == nil {
525523
if id, err := strconv.Atoi(strings.TrimSpace(idStr)); err == nil {
526-
for _, c := range comments {
527-
if c.ID == id {
528-
diffComment = &c
529-
break
530-
}
524+
if comment, err := gh.GetPullRequestComment(id); err == nil {
525+
diffComment = &comment
526+
} else {
527+
fmt.Printf("Warning: failed to fetch comment %d by ID: %v\n", id, err)
531528
}
532529
}
533530
}
@@ -538,7 +535,7 @@ func appendVCRResultToDiffComment(prNumber string, content string, gh GithubClie
538535
}
539536

540537
// Fallback to posting a new comment if diff report not found
541-
_, err = gh.PostComment(prNumber, content)
538+
_, err := gh.PostComment(prNumber, content)
542539
return err
543540
}
544541

.ci/magician/github/get.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ func (c *Client) GetPullRequestComments(prNumber string) ([]PullRequestComment,
184184
return convertGHComments(allComments), nil
185185
}
186186

187+
// GetPullRequestComment fetches a single comment by ID
188+
func (c *Client) GetPullRequestComment(commentID int) (PullRequestComment, error) {
189+
comment, _, err := c.gh.Issues.GetComment(c.ctx, defaultOwner, defaultRepo, int64(commentID))
190+
if err != nil {
191+
return PullRequestComment{}, err
192+
}
193+
return convertGHComment(comment), nil
194+
}
195+
187196
// GetTeamMembers gets all members of a team, handling pagination
188197
func (c *Client) GetTeamMembers(organization, team string) ([]User, error) {
189198
var allMembers []*gh.User

0 commit comments

Comments
 (0)