-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitlab.go
169 lines (134 loc) · 4.45 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import "github.com/xanzy/go-gitlab"
//go:generate mockery --name GitLabClient
type GitLabClient interface {
ListGroupProjects(groupID int, options *gitlab.ListGroupProjectsOptions) ([]*gitlab.Project, *gitlab.Response, error)
ListSubGroups(groupID int, opt *gitlab.ListSubGroupsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Group, *gitlab.Response, error)
ListProjectMergeRequests(projectID int, options *gitlab.ListProjectMergeRequestsOptions) ([]*gitlab.MergeRequest, *gitlab.Response, error)
GetMergeRequestApprovalsConfiguration(projectID int, mergeRequestID int) (*gitlab.MergeRequestApprovals, *gitlab.Response, error)
}
type MergeRequestWithApprovals struct {
MergeRequest *gitlab.MergeRequest
ApprovedBy []string
}
type gitLabClient struct {
client *gitlab.Client
}
func (c *gitLabClient) ListGroupProjects(groupID int, options *gitlab.ListGroupProjectsOptions) ([]*gitlab.Project, *gitlab.Response, error) {
return c.client.Groups.ListGroupProjects(groupID, options)
}
func (c *gitLabClient) ListSubGroups(groupID int, opt *gitlab.ListSubGroupsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Group, *gitlab.Response, error) {
return c.client.Groups.ListSubGroups(groupID, opt, options...)
}
func (c *gitLabClient) ListProjectMergeRequests(projectID int, options *gitlab.ListProjectMergeRequestsOptions) ([]*gitlab.MergeRequest, *gitlab.Response, error) {
return c.client.MergeRequests.ListProjectMergeRequests(projectID, options)
}
func (c *gitLabClient) GetMergeRequestApprovalsConfiguration(projectID int, mergeRequestID int) (*gitlab.MergeRequestApprovals, *gitlab.Response, error) {
return c.client.MergeRequestApprovals.GetConfiguration(projectID, mergeRequestID)
}
func fetchOpenedMergeRequests(config *Config, client GitLabClient) ([]*MergeRequestWithApprovals, error) {
var groupIDs []int
for _, group := range config.Groups {
groupIDs = append(groupIDs, group.ID)
// Add subgroups to the groups list.
subgroupIDs, err := fetchSubGroups(group.ID, client)
if err != nil {
return nil, err
}
groupIDs = append(groupIDs, subgroupIDs...)
}
// Add projects from groups to the projects list.
projectIDs, err := fetchProjectsFromGroups(groupIDs, client)
if err != nil {
return nil, err
}
for _, project := range config.Projects {
projectIDs = append(projectIDs, project.ID)
}
var allMRs []*MergeRequestWithApprovals
for _, projectID := range projectIDs {
options := &gitlab.ListProjectMergeRequestsOptions{
State: gitlab.String("opened"),
OrderBy: gitlab.String("updated_at"),
Sort: gitlab.String("desc"),
WIP: gitlab.String("no"),
ListOptions: gitlab.ListOptions{
PerPage: 50,
Page: 1,
},
}
for {
mrs, resp, err := client.ListProjectMergeRequests(projectID, options)
if err != nil {
return nil, err
}
for _, mr := range mrs {
approvals, _, err := client.GetMergeRequestApprovalsConfiguration(projectID, mr.IID)
if err != nil {
return nil, err
}
approvedBy := make([]string, len(approvals.ApprovedBy))
for i, approver := range approvals.ApprovedBy {
approvedBy[i] = approver.User.Name
}
allMRs = append(allMRs, &MergeRequestWithApprovals{
MergeRequest: mr,
ApprovedBy: approvedBy,
})
}
if resp.CurrentPage >= resp.TotalPages {
break
}
options.Page = resp.NextPage
}
}
return allMRs, nil
}
func fetchProjectsFromGroups(groupIDs []int, client GitLabClient) ([]int, error) {
var projectIDs []int
for _, groupID := range groupIDs {
options := &gitlab.ListGroupProjectsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 50,
Page: 1,
},
}
for {
projects, resp, err := client.ListGroupProjects(groupID, options)
if err != nil {
return nil, err
}
for _, project := range projects {
projectIDs = append(projectIDs, project.ID)
}
if resp.CurrentPage >= resp.TotalPages {
break
}
options.Page = resp.NextPage
}
}
return projectIDs, nil
}
func fetchSubGroups(groupID int, client GitLabClient) ([]int, error) {
var groupIDs []int
options := &gitlab.ListSubGroupsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 50,
Page: 1,
},
}
for {
groups, resp, err := client.ListSubGroups(groupID, options)
if err != nil {
return nil, err
}
for _, group := range groups {
groupIDs = append(groupIDs, group.ID)
}
if resp.CurrentPage >= resp.TotalPages {
break
}
options.Page = resp.NextPage
}
return groupIDs, nil
}