forked from mercari/tfnotify
/
comment.go
86 lines (75 loc) · 2.07 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package github
import (
"context"
"fmt"
"regexp"
"github.com/google/go-github/github"
)
// CommentService handles communication with the comment related
// methods of GitHub API
type CommentService service
// PostOptions specifies the optional parameters to post comments to a pull request
type PostOptions struct {
Number int
Revision string
}
// Post posts comment
func (g *CommentService) Post(body string, opt PostOptions) error {
if opt.Number != 0 {
_, _, err := g.client.API.IssuesCreateComment(
context.Background(),
opt.Number,
&github.IssueComment{Body: &body},
)
return err
}
if opt.Revision != "" {
_, _, err := g.client.API.RepositoriesCreateComment(
context.Background(),
opt.Revision,
&github.RepositoryComment{Body: &body},
)
return err
}
return fmt.Errorf("github.comment.post: Number or Revision is required")
}
// List lists comments on GitHub issues/pull requests
func (g *CommentService) List(number int) ([]*github.IssueComment, error) {
comments, _, err := g.client.API.IssuesListComments(
context.Background(),
number,
&github.IssueListCommentsOptions{},
)
return comments, err
}
// Delete deletes comment on GitHub issues/pull requests
func (g *CommentService) Delete(id int) error {
_, err := g.client.API.IssuesDeleteComment(
context.Background(),
int64(id),
)
return err
}
// DeleteDuplicates deletes duplicate comments containing arbitrary character strings
func (g *CommentService) DeleteDuplicates(title string) {
var ids []int64
comments := g.getDuplicates(title)
for _, comment := range comments {
ids = append(ids, *comment.ID)
}
for _, id := range ids {
// don't handle error
g.client.Comment.Delete(int(id))
}
}
func (g *CommentService) getDuplicates(title string) []*github.IssueComment {
var dup []*github.IssueComment
re := regexp.MustCompile(`(?m)^(\n+)?` + title + `\n+` + g.client.Config.PR.Message + `\n+`)
comments, _ := g.client.Comment.List(g.client.Config.PR.Number)
for _, comment := range comments {
if re.MatchString(*comment.Body) {
dup = append(dup, comment)
}
}
return dup
}