Make sure a JSON error in build or trigger config never causes a crash

This commit is contained in:
Joseph Schorr 2014-03-11 14:52:20 -04:00
parent c5fa12329c
commit e650da5278

View file

@ -1129,9 +1129,16 @@ def get_repo(namespace, repository):
abort(403) # Permission denied abort(403) # Permission denied
def get_trigger_config(trigger):
try:
return json.loads(trigger.config)
except:
return {}
def trigger_view(trigger): def trigger_view(trigger):
if trigger and trigger.uuid: if trigger and trigger.uuid:
config_dict = json.loads(trigger.config) config_dict = get_trigger_config(trigger)
build_trigger = BuildTrigger.get_trigger_for_service(trigger.service.name) build_trigger = BuildTrigger.get_trigger_for_service(trigger.service.name)
return { return {
'service': trigger.service.name, 'service': trigger.service.name,
@ -1144,15 +1151,23 @@ def trigger_view(trigger):
return None return None
def get_job_config(build_obj):
try:
return json.loads(build_obj.job_config)
except:
return None
def build_status_view(build_obj, can_write=False): def build_status_view(build_obj, can_write=False):
status = build_logs.get_status(build_obj.uuid) status = build_logs.get_status(build_obj.uuid)
return { return {
'id': build_obj.uuid, 'id': build_obj.uuid,
'phase': build_obj.phase, 'phase': build_obj.phase,
'started': build_obj.started, 'started': build_obj.started,
'display_name': build_obj.display_name, 'display_name': build_obj.display_name,
'status': status, 'status': status,
'job_config': json.loads(build_obj.job_config) if can_write else None, 'job_config': get_job_config(build_obj) if can_write else None,
'is_writer': can_write, 'is_writer': can_write,
'trigger': trigger_view(build_obj.trigger), 'trigger': trigger_view(build_obj.trigger),
'resource_key': build_obj.resource_key, 'resource_key': build_obj.resource_key,
@ -1419,7 +1434,7 @@ def activate_build_trigger(namespace, repository, trigger_uuid):
return return
handler = BuildTrigger.get_trigger_for_service(trigger.service.name) handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
existing_config_dict = json.loads(trigger.config) existing_config_dict = get_trigger_config(trigger)
if handler.is_active(existing_config_dict): if handler.is_active(existing_config_dict):
abort(400) abort(400)
return return
@ -1478,13 +1493,12 @@ def manually_start_build_trigger(namespace, repository, trigger_uuid):
return return
handler = BuildTrigger.get_trigger_for_service(trigger.service.name) handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
existing_config_dict = json.loads(trigger.config) config_dict = get_trigger_config(trigger)
if not handler.is_active(existing_config_dict): if not handler.is_active(config_dict):
abort(400) abort(400)
return return
specs = handler.manual_start(trigger.auth_token, specs = handler.manual_start(trigger.auth_token, config_dict)
json.loads(trigger.config))
dockerfile_id, tags, name, subdir = specs dockerfile_id, tags, name, subdir = specs
repo = model.get_repository(namespace, repository) repo = model.get_repository(namespace, repository)
@ -1571,7 +1585,7 @@ def delete_build_trigger(namespace, repository, trigger_uuid):
return return
handler = BuildTrigger.get_trigger_for_service(trigger.service.name) handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
config_dict = json.loads(trigger.config) config_dict = get_trigger_config(trigger)
if handler.is_active(config_dict): if handler.is_active(config_dict):
try: try:
handler.deactivate(trigger.auth_token, config_dict) handler.deactivate(trigger.auth_token, config_dict)