From a31419d74c4562f3f9d4dad02a7b8ad1590aa97b Mon Sep 17 00:00:00 2001
From: Jayesh Velayudhan <jayesh@softwareheritage.org>
Date: Wed, 24 Aug 2022 17:04:56 +0200
Subject: [PATCH] Add revision log field in the revision type

Storage is returning a list of dicts for the revision log.
Add a from_dict converter to create the model objects from these dicts
This is not efficient and adds an extra iteration. Has to be
fixed in the storage in the future.

Related to T4366
---
 swh/graphql/resolvers/resolvers.py            |  8 ++--
 swh/graphql/resolvers/revision.py             |  6 ++-
 swh/graphql/tests/data.py                     | 27 ++++++++++-
 swh/graphql/tests/functional/test_revision.py | 47 +++++++++++++++++++
 4 files changed, 81 insertions(+), 7 deletions(-)

diff --git a/swh/graphql/resolvers/resolvers.py b/swh/graphql/resolvers/resolvers.py
index 946b423..2be89f2 100644
--- a/swh/graphql/resolvers/resolvers.py
+++ b/swh/graphql/resolvers/resolvers.py
@@ -245,10 +245,10 @@ def revision_parents_resolver(
     return resolver(obj, info, **kw)
 
 
-# @revision.field("revisionLog")
-# def revision_log_resolver(obj, info, **kw):
-#     resolver = get_connection_resolver("revision-log")
-#     return resolver(obj, info, **kw)
+@revision.field("revisionLog")
+def revision_log_resolver(obj, info, **kw):
+    resolver = get_connection_resolver("revision-log")
+    return resolver(obj, info, **kw)
 
 
 @directory.field("entries")
diff --git a/swh/graphql/resolvers/revision.py b/swh/graphql/resolvers/revision.py
index e5cd18b..ab25cb3 100644
--- a/swh/graphql/resolvers/revision.py
+++ b/swh/graphql/resolvers/revision.py
@@ -7,6 +7,7 @@ from typing import Union
 
 from swh.graphql.backends import archive
 from swh.graphql.utils import utils
+from swh.model.model import Revision
 from swh.model.swhids import CoreSWHID, ObjectType
 from swh.storage.interface import PagedResult
 
@@ -98,8 +99,11 @@ class LogRevisionConnection(BaseConnection):
     _node_class = BaseRevisionNode
 
     def _get_paged_result(self) -> PagedResult:
-        # STORAGE-TODO (date in revisionlog is a dict)
         log = archive.Archive().get_revision_log([self.obj.swhid.object_id])
+        # Storage is returning a list of dicts instead of model objects
+        # Following loop is to reverse that operation
+        # STORAGE-TODO; remove to_dict from storage.revision_log
+        log = [Revision.from_dict(rev) for rev in log]
         # FIXME, using dummy(local) pagination, move pagination to backend
         # To remove localpagination, just drop the paginated call
         # STORAGE-TODO (pagination)
diff --git a/swh/graphql/tests/data.py b/swh/graphql/tests/data.py
index aebdcc9..1216816 100644
--- a/swh/graphql/tests/data.py
+++ b/swh/graphql/tests/data.py
@@ -4,7 +4,7 @@
 # See top-level LICENSE file for more information
 
 from swh.model.hashutil import hash_to_bytes
-from swh.model.model import ObjectType, Release
+from swh.model.model import ObjectType, Release, Revision, RevisionType
 from swh.model.tests import swh_model_data
 
 
@@ -78,7 +78,30 @@ def get_releases_with_target():
     return [with_revision, with_release, with_directory, with_content]
 
 
-GRAPHQL_EXTRA_TEST_OBJECTS = {"release": get_releases_with_target()}
+def get_revisions_with_parents():
+    """
+    Revisions with real revisions as parents
+    """
+    return [
+        Revision(
+            id=hash_to_bytes("37580d63b8dcc0ec73e74994e66896858542844c"),
+            message=b"hello",
+            date=swh_model_data.DATES[0],
+            committer=swh_model_data.COMMITTERS[0],
+            author=swh_model_data.COMMITTERS[0],
+            committer_date=swh_model_data.DATES[0],
+            type=RevisionType.GIT,
+            directory=b"\x01" * 20,
+            synthetic=False,
+            parents=(get_revisions()[0].id, get_revisions()[1].id),
+        )
+    ]
+
+
+GRAPHQL_EXTRA_TEST_OBJECTS = {
+    "release": get_releases_with_target(),
+    "revision": get_revisions_with_parents(),
+}
 
 
 def populate_dummy_data(storage):
diff --git a/swh/graphql/tests/functional/test_revision.py b/swh/graphql/tests/functional/test_revision.py
index a0e7042..6c20c0b 100644
--- a/swh/graphql/tests/functional/test_revision.py
+++ b/swh/graphql/tests/functional/test_revision.py
@@ -110,3 +110,50 @@ def test_get_revision_as_target(client):
     assert revision_obj == {
         "swhid": "swh:1:rev:66c7c1cd9673275037140f2abff7b7b11fc9439c"
     }
+
+
+def test_get_revision_log(client):
+    revision_swhid = "swh:1:rev:37580d63b8dcc0ec73e74994e66896858542844c"
+    query_str = """
+    {
+      revision(swhid: "%s") {
+        swhid
+        revisionLog(first: 3) {
+          nodes {
+            swhid
+          }
+        }
+      }
+    }
+    """
+    data, _ = utils.get_query_response(client, query_str % revision_swhid)
+    assert data["revision"]["revisionLog"] == {
+        "nodes": [
+            {"swhid": "swh:1:rev:37580d63b8dcc0ec73e74994e66896858542844c"},
+            {"swhid": "swh:1:rev:66c7c1cd9673275037140f2abff7b7b11fc9439c"},
+            {"swhid": "swh:1:rev:c7f96242d73c267adc77c2908e64e0c1cb6a4431"},
+        ]
+    }
+
+
+def test_get_revision_parents(client):
+    revision_swhid = "swh:1:rev:37580d63b8dcc0ec73e74994e66896858542844c"
+    query_str = """
+    {
+      revision(swhid: "%s") {
+        swhid
+        parents {
+          nodes {
+            swhid
+          }
+        }
+      }
+    }
+    """
+    data, _ = utils.get_query_response(client, query_str % revision_swhid)
+    assert data["revision"]["parents"] == {
+        "nodes": [
+            {"swhid": "swh:1:rev:66c7c1cd9673275037140f2abff7b7b11fc9439c"},
+            {"swhid": "swh:1:rev:c7f96242d73c267adc77c2908e64e0c1cb6a4431"},
+        ]
+    }
-- 
GitLab