-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomment.go
69 lines (55 loc) · 1.54 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
package github
import (
"context"
"errors"
"strings"
"github.com/google/go-github/v63/github"
)
var (
ErrCommentNotFound = errors.New("comment not found")
)
type IssueIdentifier struct {
Owner string
Repo string
Number int
}
// every trait is treated as AND
type CommentTraits struct {
BodyContains *string
UserLogin *string
}
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 != nil {
matcher = matcher &&
c.User != nil && c.User.Login != nil &&
*c.User.Login == *targetComment.UserLogin
}
if targetComment.BodyContains != nil {
matcher = matcher &&
c.Body != nil &&
strings.Contains(*c.Body, *targetComment.BodyContains)
}
if matcher {
return c, nil
}
}
return nil, ErrCommentNotFound
}
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
}
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
}