Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-timothy-albert committed Feb 19, 2025
1 parent b902dc2 commit 40b23ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
7 changes: 3 additions & 4 deletions internal/actions/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"strings"

"github.com/google/go-github/v63/github"
config "github.com/speakeasy-api/sdk-gen-config"
"github.com/speakeasy-api/sdk-generation-action/internal/cli"
"github.com/speakeasy-api/sdk-generation-action/internal/configuration"
Expand Down Expand Up @@ -156,11 +155,11 @@ func writeTestReportComment(g *git.Git, prNumber *int, testReportURL, targetName
return nil
}

currentPRComments, _ := g.ListPRComments(*prNumber)
currentPRComments, _ := g.ListIssueComments(*prNumber)
for _, comment := range currentPRComments {
commentBody := comment.GetBody()
if strings.Contains(commentBody, fmt.Sprintf(testReportCommentPrefix, targetName)) {
if err := g.DeletePRComment(comment.GetID()); err != nil {
if err := g.DeleteIssueComment(comment.GetID()); err != nil {
fmt.Println(fmt.Sprintf("Failed to delete existing test report comment: %s\n", err.Error()))
}
}
Expand All @@ -173,7 +172,7 @@ func writeTestReportComment(g *git.Git, prNumber *int, testReportURL, targetName

body := titleComment + "\n\n" + fmt.Sprintf(testReportCommentPrefix, targetName) + " " + fmt.Sprintf("[here](%s)", testReportURL)

err := g.WritePRComment(*prNumber, body, github.PullRequestComment{})
err := g.WriteIssueComment(*prNumber, body)

return err
}
23 changes: 18 additions & 5 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,19 +875,19 @@ func (g *Git) WritePRBody(prNumber int, body string) error {
return nil
}

func (g *Git) ListPRComments(prNumber int) ([]*github.PullRequestComment, error) {
comments, _, err := g.client.PullRequests.ListComments(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), GetRepo(), prNumber, nil)
func (g *Git) ListIssueComments(prNumber int) ([]*github.IssueComment, error) {
comments, _, err := g.client.Issues.ListComments(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), GetRepo(), prNumber, nil)
if err != nil {
return nil, fmt.Errorf("failed to get PR comments: %w", err)
}

return comments, nil
}

func (g *Git) DeletePRComment(commentID int64) error {
_, err := g.client.PullRequests.DeleteComment(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), GetRepo(), commentID)
func (g *Git) DeleteIssueComment(commentID int64) error {
_, err := g.client.Issues.DeleteComment(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), GetRepo(), commentID)
if err != nil {
return fmt.Errorf("failed to delete PR comment: %w", err)
return fmt.Errorf("failed to delete issue comment: %w", err)
}

return nil
Expand All @@ -912,6 +912,19 @@ func (g *Git) WritePRComment(prNumber int, fileName, body string, line int) erro
return nil
}

func (g *Git) WriteIssueComment(prNumber int, body string) error {
comment := &github.IssueComment{
Body: github.String(sanitizeExplanations(body)),
}

_, _, err := g.client.Issues.CreateComment(context.Background(), os.Getenv("GITHUB_REPOSITORY_OWNER"), GetRepo(), prNumber, comment)
if err != nil {
return fmt.Errorf("failed to create issue comment: %w", err)
}

return nil
}

func sanitizeExplanations(str string) string {
// Remove ANSI sequences
ansiEscape := regexp.MustCompile(`\x1b[^m]*m`)
Expand Down

0 comments on commit 40b23ee

Please sign in to comment.