1
1
package main
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
6
"fmt"
6
7
"github.com/aws/aws-lambda-go/events"
@@ -39,16 +40,11 @@ func ParseWebHookJSON(secret string, lambdaRequest events.LambdaFunctionURLReque
39
40
40
41
fmt .Printf ("gitlabEvent: %s\n " , gitlabEvent )
41
42
42
- gitLabClient , err := CreateGitLabClient ()
43
+ gitLabClients , err := CreateGitLabClient ()
43
44
if err != nil {
44
45
return fmt .Errorf ("failed to create GitLab client, %v\n " , err )
45
46
}
46
47
47
- currentUser , _ , err := gitLabClient .Users .CurrentUser ()
48
- if err != nil {
49
- return fmt .Errorf ("failed to get current GitLab user info, %v" , err )
50
- }
51
-
52
48
eventType := gitlab .EventType (gitlabEvent )
53
49
54
50
payload := lambdaRequest .Body
@@ -70,8 +66,14 @@ func ParseWebHookJSON(secret string, lambdaRequest events.LambdaFunctionURLReque
70
66
mergeRequestIID := event .ObjectAttributes .IID
71
67
mergeRequestID := event .ObjectAttributes .ID
72
68
69
+ currentUser , _ , err := gitLabClients [projectId ].Users .CurrentUser ()
70
+ if err != nil {
71
+ return fmt .Errorf ("failed to get current GitLab user info, %v" , err )
72
+ }
73
+ fmt .Printf ("Current GitLab user id: %d\n " , currentUser .ID )
74
+
73
75
//check if Merge Request mergeable
74
- isMergeable , err := IsMergeable (gitLabClient , projectId , mergeRequestIID )
76
+ isMergeable , err := IsMergeable (gitLabClients [ projectId ] , projectId , mergeRequestIID )
75
77
if err != nil {
76
78
return fmt .Errorf ("failed to parse webhook event: %w\n " , err )
77
79
}
@@ -81,7 +83,7 @@ func ParseWebHookJSON(secret string, lambdaRequest events.LambdaFunctionURLReque
81
83
fmt .Printf ("branchName: %s\n " , branchName )
82
84
83
85
// check if branch exist
84
- branchExists , err := checkIfBranchExist (gitLabClient , projectId , branchName )
86
+ branchExists , err := checkIfBranchExist (gitLabClients [ projectId ] , projectId , branchName )
85
87
if err != nil {
86
88
return err
87
89
}
@@ -135,8 +137,15 @@ func ParseWebHookJSON(secret string, lambdaRequest events.LambdaFunctionURLReque
135
137
case gitlab .EventTypeNote :
136
138
event := result .(* gitlab.MergeCommentEvent )
137
139
comment := event .ObjectAttributes .Note
140
+ projectId := event .ProjectID
138
141
fmt .Printf ("note event: %v\n " , event )
139
142
143
+ currentUser , _ , err := gitLabClients [projectId ].Users .CurrentUser ()
144
+ if err != nil {
145
+ return fmt .Errorf ("failed to get current GitLab user info, %v" , err )
146
+ }
147
+ fmt .Printf ("Current GitLab user id: %d\n " , currentUser .ID )
148
+
140
149
if event .User .ID == currentUser .ID {
141
150
fmt .Println ("Webhook triggered by lambda, do nothing." )
142
151
// do nothing if comment has been created by the same webhook user
@@ -148,13 +157,12 @@ func ParseWebHookJSON(secret string, lambdaRequest events.LambdaFunctionURLReque
148
157
fmt .Println ("Comment is not a digger command, ignoring." )
149
158
return nil
150
159
}
151
- projectId := event .ProjectID
152
160
branchName := event .MergeRequest .SourceBranch
153
161
mergeRequestIID := event .MergeRequest .IID
154
162
mergeRequestID := event .MergeRequest .ID
155
163
156
164
//check if Merge Request mergeable
157
- isMergeable , err := IsMergeable (gitLabClient , projectId , mergeRequestIID )
165
+ isMergeable , err := IsMergeable (gitLabClients [ projectId ] , projectId , mergeRequestIID )
158
166
if err != nil {
159
167
return fmt .Errorf ("failed to parse webhook event: %w\n " , err )
160
168
}
@@ -173,11 +181,13 @@ func ParseWebHookJSON(secret string, lambdaRequest events.LambdaFunctionURLReque
173
181
}
174
182
175
183
func TriggerPipeline (projectId int , branchName string , eventType string , diggerCommand string , discussionId string , mergeRequestID int , mergeRequestIID int , isMergeable bool ) error {
176
- git , err := CreateGitLabClient ()
184
+ gitLabClients , err := CreateGitLabClient ()
177
185
if err != nil {
178
186
return err
179
187
}
180
188
189
+ gitLabClient := gitLabClients [projectId ]
190
+
181
191
log .Printf ("TriggerPipeline: projectId: %d, branchName: %s, mergeRequestIID:%d, mergeRequestID:%d, eventType: %s, discussionId: %s, diggerCommand: %s" , projectId , branchName , mergeRequestIID , mergeRequestID , eventType , discussionId , diggerCommand )
182
192
183
193
variables := make ([]* gitlab.PipelineVariableOptions , 0 )
@@ -225,7 +235,7 @@ func TriggerPipeline(projectId int, branchName string, eventType string, diggerC
225
235
fmt .Printf ("key: %s, value: %s\n " , * v .Key , * v .Value )
226
236
}
227
237
228
- build , _ , err := git .Pipelines .CreatePipeline (projectId , opt )
238
+ build , _ , err := gitLabClient .Pipelines .CreatePipeline (projectId , opt )
229
239
if err != nil {
230
240
return fmt .Errorf ("failed to create pipeline, %v" , err )
231
241
}
@@ -234,24 +244,57 @@ func TriggerPipeline(projectId int, branchName string, eventType string, diggerC
234
244
return nil
235
245
}
236
246
237
- func CreateGitLabClient () (* gitlab.Client , error ) {
238
- gitlabToken := os .Getenv ("GITLAB_TOKEN" )
239
- if gitlabToken == "" {
240
- return nil , fmt .Errorf ("GITLAB_TOKEN has not been set\n " )
247
+ func CreateGitLabClient () (map [int ]* gitlab.Client , error ) {
248
+ // GITLAB_TOKENS is a json dict of tokens, example:
249
+ //[{"46465722": "glpat-121211"}, {"44723537": "glpat-2323232323"}]
250
+
251
+ gitlabTokenJson := os .Getenv ("GITLAB_TOKENS" )
252
+
253
+ if gitlabTokenJson == "" {
254
+ return nil , fmt .Errorf ("GITLAB_TOKENS has not been set\n " )
241
255
}
242
- client , err := gitlab .NewClient (gitlabToken )
256
+
257
+ var result map [int ]* gitlab.Client
258
+
259
+ var tokens []map [string ]string
260
+ err := json .Unmarshal ([]byte (gitlabTokenJson ), & tokens )
243
261
if err != nil {
244
- return nil , fmt .Errorf ("failed to create GitLab client, %v, \n " , err )
262
+ panic (err )
263
+ }
264
+ result = make (map [int ]* gitlab.Client , len (tokens ))
265
+
266
+ for i := range tokens {
267
+ if tokens [i ]["project" ] == "" {
268
+ return nil , fmt .Errorf ("Project Id has not been set in GITLAB_TOKENS\n " )
269
+ }
270
+
271
+ if tokens [i ]["token" ] == "" {
272
+ return nil , fmt .Errorf ("GitLab token has not been set in GITLAB_TOKENS\n " )
273
+ }
274
+ token := tokens [i ]["token" ]
275
+ projectId , err := strconv .Atoi (tokens [i ]["project" ])
276
+ if err != nil {
277
+ return nil , fmt .Errorf ("Failed to parse projectId for %s\n " , tokens [i ]["project" ])
278
+ }
279
+
280
+ client , err := gitlab .NewClient (token )
281
+ if err != nil {
282
+ return nil , fmt .Errorf ("failed to create GitLab client for project %d, %v, \n " , projectId , err )
283
+ }
284
+ result [projectId ] = client
245
285
}
246
- return client , nil
286
+
287
+ return result , nil
247
288
}
248
289
249
290
func PublishComment (projectID int , mergeRequestIID int , comment string ) error {
250
- gitLabClient , err := CreateGitLabClient ()
291
+ gitLabClients , err := CreateGitLabClient ()
251
292
if err != nil {
252
293
return err
253
294
}
254
295
296
+ gitLabClient := gitLabClients [projectID ]
297
+
255
298
opt := & gitlab.CreateMergeRequestDiscussionOptions {Body : & comment }
256
299
257
300
gitLabClient .Discussions .CreateMergeRequestDiscussion (projectID , mergeRequestIID , opt )
0 commit comments