-
Notifications
You must be signed in to change notification settings - Fork 42
/
gitlab.go
121 lines (103 loc) · 2.7 KB
/
gitlab.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package pullrequest
import (
"regexp"
"strings"
"text/template"
"github.com/innogames/slack-bot/bot"
"github.com/innogames/slack-bot/bot/config"
"github.com/innogames/slack-bot/bot/matcher"
"github.com/xanzy/go-gitlab"
)
type gitlabFetcher struct {
client *gitlab.Client
}
func newGitlabCommand(base bot.BaseCommand, cfg *config.Config) bot.Command {
if cfg.Gitlab.AccessToken == "" && cfg.Gitlab.Host == "" {
return nil
}
options := gitlab.WithBaseURL(cfg.Gitlab.Host)
gitlabClient, err := gitlab.NewClient(cfg.Gitlab.AccessToken, options)
if err != nil {
return nil
}
return command{
base,
cfg.PullRequest,
&gitlabFetcher{gitlabClient},
"(?s).*" + regexp.QuoteMeta(cfg.Gitlab.Host) + "/(?P<repo>.+/.+)/merge_requests/(?P<number>\\d+).*",
}
}
func (c *gitlabFetcher) getPullRequest(match matcher.Result) (pullRequest, error) {
var pr pullRequest
repo := match.GetString("repo")
repo = strings.TrimSuffix(repo, "/-")
prNumber := match.GetInt("number")
rawPullRequest, resp, err := c.client.MergeRequests.GetMergeRequest(
repo,
prNumber,
&gitlab.GetMergeRequestsOptions{},
)
if err != nil {
return pr, err
}
resp.Body.Close()
// todo add buildStatus!
pr = pullRequest{
Name: rawPullRequest.Title,
Approvers: c.getApprovers(rawPullRequest, prNumber),
Status: c.getStatus(rawPullRequest),
}
return pr, nil
}
func (c *gitlabFetcher) getStatus(pr *gitlab.MergeRequest) prStatus {
// https://docs.gitlab.com/ce/api/merge_requests.html
switch pr.State {
case "merged":
return prStatusMerged
case "closed", "locked":
return prStatusClosed
default:
return prStatusOpen
}
}
func (c *gitlabFetcher) getApprovers(rawPullRequest *gitlab.MergeRequest, prNumber int) []string {
approvers := make([]string, 0)
if rawPullRequest.Upvotes > 0 {
emojis, resp, _ := c.client.AwardEmoji.ListMergeRequestAwardEmoji(
rawPullRequest.SourceProjectID,
prNumber,
&gitlab.ListAwardEmojiOptions{},
)
if resp != nil {
resp.Body.Close()
}
for _, emoji := range emojis {
if emoji.Name == "thumbsup" {
approvers = append(approvers, emoji.User.Username)
}
}
}
return approvers
}
func (c *gitlabFetcher) GetTemplateFunction() template.FuncMap {
return template.FuncMap{
"gitlabPullRequest": func(repo string, number string) (pullRequest, error) {
return c.getPullRequest(matcher.MapResult{
"repo": repo,
"number": number,
})
},
}
}
func (c *gitlabFetcher) getHelp() []bot.Help {
return []bot.Help{
{
Command: "gitlab pull request",
Category: category,
Description: "tracks the state of gitlab pull requests",
Examples: []string{
"https://gitlab.example.com/home-assistant/home-assistant/merge_requests/13958",
},
},
}
}