← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/launchpad:expose-build_metadata_url into launchpad:master

 

Jürgen Gmach has proposed merging ~jugmac00/launchpad:expose-build_metadata_url into launchpad:master.

Commit message:
Expose CharmRecipe.build_metadata_url

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/launchpad/+git/launchpad/+merge/490358

After a build has finished, this way the generated metadata of the fetch service can be retrieved.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/launchpad:expose-build_metadata_url into launchpad:master.
diff --git a/lib/lp/charms/interfaces/charmrecipebuild.py b/lib/lp/charms/interfaces/charmrecipebuild.py
index e744368..db978c3 100644
--- a/lib/lp/charms/interfaces/charmrecipebuild.py
+++ b/lib/lp/charms/interfaces/charmrecipebuild.py
@@ -280,6 +280,18 @@ class ICharmRecipeBuildView(IPackageBuildView):
 
         :return: A collection of URLs for this build."""
 
+    build_metadata_url = exported(
+        TextLine(
+            title=_("URL of the build metadata file"),
+            description=_(
+                "URL of the metadata file generated by the fetch service, if "
+                "it exists."
+            ),
+            required=False,
+            readonly=True,
+        )
+    )
+
 
 class ICharmRecipeBuildEdit(IBuildFarmJobEdit):
     """`ICharmRecipeBuild` methods that require launchpad.Edit."""
diff --git a/lib/lp/charms/model/charmrecipebuild.py b/lib/lp/charms/model/charmrecipebuild.py
index 3e6e95b..7a1648a 100644
--- a/lib/lp/charms/model/charmrecipebuild.py
+++ b/lib/lp/charms/model/charmrecipebuild.py
@@ -31,6 +31,7 @@ from zope.component import getUtility
 from zope.interface import implementer
 
 from lp.app.errors import NotFoundError
+from lp.buildmaster.builderproxy import BUILD_METADATA_FILENAME_FORMAT
 from lp.buildmaster.enums import (
     BuildFarmJobType,
     BuildQueueStatus,
@@ -415,6 +416,16 @@ class CharmRecipeBuild(PackageBuildMixin, StormBase):
             for _, lfa, _ in self.getFiles()
         ]
 
+    @property
+    def build_metadata_url(self):
+        metadata_filename = BUILD_METADATA_FILENAME_FORMAT.format(
+            build_id=self.build_cookie
+        )
+        for url in self.getFileUrls():
+            if url.endswith(metadata_filename):
+                return url
+        return None
+
     def addFile(self, lfa):
         """See `ICharmRecipeBuild`."""
         charm_file = CharmFile(build=self, library_file=lfa)
diff --git a/lib/lp/charms/tests/test_charmrecipebuild.py b/lib/lp/charms/tests/test_charmrecipebuild.py
index 4b59b37..a11d7fc 100644
--- a/lib/lp/charms/tests/test_charmrecipebuild.py
+++ b/lib/lp/charms/tests/test_charmrecipebuild.py
@@ -1035,3 +1035,50 @@ class TestCharmRecipeBuildWebservice(TestCaseWithFactory):
         browser.raiseHttpErrors = False
         for file_url in file_urls:
             self.assertCanOpenRedirectedUrl(browser, file_url)
+
+    def test_build_metadata_url(self):
+        # API clients can fetch the metadata from the build, generated by the
+        # fetch service
+        db_build = self.factory.makeCharmRecipeBuild(requester=self.person)
+        metadata_filename = f"{db_build.build_cookie}_metadata.json"
+        with person_logged_in(self.person):
+            file_1 = self.factory.makeLibraryFileAlias(
+                content="some_json",
+                filename="test_file.json",
+            )
+            db_build.addFile(file_1)
+            metadata_file = self.factory.makeLibraryFileAlias(
+                content="some_json",
+                filename=metadata_filename,
+            )
+            db_build.addFile(metadata_file)
+            file_2 = self.factory.makeLibraryFileAlias(
+                content="some_json",
+                filename="another_test_file.tar",
+            )
+            db_build.addFile(file_2)
+        build_url = api_url(db_build)
+        logout()
+        build = self.webservice.get(build_url).jsonBody()
+        self.assertIsNotNone(build["build_metadata_url"])
+        self.assertEndsWith(build["build_metadata_url"], metadata_filename)
+
+    def test_build_metadata_url_no_metadata_file(self):
+        # The attribute `build_metadata_url` returns None when metadata file
+        # does not exist.
+        db_build = self.factory.makeCharmRecipeBuild(requester=self.person)
+        with person_logged_in(self.person):
+            file_1 = self.factory.makeLibraryFileAlias(
+                content="some_json",
+                filename="test_file.json",
+            )
+            db_build.addFile(file_1)
+            file_2 = self.factory.makeLibraryFileAlias(
+                content="some_json",
+                filename="another_test_file.tar",
+            )
+            db_build.addFile(file_2)
+        build_url = api_url(db_build)
+        logout()
+        build = self.webservice.get(build_url).jsonBody()
+        self.assertIsNone(build["build_metadata_url"])