parent
8afb4691a5
commit
f99e74f0a1
3 changed files with 165 additions and 9 deletions
|
@ -1,7 +1,7 @@
|
|||
import logging
|
||||
import json
|
||||
|
||||
from jsonschema import validate
|
||||
from jsonschema import validate, ValidationError
|
||||
from buildtrigger.triggerutil import (RepositoryReadException, TriggerActivationException,
|
||||
TriggerStartException, ValidationRequestException,
|
||||
InvalidPayloadException,
|
||||
|
@ -10,11 +10,60 @@ from buildtrigger.triggerutil import (RepositoryReadException, TriggerActivation
|
|||
|
||||
from buildtrigger.basehandler import BuildTriggerHandler
|
||||
|
||||
from buildtrigger.bitbuckethandler import (BITBUCKET_WEBHOOK_PAYLOAD_SCHEMA as bb_schema,
|
||||
get_transformed_webhook_payload as bb_payload)
|
||||
|
||||
from buildtrigger.githubhandler import (GITHUB_WEBHOOK_PAYLOAD_SCHEMA as gh_schema,
|
||||
get_transformed_webhook_payload as gh_payload)
|
||||
|
||||
from buildtrigger.bitbuckethandler import (BITBUCKET_WEBHOOK_PAYLOAD_SCHEMA as bb_schema,
|
||||
get_transformed_webhook_payload as bb_payload)
|
||||
|
||||
from buildtrigger.gitlabhandler import (GITLAB_WEBHOOK_PAYLOAD_SCHEMA as gl_schema,
|
||||
get_transformed_webhook_payload as gl_payload)
|
||||
|
||||
from util.security.ssh import generate_ssh_keypair
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Defines an ordered set of tuples of the schemas and associated transformation functions
|
||||
# for incoming webhook payloads.
|
||||
SCHEMA_AND_HANDLERS = [
|
||||
(gh_schema, gh_payload),
|
||||
(bb_schema, bb_payload),
|
||||
(gl_schema, gl_payload),
|
||||
]
|
||||
|
||||
|
||||
def custom_trigger_payload(metadata, git_url):
|
||||
# First try the customhandler schema. If it matches, nothing more to do.
|
||||
custom_handler_validation_error = None
|
||||
try:
|
||||
validate(metadata, CustomBuildTrigger.payload_schema)
|
||||
except ValidationError as vex:
|
||||
custom_handler_validation_error = vex
|
||||
|
||||
# Otherwise, try the defined schemas, in order, until we find a match.
|
||||
for schema, handler in SCHEMA_AND_HANDLERS:
|
||||
try:
|
||||
validate(metadata, schema)
|
||||
except ValidationError:
|
||||
continue
|
||||
|
||||
result = handler(metadata)
|
||||
result['git_url'] = git_url
|
||||
return result
|
||||
|
||||
# If we have reached this point and no other schemas validated, then raise the error for the
|
||||
# custom schema.
|
||||
if custom_handler_validation_error is not None:
|
||||
raise InvalidPayloadException(custom_handler_validation_error.message)
|
||||
|
||||
metadata['git_url'] = git_url
|
||||
return metadata
|
||||
|
||||
|
||||
class CustomBuildTrigger(BuildTriggerHandler):
|
||||
payload_schema = {
|
||||
'type': 'object',
|
||||
|
@ -101,13 +150,14 @@ class CustomBuildTrigger(BuildTriggerHandler):
|
|||
def is_active(self):
|
||||
return self.config.has_key('credentials')
|
||||
|
||||
def _metadata_from_payload(self, payload):
|
||||
def _metadata_from_payload(self, payload, git_url):
|
||||
# Parse the JSON payload.
|
||||
try:
|
||||
metadata = json.loads(payload)
|
||||
validate(metadata, self.payload_schema)
|
||||
except Exception as e:
|
||||
raise InvalidPayloadException(e.message)
|
||||
return metadata
|
||||
except ValueError as vex:
|
||||
raise InvalidPayloadException(vex.message)
|
||||
|
||||
return custom_trigger_payload(metadata, git_url)
|
||||
|
||||
def handle_trigger_request(self, request):
|
||||
payload = request.data
|
||||
|
@ -116,9 +166,7 @@ class CustomBuildTrigger(BuildTriggerHandler):
|
|||
|
||||
logger.debug('Payload %s', payload)
|
||||
|
||||
metadata = self._metadata_from_payload(payload)
|
||||
metadata['git_url'] = self.config['build_source']
|
||||
|
||||
metadata = self._metadata_from_payload(payload, self.config['build_source'])
|
||||
prepared = self.prepare_build(metadata)
|
||||
|
||||
# Check if we should skip this build.
|
||||
|
|
Reference in a new issue