Repository: fedora-infra/bodhi

Issue:
Graphql : Query specific BuildrootOverride
I think we should be able to query builds using the following attributes :

  build nvr (relationship)
  submitter username (relationship User)
  submission_date
  expiration_date


Discussion:

Resolving patch:
diff --git a/bodhi/server/graphql_schemas.py b/bodhi/server/graphql_schemas.py
--- a/bodhi/server/graphql_schemas.py
+++ b/bodhi/server/graphql_schemas.py
@@ -19,7 +19,11 @@
 from graphene import relay, Field, String
 from graphene_sqlalchemy import SQLAlchemyObjectType
 
-from bodhi.server.models import Release as ReleaseModel, Update as UpdateModel
+from bodhi.server.models import (
+    Release as ReleaseModel,
+    Update as UpdateModel,
+    BuildrootOverride as BuildrootOverrideModel
+)
 
 
 class Release(SQLAlchemyObjectType):
@@ -45,3 +49,14 @@ class Meta:
     status = Field(String)
     request = Field(String)
     date_approved = Field(String)
+
+
+class BuildrootOverride(SQLAlchemyObjectType):
+    """Type object representing an update from bodhi.server.models."""
+
+    class Meta:
+        """Allow to set different options to the class."""
+
+        model = BuildrootOverrideModel
+        interfaces = (relay.Node, )
+    submitter = Field(String)
diff --git a/bodhi/server/services/graphql.py b/bodhi/server/services/graphql.py
--- a/bodhi/server/services/graphql.py
+++ b/bodhi/server/services/graphql.py
@@ -21,7 +21,15 @@
 from webob_graphql import serve_graphql_request
 
 from bodhi.server.config import config
-from bodhi.server.graphql_schemas import Release, ReleaseModel, Update, UpdateModel
+from bodhi.server.models import Build as BuildModel, User as UserModel
+from bodhi.server.graphql_schemas import (
+    Release,
+    ReleaseModel,
+    Update,
+    UpdateModel,
+    BuildrootOverride,
+    BuildrootOverrideModel
+)
 
 graphql = Service(name='graphql', path='/graphql', description='graphql service')
 
@@ -60,6 +68,13 @@ class Query(graphene.ObjectType):
         date_approved=graphene.String(), alias=graphene.String(),
         user_id=graphene.Int(), release_name=graphene.String())
 
+    getBuildrootOverrides = graphene.Field(
+        lambda: graphene.List(BuildrootOverride),
+        submission_date=graphene.DateTime(),
+        expiration_date=graphene.DateTime(),
+        build_nvr=graphene.String(),
+        submitter_username=graphene.String())
+
     def resolve_allReleases(self, info):
         """Answer Queries by fetching data from the Schema."""
         query = Release.get_query(info)  # SQLAlchemy query
@@ -137,5 +152,28 @@ def resolve_getUpdates(self, info, **args):
 
         return query.all()
 
+    def resolve_getBuildrootOverrides(self, info, **args):
+        """Answer Release queries with a given argument."""
+        query = BuildrootOverride.get_query(info)
+
+        submission_date = args.get("submission_date")
+        if submission_date is not None:
+            query = query.filter(BuildrootOverrideModel.submission_date == submission_date)
+
+        expiration_date = args.get("expiration_date")
+        if expiration_date is not None:
+            query = query.filter(BuildrootOverrideModel.expiration_date == expiration_date)
+
+        build_nvr = args.get("build_nvr")
+        if build_nvr is not None:
+            query = query.join(BuildrootOverrideModel.build).filter(BuildModel.nvr == build_nvr)
+
+        submitter_username = args.get("submitter_username")
+        if submitter_username is not None:
+            query = query.join(BuildrootOverrideModel.submitter).filter(
+                UserModel.name == submitter_username)
+
+        return query.all()
+
 
 schema = graphene.Schema(query=Query)


Test patch:
diff --git a/bodhi/tests/server/services/test_graphql.py b/bodhi/tests/server/services/test_graphql.py
--- a/bodhi/tests/server/services/test_graphql.py
+++ b/bodhi/tests/server/services/test_graphql.py
@@ -21,6 +21,7 @@
 
 from bodhi.tests.server import base
 from bodhi.server.services.graphql import schema
+from bodhi.server import models
 
 
 class TestGraphQLService(base.BasePyTestCase):
@@ -192,3 +193,59 @@ def test_getUpdates(self):
                 }]
             }
         }
+
+    def test_getBuildrootOverrides(self):
+        """Testing getBuildOverrides query."""
+        release = models.Release.get('F17')
+
+        package = models.RpmPackage(name='just-testing')
+        self.db.add(package)
+        build = models.RpmBuild(nvr='just-testing-1.0-2.fc17', package=package, release=release)
+        self.db.add(build)
+        another_user = models.User(name='aUser')
+        self.db.add(another_user)
+
+        expiration_date = datetime.datetime(2020, 10, 13, 16, 16, 22, 438484)
+        submission_date = datetime.datetime(2020, 10, 12, 16, 16, 22, 438484)
+
+        override = models.BuildrootOverride(build=build, submitter=another_user,
+                                            notes='Crazy! 😱',
+                                            expiration_date=expiration_date,
+                                            submission_date=submission_date)
+        self.db.add(override)
+        self.db.flush()
+        client = Client(schema)
+        self.db.commit()
+
+        executed = client.execute("""{  getBuildrootOverrides(buildNvr: "just-testing-1.0-2.fc17")
+                                  {  submissionDate  expirationDate  }}""")
+        assert executed == {
+            'data': {
+                'getBuildrootOverrides': [{
+                    'expirationDate': "2020-10-13T16:16:22.438484",
+                    'submissionDate': "2020-10-12T16:16:22.438484",
+                }]
+            }
+        }
+
+        executed = client.execute("""{  getBuildrootOverrides(submissionDate: "2020-10-12T16:16:22.438484",
+                                  expirationDate: "2020-10-13T16:16:22.438484")
+                                  {  notes  }}""")
+        assert executed == {
+            'data': {
+                'getBuildrootOverrides': [{
+                    'notes': "Crazy! 😱"
+                }]
+            }
+        }
+
+        executed = client.execute("""{  getBuildrootOverrides(submitterUsername: "aUser",
+                                  expirationDate: "2020-10-13T16:16:22.438484")
+                                  {  notes  }}""")
+        assert executed == {
+            'data': {
+                'getBuildrootOverrides': [{
+                    'notes': "Crazy! 😱"
+                }]
+            }
+        }