← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~ilasc/launchpad:add-get-build-by-store-revision-to-snap into launchpad:master

 

Ioana Lasc has proposed merging ~ilasc/launchpad:add-get-build-by-store-revision-to-snap into launchpad:master.

Commit message:
Add getBuildByStoreRevision to Snap

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~ilasc/launchpad/+git/launchpad/+merge/407250
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~ilasc/launchpad:add-get-build-by-store-revision-to-snap into launchpad:master.
diff --git a/lib/lp/snappy/interfaces/snap.py b/lib/lp/snappy/interfaces/snap.py
index 2445553..400b92e 100644
--- a/lib/lp/snappy/interfaces/snap.py
+++ b/lib/lp/snappy/interfaces/snap.py
@@ -529,6 +529,21 @@ class ISnapView(Interface):
 
     @call_with(user=REQUEST_USER)
     @operation_parameters(
+        store_upload_revision=Int(title="Store revision",
+                                  required=True))
+    @export_read_operation()
+    @operation_for_version("devel")
+    def getBuildByStoreRevision(store_upload_revision, user=None):
+        """Returns the build (if any) of that snap recipe
+            that has the given store_upload_revision.
+
+        :param store_upload_revision: A list of snap build request IDs.
+        :param user: The `IPerson` requesting this information.
+        :return: An 'ISnapBuild' or None.
+        """
+
+    @call_with(user=REQUEST_USER)
+    @operation_parameters(
         request_ids=List(
             title=_("A list of snap build request IDs."), value_type=Int(),
             required=False),
diff --git a/lib/lp/snappy/model/snap.py b/lib/lp/snappy/model/snap.py
index 24aa2be..fa20509 100644
--- a/lib/lp/snappy/model/snap.py
+++ b/lib/lp/snappy/model/snap.py
@@ -1111,6 +1111,15 @@ class Snap(Storm, WebhookTargetMixin):
 
         return result
 
+    def getBuildByStoreRevision(self, store_upload_revision):
+        results = list(Store.of(self).find(
+            SnapBuild,
+            SnapBuild.snap == self))
+        for build in results:
+            if build.store_upload_revision == store_upload_revision:
+                return build
+        return None
+
     @property
     def builds(self):
         """See `ISnap`."""
diff --git a/lib/lp/snappy/tests/test_snap.py b/lib/lp/snappy/tests/test_snap.py
index 4d9c92f..b64ddc4 100644
--- a/lib/lp/snappy/tests/test_snap.py
+++ b/lib/lp/snappy/tests/test_snap.py
@@ -124,8 +124,8 @@ from lp.snappy.interfaces.snapbase import (
     )
 from lp.snappy.interfaces.snapbuild import (
     ISnapBuild,
-    ISnapBuildSet,
-    )
+    ISnapBuildSet, SnapBuildStoreUploadStatus,
+)
 from lp.snappy.interfaces.snapbuildjob import ISnapStoreUploadJobSource
 from lp.snappy.interfaces.snapjob import ISnapRequestBuildsJobSource
 from lp.snappy.interfaces.snapstoreclient import ISnapStoreClient
@@ -137,6 +137,9 @@ from lp.snappy.model.snap import (
 from lp.snappy.model.snapbuild import SnapFile
 from lp.snappy.model.snapbuildjob import SnapBuildJob
 from lp.snappy.model.snapjob import SnapJob
+from lp.snappy.tests.test_snapbuildjob import (
+    FakeSnapStoreClient,
+    run_isolated_jobs)
 from lp.testing import (
     admin_logged_in,
     ANONYMOUS,
@@ -1079,6 +1082,45 @@ class TestSnap(TestCaseWithFactory):
             snap.destroySelf()
         self.assertFalse(getUtility(ISnapSet).exists(owner, "condemned"))
 
+    def test_getBuildByStoreRevision(self):
+        snap1 = self.factory.makeSnap()
+        build = self.factory.makeSnapBuild(
+            snap=snap1,
+            status=BuildStatus.FULLYBUILT)
+        job = getUtility(ISnapStoreUploadJobSource).create(build)
+        client = FakeSnapStoreClient()
+        client.upload.result = (
+            "http://sca.example/dev/api/snaps/1/builds/1/status";)
+        client.checkStatus.result = (
+            "http://sca.example/dev/click-apps/1/rev/1/";, 1)
+        self.useFixture(ZopeUtilityFixture(client, ISnapStoreClient))
+        with dbuser(config.ISnapStoreUploadJobSource.dbuser):
+            run_isolated_jobs([job])
+        self.assertEqual(
+            SnapBuildStoreUploadStatus.UPLOADED, build.store_upload_status)
+        self.assertEqual(build.store_upload_revision, 1)
+        self.assertEqual(snap1.getBuildByStoreRevision(1), build)
+
+        # build & upload again, check revision
+        # and that we return the second build for revision 2
+        build2 = self.factory.makeSnapBuild(
+            snap=snap1,
+            status=BuildStatus.FULLYBUILT)
+        job = getUtility(ISnapStoreUploadJobSource).create(build2)
+        client = FakeSnapStoreClient()
+        client.upload.result = (
+            "http://sca.example/dev/api/snaps/1/builds/2/status";)
+        client.checkStatus.result = (
+            "http://sca.example/dev/click-apps/1/rev/2/";, 2)
+        self.useFixture(ZopeUtilityFixture(client, ISnapStoreClient))
+        with dbuser(config.ISnapStoreUploadJobSource.dbuser):
+            run_isolated_jobs([job])
+        self.assertEqual(
+            SnapBuildStoreUploadStatus.UPLOADED, build2.store_upload_status)
+        self.assertEqual(build2.store_upload_revision, 2)
+        self.assertEqual(snap1.getBuildByStoreRevision(2), build2)
+        self.assertEqual(snap1.getBuildByStoreRevision(1), build)
+
     def test_getBuildSummariesForSnapBuildIds(self):
         snap1 = self.factory.makeSnap()
         snap2 = self.factory.makeSnap()

References