From 1c521cb04b99fc1f5a2315b1b4633107d3a3fee8 Mon Sep 17 00:00:00 2001
From: Qixiang Wan <qwan@redhat.com>
Date: Jun 21 2017 10:49:44 +0000
Subject: Support querying builds with event_type_id and/or event_search_key


---

diff --git a/freshmaker/api_utils.py b/freshmaker/api_utils.py
index d9b1e34..733c783 100644
--- a/freshmaker/api_utils.py
+++ b/freshmaker/api_utils.py
@@ -21,6 +21,7 @@
 
 from flask import request, url_for
 
+from freshmaker import db
 from freshmaker.types import ArtifactType, ArtifactBuildState
 from freshmaker.models import ArtifactBuild, Event
 
@@ -94,6 +95,17 @@ def filter_artifact_builds(flask_request):
     if search_query:
         query = query.filter_by(**search_query)
 
+    event_type_id = flask_request.args.get('event_type_id', None)
+    if event_type_id:
+        query = query.join(Event).filter(Event.event_type_id == event_type_id)
+
+    event_search_key = flask_request.args.get('event_search_key', None)
+    if event_search_key:
+        # use alias to avoid 'ambiguous column name' error when we have both
+        # event_type_id and event_search_key specified.
+        ea = db.aliased(Event)
+        query = query.join(ea).filter(ea.search_key == event_search_key)
+
     page = flask_request.args.get('page', 1, type=int)
     per_page = flask_request.args.get('per_page', 10, type=int)
     return query.paginate(page, per_page, False)
diff --git a/tests/test_views.py b/tests/test_views.py
index 1d74285..156f870 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -112,6 +112,59 @@ class TestViews(unittest.TestCase):
             self.client.get('/freshmaker/1/builds/?state=100')
         self.assertEqual(str(ctx.exception), 'An invalid state was supplied')
 
+    def test_query_build_by_event_type_id(self):
+        event1 = models.Event.create(db.session,
+                                     "2018-00000000-0000-0000-0000-000000000001",
+                                     "testmodule/master/?#0000000000000000000000000000000000000001",
+                                     events.GitModuleMetadataChangeEvent)
+        build1 = models.ArtifactBuild.create(db.session, event1, "testmodule", "module", 2345)
+        event2 = models.Event.create(db.session,
+                                     "2018-00000000-0000-0000-0000-000000000002",
+                                     "2345",
+                                     events.MBSModuleStateChangeEvent)
+        models.ArtifactBuild.create(db.session, event2, "testmodule2", "module", 2346, build1)
+
+        event3 = models.Event.create(db.session,
+                                     "2018-00000000-0000-0000-0000-000000000003",
+                                     "testmodule3/master/?#0000000000000000000000000000000000000001",
+                                     events.GitModuleMetadataChangeEvent)
+        models.ArtifactBuild.create(db.session, event3, "testmodule3", "module", 2347, build1)
+        db.session.commit()
+
+        resp = self.client.get('/freshmaker/1/builds/?event_type_id=%s' % models.EVENT_TYPES[events.TestingEvent])
+        builds = json.loads(resp.data.decode('utf8'))['items']
+        self.assertEqual(len(builds), 3)
+
+        resp = self.client.get('/freshmaker/1/builds/?event_type_id=%s' % models.EVENT_TYPES[events.GitModuleMetadataChangeEvent])
+        builds = json.loads(resp.data.decode('utf8'))['items']
+        self.assertEqual(len(builds), 2)
+
+        resp = self.client.get('/freshmaker/1/builds/?event_type_id=%s' % models.EVENT_TYPES[events.MBSModuleStateChangeEvent])
+        builds = json.loads(resp.data.decode('utf8'))['items']
+        self.assertEqual(len(builds), 1)
+
+        resp = self.client.get('/freshmaker/1/builds/?event_type_id=%s' % models.EVENT_TYPES[events.KojiTaskStateChangeEvent])
+        builds = json.loads(resp.data.decode('utf8'))['items']
+        self.assertEqual(len(builds), 0)
+
+    def test_query_build_by_event_search_key(self):
+        resp = self.client.get('/freshmaker/1/builds/?event_search_key=RHSA-2018-101')
+        builds = json.loads(resp.data.decode('utf8'))['items']
+        self.assertEqual(len(builds), 3)
+
+        resp = self.client.get('/freshmaker/1/builds/?event_search_key=RHSA-2018-102')
+        builds = json.loads(resp.data.decode('utf8'))['items']
+        self.assertEqual(len(builds), 0)
+
+    def test_query_build_by_event_type_id_and_search_key(self):
+        resp = self.client.get('/freshmaker/1/builds/?event_type_id=%s&event_search_key=RHSA-2018-101' % models.EVENT_TYPES[events.TestingEvent])
+        builds = json.loads(resp.data.decode('utf8'))['items']
+        self.assertEqual(len(builds), 3)
+
+        resp = self.client.get('/freshmaker/1/builds/?event_type_id=%s&event_search_key=RHSA-2018-102' % models.EVENT_TYPES[events.TestingEvent])
+        builds = json.loads(resp.data.decode('utf8'))['items']
+        self.assertEqual(len(builds), 0)
+
     def test_query_event(self):
         resp = self.client.get('/freshmaker/1/events/1')
         data = json.loads(resp.data.decode('utf8'))

