← Back to team overview

launchpad-reviewers team mailing list archive

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

 

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

Commit message:
Expose RockRecipe.build_metadata_url

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/launchpad/+git/launchpad/+merge/474914
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/launchpad:expose-build_metadata_url-for-rock-recipes into launchpad:master.
diff --git a/lib/lp/rocks/interfaces/rockrecipebuild.py b/lib/lp/rocks/interfaces/rockrecipebuild.py
index e4b5d12..74868b8 100644
--- a/lib/lp/rocks/interfaces/rockrecipebuild.py
+++ b/lib/lp/rocks/interfaces/rockrecipebuild.py
@@ -132,6 +132,18 @@ class IRockRecipeBuildView(IPackageBuildView):
         _("A dict of data about store upload progress.")
     )
 
+    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,
+        )
+    )
+
     def getFiles():
         """Retrieve the build's `IRockFile` records.
 
diff --git a/lib/lp/rocks/model/rockrecipebuild.py b/lib/lp/rocks/model/rockrecipebuild.py
index 9905795..496903e 100644
--- a/lib/lp/rocks/model/rockrecipebuild.py
+++ b/lib/lp/rocks/model/rockrecipebuild.py
@@ -18,6 +18,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,
@@ -313,6 +314,16 @@ class RockRecipeBuild(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 `IRockRecipeBuild`."""
         rock_file = RockFile(build=self, library_file=lfa)
diff --git a/lib/lp/rocks/tests/test_rockrecipebuild.py b/lib/lp/rocks/tests/test_rockrecipebuild.py
index 98d6b83..3992c3f 100644
--- a/lib/lp/rocks/tests/test_rockrecipebuild.py
+++ b/lib/lp/rocks/tests/test_rockrecipebuild.py
@@ -612,3 +612,50 @@ class TestRockRecipeBuildWebservice(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.makeRockRecipeBuild(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.makeRockRecipeBuild(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"])

Follow ups