trigger: custom git payload JSON schema

This commit is contained in:
Jimmy Zelinskie 2015-04-15 16:52:46 -04:00
parent 876d9c53f2
commit bd57c6a8fb

View file

@ -8,6 +8,7 @@ import json
from github import Github, UnknownObjectException, GithubException
from tempfile import SpooledTemporaryFile
from jsonschema import validate
from app import app, userfiles as user_files, github_trigger
from util.tarfileappender import TarfileAppender
@ -547,6 +548,43 @@ class GithubBuildTrigger(BuildTrigger):
return None
class CustomBuildTrigger(BuildTrigger):
payload_schema = {
'type': 'object',
'properties': {
'commits': {'type': 'string'},
'ref': {'type': 'string'},
'default_branch': {'type': 'string'},
'commit_info': {
'type': 'object',
'properties': {
'url': {'type': 'string'},
'message': {'type': 'string'},
'date': {'type': 'string'},
'author': {
'type': 'object',
'properties': {
'username': {'type': 'string'},
'url': {'type': 'string'},
'avatar_url': {'type': 'string'},
},
'required': ['username', 'url', 'avatar_url'],
},
'committer': {
'type': 'object',
'properties': {
'username': {'type': 'string'},
'url': {'type': 'string'},
'avatar_url': {'type': 'string'},
},
'required': ['username', 'url', 'avatar_url'],
},
},
'required': ['url', 'message', 'date'],
},
},
'required': ['commits', 'ref', 'default_branch'],
}
@classmethod
def service_name(cls):
return 'custom'
@ -554,51 +592,12 @@ class CustomBuildTrigger(BuildTrigger):
def is_active(self, config):
return 'public_key' in config
@staticmethod
def _metadata_from_payload(payload):
def _metadata_from_payload(self, payload):
try:
metadata = {
'commit_sha': payload['commit'],
'ref': payload['ref'],
'default_branch': payload.get('default_branch', 'master'),
}
except KeyError:
validate(json.loads(payload), self.payload_schema)
except:
raise InvalidPayloadException()
commit_info = payload['commit_info']
if commit_info is not None:
try:
metadata['commit_info'] = {
'url': commit_info['url'],
'message': commit_info['message'],
'date': commit_info['date'],
}
except KeyError:
raise InvalidPayloadException()
author = commit_info['author']
if author is not None:
try:
metadata['commit_info']['author'] = {
'username': author['username'],
'avatar_url': author['avatar_url'],
'url': author['url'],
}
except KeyError:
raise InvalidPayloadException()
committer = commit_info['committer']
if committer is not None:
try:
metadata['commit_info']['committer'] = {
'username': committer['username'],
'avatar_url': committer['avatar_url'],
'url': committer['url'],
}
except KeyError:
raise InvalidPayloadException()
return metadata
return payload
def handle_trigger_request(self, request, trigger):
payload = request.get_json()