More deploy stuff
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
from .bitbucket import BitBucketRequestParser
|
||||
from .github import GitHubRequestParser
|
||||
from .gitlab import GitLabRequestParser
|
||||
from .gitlabci import GitLabCIRequestParser
|
||||
from .generic import GenericRequestParser
|
||||
from .coding import CodingRequestParser
|
||||
|
||||
|
||||
def get_service_handler(request_headers, request_body, action):
|
||||
"""Parses the incoming request and attempts to determine whether
|
||||
it originates from GitHub, GitLab or any other known service."""
|
||||
import json
|
||||
|
||||
payload = json.loads(request_body)
|
||||
|
||||
if not isinstance(payload, dict):
|
||||
raise ValueError("Invalid JSON object")
|
||||
|
||||
user_agent = 'user-agent' in request_headers and request_headers['user-agent']
|
||||
content_type = 'content-type' in request_headers and request_headers['content-type']
|
||||
|
||||
# Assume Coding if the X-Coding-Event HTTP header is set
|
||||
if 'x-coding-event' in request_headers:
|
||||
return CodingRequestParser
|
||||
|
||||
# Assume GitLab if the X-Gitlab-Event HTTP header is set
|
||||
elif 'x-gitlab-event' in request_headers:
|
||||
|
||||
# Special Case for Gitlab CI
|
||||
if content_type == "application/json" and "build_status" in payload:
|
||||
return GitLabCIRequestParser
|
||||
else:
|
||||
return GitLabRequestParser
|
||||
|
||||
# Assume GitHub if the X-GitHub-Event HTTP header is set
|
||||
elif 'x-github-event' in request_headers:
|
||||
|
||||
return GitHubRequestParser
|
||||
|
||||
# Assume BitBucket if the User-Agent HTTP header is set to
|
||||
# 'Bitbucket-Webhooks/2.0' (or something similar)
|
||||
elif user_agent and user_agent.lower().find('bitbucket') != -1:
|
||||
|
||||
return BitBucketRequestParser
|
||||
|
||||
# This handles old GitLab requests and Gogs requests for example.
|
||||
elif content_type == "application/json":
|
||||
|
||||
action.log_info("Received event from unknown origin.")
|
||||
return GenericRequestParser
|
||||
|
||||
action.log_error("Unable to recognize request origin. Don't know how to handle the request.")
|
||||
return
|
||||
@@ -0,0 +1,29 @@
|
||||
class WebhookRequestParserBase(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, 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"""
|
||||
|
||||
configs = []
|
||||
for url in urls:
|
||||
for repo_config in self._config['repositories']:
|
||||
if repo_config in configs:
|
||||
continue
|
||||
if repo_config.get('match-url', repo_config.get('url')) == url:
|
||||
configs.append(repo_config)
|
||||
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, request_body, repo_configs, action):
|
||||
return True
|
||||
@@ -0,0 +1,44 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class BitBucketRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
action.log_debug("Received event from BitBucket")
|
||||
|
||||
if 'repository' not in data:
|
||||
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 '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']))
|
||||
|
||||
if 'fullName' in data['repository']:
|
||||
# For Bitbucket Server add repository name (OWNER/REPO_NAME).
|
||||
# Support "Post Webhooks for Bitbucket".
|
||||
repo_urls.append(data['repository']['fullName'])
|
||||
|
||||
if 'slug' in data['repository']:
|
||||
# For Bitbucket Server add repository slug.
|
||||
# Support "Post-Receive WebHooks" and "Post Webhooks for Bitbucket".
|
||||
repo_urls.append(data['repository']['slug'])
|
||||
|
||||
# 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
|
||||
@@ -0,0 +1,40 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class CodingRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
coding_event = 'x-coding-event' in request_headers and request_headers['x-coding-event']
|
||||
|
||||
if 'repository' not in data:
|
||||
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])
|
||||
|
||||
# Get a list of configured repositories that matches the incoming web hook reqeust
|
||||
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']):
|
||||
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
|
||||
|
||||
def verify_token(self, secret_token, request_token):
|
||||
return secret_token == request_token
|
||||
@@ -0,0 +1,28 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class GenericRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
action.log_info("Received event from unknown origin. Assume generic data format.")
|
||||
|
||||
if 'repository' not in data:
|
||||
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']:
|
||||
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, action)
|
||||
|
||||
return repo_configs
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class GitHubRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
github_event = 'x-github-event' in request_headers and request_headers['x-github-event']
|
||||
|
||||
action.log_info("Received '%s' event from GitHub" % github_event)
|
||||
|
||||
if 'repository' not in data:
|
||||
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])
|
||||
|
||||
# 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
|
||||
|
||||
def validate_request(self, request_headers, request_body, 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']):
|
||||
action.log_info("Request signature does not match the 'secret-token' configured for repository %s." % repo_config['url'])
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def verify_signature(self, token, body, signature):
|
||||
import hashlib
|
||||
import hmac
|
||||
|
||||
result = "sha1=" + hmac.new(str(token), body, hashlib.sha1).hexdigest()
|
||||
return result == signature
|
||||
@@ -0,0 +1,42 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class GitLabRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(self, request_headers, request_body, action):
|
||||
import json
|
||||
|
||||
data = json.loads(request_body)
|
||||
|
||||
repo_urls = []
|
||||
|
||||
gitlab_event = 'x-gitlab-event' in request_headers and request_headers['x-gitlab-event']
|
||||
|
||||
action.log_info("Received '%s' event from GitLab" % gitlab_event)
|
||||
|
||||
if 'repository' not in data:
|
||||
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])
|
||||
|
||||
# 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
|
||||
|
||||
def validate_request(self, request_headers, request_body, repo_configs, action):
|
||||
|
||||
for repo_config in repo_configs:
|
||||
|
||||
# Validate secret token if present
|
||||
if 'secret-token' in repo_config and 'x-gitlab-token' in request_headers:
|
||||
|
||||
if repo_config['secret-token'] != request_headers['x-gitlab-token']:
|
||||
action.log_info("Request token does not match the 'secret-token' configured for repository %s." % repo_config['url'])
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -0,0 +1,31 @@
|
||||
from .base import WebhookRequestParserBase
|
||||
|
||||
|
||||
class GitLabCIRequestParser(WebhookRequestParserBase):
|
||||
|
||||
def get_matching_projects(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']:
|
||||
for n in ['repository', 'project']:
|
||||
if n in data and k in data[n]:
|
||||
repo_urls.append(data[n][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
|
||||
Reference in New Issue
Block a user