summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/parsers
diff options
context:
space:
mode:
Diffstat (limited to 'gitautodeploy/parsers')
-rw-r--r--gitautodeploy/parsers/__init__.py3
-rw-r--r--gitautodeploy/parsers/bitbucket.py16
-rw-r--r--gitautodeploy/parsers/coding.py26
-rw-r--r--gitautodeploy/parsers/common.py7
-rw-r--r--gitautodeploy/parsers/generic.py16
-rw-r--r--gitautodeploy/parsers/github.py36
-rw-r--r--gitautodeploy/parsers/gitlab.py73
-rw-r--r--gitautodeploy/parsers/gitlabci.py28
8 files changed, 69 insertions, 136 deletions
diff --git a/gitautodeploy/parsers/__init__.py b/gitautodeploy/parsers/__init__.py
index 168c781..c993454 100644
--- a/gitautodeploy/parsers/__init__.py
+++ b/gitautodeploy/parsers/__init__.py
@@ -1,5 +1,6 @@
from bitbucket import BitBucketRequestParser
from github import GitHubRequestParser
-from gitlab import GitLabRequestParser, GitLabCIRequestParser
+from gitlab import GitLabRequestParser
+from gitlabci import GitLabCIRequestParser
from generic import GenericRequestParser
from coding import CodingRequestParser \ No newline at end of file
diff --git a/gitautodeploy/parsers/bitbucket.py b/gitautodeploy/parsers/bitbucket.py
index 353435c..80899fd 100644
--- a/gitautodeploy/parsers/bitbucket.py
+++ b/gitautodeploy/parsers/bitbucket.py
@@ -2,22 +2,18 @@ from common import WebhookRequestParser
class BitBucketRequestParser(WebhookRequestParser):
- def get_repo_params_from_request(self, request_headers, request_body):
+ def get_repo_configs(self, request_headers, request_body, action):
import json
- import logging
- logger = logging.getLogger()
data = json.loads(request_body)
repo_urls = []
- ref = ""
- action = ""
- logger.debug("Received event from BitBucket")
+ action.log_debug("Received event from BitBucket")
if 'repository' not in data:
- logger.error("Unable to recognize data format")
- return [], ref or "master", action
+ action.log_error("Unable to recognize data format")
+ return []
# One repository may posses multiple URLs for different protocols
for k in ['url', 'git_url', 'clone_url', 'ssh_url']:
@@ -32,6 +28,6 @@ class BitBucketRequestParser(WebhookRequestParser):
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)
+ repo_configs = self.get_matching_repo_configs(repo_urls, action)
- return repo_configs, ref or "master", action, repo_urls \ No newline at end of file
+ return repo_configs \ No newline at end of file
diff --git a/gitautodeploy/parsers/coding.py b/gitautodeploy/parsers/coding.py
index 80ab77e..889c3ee 100644
--- a/gitautodeploy/parsers/coding.py
+++ b/gitautodeploy/parsers/coding.py
@@ -2,52 +2,38 @@ from common import WebhookRequestParser
class CodingRequestParser(WebhookRequestParser):
- def get_repo_params_from_request(self, request_headers, request_body):
+ def get_repo_configs(self, request_headers, request_body, action):
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
+ action.log_error("Unable to recognize data format")
+ return []
# 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)
+ items = self.get_matching_repo_configs(repo_urls, action)
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'])
+ action.log_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
+ return repo_configs
def verify_token(self, secret_token, request_token):
diff --git a/gitautodeploy/parsers/common.py b/gitautodeploy/parsers/common.py
index 1b40b73..b194062 100644
--- a/gitautodeploy/parsers/common.py
+++ b/gitautodeploy/parsers/common.py
@@ -6,7 +6,7 @@ class WebhookRequestParser(object):
def __init__(self, config):
self._config = config
- def get_matching_repo_configs(self, urls):
+ def get_matching_repo_configs(self, urls, action):
"""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"""
@@ -21,7 +21,10 @@ class WebhookRequestParser(object):
elif 'url_without_usernme' in repo_config and repo_config['url_without_usernme'] == url:
configs.append(repo_config)
+ if len(configs) == 0:
+ action.log_warning('The URLs references in the webhook did not match any repository entry in the config. For this webhook to work, make sure you have at least one repository configured with one of the following URLs; %s' % ', '.join(urls))
+
return configs
- def validate_request(self, request_headers, repo_configs):
+ def validate_request(self, request_headers, repo_configs, action):
return True \ No newline at end of file
diff --git a/gitautodeploy/parsers/generic.py b/gitautodeploy/parsers/generic.py
index 7b150d2..a93e90b 100644
--- a/gitautodeploy/parsers/generic.py
+++ b/gitautodeploy/parsers/generic.py
@@ -2,22 +2,18 @@ from common import WebhookRequestParser
class GenericRequestParser(WebhookRequestParser):
- def get_repo_params_from_request(self, request_headers, request_body):
+ def get_repo_configs(self, request_headers, request_body, action):
import json
- import logging
- logger = logging.getLogger()
data = json.loads(request_body)
repo_urls = []
- ref = ""
- action = ""
- logger.debug("Received event from unknown origin. Assume generic data format.")
+ action.log_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
+ action.log_error("Unable to recognize data format")
+ return []
# One repository may posses multiple URLs for different protocols
for k in ['url', 'git_http_url', 'git_ssh_url', 'http_url', 'ssh_url']:
@@ -25,7 +21,7 @@ class GenericRequestParser(WebhookRequestParser):
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)
+ repo_configs = self.get_matching_repo_configs(repo_urls, action)
- return repo_configs, ref or "master", action, repo_urls
+ return repo_configs
diff --git a/gitautodeploy/parsers/github.py b/gitautodeploy/parsers/github.py
index 4d24648..bede7bb 100644
--- a/gitautodeploy/parsers/github.py
+++ b/gitautodeploy/parsers/github.py
@@ -2,59 +2,39 @@ from common import WebhookRequestParser
class GitHubRequestParser(WebhookRequestParser):
- def get_repo_params_from_request(self, request_headers, request_body):
+ def get_repo_configs(self, request_headers, request_body, action):
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.debug("Received '%s' event from GitHub" % github_event)
+ action.log_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
+ action.log_error("Unable to recognize data format")
+ return []
# 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.debug("Pull request to branch '%s' was fired" % ref)
- elif 'ref' in data:
- ref = data['ref']
- logger.debug("Push to branch '%s' was fired" % ref)
-
- if 'action' in data:
- action = data['action']
- logger.debug("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, repo_urls
+ repo_configs = self.get_matching_repo_configs(repo_urls, action)
- def validate_request(self, request_headers, repo_configs):
- import logging
+ return repo_configs
- logger = logging.getLogger()
+ def validate_request(self, request_headers, repo_configs, action):
for repo_config in repo_configs:
# Validate secret token if present
if 'secret-token' in repo_config and 'x-hub-signature' in request_headers:
if not self.verify_signature(repo_config['secret-token'], request_body, request_headers['x-hub-signature']):
- logger.info("Request signature does not match the 'secret-token' configured for repository %s." % repo_config['url'])
+ action.log_info("Request signature does not match the 'secret-token' configured for repository %s." % repo_config['url'])
return False
return True
diff --git a/gitautodeploy/parsers/gitlab.py b/gitautodeploy/parsers/gitlab.py
index 68a1982..5d0d348 100644
--- a/gitautodeploy/parsers/gitlab.py
+++ b/gitautodeploy/parsers/gitlab.py
@@ -2,47 +2,32 @@ from common import WebhookRequestParser
class GitLabRequestParser(WebhookRequestParser):
- def get_repo_params_from_request(self, request_headers, request_body):
+ def get_repo_configs(self, request_headers, request_body, action):
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.debug("Received '%s' event from GitLab" % gitlab_event)
+ action.log_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
+ action.log_error("Unable to recognize data format")
+ return []
# 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, repo_urls
+ repo_configs = self.get_matching_repo_configs(repo_urls, action)
- def validate_request(self, request_headers, repo_configs):
- import logging
+ return repo_configs
- logger = logging.getLogger()
+ def validate_request(self, request_headers, repo_configs, action):
for repo_config in repo_configs:
@@ -50,49 +35,7 @@ class GitLabRequestParser(WebhookRequestParser):
if 'secret-token' in repo_config and 'x-gitlab-token' in request_headers:
if repo_config['secret-token'] != request_headers['x-gitlab-token']:
- logger.info("Request token does not match the 'secret-token' configured for repository %s." % repo_config['url'])
+ action.log_info("Request token does not match the 'secret-token' configured for repository %s." % repo_config['url'])
return False
return True
-
-
-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.debug('Received event from Gitlab CI')
-
- if 'repository' 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['repository']:
- repo_urls.append(data['repository'][k])
- else:
- logger.warning("Gitlab CI build '%d' has status '%s'. Not pull will be done" % (
- data['build_id'], data['build_status']))
-
- # 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, repo_urls
diff --git a/gitautodeploy/parsers/gitlabci.py b/gitautodeploy/parsers/gitlabci.py
index 6b5e3ca..30a8b5f 100644
--- a/gitautodeploy/parsers/gitlabci.py
+++ b/gitautodeploy/parsers/gitlabci.py
@@ -1 +1,29 @@
from common import WebhookRequestParser
+
+class GitLabCIRequestParser(WebhookRequestParser):
+
+ def get_repo_configs(self, request_headers, request_body, action):
+ import json
+
+ data = json.loads(request_body)
+
+ repo_urls = []
+
+ action.log_info('Received event from Gitlab CI')
+
+ if 'repository' not in data:
+ action.log_error("Unable to recognize data format")
+ return []
+
+ # 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['repository']:
+ repo_urls.append(data['repository'][k])
+ else:
+ action.log_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, action)
+
+ return repo_configs