summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/parsers/gitlab.py
blob: 6ebdb6f0e37a953098550ca8c4c9a46359403229 (plain)
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
from common import WebhookRequestParser

class GitLabRequestParser(WebhookRequestParser):

    def get_repo_params_from_request(self, request_headers, request_body):
        import json
        import logging

        logger = logging.getLogger()
        data = json.loads(request_body)

        repo_urls = []
        ref = ""
        action = ""

        gitlab_event = 'x-gitlab-event' in request_headers and request_headers['x-gitlab-event']

        logger.info("Received '%s' event from GitLab" % gitlab_event)

        if 'repository' not in data:
            logger.error("Unable to recognize data format")
            return [], ref or "master", action

        # One repository may posses multiple URLs for different protocols
        for k in ['url', 'git_http_url', 'git_ssh_url']:
            if k in data['repository']:
                repo_urls.append(data['repository'][k])

        # Get a list of configured repositories that matches the incoming web hook reqeust
        repo_configs = self.get_matching_repo_configs(repo_urls)

        return repo_configs, ref or "master", action


class GitLabCIRequestParser(WebhookRequestParser):

    def get_repo_params_from_request(self, request_headers, request_body):
        import json
        import logging

        logger = logging.getLogger()
        data = json.loads(request_body)

        repo_urls = []
        ref = ""
        action = ""

        logger.info('Received event from Gitlab CI')

        if 'push_data' not in data:
            logger.error("Unable to recognize data format")
            return [], ref or "master", action

        # Only add repositories if the build is successful. Ignore it in other case.
        if data['build_status'] == "success":
            for k in ['url', 'git_http_url', 'git_ssh_url']:
                if k in data['push_data']['repository']:
                    repo_urls.append(data['push_data']['repository'][k])
        else:
            logger.warning("Gitlab CI build '%d' has status '%s'. Not pull will be done" % (
                data['build_id'], data['build_status']))

        # Get a list of configured repositories that matches the incoming web hook reqeust
        repo_configs = self.get_matching_repo_configs(repo_urls)

        return repo_configs, ref or "master", action