-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomment.go
70 lines (56 loc) · 1.83 KB
/
comment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package github
import (
"context"
"errors"
"strings"
"github.com/google/go-github/v63/github"
)
var (
ErrCommentNotFound = errors.New("comment not found")
)
// IssueIdentifier represents an issue or PR on GitHub
type IssueIdentifier struct {
Owner string
Repo string
Number int
}
// CommentTraits defines optional traits to filter comments.
// Every trait (if non-empty-string) is matched with an "AND" operator
type CommentTraits struct {
BodyContains string
UserLogin string
}
// FindCommentByTraits searches for a comment in an PR or issue based on specified traits
func (c *Client) FindCommentByTraits(ctx context.Context, issue IssueIdentifier, targetComment CommentTraits) (*github.IssueComment, error) {
comments, _, err := c.client.Issues.ListComments(ctx, issue.Owner, issue.Repo, issue.Number, nil)
if err != nil {
return nil, err
}
for _, c := range comments {
matcher := true
if targetComment.UserLogin != "" {
matcher = matcher && c.User.GetLogin() == targetComment.UserLogin
}
if targetComment.BodyContains != "" {
matcher = matcher && strings.Contains(c.GetBody(), targetComment.BodyContains)
}
if matcher {
return c, nil
}
}
return nil, ErrCommentNotFound
}
// CreateComment creates a new comment on an issue or PR
func (c *Client) CreateComment(ctx context.Context, issue IssueIdentifier, commentBody string) error {
_, _, err := c.client.Issues.CreateComment(ctx, issue.Owner, issue.Repo, issue.Number, &github.IssueComment{
Body: &commentBody,
})
return err
}
// UpdateComment updates an existing comment on an issue or PR
func (c *Client) UpdateComment(ctx context.Context, issue IssueIdentifier, commentId int64, commentBody string) error {
_, _, err := c.client.Issues.EditComment(ctx, issue.Owner, issue.Repo, commentId, &github.IssueComment{
Body: &commentBody,
})
return err
}