Skip to content

Commit

Permalink
remove comment before writing
Browse files Browse the repository at this point in the history
update tests accordingly
  • Loading branch information
owenrumney committed Apr 29, 2021
1 parent 2e7a643 commit a3fb753
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
13 changes: 4 additions & 9 deletions commenter/commenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func (c *Commenter) WriteMultiLineComment(file, comment string, startLine, endLi

prComment := buildComment(file, comment, endLine, *info)
prComment.StartLine = &startLine
fmt.Printf("%#v", prComment)
return c.writeCommentIfRequired(prComment)
}

Expand Down Expand Up @@ -107,27 +106,23 @@ func (c *Commenter) WriteGeneralComment(comment string) error {
return err
}
}

issueComment := &github.IssueComment{
Body: &comment,
}
return c.pr.writeGeneralComment(issueComment)
}

func (c *Commenter) writeCommentIfRequired(prComment *github.PullRequestComment) error {
var commentId *int64
for _, existing := range c.existingComments {
err := func(ec *existingComment) error {
commentId = func(ec *existingComment) *int64 {
if *ec.filename == *prComment.Path && *ec.comment == *prComment.Body {
return newCommentAlreadyWrittenError(*existing.filename, *existing.comment)
return ec.commentId
}
return nil
}(existing)
if err != nil {
return err
}

}
return c.pr.writeReviewComment(prComment)
return c.pr.writeReviewComment(prComment, commentId)
}

func (c *Commenter) getCommitFileInfo() error {
Expand Down
19 changes: 13 additions & 6 deletions commenter/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ type connector struct {
}

type existingComment struct {
filename *string
comment *string
filename *string
comment *string
commentId *int64
}

func createConnector(token, owner, repo string, prNumber int) *connector {
Expand All @@ -36,14 +37,19 @@ func createConnector(token, owner, repo string, prNumber int) *connector {
}
}

func (c *connector) writeReviewComment(block *github.PullRequestComment) error {
func (c *connector) writeReviewComment(block *github.PullRequestComment, commentId *int64) error {
ctx := context.Background()

if commentId != nil {
var _, err = c.prs.DeleteComment(ctx, c.owner, c.repo, *commentId)
if err != nil {
return err
}
}
var _, _, err = c.prs.CreateComment(ctx, c.owner, c.repo, c.prNumber, block)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -83,8 +89,9 @@ func (c *connector) getExistingComments() ([]*existingComment, error) {
var existingComments []*existingComment
for _, comment := range comments {
existingComments = append(existingComments, &existingComment{
filename: comment.Path,
comment: comment.Body,
filename: comment.Path,
comment: comment.Body,
commentId: comment.ID,
})
}
return existingComments, nil
Expand Down
6 changes: 0 additions & 6 deletions test/commenter_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ func (ct *commenterTest) theSingleLineCommentWithDuplicateHasBeenWritten() {
assert.True(ct.t, checkForComment("commitFileInfo.go", "This is going to be duped", 7))
}

func (ct *commenterTest) theErrorIsCommentAlreadyWritten() {
existError := ct.err.(commenter.CommentAlreadyWrittenError)
assert.NotNil(ct.t, existError)
assert.Equal(ct.t, ct.err.Error(), "The file [commitFileInfo.go] already has the comment written [This is going to be duped]")
}

func (ct *commenterTest) theErrorIsCommentIsInvalid() {
existError := ct.err.(commenter.CommentNotValidError)
assert.NotNil(ct.t, existError)
Expand Down
22 changes: 0 additions & 22 deletions test/commenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,6 @@ func Test_add_a_general_comment(t *testing.T) {
then.thereIsNoErrors()
}

func Test_can_add_a_single_line_comment_second_one_has_an_error(t *testing.T) {
given, when, then := newCommenterTest(t)

given.thePullRequest(1).
forOwner("owenrumney").
inRepo("go-github-pr-commenter").
usingTokenFromEnvironment()

when.aNewCommenterIsCreated().
and().aSingleLineCommentIsCreatedWithDuplicate()

then.thereIsNoErrors().
and().theSingleLineCommentWithDuplicateHasBeenWritten()

then.aNewCommenterIsCreated().
and().aSingleLineCommentIsCreatedWithDuplicate()

then.thereIsAnError().
and().theErrorIsCommentAlreadyWritten()

}

func Test_can_add_a_multi_line_comment(t *testing.T) {
given, when, then := newCommenterTest(t)

Expand Down

0 comments on commit a3fb753

Please sign in to comment.