diff options
author | Oliver Poignant <oliver@poignant.se> | 2016-03-13 23:07:50 +0100 |
---|---|---|
committer | Oliver Poignant <oliver@poignant.se> | 2016-03-13 23:07:50 +0100 |
commit | 2465cfc4a3669282523daf168e8c408880f3dd4d (patch) | |
tree | 18abeadf13559334abe17044f80dea3fa6f3ccbe /gitautodeploy/parsers | |
parent | 5a8caf8b9c9f4f283dffac33b461b5c43ad64eed (diff) | |
parent | 86ff375bf4c4b6c04ab44ea4be7dd90730f824c1 (diff) | |
download | Git-Auto-Deploy-2465cfc4a3669282523daf168e8c408880f3dd4d.zip Git-Auto-Deploy-2465cfc4a3669282523daf168e8c408880f3dd4d.tar.gz Git-Auto-Deploy-2465cfc4a3669282523daf168e8c408880f3dd4d.tar.bz2 |
Merge pull request #77 from olipo186/developmentv0.2.0
Development
Diffstat (limited to 'gitautodeploy/parsers')
-rw-r--r-- | gitautodeploy/parsers/__init__.py | 4 | ||||
-rw-r--r-- | gitautodeploy/parsers/bitbucket.py | 37 | ||||
-rw-r--r-- | gitautodeploy/parsers/common.py | 24 | ||||
-rw-r--r-- | gitautodeploy/parsers/generic.py | 30 | ||||
-rw-r--r-- | gitautodeploy/parsers/github.py | 45 | ||||
-rw-r--r-- | gitautodeploy/parsers/gitlab.py | 74 | ||||
-rw-r--r-- | gitautodeploy/parsers/gitlabci.py | 1 |
7 files changed, 215 insertions, 0 deletions
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..791b491 --- /dev/null +++ b/gitautodeploy/parsers/bitbucket.py @@ -0,0 +1,37 @@ +from common 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/common.py b/gitautodeploy/parsers/common.py new file mode 100644 index 0000000..42cbbdd --- /dev/null +++ b/gitautodeploy/parsers/common.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/generic.py b/gitautodeploy/parsers/generic.py new file mode 100644 index 0000000..7c7c7f5 --- /dev/null +++ b/gitautodeploy/parsers/generic.py @@ -0,0 +1,30 @@ +from common 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..028663d --- /dev/null +++ b/gitautodeploy/parsers/github.py @@ -0,0 +1,45 @@ +from common 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..580fed2 --- /dev/null +++ b/gitautodeploy/parsers/gitlab.py @@ -0,0 +1,74 @@ +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]) + + # extract the branch + if 'ref' in data: + ref = data['ref'] + + # set the action + if 'object_kind' in data: + action = data['object_kind'] + + # 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..6b5e3ca --- /dev/null +++ b/gitautodeploy/parsers/gitlabci.py @@ -0,0 +1 @@ +from common import WebhookRequestParser |