zuul/zuul/lib/result_data.py
Tobias Henkel 9879cab0d1
Support emitting warnings via zuul_return
We already have the infrastructure in place for adding warnings to the
reporting. Plumb that through to zuul_return so jobs can do that on
purpose as well. An example could be a post playbook that analyzes
performance statistics and emits a warning about inefficient usage of
the build node resources.

Change-Id: I4c3b85dc8f4c69c55cbc6168b8a66afce8b50a97
2020-08-31 11:54:46 +02:00

93 lines
2.6 KiB
Python

# Copyright 2018-2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import voluptuous as v
import urllib.parse
artifact = {
'name': v.Required(str),
'url': v.Required(str),
'metadata': dict,
}
artifact_data = {
'zuul': {
'log_url': str,
'artifacts': [artifact],
v.Extra: object,
},
v.Extra: object,
}
warning_data = {
'zuul': {
'log_url': str,
'warnings': [str],
v.Extra: object,
},
v.Extra: object,
}
artifact_schema = v.Schema(artifact_data)
warning_schema = v.Schema(warning_data)
def validate_schema(data, schema):
try:
schema(data)
except Exception:
return False
return True
def get_artifacts_from_result_data(result_data, logger=None):
ret = []
if validate_schema(result_data, artifact_schema):
artifacts = result_data.get('zuul', {}).get(
'artifacts', [])
default_url = result_data.get('zuul', {}).get(
'log_url')
if default_url:
if default_url[-1] != '/':
default_url += '/'
for artifact in artifacts:
url = artifact['url']
if default_url:
# If the artifact url is relative, it will be combined
# with the log_url; if it is absolute, it will replace
# it.
try:
url = urllib.parse.urljoin(default_url, url)
except Exception:
if logger:
logger.debug("Error parsing URL:",
exc_info=1)
d = artifact.copy()
d['url'] = url
ret.append(d)
else:
if logger:
logger.debug("Result data did not pass artifact schema "
"validation: %s", result_data)
return ret
def get_warnings_from_result_data(result_data, logger=None):
if validate_schema(result_data, warning_schema):
return result_data.get('zuul', {}).get('warnings', [])
else:
if logger:
logger.debug("Result data did not pass warnings schema "
"validation: %s", result_data)