From 7e9b6b62287888aa5798bf8e81547b8480cf2679 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: May 26 2021 08:24:50 +0000
Subject: Fix problems with buildtag_inherits_from


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

---

diff --git a/hub/kojihub.py b/hub/kojihub.py
index e7a6b5e..f2b7312 100644
--- a/hub/kojihub.py
+++ b/hub/kojihub.py
@@ -9413,16 +9413,25 @@ 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']]
+        if taginfo:
+            return [get_tag(data['build_tag'], strict=True)]
+        else:
+            return [get_tag(data['build_tag'], strict=True)['name']]
     elif 'build_tags' in data:
-        return [get_tag(t, strict=True)['name'] for t in data['build_tags']]
+        if taginfo:
+            return [get_tag(t, strict=True) for t in data['build_tags']]
+        else:
+            return [get_tag(t, strict=True)['name'] for t in data['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_name'], strict=True)]
+            else:
+                return [target['build_tag_name']]
 
     # otherwise look at buildroots
     tags = {}
@@ -9430,13 +9439,13 @@ def policy_get_build_tags(data, taginfo=False):
         if br_id is None:
             tags[None] = None
         else:
-            tinfo = get_buildroot(br_id, strict=True)
-            tags[tinfo['tag_name']] = tinfo
+            br = get_buildroot(br_id, strict=True)
+            tags[br['tag_name']] = br['tag_id']
 
     if taginfo:
-        tags = tags.values()
+        tags = [get_tag(tag_id, strict=True) for tag_id in tags.values()]
     else:
-        tags = tags.keys()
+        tags = list(tags.keys())
     return tags
 
 
@@ -9650,7 +9659,7 @@ class BuildTagInheritsFromTest(koji.policy.BaseSimpleTest):
                 # content generator buildroots might not have tag info
                 continue
 
-            for tag in readFullInheritance(tinfo['tag_id']):
+            for tag in readFullInheritance(tinfo['id']):
                 if multi_fnmatch(tag['name'], args):
                     return True
         # otherwise...
diff --git a/tests/test_hub/test_policy_tests.py b/tests/test_hub/test_policy_tests.py
index 3574d5f..4a9831b 100644
--- a/tests/test_hub/test_policy_tests.py
+++ b/tests/test_hub/test_policy_tests.py
@@ -205,7 +205,7 @@ 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), 'tag_id': 1234}
 
     def _brtag(self, br_id):
         if br_id == '':
@@ -286,7 +286,7 @@ 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)
@@ -295,7 +295,7 @@ class TestBuildTagInheritsFromTest(unittest.TestCase):
     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'},
@@ -307,7 +307,7 @@ class TestBuildTagInheritsFromTest(unittest.TestCase):
     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'},

