forked from diggerhq/gitlab-webhook-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
361 lines (298 loc) · 11.4 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/xanzy/go-gitlab"
"log"
"os"
"strconv"
"strings"
)
func ConvertMapKeysToLowerCase(m map[string]string) map[string]string {
result := make(map[string]string)
for k, v := range m {
lowerCaseKey := strings.ToLower(k)
result[lowerCaseKey] = v
//fmt.Printf("old key: %s, new key: %s \n", k, lowerCaseKey)
}
return result
}
func ParseWebHookJSON(secret string, lambdaRequest events.LambdaFunctionURLRequest) error {
headers := ConvertMapKeysToLowerCase(lambdaRequest.Headers)
// If we have a secret set, we should check if the request matches it.
if len(secret) > 0 {
//fmt.Printf("lambda token: %s, request token: %s \n", secret, headers["x-gitlab-token"])
signature := headers["x-gitlab-token"]
if signature != secret {
return errors.New("token validation failed")
}
}
gitlabEvent := headers["x-gitlab-event"]
if strings.TrimSpace(gitlabEvent) == "" {
return errors.New("missing X-Gitlab-Event Header")
}
fmt.Printf("gitlabEvent: %s\n", gitlabEvent)
gitLabClients, err := CreateGitLabClient()
if err != nil {
return fmt.Errorf("failed to create GitLab client, %v\n", err)
}
eventType := gitlab.EventType(gitlabEvent)
payload := lambdaRequest.Body
if len(payload) == 0 {
return errors.New("request body is empty")
}
payloadBytes := []byte(payload)
result, err := gitlab.ParseWebhook(eventType, payloadBytes)
if err != nil {
return fmt.Errorf("failed to parse webhook event: %w\n", err)
}
switch eventType {
case gitlab.EventTypeMergeRequest:
event := result.(*gitlab.MergeEvent)
projectId := event.Project.ID
branchName := event.ObjectAttributes.SourceBranch
mergeRequestIID := event.ObjectAttributes.IID
mergeRequestID := event.ObjectAttributes.ID
currentUser, _, err := gitLabClients[projectId].Users.CurrentUser()
if err != nil {
return fmt.Errorf("failed to get current GitLab user info, projectId: %v, %v", projectId, err)
}
fmt.Printf("Current GitLab user id: %d\n", currentUser.ID)
//check if Merge Request mergeable
isMergeable, err := IsMergeable(gitLabClients[projectId], projectId, mergeRequestIID)
if err != nil {
return fmt.Errorf("failed to parse webhook event: %w\n", err)
}
fmt.Printf("event action: %s\n", event.ObjectAttributes.Action)
fmt.Printf("event ObjectKind: %s\n", event.ObjectKind)
fmt.Printf("branchName: %s\n", branchName)
// check if branch exist
branchExists, err := checkIfBranchExist(gitLabClients[projectId], projectId, branchName)
if err != nil {
return err
}
// if branch doesn't exist, digger will not be able to do anything, so we can log an error as a comment to pull request
if !branchExists && event.ObjectAttributes.Action != "merge" {
fmt.Printf("Specified branch: %s doesn't exist. eventType: %s \n", branchName, eventType)
err = PublishComment(projectId, mergeRequestIID, fmt.Sprintf("Failed to trigger pipeline. Specified branch: %s doesn't exist.", branchName))
if err != nil {
return err
}
}
var eventType string
switch event.ObjectAttributes.Action {
case "open":
eventType = "merge_request_opened"
case "close":
eventType = "merge_request_closed"
case "reopen":
eventType = "merge_request_reopened"
case "update":
eventType = "merge_request_updated"
// this event will be handled by GitLab in pipeline, no need to trigger pipeline from lambda
fmt.Printf("Ignoring merge request update event notification for mergeRequestIID: %d\n", mergeRequestIID)
return nil
case "approved":
eventType = "merge_request_approved"
case "unapproved":
eventType = "merge_request_unapproved"
case "approval":
eventType = "merge_request_approval"
case "unapproval":
eventType = "merge_request_unapproval"
case "merge":
eventType = "merge_request_merge"
// when merge request merged, original branch could be deleted, so we need to run it in target branch
branchName = event.ObjectAttributes.TargetBranch
// this event will be handled by GitLab in pipeline, no need to trigger pipeline from lambda
//fmt.Printf("Ignoring merge request merged event notification for mergeRequestIID: %d\n", mergeRequestIID)
//return nil
default:
return fmt.Errorf("unknown gitlab event action %s\n", event.ObjectAttributes.Action)
}
err = TriggerPipeline(projectId, branchName, eventType, "", "", mergeRequestID, mergeRequestIID, isMergeable)
if err != nil {
return err
}
case gitlab.EventTypeNote:
event := result.(*gitlab.MergeCommentEvent)
comment := event.ObjectAttributes.Note
projectId := event.ProjectID
fmt.Printf("note event: %v\n", event)
currentUser, _, err := gitLabClients[projectId].Users.CurrentUser()
if err != nil {
return fmt.Errorf("failed to get current GitLab user info, projectId: %v, %v", projectId, err)
}
fmt.Printf("Current GitLab user id: %d\n", currentUser.ID)
if event.User.ID == currentUser.ID {
fmt.Println("Webhook triggered by lambda, do nothing.")
// do nothing if comment has been created by the same webhook user
return nil
}
if !strings.HasPrefix(comment, "digger") {
// ignoring any comments that do not start with digger
fmt.Println("Comment is not a digger command, ignoring.")
return nil
}
branchName := event.MergeRequest.SourceBranch
mergeRequestIID := event.MergeRequest.IID
mergeRequestID := event.MergeRequest.ID
//check if Merge Request mergeable
isMergeable, err := IsMergeable(gitLabClients[projectId], projectId, mergeRequestIID)
if err != nil {
return fmt.Errorf("failed to parse webhook event: %w\n", err)
}
err = TriggerPipeline(projectId, branchName, "merge_request_commented", comment, event.ObjectAttributes.DiscussionID, mergeRequestID, mergeRequestIID, isMergeable)
if err != nil {
return err
}
default:
fmt.Printf("Skipping '%s' GitLab event\n", eventType)
}
fmt.Println("webhook event parsed successfully")
return nil
}
func TriggerPipeline(projectId int, branchName string, eventType string, diggerCommand string, discussionId string, mergeRequestID int, mergeRequestIID int, isMergeable bool) error {
gitLabClients, err := CreateGitLabClient()
if err != nil {
return err
}
gitLabClient := gitLabClients[projectId]
log.Printf("TriggerPipeline: projectId: %d, branchName: %s, mergeRequestIID:%d, mergeRequestID:%d, eventType: %s, discussionId: %s, diggerCommand: %s", projectId, branchName, mergeRequestIID, mergeRequestID, eventType, discussionId, diggerCommand)
variables := make([]*gitlab.PipelineVariableOptions, 0)
variables = append(variables, &gitlab.PipelineVariableOptions{
Key: gitlab.String("MERGE_REQUEST_EVENT_NAME"),
Value: gitlab.String(eventType),
VariableType: gitlab.String("env_var"),
})
variables = append(variables, &gitlab.PipelineVariableOptions{
Key: gitlab.String("IS_MERGEABLE"),
Value: gitlab.String(strconv.FormatBool(isMergeable)),
VariableType: gitlab.String("env_var"),
})
if mergeRequestIID != 0 {
variables = append(variables, &gitlab.PipelineVariableOptions{
Key: gitlab.String("CI_MERGE_REQUEST_IID"),
Value: gitlab.String(strconv.Itoa(mergeRequestIID)),
VariableType: gitlab.String("env_var"),
})
}
if diggerCommand != "" {
variables = append(variables, &gitlab.PipelineVariableOptions{
Key: gitlab.String("DIGGER_COMMAND"),
Value: gitlab.String(diggerCommand),
VariableType: gitlab.String("env_var"),
})
}
if discussionId != "" {
variables = append(variables, &gitlab.PipelineVariableOptions{
Key: gitlab.String("DISCUSSION_ID"),
Value: gitlab.String(discussionId),
VariableType: gitlab.String("env_var"),
})
}
opt := &gitlab.CreatePipelineOptions{Ref: &branchName, Variables: &variables}
fmt.Printf("trigger gitlab pipeline. branch: %s\n", branchName)
fmt.Println("variables: ")
for _, v := range variables {
fmt.Printf("key: %s, value: %s\n", *v.Key, *v.Value)
}
build, _, err := gitLabClient.Pipelines.CreatePipeline(projectId, opt)
if err != nil {
return fmt.Errorf("failed to create pipeline, %v", err)
}
fmt.Printf("build %v\n", build)
return nil
}
func CreateGitLabClient() (map[int]*gitlab.Client, error) {
// GITLAB_TOKENS is a json dict of tokens, example:
//[{"project": "46465722", "token": "glpat-121211"}, {"project": "44723537": "token": "glpat-2323232323"}]
gitlabTokenJson := os.Getenv("GITLAB_TOKENS")
if gitlabTokenJson == "" {
return nil, fmt.Errorf("GITLAB_TOKENS has not been set\n")
}
var result map[int]*gitlab.Client
var tokens []map[string]string
err := json.Unmarshal([]byte(gitlabTokenJson), &tokens)
if err != nil {
fmt.Printf("failed to unmarshal gitlabTokenJson, %v", err)
return nil, err
}
result = make(map[int]*gitlab.Client, len(tokens))
for i := range tokens {
if tokens[i]["project"] == "" {
return nil, fmt.Errorf("Project Id has not been set in GITLAB_TOKENS\n")
}
if tokens[i]["token"] == "" {
return nil, fmt.Errorf("GitLab token has not been set in GITLAB_TOKENS\n")
}
token := tokens[i]["token"]
projectId, err := strconv.Atoi(tokens[i]["project"])
if err != nil {
return nil, fmt.Errorf("Failed to parse projectId for %s\n", tokens[i]["project"])
}
client, err := gitlab.NewClient(token)
if err != nil {
return nil, fmt.Errorf("failed to create GitLab client for project %d, %v, \n", projectId, err)
}
result[projectId] = client
}
return result, nil
}
func PublishComment(projectID int, mergeRequestIID int, comment string) error {
gitLabClients, err := CreateGitLabClient()
if err != nil {
return err
}
gitLabClient := gitLabClients[projectID]
opt := &gitlab.CreateMergeRequestDiscussionOptions{Body: &comment}
gitLabClient.Discussions.CreateMergeRequestDiscussion(projectID, mergeRequestIID, opt)
if err != nil {
fmt.Printf("Failed to publish a comment. %v\n", err)
return err
}
return nil
}
func checkIfBranchExist(gitLabClient *gitlab.Client, projectID int, branchName string) (bool, error) {
fmt.Printf("projectID: %d, branchName: %s\n", projectID, branchName)
branch, response, err := gitLabClient.Branches.GetBranch(projectID, branchName)
if err != nil {
if response.Status == "404 Not Found" {
return false, nil
}
fmt.Printf("Failed to get GitLab branch info. %v\n", err)
return false, err
}
if branch == nil {
return false, nil
}
return true, nil
}
func GetChangedFiles(gitLabClient *gitlab.Client, projectId int, mergeRequestId int) ([]string, error) {
opt := &gitlab.GetMergeRequestChangesOptions{}
log.Printf("mergeRequestId: %d", mergeRequestId)
mergeRequestChanges, _, err := gitLabClient.MergeRequests.GetMergeRequestChanges(projectId, mergeRequestId, opt)
if err != nil {
log.Fatalf("error getting gitlab's merge request: %v", err)
}
fileNames := make([]string, len(mergeRequestChanges.Changes))
for i, change := range mergeRequestChanges.Changes {
fileNames[i] = change.NewPath
}
return fileNames, nil
}
func IsMergeable(gitLabClient *gitlab.Client, projectId int, mergeRequestIID int) (bool, error) {
opt := &gitlab.GetMergeRequestsOptions{}
mergeRequest, _, err := gitLabClient.MergeRequests.GetMergeRequest(projectId, mergeRequestIID, opt)
if err != nil {
fmt.Printf("Failed to get a MergeRequest: %d, %v \n", mergeRequestIID, err)
print(err.Error())
}
fmt.Printf("mergeRequest.DetailedMergeStatus: %s\n", mergeRequest.DetailedMergeStatus)
if mergeRequest.DetailedMergeStatus == "mergeable" {
return true, nil
}
return false, nil
}