From 5e9d398356ee2ca1e3f752d4f193cb58672ec20f Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Aug 25 2021 11:20:28 +0000
Subject: [PATCH 1/6] honour taginfo option in policy_get_build_tags


Related: https://pagure.io/koji/issue/2917

---

diff --git a/hub/kojihub.py b/hub/kojihub.py
index f7efcc4..e59a19b 100644
--- a/hub/kojihub.py
+++ b/hub/kojihub.py
@@ -9580,16 +9580,27 @@ def policy_get_cgs(data):
 def policy_get_build_tags(data, taginfo=False):
     """If taginfo is set, return list of taginfos, else list of names only"""
     if 'build_tag' in data:
-        return [get_tag(data['build_tag'], strict=True)['name']]
+        buildtag = get_tag(data['build_tag'], strict=True, event="auto")
+        if taginfo:
+            return buildtag
+        else:
+            return buildtag['name']
     elif 'build_tags' in data:
-        return [get_tag(t, strict=True)['name'] for t in data['build_tags']]
+        build_tags = [get_tag(t, strict=True, event="auto") for t in data['build_tags']]
+        if taginfo:
+            return build_tags
+        else:
+            return [t['name'] for t in build_tags]
 
     # see if we have a target
     target = data.get('target')
     if target:
         target = get_build_target(target, strict=False)
         if target:
-            return [target['build_tag_name']]
+            if taginfo:
+                return [get_tag(target['build_tag'], strict=True, event="auto")]
+            else:
+                return [target['build_tag_name']]
 
     # otherwise look at buildroots
     tags = {}
@@ -9598,7 +9609,10 @@ def policy_get_build_tags(data, taginfo=False):
             tags[None] = None
         else:
             tinfo = get_buildroot(br_id, strict=True)
-            tags[tinfo['tag_name']] = tinfo
+            if taginfo:
+                tags[tinfo['tag_name']] = get_tag(tinfo, strict=True)
+            else:
+                tags[tinfo['tag_name']] = tinfo
 
     if taginfo:
         tags = tags.values()

From 704bce846c018e1724267b8dd42349d586aed32f Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Aug 25 2021 14:15:01 +0000
Subject: [PATCH 2/6] fix getting tag from buildroot


---

diff --git a/hub/kojihub.py b/hub/kojihub.py
index e59a19b..18c0294 100644
--- a/hub/kojihub.py
+++ b/hub/kojihub.py
@@ -9610,9 +9610,9 @@ def policy_get_build_tags(data, taginfo=False):
         else:
             tinfo = get_buildroot(br_id, strict=True)
             if taginfo:
-                tags[tinfo['tag_name']] = get_tag(tinfo, strict=True)
+                tags[tinfo['tag_name']] = get_tag(tinfo['tag_name'], strict=True, event="auto")
             else:
-                tags[tinfo['tag_name']] = tinfo
+                tags[tinfo['tag_name']] = tinfo['tag_name']
 
     if taginfo:
         tags = tags.values()

From 9ebad965562fac950363db5acb38ce9520a5dca8 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Aug 25 2021 14:17:52 +0000
Subject: [PATCH 3/6] query inheritance from last known tag appearance


---

diff --git a/hub/kojihub.py b/hub/kojihub.py
index 18c0294..9c1c841 100644
--- a/hub/kojihub.py
+++ b/hub/kojihub.py
@@ -9831,7 +9831,9 @@ class BuildTagInheritsFromTest(koji.policy.BaseSimpleTest):
                 # content generator buildroots might not have tag info
                 continue
 
-            for tag in readFullInheritance(tinfo['tag_id']):
+            # for possibly deleted tags in inheritance chain
+            # we need to look into event from last known configuration
+            for tag in readFullInheritance(tinfo['id'], event=tinfo.get('revoke_event')):
                 if multi_fnmatch(tag['name'], args):
                     return True
         # otherwise...

From b9b952e4d6fca99405a51697ebec7c361e96c474 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Aug 30 2021 11:31:31 +0000
Subject: [PATCH 4/6] getTag returns query_event


---

diff --git a/hub/kojihub.py b/hub/kojihub.py
index 9c1c841..786b5c1 100644
--- a/hub/kojihub.py
+++ b/hub/kojihub.py
@@ -3380,6 +3380,8 @@ def get_tag(tagInfo, strict=False, event=None, blocked=False):
     - maven_support :           maven support flag (boolean)
     - maven_include_all :       maven include all flag (boolean)
     - extra :   extra tag parameters (dictionary)
+    - query_event : return "event" parameter for current call
+                    if something was passed in
 
     If there is no tag matching the given tagInfo, and strict is False,
     return None.  If strict is True, raise a GenericError.
@@ -3442,6 +3444,8 @@ def get_tag(tagInfo, strict=False, event=None, blocked=False):
             raise koji.GenericError("No such tagInfo: %r" % tagInfo)
         return None
     result['extra'] = get_tag_extra(result, event, blocked=blocked)
+    if event is not None:
+        result['query_event'] = event
     return result
 
 
@@ -9579,40 +9583,35 @@ def policy_get_cgs(data):
 
 def policy_get_build_tags(data, taginfo=False):
     """If taginfo is set, return list of taginfos, else list of names only"""
+    tags = {}
     if 'build_tag' in data:
         buildtag = get_tag(data['build_tag'], strict=True, event="auto")
+        # only cases we don't return list
         if taginfo:
             return buildtag
         else:
             return buildtag['name']
     elif 'build_tags' in data:
         build_tags = [get_tag(t, strict=True, event="auto") for t in data['build_tags']]
-        if taginfo:
-            return build_tags
-        else:
-            return [t['name'] for t in build_tags]
+        for tag in build_tags:
+            tags[tag['name']] = tag
 
     # see if we have a target
     target = data.get('target')
     if target:
         target = get_build_target(target, strict=False)
         if target:
-            if taginfo:
-                return [get_tag(target['build_tag'], strict=True, event="auto")]
-            else:
-                return [target['build_tag_name']]
+            tags[target['build_tag_name']] = get_tag(target['build_tag'], strict=True,
+                                                     event="auto")
 
     # otherwise look at buildroots
-    tags = {}
     for br_id in policy_get_brs(data):
         if br_id is None:
             tags[None] = None
         else:
             tinfo = get_buildroot(br_id, strict=True)
-            if taginfo:
-                tags[tinfo['tag_name']] = get_tag(tinfo['tag_name'], strict=True, event="auto")
-            else:
-                tags[tinfo['tag_name']] = tinfo['tag_name']
+            tags[tinfo['tag_name']] = get_tag(tinfo['tag_name'], strict=True,
+                                              event=tinfo['repo_create_event_id'])
 
     if taginfo:
         tags = tags.values()
@@ -9831,9 +9830,15 @@ class BuildTagInheritsFromTest(koji.policy.BaseSimpleTest):
                 # content generator buildroots might not have tag info
                 continue
 
-            # for possibly deleted tags in inheritance chain
-            # we need to look into event from last known configuration
-            for tag in readFullInheritance(tinfo['id'], event=tinfo.get('revoke_event')):
+            if tinfo.get('query_event') == 'auto':
+                # in case of "auto" for deleted tag revoke_event is last known event
+                # in case of "auto" for non-deleted tag, None is a correct active value
+                event = tinfo.get('revoke_event')
+            else:
+                # if there is "query_event" other then "auto" we want to propagate it
+                # in rest of cases (non-existent "query_event" field), pass None
+                event = tinfo.get('query_event')
+            for tag in readFullInheritance(tinfo['id'], event=event):
                 if multi_fnmatch(tag['name'], args):
                     return True
         # otherwise...

From ea22f52e2af8e89d6e2b4bc4603890340b7620ca Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Aug 31 2021 08:18:42 +0000
Subject: [PATCH 5/6] fix query logic


---

diff --git a/hub/kojihub.py b/hub/kojihub.py
index 786b5c1..f892e0c 100644
--- a/hub/kojihub.py
+++ b/hub/kojihub.py
@@ -9586,32 +9586,30 @@ def policy_get_build_tags(data, taginfo=False):
     tags = {}
     if 'build_tag' in data:
         buildtag = get_tag(data['build_tag'], strict=True, event="auto")
-        # only cases we don't return list
-        if taginfo:
-            return buildtag
-        else:
-            return buildtag['name']
+        tags[buildtag['name']] = buildtag
     elif 'build_tags' in data:
         build_tags = [get_tag(t, strict=True, event="auto") for t in data['build_tags']]
         for tag in build_tags:
             tags[tag['name']] = tag
 
-    # see if we have a target
-    target = data.get('target')
-    if target:
-        target = get_build_target(target, strict=False)
+    if not tags:
+        # see if we have a target
+        target = data.get('target')
         if target:
-            tags[target['build_tag_name']] = get_tag(target['build_tag'], strict=True,
-                                                     event="auto")
-
-    # otherwise look at buildroots
-    for br_id in policy_get_brs(data):
-        if br_id is None:
-            tags[None] = None
-        else:
-            tinfo = get_buildroot(br_id, strict=True)
-            tags[tinfo['tag_name']] = get_tag(tinfo['tag_name'], strict=True,
-                                              event=tinfo['repo_create_event_id'])
+            target = get_build_target(target, strict=False)
+            if target:
+                tags[target['build_tag_name']] = get_tag(target['build_tag'], strict=True,
+                                                         event="auto")
+
+    if not tags:
+        # otherwise look at buildroots
+        for br_id in policy_get_brs(data):
+            if br_id is None:
+                tags[None] = None
+            else:
+                tinfo = get_buildroot(br_id, strict=True)
+                tags[tinfo['tag_name']] = get_tag(tinfo['tag_name'], strict=True,
+                                                  event=tinfo['repo_create_event_id'])
 
     if taginfo:
         tags = tags.values()
@@ -9830,15 +9828,7 @@ class BuildTagInheritsFromTest(koji.policy.BaseSimpleTest):
                 # content generator buildroots might not have tag info
                 continue
 
-            if tinfo.get('query_event') == 'auto':
-                # in case of "auto" for deleted tag revoke_event is last known event
-                # in case of "auto" for non-deleted tag, None is a correct active value
-                event = tinfo.get('revoke_event')
-            else:
-                # if there is "query_event" other then "auto" we want to propagate it
-                # in rest of cases (non-existent "query_event" field), pass None
-                event = tinfo.get('query_event')
-            for tag in readFullInheritance(tinfo['id'], event=event):
+            for tag in readFullInheritance(tinfo['id'], event=tinfo.get('query_event')):
                 if multi_fnmatch(tag['name'], args):
                     return True
         # otherwise...

From fb480687c7496bfe7074f4f1dac053620c304e9d Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Aug 31 2021 09:28:38 +0000
Subject: [PATCH 6/6] fix tests


---

diff --git a/tests/test_hub/test_policy_tests.py b/tests/test_hub/test_policy_tests.py
index 1522be5..b022ccd 100644
--- a/tests/test_hub/test_policy_tests.py
+++ b/tests/test_hub/test_policy_tests.py
@@ -108,7 +108,10 @@ class TestPolicyGetCGs(unittest.TestCase):
 
     def _fakebr(self, br_id, strict):
         self.assertEqual(strict, True)
-        return {'cg_name': self._cgname(br_id)}
+        return {
+            'cg_name': self._cgname(br_id),
+            'repo_create_event_id': 1234,
+        }
 
     def _cgname(self, br_id):
         if br_id is None:
@@ -176,7 +179,7 @@ class TestBuildTagTest(unittest.TestCase):
         self.list_rpms.assert_not_called()
         self.list_archives.assert_not_called()
         self.get_buildroot.assert_not_called()
-        self.get_tag.assert_called_once_with('TAGINFO', strict=True)
+        self.get_tag.assert_called_once_with('TAGINFO', event='auto', strict=True)
 
     def test_build_tag_given_alt(self):
         obj = kojihub.BuildTagTest('buildtag foo*')
@@ -206,7 +209,10 @@ class TestBuildTagTest(unittest.TestCase):
         self.get_buildroot.assert_not_called()
 
     def _fakebr(self, br_id, strict=None):
-        return {'tag_name': self._brtag(br_id)}
+        return {
+            'tag_name': self._brtag(br_id),
+            'repo_create_event_id': 123,
+        }
 
     def _brtag(self, br_id):
         if br_id == '':
@@ -228,7 +234,14 @@ class TestBuildTagTest(unittest.TestCase):
         data = {'build': 'BUILDINFO'}
         self.assertFalse(obj.run(data))
 
-        self.get_tag.assert_not_called()
+        self.get_tag.assert_has_calls([
+            mock.call('a', event=123, strict=True),
+            mock.call('b', event=123, strict=True),
+            mock.call('c', event=123, strict=True),
+            mock.call('d', event=123, strict=True),
+            mock.call('not-foo-5', event=123, strict=True),
+            mock.call('foo-3-build', event=123, strict=True),
+        ], any_order=True)
 
     def test_build_tag_no_info(self):
         obj = kojihub.BuildTagTest('buildtag foo*')
@@ -287,35 +300,35 @@ class TestBuildTagInheritsFromTest(unittest.TestCase):
     def test_no_inheritance(self):
         obj = kojihub.BuildTagInheritsFromTest('buildtag_inherits_from foo*')
         data = {}
-        self.policy_get_build_tags.return_value = [{'tag_id': 123, 'tag_name': 'foox'}]
+        self.policy_get_build_tags.return_value = [{'id': 123, 'tag_name': 'foox'}]
         self.readFullInheritance.return_value = []
         self.assertFalse(obj.run(data))
         self.policy_get_build_tags.assert_called_once_with(data, taginfo=True)
-        self.readFullInheritance.assert_called_once_with(123)
+        self.readFullInheritance.assert_called_once_with(123, event=None)
 
     def test_inheritance_pass(self):
         obj = kojihub.BuildTagInheritsFromTest('buildtag_inherits_from foo*')
         data = {}
-        self.policy_get_build_tags.return_value = [{'tag_id': 123, 'tag_name': 'wrong'}]
+        self.policy_get_build_tags.return_value = [{'id': 123, 'tag_name': 'wrong'}]
         self.readFullInheritance.return_value = [
             {'name': 'still-wrong'},
             {'name': 'foox'},
         ]
         self.assertTrue(obj.run(data))
         self.policy_get_build_tags.assert_called_once_with(data, taginfo=True)
-        self.readFullInheritance.assert_called_once_with(123)
+        self.readFullInheritance.assert_called_once_with(123, event=None)
 
     def test_inheritance_fail(self):
         obj = kojihub.BuildTagInheritsFromTest('buildtag_inherits_from foo*')
         data = {}
-        self.policy_get_build_tags.return_value = [{'tag_id': 123, 'tag_name': 'wrong'}]
+        self.policy_get_build_tags.return_value = [{'id': 123, 'tag_name': 'wrong'}]
         self.readFullInheritance.return_value = [
             {'name': 'still-wrong'},
             {'name': 'still-still-wrong'},
         ]
         self.assertFalse(obj.run(data))
         self.policy_get_build_tags.assert_called_once_with(data, taginfo=True)
-        self.readFullInheritance.assert_called_once_with(123)
+        self.readFullInheritance.assert_called_once_with(123, event=None)
 
 
 class TestBuildTypeTest(unittest.TestCase):

