diff options
author | Oliver Poignant <oliver@poignant.se> | 2017-01-08 17:15:58 +0100 |
---|---|---|
committer | Oliver Poignant <oliver@poignant.se> | 2017-01-08 17:15:58 +0100 |
commit | 465cb9263fb1ffc9ba2d4a912847322920180caa (patch) | |
tree | 59932075b5f6426088704abac7c53f8c476da2a4 /gitautodeploy/parsers | |
parent | 324c3518d4b98da5bb660c62fa1503c47139c088 (diff) | |
download | Git-Auto-Deploy-origin/HEAD.zip Git-Auto-Deploy-origin/HEAD.tar.gz Git-Auto-Deploy-origin/HEAD.tar.bz2 |
RefactoringHEADorigin/masterorigin/HEADmaster
Diffstat (limited to 'gitautodeploy/parsers')
-rw-r--r-- | gitautodeploy/parsers/__init__.py | 49 | ||||
-rw-r--r-- | gitautodeploy/parsers/base.py (renamed from gitautodeploy/parsers/common.py) | 5 | ||||
-rw-r--r-- | gitautodeploy/parsers/bitbucket.py | 7 | ||||
-rw-r--r-- | gitautodeploy/parsers/coding.py | 8 | ||||
-rw-r--r-- | gitautodeploy/parsers/generic.py | 7 | ||||
-rw-r--r-- | gitautodeploy/parsers/github.py | 7 | ||||
-rw-r--r-- | gitautodeploy/parsers/gitlab.py | 7 | ||||
-rw-r--r-- | gitautodeploy/parsers/gitlabci.py | 7 |
8 files changed, 74 insertions, 23 deletions
diff --git a/gitautodeploy/parsers/__init__.py b/gitautodeploy/parsers/__init__.py index cecd1c7..f213339 100644 --- a/gitautodeploy/parsers/__init__.py +++ b/gitautodeploy/parsers/__init__.py @@ -3,4 +3,51 @@ from .github import GitHubRequestParser from .gitlab import GitLabRequestParser from .gitlabci import GitLabCIRequestParser from .generic import GenericRequestParser -from .coding import CodingRequestParser
\ No newline at end of file +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
\ No newline at end of file diff --git a/gitautodeploy/parsers/common.py b/gitautodeploy/parsers/base.py index b194062..f641d4c 100644 --- a/gitautodeploy/parsers/common.py +++ b/gitautodeploy/parsers/base.py @@ -1,5 +1,4 @@ - -class WebhookRequestParser(object): +class WebhookRequestParserBase(object): """Abstract parent class for git service parsers. Contains helper methods.""" @@ -27,4 +26,4 @@ class WebhookRequestParser(object): return configs def validate_request(self, request_headers, repo_configs, action): - return True
\ No newline at end of file + return True diff --git a/gitautodeploy/parsers/bitbucket.py b/gitautodeploy/parsers/bitbucket.py index b80f6aa..8905bfc 100644 --- a/gitautodeploy/parsers/bitbucket.py +++ b/gitautodeploy/parsers/bitbucket.py @@ -1,8 +1,9 @@ -from .common import WebhookRequestParser +from .base import WebhookRequestParserBase -class BitBucketRequestParser(WebhookRequestParser): - def get_repo_configs(self, request_headers, request_body, action): +class BitBucketRequestParser(WebhookRequestParserBase): + + def get_matching_projects(self, request_headers, request_body, action): import json data = json.loads(request_body) diff --git a/gitautodeploy/parsers/coding.py b/gitautodeploy/parsers/coding.py index 3d114d4..588ea22 100644 --- a/gitautodeploy/parsers/coding.py +++ b/gitautodeploy/parsers/coding.py @@ -1,8 +1,9 @@ -from .common import WebhookRequestParser +from .base import WebhookRequestParserBase -class CodingRequestParser(WebhookRequestParser): - def get_repo_configs(self, request_headers, request_body, action): +class CodingRequestParser(WebhookRequestParserBase): + + def get_matching_projects(self, request_headers, request_body, action): import json data = json.loads(request_body) @@ -35,6 +36,5 @@ class CodingRequestParser(WebhookRequestParser): return repo_configs - def verify_token(self, secret_token, request_token): return secret_token == request_token diff --git a/gitautodeploy/parsers/generic.py b/gitautodeploy/parsers/generic.py index c71bced..f8e8f31 100644 --- a/gitautodeploy/parsers/generic.py +++ b/gitautodeploy/parsers/generic.py @@ -1,8 +1,9 @@ -from .common import WebhookRequestParser +from .base import WebhookRequestParserBase -class GenericRequestParser(WebhookRequestParser): - def get_repo_configs(self, request_headers, request_body, action): +class GenericRequestParser(WebhookRequestParserBase): + + def get_matching_projects(self, request_headers, request_body, action): import json data = json.loads(request_body) diff --git a/gitautodeploy/parsers/github.py b/gitautodeploy/parsers/github.py index 5f2ba5d..a6659bd 100644 --- a/gitautodeploy/parsers/github.py +++ b/gitautodeploy/parsers/github.py @@ -1,8 +1,9 @@ -from .common import WebhookRequestParser +from .base import WebhookRequestParserBase -class GitHubRequestParser(WebhookRequestParser): - def get_repo_configs(self, request_headers, request_body, action): +class GitHubRequestParser(WebhookRequestParserBase): + + def get_matching_projects(self, request_headers, request_body, action): import json data = json.loads(request_body) diff --git a/gitautodeploy/parsers/gitlab.py b/gitautodeploy/parsers/gitlab.py index 6767907..6c6ca90 100644 --- a/gitautodeploy/parsers/gitlab.py +++ b/gitautodeploy/parsers/gitlab.py @@ -1,8 +1,9 @@ -from .common import WebhookRequestParser +from .base import WebhookRequestParserBase -class GitLabRequestParser(WebhookRequestParser): - def get_repo_configs(self, request_headers, request_body, action): +class GitLabRequestParser(WebhookRequestParserBase): + + def get_matching_projects(self, request_headers, request_body, action): import json data = json.loads(request_body) diff --git a/gitautodeploy/parsers/gitlabci.py b/gitautodeploy/parsers/gitlabci.py index eebf2bd..35d5ed4 100644 --- a/gitautodeploy/parsers/gitlabci.py +++ b/gitautodeploy/parsers/gitlabci.py @@ -1,8 +1,9 @@ -from .common import WebhookRequestParser +from .base import WebhookRequestParserBase -class GitLabCIRequestParser(WebhookRequestParser): - def get_repo_configs(self, request_headers, request_body, action): +class GitLabCIRequestParser(WebhookRequestParserBase): + + def get_matching_projects(self, request_headers, request_body, action): import json data = json.loads(request_body) |