summaryrefslogtreecommitdiffstats
path: root/gitautodeploy/parsers/__init__.py
diff options
context:
space:
mode:
authorOliver Poignant <oliver@poignant.se>2017-01-08 17:15:58 +0100
committerOliver Poignant <oliver@poignant.se>2017-01-08 17:15:58 +0100
commit465cb9263fb1ffc9ba2d4a912847322920180caa (patch)
tree59932075b5f6426088704abac7c53f8c476da2a4 /gitautodeploy/parsers/__init__.py
parent324c3518d4b98da5bb660c62fa1503c47139c088 (diff)
downloadGit-Auto-Deploy-origin/master.zip
Git-Auto-Deploy-origin/master.tar.gz
Git-Auto-Deploy-origin/master.tar.bz2
Diffstat (limited to 'gitautodeploy/parsers/__init__.py')
-rw-r--r--gitautodeploy/parsers/__init__.py49
1 files changed, 48 insertions, 1 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