summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/parsers/coding.py
blob: 80ab77ee16df88b2bb8fec41feca89bd968342b2 (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
from common import WebhookRequestParser

class CodingRequestParser(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 = ""

        coding_event = 'x-coding-event' in request_headers and request_headers['x-coding-event']

        logger.debug("Received '%s' event from Coding" % coding_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 ['web_url', 'https_url', 'ssh_url']:
            if k in data['repository']:
                repo_urls.append(data['repository'][k])

        # extract the branch
        if 'ref' in data:
            ref = data['ref']

        # set the action
        if 'event' in data:
            action = data['event']

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

        repo_configs = []
        for repo_config in items:
            # Validate secret token if present
            if 'secret-token' in repo_config:
                if 'token' not in data or not self.verify_token(repo_config['secret-token'], data['token']):
                    logger.warning("Request token does not match the 'secret-token' configured for repository %s." % repo_config['url'])
                    continue

            repo_configs.append(repo_config)

        return repo_configs, ref or "master", action, repo_urls


    def verify_token(self, secret_token, request_token):
        return secret_token == request_token