diff options
author | Oliver Poignant <oliver@poignant.se> | 2016-03-06 21:05:20 +0100 |
---|---|---|
committer | Oliver Poignant <oliver@poignant.se> | 2016-03-06 21:05:20 +0100 |
commit | 3f7a830cdadc82f10bad26b060f2701ed08b34a9 (patch) | |
tree | e62af59711f37b88e64b6155aaa57904c3f3e7c7 /gitautodeploy/parsers | |
parent | ec1f2a28c902855aa470f8523eea33f8aed6a2b5 (diff) | |
download | Git-Auto-Deploy-3f7a830cdadc82f10bad26b060f2701ed08b34a9.zip Git-Auto-Deploy-3f7a830cdadc82f10bad26b060f2701ed08b34a9.tar.gz Git-Auto-Deploy-3f7a830cdadc82f10bad26b060f2701ed08b34a9.tar.bz2 |
Moved files around into new project structure
Diffstat (limited to 'gitautodeploy/parsers')
-rw-r--r-- | gitautodeploy/parsers/WebhookRequestParser.py | 24 | ||||
-rw-r--r-- | gitautodeploy/parsers/__init__.py | 4 | ||||
-rw-r--r-- | gitautodeploy/parsers/bitbucket.py | 37 | ||||
-rw-r--r-- | gitautodeploy/parsers/generic.py | 30 | ||||
-rw-r--r-- | gitautodeploy/parsers/github.py | 45 | ||||
-rw-r--r-- | gitautodeploy/parsers/gitlab.py | 66 | ||||
-rw-r--r-- | gitautodeploy/parsers/gitlabci.py | 1 |
7 files changed, 207 insertions, 0 deletions
diff --git a/gitautodeploy/parsers/WebhookRequestParser.py b/gitautodeploy/parsers/WebhookRequestParser.py new file mode 100644 index 0000000..42cbbdd --- /dev/null +++ b/gitautodeploy/parsers/WebhookRequestParser.py @@ -0,0 +1,24 @@ + +class WebhookRequestParser(object): + """Abstract parent class for git service parsers. Contains helper + methods.""" + + def __init__(self, config): + self._config = config + + def get_matching_repo_configs(self, urls): + """Iterates over the various repo URLs provided as argument (git://, + ssh:// and https:// for the repo) and compare them to any repo URL + specified in the config""" + + configs = [] + for url in urls: + for repo_config in self._config['repositories']: + if repo_config in configs: + continue + if repo_config['url'] == url: + configs.append(repo_config) + elif 'url_without_usernme' in repo_config and repo_config['url_without_usernme'] == url: + configs.append(repo_config) + + return configs
\ No newline at end of file diff --git a/gitautodeploy/parsers/__init__.py b/gitautodeploy/parsers/__init__.py new file mode 100644 index 0000000..59d2b50 --- /dev/null +++ b/gitautodeploy/parsers/__init__.py @@ -0,0 +1,4 @@ +from bitbucket import BitBucketRequestParser +from github import GitHubRequestParser +from gitlab import GitLabRequestParser, GitLabCIRequestParser +from generic import GenericRequestParser
\ No newline at end of file diff --git a/gitautodeploy/parsers/bitbucket.py b/gitautodeploy/parsers/bitbucket.py new file mode 100644 index 0000000..b02a6ac --- /dev/null +++ b/gitautodeploy/parsers/bitbucket.py @@ -0,0 +1,37 @@ +from WebhookRequestParser import WebhookRequestParser + +class BitBucketRequestParser(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 BitBucket") + + 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_url', 'clone_url', 'ssh_url']: + if k in data['repository']: + repo_urls.append(data['repository'][k]) + + if 'full_name' in data['repository']: + repo_urls.append('git@bitbucket.org:%s.git' % data['repository']['full_name']) + + # Add a simplified version of the bitbucket HTTPS URL - without the username@bitbucket.com part. This is + # needed since the configured repositories might be configured using a different username. + repo_urls.append('https://bitbucket.org/%s.git' % (data['repository']['full_name'])) + + # 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
\ No newline at end of file diff --git a/gitautodeploy/parsers/generic.py b/gitautodeploy/parsers/generic.py new file mode 100644 index 0000000..cb83a71 --- /dev/null +++ b/gitautodeploy/parsers/generic.py @@ -0,0 +1,30 @@ +from WebhookRequestParser import WebhookRequestParser + +class GenericRequestParser(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 unknown origin. Assume generic data format.") + + 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', 'http_url', '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
\ No newline at end of file diff --git a/gitautodeploy/parsers/github.py b/gitautodeploy/parsers/github.py new file mode 100644 index 0000000..bd20b98 --- /dev/null +++ b/gitautodeploy/parsers/github.py @@ -0,0 +1,45 @@ +from WebhookRequestParser import WebhookRequestParser + +class GitHubRequestParser(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 = "" + + github_event = 'x-github-event' in request_headers and request_headers['x-github-event'] + + logger.info("Received '%s' event from GitHub" % github_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_url', 'clone_url', 'ssh_url']: + if k in data['repository']: + repo_urls.append(data['repository'][k]) + + if 'pull_request' in data: + if 'base' in data['pull_request']: + if 'ref' in data['pull_request']['base']: + ref = data['pull_request']['base']['ref'] + logger.info("Pull request to branch '%s' was fired" % ref) + elif 'ref' in data: + ref = data['ref'] + logger.info("Push to branch '%s' was fired" % ref) + + if 'action' in data: + action = data['action'] + logger.info("Action '%s' was fired" % action) + + # 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
\ No newline at end of file diff --git a/gitautodeploy/parsers/gitlab.py b/gitautodeploy/parsers/gitlab.py new file mode 100644 index 0000000..7a8e374 --- /dev/null +++ b/gitautodeploy/parsers/gitlab.py @@ -0,0 +1,66 @@ +from WebhookRequestParser 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
\ No newline at end of file diff --git a/gitautodeploy/parsers/gitlabci.py b/gitautodeploy/parsers/gitlabci.py new file mode 100644 index 0000000..9fc66be --- /dev/null +++ b/gitautodeploy/parsers/gitlabci.py @@ -0,0 +1 @@ +from WebhookRequestParser import WebhookRequestParser |