From 0bb697f1fcacdfc9a2245e14c348bebcb9b37d20 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Mon, 26 Apr 2021 17:09:44 -0700 Subject: [PATCH] Add artifacts to MQTT reporter The MQTT reporter now includes artifacts for completed builds. Systems which watch for MQTT events can now directly consume those artifacts without the intermediate step of looking them up via the API. Change-Id: I9df9e1dfd6854518c110dd65d4f89dea449c6fc0 --- doc/source/reference/drivers/mqtt.rst | 23 ++++++++++++++++++- .../mqtt-artifacts-4bdd3a8a7f7e3f33.yaml | 4 ++++ tests/unit/test_connection.py | 11 ++++++++- zuul/driver/mqtt/mqttreporter.py | 4 ++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/mqtt-artifacts-4bdd3a8a7f7e3f33.yaml diff --git a/doc/source/reference/drivers/mqtt.rst b/doc/source/reference/drivers/mqtt.rst index 68dfd9d0ad..be8fe67738 100644 --- a/doc/source/reference/drivers/mqtt.rst +++ b/doc/source/reference/drivers/mqtt.rst @@ -128,6 +128,26 @@ An MQTT report uses this schema: The build results (not present in start report). + .. attr:: artifacts + :type: list + + The build artifacts (not present in start report). + + This is a list of dictionaries corresponding to the returned artifacts. + + .. attr:: name + + The name of the artifact. + + .. attr:: url + + The url of the artifact. + + .. attr:: metadata + :type: dict + + The metadata of the artifact. This is a dictionary of + arbitrary key values determined by the job. Here is an example of a start message: @@ -191,7 +211,8 @@ Here is an example of a success message: 'log_url': 'https://logs.example.com/logs/3/3/1/check/linters/16e3e55/', 'web_url': 'https://tenant.example.com/t/tenant-one/build/16e3e55aca984c6c9a50cc3c5b21bb83/', 'result': 'SUCCESS', - 'dependencies': [] + 'dependencies': [], + 'artifacts': [], }], }, } diff --git a/releasenotes/notes/mqtt-artifacts-4bdd3a8a7f7e3f33.yaml b/releasenotes/notes/mqtt-artifacts-4bdd3a8a7f7e3f33.yaml new file mode 100644 index 0000000000..9ffe70f45d --- /dev/null +++ b/releasenotes/notes/mqtt-artifacts-4bdd3a8a7f7e3f33.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + The MQTT reporter now includes artifact information along with build results. diff --git a/tests/unit/test_connection.py b/tests/unit/test_connection.py index 570fd052d8..d78342e3cf 100644 --- a/tests/unit/test_connection.py +++ b/tests/unit/test_connection.py @@ -586,8 +586,15 @@ class TestMQTTConnection(ZuulTestCase): "Test the MQTT reporter" # Add a success result A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') + artifact = {'name': 'image', + 'url': 'http://example.com/image', + 'metadata': { + 'type': 'container_image' + }} self.executor_server.returnData( - "test", A, {"zuul": {"log_url": "some-log-url"}} + "test", A, {"zuul": {"log_url": "some-log-url", + 'artifacts': [artifact], + }} ) self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) self.waitUntilSettled() @@ -606,6 +613,7 @@ class TestMQTTConnection(ZuulTestCase): self.assertEquals(mqtt_payload['buildset']['builds'][0]['job_name'], 'test') self.assertNotIn('result', mqtt_payload['buildset']['builds'][0]) + self.assertNotIn('artifacts', mqtt_payload['buildset']['builds'][0]) self.assertEquals(success_event.get('topic'), 'tenant-one/zuul_buildset/check/org/project/master') @@ -621,6 +629,7 @@ class TestMQTTConnection(ZuulTestCase): self.assertEquals(test_job['job_name'], 'test') self.assertEquals(test_job['result'], 'SUCCESS') self.assertEquals(test_job['dependencies'], []) + self.assertEquals(test_job['artifacts'], [artifact]) # Both log- and web-url should point to the same URL which is specified # in the build result data under zuul.log_url. self.assertEquals(test_job['log_url'], 'some-log-url/') diff --git a/zuul/driver/mqtt/mqttreporter.py b/zuul/driver/mqtt/mqttreporter.py index e489f5908a..3376f18666 100644 --- a/zuul/driver/mqtt/mqttreporter.py +++ b/zuul/driver/mqtt/mqttreporter.py @@ -17,6 +17,7 @@ import time import voluptuous as v from zuul.lib.logutil import get_annotated_logger +from zuul.lib.result_data import get_artifacts_from_result_data from zuul.reporter import BaseReporter @@ -73,7 +74,10 @@ class MQTTReporter(BaseReporter): 'web_url': web_url, 'result': result, 'dependencies': [j.name for j in job.dependencies], + 'artifacts': get_artifacts_from_result_data( + build.result_data, logger=log) }) + # Report build data of retried builds if available retry_builds = item.current_build_set.getRetryBuildsForJob( job.name)