forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathgitlab.go
234 lines (206 loc) · 5.95 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
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
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package gitlab implements a GitLab client.
package gitlab
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/jenkins-x/go-scm/scm"
"github.com/shurcooL/graphql"
)
// NewWebHookService creates a new instance of the webhook service without the rest of the client
func NewWebHookService() scm.WebhookService {
return &webhookService{nil, nil}
}
// New returns a new GitLab API client.
func New(uri string) (*scm.Client, error) {
base, err := url.Parse(uri)
if err != nil {
return nil, err
}
if !strings.HasSuffix(base.Path, "/") {
base.Path += "/"
}
client := &wrapper{new(scm.Client)}
client.BaseURL = base
// initialize services
client.Driver = scm.DriverGitlab
client.Contents = &contentService{client}
client.Git = &gitService{client}
client.Issues = &issueService{client}
client.Releases = &releaseService{client}
client.Milestones = &milestoneService{client}
client.Organizations = &organizationService{client}
client.PullRequests = &pullService{client}
client.Repositories = &repositoryService{client}
client.Reviews = &reviewService{client}
client.Commits = &commitService{client}
// add the user service to the webhook service so it can be used for fetching users
us := &userService{client}
client.Users = us
client.Webhooks = &webhookService{
client: client,
userService: us,
}
graphqlEndpoint := scm.URLJoin(uri, "/api/graphql")
client.GraphQLURL, err = url.Parse(graphqlEndpoint)
if err != nil {
return nil, err
}
client.GraphQL = &dynamicGraphQLClient{client, graphqlEndpoint}
return client.Client, nil
}
type dynamicGraphQLClient struct {
wrapper *wrapper
graphqlEndpoint string
}
func (d *dynamicGraphQLClient) Query(ctx context.Context, q interface{}, vars map[string]interface{}) error {
httpClient := d.wrapper.Client.Client
if httpClient != nil {
transport := httpClient.Transport
if transport != nil {
query := graphql.NewClient(
d.graphqlEndpoint,
&http.Client{
Transport: transport,
})
return query.Query(ctx, q, vars)
}
}
fmt.Println("WARNING: no http transport configured for GraphQL and Gitlab")
return nil
}
// NewDefault returns a new GitLab API client using the
// default gitlab.com address.
func NewDefault() *scm.Client {
client, _ := New("https://gitlab.com")
return client
}
// wraper wraps the Client to provide high level helper functions
// for making http requests and unmarshaling the response.
type wrapper struct {
*scm.Client
}
type gitlabNamespace struct {
ID int `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Kind string `json:"kind"`
FullPath string `json:"full_path"`
ParentID int `json:"parent_id"`
MembersCountWithDescendants int `json:"members_count_with_descendants"`
}
// findNamespaceByName will look up the namespace for the given name
func (c *wrapper) findNamespaceByName(ctx context.Context, name string) (*gitlabNamespace, error) {
in := url.Values{}
in.Set("search", name)
path := fmt.Sprintf("api/v4/namespaces?%s", in.Encode())
var out []*gitlabNamespace
_, err := c.do(ctx, "GET", path, nil, &out)
if len(out) > 0 {
return out[0], err
}
return nil, err
}
// do wraps the Client.Do function by creating the Request and
// unmarshalling the response.
func (c *wrapper) do(ctx context.Context, method, path string, in, out interface{}) (*scm.Response, error) {
req := &scm.Request{
Method: method,
Path: path,
}
// if we are posting or putting data, we need to
// write it to the body of the request.
if in != nil {
buf := new(bytes.Buffer)
err := json.NewEncoder(buf).Encode(in) // #nosec
if err != nil {
return nil, err
}
if req.Header == nil {
req.Header = map[string][]string{}
}
req.Header.Set("Content-Type", "application/json")
req.Body = buf
}
// execute the http request
res, err := c.Client.Do(ctx, req)
if err != nil {
return nil, err
}
defer res.Body.Close()
// parse the gitlab request id.
res.ID = res.Header.Get("X-Request-Id")
// parse the gitlab rate limit details.
res.Rate.Limit, _ = strconv.Atoi(
res.Header.Get("RateLimit-Limit"),
)
res.Rate.Remaining, _ = strconv.Atoi(
res.Header.Get("RateLimit-Remaining"),
)
res.Rate.Reset, _ = strconv.ParseInt(
res.Header.Get("RateLimit-Reset"), 10, 64,
)
// snapshot the request rate limit
c.Client.SetRate(res.Rate)
// if an error is encountered, unmarshal and return the
// error response.
if res.Status > 300 {
return res, errors.New(
http.StatusText(res.Status),
)
}
if out == nil {
return res, nil
}
// if a json response is expected, parse and return
// the json response.
return res, json.NewDecoder(res.Body).Decode(out)
}
// Error represents a GitLab error.
type Error struct {
Message string `json:"message"`
}
func (e *Error) Error() string {
return e.Message
}
type updateNoteOptions struct {
Body string `json:"body"`
}
type labelEvent struct {
ID int `json:"id"`
Action string `json:"action"`
CreatedAt *time.Time `json:"created_at"`
ResourceType string `json:"resource_type"`
ResourceID int `json:"resource_id"`
User user `json:"user"`
Label label `json:"label"`
}
func convertLabelEvents(src []*labelEvent) []*scm.ListedIssueEvent {
var answer []*scm.ListedIssueEvent
for _, from := range src {
answer = append(answer, convertLabelEvent(from))
}
return answer
}
func convertLabelEvent(from *labelEvent) *scm.ListedIssueEvent {
event := "labeled"
if from.Action == "remove" {
event = "unlabeled"
}
return &scm.ListedIssueEvent{
Event: event,
Actor: *convertUser(&from.User),
Label: *convertLabel(&from.Label),
Created: *from.CreatedAt,
}
}