From c64d8a9de5a138df6d40bc0840dab3f8fe26ccdc Mon Sep 17 00:00:00 2001 From: Owen Rumney Date: Wed, 19 Jan 2022 13:36:13 +0000 Subject: [PATCH] Stop checking comment ids when comment found - edit the comment rather than delete and create - stop searching for comment when its found - add abuse backoff for the edit comment --- commenter/commenter.go | 3 +++ commenter/connector.go | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/commenter/commenter.go b/commenter/commenter.go index d45afe9..91603ac 100644 --- a/commenter/commenter.go +++ b/commenter/commenter.go @@ -141,6 +141,9 @@ func (c *Commenter) writeCommentIfRequired(prComment *github.PullRequestComment) } return nil }(existing) + if commentId != nil { + break + } } if err := c.ghConnector.writeReviewComment(prComment, commentId); err != nil { diff --git a/commenter/connector.go b/commenter/connector.go index 05d8d57..21ac55e 100644 --- a/commenter/connector.go +++ b/commenter/connector.go @@ -81,7 +81,7 @@ func newEnterpriseGithubClient(token, baseUrl, uploadUrl string) (*github.Client client, err := github.NewEnterpriseClient(baseUrl, uploadUrl, tc) if err != nil { - return nil, err + return nil, err } return client, nil @@ -91,16 +91,16 @@ func (c *connector) writeReviewComment(block *github.PullRequestComment, comment ctx := context.Background() if commentId != nil { - if _, err := c.prs.DeleteComment(ctx, c.owner, c.repo, *commentId); err != nil { - return fmt.Errorf("delete existing comment %d: %w", *commentId, err) - } + return writeCommentWithRetries(c.owner, c.repo, c.prNumber, func() (*github.Response, error) { + _, resp, err := c.prs.EditComment(ctx, c.owner, c.repo, *commentId, block) + return resp, err + }) } - writeReviewCommentFn := func() (*github.Response, error) { + return writeCommentWithRetries(c.owner, c.repo, c.prNumber, func() (*github.Response, error) { _, resp, err := c.prs.CreateComment(ctx, c.owner, c.repo, c.prNumber, block) return resp, err - } - return writeCommentWithRetries(c.owner, c.repo, c.prNumber, writeReviewCommentFn) + }) } func (c *connector) writeGeneralComment(comment *github.IssueComment) error {