Skip to content

Commit

Permalink
test: add more unit tests for the git comment
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Sep 22, 2023
1 parent 04e31be commit db78191
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
29 changes: 21 additions & 8 deletions pkg/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,18 @@ func (s *StatusMaker) CreateComment(ctx context.Context, message, endMarker stri
}
}

var commentIDs []int //:= -1
for _, comment := range comments {
if strings.HasSuffix(comment.Body, endMarker) {
commentIDs = append(commentIDs, comment.ID)
}
}

commentIDs := getCommentIDs(comments, endMarker)
commentInput := &scm.CommentInput{
Body: fmt.Sprintf("%s\n\n%s", message, endMarker),
}

if len(commentIDs) == 0 {
// not found existing comment, create a new one
_, _, err = scmClient.PullRequests.CreateComment(ctx, s.repo, s.pr, commentInput)
err = wrapError(err, "failed to create comment, repo is %q, pr is %d: %v", s.repo, s.pr)
} else {
_, _, err = scmClient.PullRequests.EditComment(ctx, s.repo, s.pr, commentIDs[0], commentInput)
err = wrapError(err, "failed to edit comment: %v")

// remove the duplicated comments
for i := 1; i < len(commentIDs); i++ {
Expand All @@ -161,8 +157,25 @@ func (s *StatusMaker) CreateComment(ctx context.Context, message, endMarker stri
return
}

func getCommentIDs(comments []*scm.Comment, endMarker string) (commentIDs []int) {
for _, comment := range comments {
// comment := comments[i]
if strings.HasSuffix(comment.Body, endMarker) {
commentIDs = append(commentIDs, comment.ID)
}
}
return
}

func wrapError(err error, msg string, args ...any) error {
if err != nil {
err = fmt.Errorf(msg, append(args, err)...)
}
return err
}

func ignoreError(err error, msg string) error {
if err.Error() == msg {
if err == nil || err.Error() == msg {
return nil
} else {
return err
Expand Down
54 changes: 54 additions & 0 deletions pkg/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"context"
"errors"
"testing"

"github.com/jenkins-x/go-scm/scm"
Expand Down Expand Up @@ -45,3 +46,56 @@ func TestNewStatusMaker(t *testing.T) {
assert.True(t, maker.expirationCheck(&scm.Status{State: scm.StateSuccess},
&scm.StatusInput{State: scm.StateSuccess}))
}

func TestGetCommentIDs(t *testing.T) {
tests := []struct {
comments []*scm.Comment
expect []int
}{{
comments: []*scm.Comment{{
Body: "start",
ID: 1,
}, {
Body: "start - end",
ID: 2,
}, {
Body: "other - end",
ID: 3,
}, {
Body: "other",
ID: 4,
}},
expect: []int{2, 3},
}}
for _, tt := range tests {
assert.Equal(t, tt.expect, getCommentIDs(tt.comments, "end"))
}
}

func TestWrapError(t *testing.T) {
tests := []struct {
err error
msg string
args []any
expect error
}{{
err: nil,
expect: nil,
}, {
err: defaultErr,
msg: "wrap %v",
expect: errors.New("wrap [fake]"),
}}
for _, tt := range tests {
err := wrapError(tt.err, tt.msg, tt.args...)
assert.Equal(t, tt.expect, err)
}
}

func TestIgnoreError(t *testing.T) {
assert.Equal(t, nil, ignoreError(nil, "fake"))
assert.Equal(t, nil, ignoreError(defaultErr, "fake"))
assert.Equal(t, defaultErr, ignoreError(defaultErr, "other"))
}

var defaultErr = errors.New("fake")

0 comments on commit db78191

Please sign in to comment.