-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
101 lines (82 loc) · 2.76 KB
/
utils.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"strconv"
"github.com/google/go-github/github"
)
// Check if an error is a rate limit error
func (p PRMirror) isRatelimit(err error) bool {
if _, ok := err.(*github.RateLimitError); ok {
// TODO: Maybe add some context here
log.Error("The github.com rate limit has been hit")
return true
}
return false
}
// GetRepoEvents returns a list a list of RepoEvents
func (p PRMirror) GetRepoEvents() ([]*github.Event, int64, error) {
var allEvents []*github.Event
var pollInterval = int64(0)
opt := &github.ListOptions{
PerPage: 100,
}
for {
log.Debugf("Getting RepoEvents Page %d\n", opt.Page)
events, resp, err := p.GitHubClient.Activity.ListRepositoryEvents(*p.Context, p.Configuration.UpstreamOwner, p.Configuration.UpstreamRepo, opt)
if err != nil {
log.Errorf("Error while listing repository events. %s", err.Error())
return nil, 60, err
}
allEvents = append(allEvents, events...)
if resp.NextPage == 0 {
pollInterval, err = strconv.ParseInt(resp.Response.Header.Get("X-Poll-Interval"), 10, 64)
if err != nil {
panic(err)
}
break
}
opt.Page = resp.NextPage
}
return allEvents, pollInterval, nil
}
// CreateLabel creates a new label
func (p PRMirror) CreateLabel(labelText string, labelColour string) bool {
label := github.Label{
Name: &labelText,
Color: &labelColour,
}
_, _, err := p.GitHubClient.Issues.CreateLabel(*p.Context, p.Configuration.DownstreamOwner, p.Configuration.DownstreamRepo, &label)
if err != nil {
log.Errorf("Error while creating a label - %s", err.Error())
return false
}
return true
}
// AddLabels adds label/s to a pull request
func (p PRMirror) AddLabels(id int, labels []string) bool {
_, _, err := p.GitHubClient.Issues.AddLabelsToIssue(*p.Context, p.Configuration.DownstreamOwner, p.Configuration.DownstreamRepo, id, labels)
if err != nil {
log.Errorf("Error while adding a label on issue#:%d - %s", id, err.Error())
return false
}
return true
}
// RemoveLabel Removes a label from a pull request
func (p PRMirror) RemoveLabel(id int, labels string) bool {
_, err := p.GitHubClient.Issues.RemoveLabelForIssue(*p.Context, p.Configuration.DownstreamOwner, p.Configuration.DownstreamRepo, id, labels)
if err != nil {
log.Errorf("Error while removing a label on issue#:%d - %s", id, err.Error())
return false
}
return true
}
// AddComment adds a comment to a pull request
func (p PRMirror) AddComment(id int, comment string) bool {
issueComment := github.IssueComment{}
issueComment.Body = &comment
_, _, err := p.GitHubClient.Issues.CreateComment(*p.Context, p.Configuration.DownstreamOwner, p.Configuration.DownstreamRepo, id, &issueComment)
if err != nil {
log.Errorf("Error while adding a comment to issue#:%d - %s", id, err.Error())
return false
}
return true
}