0
0
mirror of https://github.com/thegeeklab/wp-github-comment.git synced 2024-06-02 18:39:39 +02:00

simplify AddComment

This commit is contained in:
Robert Kaussow 2024-05-07 21:03:35 +02:00
parent 560020ac19
commit c3ac04e908
Signed by: xoxys
GPG Key ID: 4E692A2EAECC03C0
3 changed files with 47 additions and 39 deletions

View File

@ -2,12 +2,15 @@ package plugin
import (
"context"
"errors"
"fmt"
"strings"
"github.com/google/go-github/v61/github"
)
var ErrCommentNotFound = errors.New("no comment found")
type GithubClient struct {
Client *github.Client
Issue *GithubIssue
@ -35,38 +38,30 @@ func NewGithubClient(client *github.Client) *GithubClient {
// If the Update field is true, it will append a unique identifier to the comment
// body and attempt to find and update the existing comment with that identifier.
// Otherwise, it will create a new comment on the issue.
func (i *GithubIssue) AddComment(ctx context.Context) error {
var (
err error
comment *github.IssueComment
resp *github.Response
)
func (i *GithubIssue) AddComment(ctx context.Context) (*github.IssueComment, error) {
issueComment := &github.IssueComment{
Body: &i.Message,
}
if i.Update {
// Append plugin comment ID to comment message so we can search for it later
message := fmt.Sprintf("%s\n<!-- id: %s -->\n", i.Message, i.Key)
issueComment.Body = &message
*issueComment.Body = fmt.Sprintf("%s\n<!-- id: %s -->\n", i.Message, i.Key)
comment, err = i.FindComment(ctx)
comment, err := i.FindComment(ctx)
if err != nil && !errors.Is(err, ErrCommentNotFound) {
return comment, err
}
if err == nil && comment != nil {
_, resp, err = i.Client.Issues.EditComment(ctx, i.Owner, i.Repo, *comment.ID, issueComment)
if comment != nil {
comment, _, err = i.Client.Issues.EditComment(ctx, i.Owner, i.Repo, *comment.ID, issueComment)
return comment, err
}
}
if err == nil && resp == nil {
_, _, err = i.Client.Issues.CreateComment(ctx, i.Owner, i.Repo, i.Number, issueComment)
}
comment, _, err := i.Client.Issues.CreateComment(ctx, i.Owner, i.Repo, i.Number, issueComment)
if err != nil {
return err
}
return nil
return comment, err
}
// FindComment returns the GitHub issue comment that contains the specified key, or nil if no such comment exists.
@ -97,6 +92,5 @@ func (i *GithubIssue) FindComment(ctx context.Context) (*github.IssueComment, er
}
}
//nolint:nilnil
return nil, nil
return nil, fmt.Errorf("%w key: %s", ErrCommentNotFound, i.Key)
}

View File

@ -2,6 +2,7 @@ package plugin
import (
"context"
"fmt"
"net/http"
"testing"
@ -22,10 +23,12 @@ func TestGithubIssue_FindComment(t *testing.T) {
name string
comments []*github.IssueComment
want *github.IssueComment
wantErr error
}{
{
name: "no comments",
want: nil,
name: "no comments",
want: nil,
wantErr: ErrCommentNotFound,
},
{
name: "comment found",
@ -39,7 +42,8 @@ func TestGithubIssue_FindComment(t *testing.T) {
comments: []*github.IssueComment{
{Body: github.String("other comment")},
},
want: nil,
want: nil,
wantErr: ErrCommentNotFound,
},
{
name: "multiple comments",
@ -71,6 +75,12 @@ func TestGithubIssue_FindComment(t *testing.T) {
}
got, err := issue.FindComment(ctx)
if tt.wantErr != nil {
assert.Error(t, err)
assert.Equal(t, tt.want, got)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
@ -92,11 +102,13 @@ func TestGithubIssue_AddComment(t *testing.T) {
update bool
existingKey string
comments []*github.IssueComment
want *github.IssueComment
wantErr bool
}{
{
name: "create new comment",
update: false,
want: &github.IssueComment{Body: github.String("test message\n<!-- id: test-key -->\n")},
},
{
name: "update existing comment",
@ -104,10 +116,12 @@ func TestGithubIssue_AddComment(t *testing.T) {
comments: []*github.IssueComment{
{ID: github.Int64(123), Body: github.String(keyPattern)},
},
want: &github.IssueComment{Body: github.String("test message\n<!-- id: test-key -->\n")},
},
{
name: "update non-existing comment",
update: true,
want: &github.IssueComment{Body: github.String("test message\n<!-- id: test-key -->\n")},
},
{
name: "create new comment with error",
@ -128,7 +142,11 @@ func TestGithubIssue_AddComment(t *testing.T) {
if tt.wantErr {
mock.WriteError(w, http.StatusInternalServerError, "internal server error")
} else {
_, _ = w.Write(mock.MustMarshal(tt.comments))
_, _ = w.Write(mock.MustMarshal(
&github.IssueComment{
Body: github.String(fmt.Sprintf("%s\n%s\n", message, keyPattern)),
},
))
}
}),
),
@ -138,13 +156,11 @@ func TestGithubIssue_AddComment(t *testing.T) {
if tt.wantErr {
mock.WriteError(w, http.StatusInternalServerError, "internal server error")
} else {
patchResp := &github.IssueComment{}
if len(tt.comments) > 0 {
patchResp = tt.comments[0]
}
_, _ = w.Write(mock.MustMarshal(patchResp))
_, _ = w.Write(mock.MustMarshal(
&github.IssueComment{
Body: github.String(fmt.Sprintf("%s\n%s\n", message, keyPattern)),
},
))
}
}),
),
@ -161,18 +177,16 @@ func TestGithubIssue_AddComment(t *testing.T) {
Update: tt.update,
}
if tt.wantErr {
issue.Repo = "999"
}
err := issue.AddComment(ctx)
got, err := issue.AddComment(ctx)
if tt.wantErr {
assert.Error(t, err)
assert.Equal(t, tt.want, got)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}

View File

@ -92,7 +92,7 @@ func (p *Plugin) Execute() error {
return nil
}
err := client.Issue.AddComment(p.Network.Context)
_, err := client.Issue.AddComment(p.Network.Context)
if err != nil {
return fmt.Errorf("failed to create or update comment: %w", err)
}