42
42
GITHUB_PULL_REQUEST_SYNC = 'synchronize'
43
43
GITHUB_CREATE = 'create'
44
44
GITHUB_DELETE = 'delete'
45
+ GITLAB_MERGE_REQUEST = 'merge_request'
46
+ GITLAB_MERGE_REQUEST_CLOSE = 'close'
47
+ GITLAB_MERGE_REQUEST_MERGE = 'merge'
48
+ GITLAB_MERGE_REQUEST_OPEN = 'open'
49
+ GITLAB_MERGE_REQUEST_REOPEN = 'reopen'
50
+ GITLAB_MERGE_REQUEST_UPDATE = 'update'
45
51
GITLAB_TOKEN_HEADER = 'HTTP_X_GITLAB_TOKEN'
46
52
GITLAB_PUSH = 'push'
47
53
GITLAB_NULL_HASH = '0' * 40
@@ -401,7 +407,7 @@ class GitLabWebhookView(WebhookMixin, APIView):
401
407
"""
402
408
Webhook consumer for GitLab.
403
409
404
- Accepts webhook events from GitLab, 'push' events trigger builds.
410
+ Accepts webhook events from GitLab, 'push' and 'merge_request' events trigger builds.
405
411
406
412
Expects the following JSON::
407
413
@@ -413,10 +419,26 @@ class GitLabWebhookView(WebhookMixin, APIView):
413
419
...
414
420
}
415
421
422
+ For merge_request events:
423
+
424
+ {
425
+ "object_kind": "merge_request",
426
+ "object_attributes": {
427
+ "iid": 2,
428
+ "last_commit": {
429
+ "id": "717abb9a6a0f3111dbd601ef6f58c70bdd165aef",
430
+ },
431
+ "action": "open"
432
+ ...
433
+ },
434
+ ...
435
+ }
436
+
416
437
See full payload here:
417
438
418
439
- https://docs.gitlab.com/ce/user/project/integrations/webhooks.html#push-events
419
440
- https://docs.gitlab.com/ce/user/project/integrations/webhooks.html#tag-events
441
+ - https://docs.gitlab.com/ce/user/project/integrations/webhooks.html#merge-request-events
420
442
"""
421
443
422
444
integration_type = Integration .GITLAB_WEBHOOK
@@ -441,6 +463,17 @@ def is_payload_valid(self):
441
463
return False
442
464
return token == secret
443
465
466
+ def get_external_version_data (self ):
467
+ """Get commit SHA and merge request number from payload."""
468
+ try :
469
+ identifier = self .data ['object_attributes' ]['last_commit' ]['id' ]
470
+ verbose_name = str (self .data ['object_attributes' ]['iid' ])
471
+
472
+ return identifier , verbose_name
473
+
474
+ except KeyError :
475
+ raise ParseError ('Parameters "id" and "iid" are required' )
476
+
444
477
def handle_webhook (self ):
445
478
"""
446
479
Handle GitLab events for push and tag_push.
@@ -450,6 +483,7 @@ def handle_webhook(self):
450
483
0000000000000000000000000000000000000000 ('0' * 40)
451
484
"""
452
485
event = self .request .data .get ('object_kind' , GITLAB_PUSH )
486
+ action = self .data .get ('object_attributes' , {}).get ('action' , None )
453
487
webhook_gitlab .send (
454
488
Project ,
455
489
project = self .project ,
@@ -470,6 +504,25 @@ def handle_webhook(self):
470
504
return self .get_response_push (self .project , branches )
471
505
except KeyError :
472
506
raise ParseError ('Parameter "ref" is required' )
507
+
508
+ if (
509
+ self .project .has_feature (Feature .EXTERNAL_VERSION_BUILD ) and
510
+ event == GITLAB_MERGE_REQUEST and action
511
+ ):
512
+ if (
513
+ action in
514
+ [
515
+ GITLAB_MERGE_REQUEST_OPEN ,
516
+ GITLAB_MERGE_REQUEST_REOPEN ,
517
+ GITLAB_MERGE_REQUEST_UPDATE
518
+ ]
519
+ ):
520
+ # Handle open, update, reopen merge_request event.
521
+ return self .get_external_version_response (self .project )
522
+
523
+ if action in [GITLAB_MERGE_REQUEST_CLOSE , GITLAB_MERGE_REQUEST_MERGE ]:
524
+ # Handle merge and close merge_request event.
525
+ return self .get_delete_external_version_response (self .project )
473
526
return None
474
527
475
528
def _normalize_ref (self , ref ):
0 commit comments