drone-github-comment/plugin/comment.go
renovate[bot] 2846f56930
fix(deps): update module github.com/google/go-github/v37 to v38 (#62)
Co-authored-by: Renovate Bot <bot@renovateapp.com>
2021-08-11 20:40:58 +02:00

82 lines
1.7 KiB
Go

package plugin
import (
"context"
"fmt"
"strings"
"github.com/google/go-github/v38/github"
)
// Release holds ties the drone env data and github client together.
type commentClient struct {
Message string
Update bool
Key string
Repo string
Owner string
IssueNum int
*github.Client
context.Context
}
func (cc *commentClient) issueComment() error {
var err error
var comment *github.IssueComment
var resp *github.Response
ic := &github.IssueComment{
Body: &cc.Message,
}
if cc.Update {
// Append plugin comment ID to comment message so we can search for it later
message := fmt.Sprintf("%s\n<!-- id: %s -->\n", cc.Message, cc.Key)
ic.Body = &message
comment, err = cc.comment()
if err == nil && comment != nil {
_, resp, err = cc.Client.Issues.EditComment(cc.Context, cc.Owner, cc.Repo, int64(*comment.ID), ic)
}
}
if err == nil && resp == nil {
_, _, err = cc.Client.Issues.CreateComment(cc.Context, cc.Owner, cc.Repo, cc.IssueNum, ic)
}
if err != nil {
return fmt.Errorf("failed to add or update comment: %w", err)
}
return nil
}
func (cc *commentClient) comment() (*github.IssueComment, error) {
var allComments []*github.IssueComment
opts := &github.IssueListCommentsOptions{}
for {
comments, resp, err := cc.Client.Issues.ListComments(cc.Context, cc.Owner, cc.Repo, cc.IssueNum, opts)
if err != nil {
return nil, err
}
allComments = append(comments, comments...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for _, comment := range allComments {
if strings.Contains(*comment.Body, fmt.Sprintf("<!-- id: %s -->", cc.Key)) {
return comment, nil
}
}
return nil, nil
}