-
Notifications
You must be signed in to change notification settings - Fork 168
/
gitlabv3.go
302 lines (253 loc) · 8.36 KB
/
gitlabv3.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
Copyright 2017 caicloud authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gitlab
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/caicloud/nirvana/log"
v3 "github.com/xanzy/go-gitlab"
c_v1alpha1 "github.com/caicloud/cyclone/pkg/apis/cyclone/v1alpha1"
"github.com/caicloud/cyclone/pkg/server/apis/v1alpha1"
"github.com/caicloud/cyclone/pkg/server/biz/scm"
"github.com/caicloud/cyclone/pkg/util/cerr"
)
// V3 represents the SCM provider of API V3 GitLab.
type V3 struct {
scmCfg *v1alpha1.SCMSource
client *v3.Client
}
// GetToken gets the token by the username and password of SCM config.
func (g *V3) GetToken() (string, error) {
return getOauthToken(g.scmCfg)
}
// CheckToken checks whether the token has the authority of repo by trying ListRepos with the token.
func (g *V3) CheckToken() error {
if _, err := g.listReposInner(false); err != nil {
return err
}
return nil
}
// ListRepos lists the repos by the SCM config.
func (g *V3) ListRepos() ([]scm.Repository, error) {
return g.listReposInner(true)
}
// listReposInner lists the projects by the SCM config,
// list all projects while the parameter 'listAll' is true,
// otherwise, list projects by default 'ListPerPageOpt' number.
func (g *V3) listReposInner(listAll bool) ([]scm.Repository, error) {
opt := &v3.ListProjectsOptions{
ListOptions: v3.ListOptions{
PerPage: scm.ListPerPageOpt,
},
}
// Get all pages of results.
var allProjects []*v3.Project
for {
projects, resp, err := g.client.Projects.ListProjects(opt)
if err != nil {
return nil, convertGitlabV3Error(err, resp)
}
allProjects = append(allProjects, projects...)
if resp.NextPage == 0 || !listAll {
break
}
opt.ListOptions.Page = resp.NextPage
}
repos := make([]scm.Repository, len(allProjects))
for i, repo := range allProjects {
repos[i].Name = repo.PathWithNamespace
repos[i].URL = repo.HTTPURLToRepo
}
return repos, nil
}
// ListBranches lists the branches for specified repo.
func (g *V3) ListBranches(repo string) ([]string, error) {
branches, resp, err := g.client.Branches.ListBranches(repo, nil)
if err != nil {
log.Errorf("Fail to list branches for %s", repo)
return nil, convertGitlabV3Error(err, resp)
}
branchNames := make([]string, len(branches))
for i, branch := range branches {
branchNames[i] = branch.Name
}
return branchNames, nil
}
// ListTags lists the tags for specified repo.
func (g *V3) ListTags(repo string) ([]string, error) {
tags, resp, err := g.client.Tags.ListTags(repo, nil)
if err != nil {
log.Errorf("Fail to list tags for %s", repo)
return nil, convertGitlabV3Error(err, resp)
}
tagNames := make([]string, len(tags))
for i, tag := range tags {
tagNames[i] = tag.Name
}
return tagNames, nil
}
// ListDockerfiles lists the Dockerfiles for specified repo.
func (g *V3) ListDockerfiles(repo string) ([]string, error) {
// List Dockerfiles in a project with gitlab v3 api is very inefficient.
// There is not a proper api can be used to do this with GitLab v3.
//
// FYI:
// https://stackoverflow.com/questions/25127695/search-filenames-with-gitlab-api
return nil, fmt.Errorf("list gitlab v3 dockerfiles not implemented")
}
// CreateStatus generate a new status for repository.
func (g *V3) CreateStatus(status c_v1alpha1.StatusPhase, targetURL, repoURL, commitSha string) error {
state, description := transStatus(status)
owner, project := scm.ParseRepo(repoURL)
context := "continuous-integration/cyclone"
opt := &v3.SetCommitStatusOptions{
State: *v3.BuildState(v3.BuildStateValue(state)),
Description: &description,
TargetURL: &targetURL,
Context: &context,
}
_, resp, err := g.client.Commits.SetCommitStatus(owner+"/"+project, commitSha, opt)
return convertGitlabV3Error(err, resp)
}
// GetPullRequestSHA gets latest commit SHA of pull request.
func (g *V3) GetPullRequestSHA(repoURL string, number int) (string, error) {
owner, name := scm.ParseRepo(repoURL)
path := fmt.Sprintf("%s/api/%s/projects/%s/merge_requests?iid=%d",
strings.TrimSuffix(g.scmCfg.Server, "/"), v3APIVersion, url.QueryEscape(owner+"/"+name), number)
req, err := http.NewRequest(http.MethodGet, path, nil)
if err != nil {
return "", err
}
if len(g.scmCfg.User) == 0 {
req.Header.Set("PRIVATE-TOKEN", g.scmCfg.Token)
} else {
req.Header.Set("Authorization", "Bearer "+g.scmCfg.Token)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorf("Fail to get project merge request as %s", err.Error())
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("Fail to get project merge request as %s", err.Error())
return "", err
}
if resp.StatusCode/100 == 2 {
mr := []mergeRequestResponse{}
err := json.Unmarshal(body, &mr)
if err != nil {
return "", err
}
if len(mr) > 0 {
return mr[0].SHA, nil
}
return "", fmt.Errorf("Merge request %d not found ", number)
}
err = fmt.Errorf("Fail to get merge request %d as %s ", number, body)
return "", err
}
// mergeRequestResponse represents the response of Gitlab merge request API.
type mergeRequestResponse struct {
ID int `json:"id"`
IID int `json:"iid"`
TargetBranch string `json:"target_branch"`
SHA string `json:"sha"`
}
// GetWebhook gets webhook from specified repo.
func (g *V3) GetWebhook(repo string, webhookURL string) (*v3.ProjectHook, error) {
owner, name := scm.ParseRepo(repo)
hooks, resp, err := g.client.Projects.ListProjectHooks(owner+"/"+name, nil)
if err != nil {
return nil, convertGitlabV3Error(err, resp)
}
for _, hook := range hooks {
if hook.URL == webhookURL {
return hook, nil
}
}
return nil, cerr.ErrorContentNotFound.Error(fmt.Sprintf("webhook url %s", webhookURL))
}
// CreateWebhook creates webhook for specified repo.
func (g *V3) CreateWebhook(repo string, webhook *scm.Webhook) error {
if webhook == nil || len(webhook.URL) == 0 || len(webhook.Events) == 0 {
return fmt.Errorf("The webhook %v is not correct", webhook)
}
_, err := g.GetWebhook(repo, webhook.URL)
if err != nil {
if !cerr.ErrorContentNotFound.Derived(err) {
return err
}
onwer, name := scm.ParseRepo(repo)
hook := generateV3ProjectHook(webhook)
_, resp, err := g.client.Projects.AddProjectHook(onwer+"/"+name, hook)
if err != nil {
return convertGitlabV3Error(err, resp)
}
return nil
}
log.Warningf("Webhook already existed: %+v", webhook)
return err
}
// DeleteWebhook deletes webhook from specified repo.
func (g *V3) DeleteWebhook(repo string, webhookURL string) error {
hook, err := g.GetWebhook(repo, webhookURL)
if err != nil {
return err
}
owner, name := scm.ParseRepo(repo)
if resp, err := g.client.Projects.DeleteProjectHook(owner+"/"+name, hook.ID); err != nil {
log.Errorf("delete project hook %s for %s/%s error: %v", hook.ID, owner, name, err)
return convertGitlabV3Error(err, resp)
}
return nil
}
func generateV3ProjectHook(webhook *scm.Webhook) *v3.AddProjectHookOptions {
enableState, disableState := true, false
// Push event is enable for Gitlab webhook in default, so need to remove this default option.
hook := &v3.AddProjectHookOptions{
PushEvents: &disableState,
}
for _, e := range webhook.Events {
switch e {
case scm.PullRequestEventType:
hook.MergeRequestsEvents = &enableState
case scm.PullRequestCommentEventType:
hook.NoteEvents = &enableState
case scm.PushEventType:
hook.PushEvents = &enableState
case scm.TagReleaseEventType:
hook.TagPushEvents = &enableState
default:
log.Errorf("The event type %s is not supported, will be ignored", e)
return nil
}
}
hook.URL = &webhook.URL
return hook
}
func convertGitlabV3Error(err error, resp *v3.Response) error {
if err == nil {
return err
}
if resp != nil && resp.StatusCode == http.StatusInternalServerError {
return cerr.ErrorSCMServerInternalError.Error(err)
}
return err
}