2020-09-19 22:00:34 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-12 13:17:29 +00:00
|
|
|
"github.com/google/go-github/v39/github"
|
2020-09-19 22:00:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2022-05-29 20:18:37 +00:00
|
|
|
return err
|
2020-09-19 22:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-25 10:53:45 +00:00
|
|
|
allComments = append(allComments, comments...)
|
2020-09-19 22:00:34 +00:00
|
|
|
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
|
|
|
|
}
|