From c9358500e3d581584bc1f34ff177002abba36ee2 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Apr 12 2021 13:11:43 +0000
Subject: [PATCH 1/2] lib: use parse_task_params for taskLabel


Fixes: https://pagure.io/koji/issue/2519

---

diff --git a/koji/__init__.py b/koji/__init__.py
index e58a8f2..9f89c52 100644
--- a/koji/__init__.py
+++ b/koji/__init__.py
@@ -61,6 +61,7 @@ from requests.adapters import HTTPAdapter
 from requests.packages.urllib3.util.retry import Retry
 from six.moves import range, zip
 
+from koji.tasks import parse_task_params
 from koji.xmlrpcplus import Fault, dumps, getparser, loads, xmlrpc_client
 from . import util
 from . import _version
@@ -3340,116 +3341,73 @@ def taskLabel(taskInfo):
 def _taskLabel(taskInfo):
     """Format taskInfo (dict) into a descriptive label."""
     method = taskInfo['method']
+    request = taskInfo['request']
     arch = taskInfo['arch']
+    params = parse_task_params(method, request)
     extra = ''
     if method in ('build', 'maven'):
-        if 'request' in taskInfo:
-            source, target = taskInfo['request'][:2]
-            if '://' in source:
-                module_info = _module_info(source)
-            else:
-                module_info = os.path.basename(source)
-            extra = '%s, %s' % (target, module_info)
+        src = params.get('src') or params.get('url')
+        if '://' in src:
+            module_info = _module_info(src)
+        else:
+            module_info = os.path.basename(src)
+        target = params.get('target') or params.get('build_tag')
+        extra = '%s, %s' % (target, module_info)
     elif method in ('indirectionimage',):
-        if 'request' in taskInfo:
-            if len(taskInfo['request']) == 1:
-                module_name = taskInfo['request'][0]['name']
-                module_version = taskInfo['request'][0]['version']
-                module_release = taskInfo['request'][0]['release']
-                extra = '%s, %s, %s' % (module_name, module_version, module_release)
-    elif method in ('buildSRPMFromSCM', 'buildSRPMFromCVS'):
-        if 'request' in taskInfo:
-            url = taskInfo['request'][0]
-            extra = _module_info(url)
+        module_name = params['opts']['name']
+        module_version = params['opts']['version']
+        module_release = params['opts']['release']
+        extra = '%s, %s, %s' % (module_name, module_version, module_release)
+    elif method in ('buildSRPMFromSCM'):
+        extra = _module_info(params['url'])
     elif method == 'buildArch':
-        if 'request' in taskInfo:
-            srpm, tagID, arch = taskInfo['request'][:3]
-            srpm = os.path.basename(srpm)
-            extra = '%s, %s' % (srpm, arch)
+        srpm = os.path.basename(params['pkg'])
+        arch = params['arch']
+        extra = '%s, %s' % (srpm, arch)
     elif method == 'buildMaven':
-        if 'request' in taskInfo:
-            build_tag = taskInfo['request'][1]
-            extra = build_tag['name']
+        extra = params['build_tag']['name']
     elif method == 'wrapperRPM':
-        if 'request' in taskInfo:
-            build_target = taskInfo['request'][1]
-            build = taskInfo['request'][2]
-            if build:
-                extra = '%s, %s' % (build_target['name'], buildLabel(build))
-            else:
-                extra = build_target['name']
+        if params['build']:
+            extra = '%s, %s' % (params['build_target']['name'], buildLabel(params['build']))
+        else:
+            extra = params['build_target']['name']
     elif method == 'winbuild':
-        if 'request' in taskInfo:
-            # vm = taskInfo['request'][0]
-            url = taskInfo['request'][1]
-            target = taskInfo['request'][2]
-            module_info = _module_info(url)
-            extra = '%s, %s' % (target, module_info)
+        module_info = _module_info(params['source_url'])
+        extra = '%s, %s' % (params['target'], module_info)
     elif method == 'vmExec':
-        if 'request' in taskInfo:
-            extra = taskInfo['request'][0]
+        extra = params['name']
     elif method == 'buildNotification':
-        if 'request' in taskInfo:
-            build = taskInfo['request'][1]
-            extra = buildLabel(build)
+        extra = buildLabel(params['build'])
     elif method in ('newRepo', 'distRepo'):
-        if 'request' in taskInfo:
-            extra = str(taskInfo['request'][0])
+        extra = str(params['tag'])
     elif method in ('tagBuild', 'tagNotification'):
         # There is no displayable information included in the request
         # for these methods
         pass
-    elif method == 'prepRepo':
-        if 'request' in taskInfo:
-            tagInfo = taskInfo['request'][0]
-            extra = tagInfo['name']
     elif method == 'createrepo':
-        if 'request' in taskInfo:
-            arch = taskInfo['request'][1]
-            extra = arch
+        extra = params['arch']
     elif method == 'createdistrepo':
-        if 'request' in taskInfo:
-            repo_id = taskInfo['request'][1]
-            arch = taskInfo['request'][2]
-            extra = '%s, %s' % (repo_id, arch)
+        extra = '%s, %s' % (params['repo_id'], params['arch'])
     elif method == 'dependantTask':
-        if 'request' in taskInfo:
-            extra = ', '.join([subtask[0] for subtask in taskInfo['request'][1]])
+        task_list = params['task_list']
+        extra = ', '.join([str(subtask[0]) for subtask in task_list])
     elif method in ('chainbuild', 'chainmaven'):
-        if 'request' in taskInfo:
-            extra = taskInfo['request'][1]
+        extra = params['target']
     elif method == 'waitrepo':
-        if 'request' in taskInfo:
-            extra = str(taskInfo['request'][0])
-            if len(taskInfo['request']) >= 3:
-                nvrs = taskInfo['request'][2]
-                if isinstance(nvrs, list):
-                    extra += ', ' + ', '.join(nvrs)
+        extra = str(params['tag'])
+        if isinstance(params['nvrs'], list):
+            extra += ', ' + ', '.join(params['nvrs'])
     elif method in ('livecd', 'appliance', 'image', 'livemedia'):
-        if 'request' in taskInfo:
-            stuff = taskInfo['request']
-            if method == 'image':
-                kickstart = os.path.basename(stuff[-1]['kickstart'])
-            else:
-                kickstart = os.path.basename(stuff[4])
-            extra = '%s, %s-%s, %s' % (stuff[3], stuff[0], stuff[1], kickstart)
+        kstart = params.get('ksfile') or params.get('inst_tree')
+        arch = params.get('arch') or params.get('arches')
+        extra = '%s, %s-%s, %s' % (arch, params['name'], params['version'], kstart)
     elif method in ('createLiveCD', 'createAppliance', 'createImage', 'createLiveMedia'):
-        if 'request' in taskInfo:
-            stuff = taskInfo['request']
-            if method == 'createImage':
-                kickstart = os.path.basename(stuff[-1]['kickstart'])
-            else:
-                kickstart = os.path.basename(stuff[7])
-            extra = '%s, %s-%s-%s, %s, %s' % (stuff[4]['name'], stuff[0],
-                                              stuff[1], stuff[2], kickstart, stuff[3])
-    elif method == 'restart':
-        if 'request' in taskInfo:
-            host = taskInfo['request'][0]
-            extra = host['name']
-    elif method == 'restartVerify':
-        if 'request' in taskInfo:
-            task_id, host = taskInfo['request'][:2]
-            extra = host['name']
+        kstart = params.get('ksfile') or params.get('inst_tree')
+        extra = '%s, %s-%s-%s, %s, %s' % (params['target_info']['name'],
+                                          params['name'], params['version'], params['release'],
+                                          kstart, params['arch'])
+    elif method in ('restart', 'restartVerify'):
+        extra = params['host']['name']
 
     if extra:
         return '%s (%s)' % (method, extra)
diff --git a/koji/tasks.py b/koji/tasks.py
index 678b513..ca741ca 100644
--- a/koji/tasks.py
+++ b/koji/tasks.py
@@ -276,6 +276,12 @@ LEGACY_SIGNATURES = {
     'saveFailedTree': [
         [['buildrootID', 'full'], None, None, (False,)],
     ],
+    'vmExec': [
+        [['name', 'task_info', 'opts'], None, None, None],
+    ],
+    'winbuild': [
+        [['name', 'source_url', 'target', 'opts'], None, None, None],
+    ],
 }
 
 
diff --git a/tests/test_lib/test_taskLabel.py b/tests/test_lib/test_taskLabel.py
new file mode 100644
index 0000000..6ca02d0
--- /dev/null
+++ b/tests/test_lib/test_taskLabel.py
@@ -0,0 +1,156 @@
+import unittest
+
+import koji
+
+class TestTaskLabel(unittest.TestCase):
+    def test_all(self):
+        url = 'https+git://git.server/path/module#branch'
+        module = '/path/module:branch'
+        build = {'name': 'n', 'version': 'v', 'release': 'r', 'epoch': None}
+        nvr = 'n-v-r'
+        test_data = [
+            ['randomdata', 'malformed task'],
+            [{}, 'malformed task'],
+            [None, 'malformed task'],
+            [
+                {'method': 'build', 'arch': 'x86_64',
+                 'request': [url, 'target', 'opts'],
+                }, 'build (target, %s)' % module
+            ],
+            [
+                {'method': 'build', 'arch': 'x86_64',
+                 'request': ['n-v-r.src.rpm', 'target', 'opts']
+                }, 'build (target, n-v-r.src.rpm)'
+            ],
+            [
+                {'method': 'maven', 'arch': 'x86_64',
+                 'request': ['https+git://git.server/path/module#branch', 'target', 'opts'],
+                }, 'maven (target, %s)' % module
+            ],
+            [
+                {'method': 'maven', 'arch': 'x86_64',
+                 'request': ['n-v-r.jar', 'target', 'opts'],
+                }, 'maven (target, n-v-r.jar)'
+            ],
+            [
+                {'method': 'indirectionimage', 'arch': 'x86_64',
+                 'request': [build],
+                }, 'indirectionimage (n, v, r)'
+            ],
+            [
+                {'method': 'buildSRPMFromSCM', 'arch': 'x86_64',
+                 'request': [url, 'build_tag', 'opts']
+                }, 'buildSRPMFromSCM (%s)' % module
+            ],
+            [
+                {'method': 'buildArch', 'arch': 'x86_64',
+                 'request': ['pkg', 'root', 'arch', True, 'opts'],
+                }, 'buildArch (pkg, arch)'
+            ],
+            [
+                {'method': 'buildMaven', 'arch': 'x86_64',
+                 'request': [url, {'name': 'build_tag', 'id': 123}, {}],
+                }, 'buildMaven (build_tag)',
+            ],
+            [
+                {'method': 'wrapperRPM', 'arch': 'x86_64',
+                 'request': [url, {'name': 'target'}, build, 'task']
+                }, 'wrapperRPM (target, n-v-r)',
+            ],
+            # winbuild, vmExec (not in legacy signatures)
+            [
+                {'method': 'buildNotification', 'arch': 'x86_64',
+                 'request': ['rpts', build, 'target', 'weburl']
+                }, 'buildNotification (n-v-r)'
+            ],
+            [
+                {'method': 'newRepo', 'arch': 'x86_64',
+                 'request': ['tag', 123, 'src']
+                }, 'newRepo (tag)'
+            ],
+            [
+                {'method': 'distRepo', 'arch': 'x86_64',
+                 'request': ['tag', 123, 'keys', 'task_opts']
+                }, 'distRepo (tag)'
+            ],
+            [
+                {'method': 'tagBuild', 'arch': 'x86_64',
+                 'request': ['tag', 123, True, 'from', True],
+                }, 'tagBuild (x86_64)'
+            ],
+            [
+                {'method': 'tagNotification', 'arch': 'x86_64',
+                 'request': ['rcpts', True, 'tag', 'from', build, 'user'],
+                }, 'tagNotification (x86_64)'
+            ],
+            [
+                {'method': 'createrepo', 'arch': 'x86_64',
+                 'request': ['repo_id', 'arch', 'oldrepo']
+                }, 'createrepo (arch)'
+            ],
+            [
+                {'method': 'createdistrepo', 'arch': 'x86_64',
+                 'request': ['tag', 'repo_id', 'arch', 'keys', 'opts']
+                }, 'createdistrepo (repo_id, arch)'
+            ],
+            [
+                {'method': 'dependantTask', 'arch': 'x86_64',
+                 'request': ['wait_list', [[1], [2]]],
+                }, 'dependantTask (1, 2)'
+            ],
+            [
+                {'method': 'chainbuild', 'arch': 'x86_64',
+                 'request': ['srcs', 'target', 'opts'],
+                }, 'chainbuild (target)'
+            ],
+            [
+                {'method': 'chainmaven', 'arch': 'x86_64',
+                 'request': ['srcs', 'target', 'opts'],
+                }, 'chainmaven (target)'
+            ],
+            [
+                {'method': 'waitrepo', 'arch': 'x86_64',
+                 'request': ['tag', 'newer', ['nvr1', 'nvr2']]
+                }, 'waitrepo (tag, nvr1, nvr2)'
+            ],
+            [
+                {'method': 'appliance', 'arch': 'x86_64',
+                 'request': ['name', 'version', 'arch', 'target', 'ksfile', 'opts'],
+                }, 'appliance (arch, name-version, ksfile)',
+            ],
+            [
+                {'method': 'livecd', 'arch': 'x86_64',
+                 'request': ['name', 'version', 'arch', 'target', 'ksfile', 'opts'],
+                }, 'livecd (arch, name-version, ksfile)',
+            ],
+            [
+                {'method': 'image', 'arch': 'x86_64',
+                 'request': ['name', 'version', 'arches', 'target', 'inst_tree', 'opts'],
+                }, 'image (arches, name-version, inst_tree)',
+            ],
+            [
+                {'method': 'livemedia', 'arch': 'x86_64',
+                 'request': ['name', 'version', 'arches', 'target', 'ksfile', 'opts'],
+                }, 'livemedia (arches, name-version, ksfile)',
+            ],
+            [
+                {'method': 'createLiveCD', 'arch': 'x86_64',
+                 'request': ['name', 'version', 'release', 'arch', {'name': 'target'}, 'build_tag',
+                             'repo_info', 'ksfile', 'opts'],
+                }, 'createLiveCD (target, name-version-release, ksfile, arch)',
+            ],
+            [
+                {'method': 'restart', 'arch': 'noarch',
+                 'request': [{'name': 'hostname'}],
+                }, 'restart (hostname)'
+            ],
+            [
+                {'method': 'restartVerify', 'arch': 'noarch',
+                 'request': [123, {'name': 'hostname'}],
+                }, 'restartVerify (hostname)'
+            ],
+        ]
+
+        for input, output in test_data:
+            result = koji.taskLabel(input)
+            self.assertEqual(result, output)

From 9bf4d8d3aa1487947479f9b6df67bbfeba8a4af5 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Apr 12 2021 13:11:43 +0000
Subject: [PATCH 2/2] fix tests


---

diff --git a/tests/test_cli/test_buildinfo.py b/tests/test_cli/test_buildinfo.py
index 14fb830..d7d94d5 100644
--- a/tests/test_cli/test_buildinfo.py
+++ b/tests/test_cli/test_buildinfo.py
@@ -26,7 +26,8 @@ class TestBuildinfo(utils.CliTestCase):
         time.tzset()
         self.taskinfo = {'arch': 'noarch',
                          'id': 8,
-                         'method': 'build'}
+                         'method': 'build',
+                         'request': ['src', 'target', 'opts']}
         self.buildinfo = {'build_id': 1,
                           'id': 1,
                           'name': 'test-build',
@@ -60,7 +61,7 @@ class TestBuildinfo(utils.CliTestCase):
 State: COMPLETE
 Built by: kojiadmin
 Volume: DEFAULT
-Task: 8 build (noarch)
+Task: 8 build (target, src)
 Finished: Thu, 04 Mar 2021 14:45:40 UTC
 Tags: 
 """
diff --git a/tests/test_cli/test_list_tasks.py b/tests/test_cli/test_list_tasks.py
index 28c1f6b..8e8b343 100644
--- a/tests/test_cli/test_list_tasks.py
+++ b/tests/test_cli/test_list_tasks.py
@@ -190,7 +190,8 @@ class TestCliListTasks(utils.CliTestCase):
                 'method': 'createrepo',
                 'parent': 3,
                 'priority': 14,
-                'arch': 'noarch'
+                'arch': 'noarch',
+                'request': ['tag'],
             },
             {
                 'children':
@@ -203,7 +204,8 @@ class TestCliListTasks(utils.CliTestCase):
                         'method': 'createrepo',
                         'parent': 3,
                         'priority': 14,
-                        'arch': 'noarch'
+                        'arch': 'noarch',
+                        'request': ['repo_id', 'noarch', 'oldrepo'],
                     },
                 ],
                 'id': 3,
@@ -212,13 +214,14 @@ class TestCliListTasks(utils.CliTestCase):
                 'method': 'newRepo',
                 'priority': 15,
                 'arch': 'noarch',
+                'request': ['tag'],
             }
         ]
 
         header = \
             "ID       Pri  Owner                State    Arch       Name\n"
         task_output = \
-            "3        15   kojiadmin            FREE     noarch     newRepo (noarch)\n" + \
+            "3        15   kojiadmin            FREE     noarch     newRepo (tag)\n" + \
             "5        14   kojiadmin            FREE     noarch      +createrepo (noarch)\n"
 
         expected = self.format_error_message(
diff --git a/tests/test_cli/test_taskinfo.py b/tests/test_cli/test_taskinfo.py
index 3886c7d..0528844 100644
--- a/tests/test_cli/test_taskinfo.py
+++ b/tests/test_cli/test_taskinfo.py
@@ -121,7 +121,7 @@ class TestParseTaskParams(utils.CliTestCase):
                     'id': 1,
                     'method': 'wrapperRPM',
                     'arch': 'x86_64',
-                    'request': [1, {'name': target}, self.build_templ]
+                    'request': [1, {'name': target}, self.build_templ, 'task', 'opts']
                   },
                   {'wrapRPM-test': True}]
         expect = ["Spec File URL: %s" % params[0]]
diff --git a/tests/test_lib/test_taskLabel.py b/tests/test_lib/test_taskLabel.py
index 6ca02d0..9af7389 100644
--- a/tests/test_lib/test_taskLabel.py
+++ b/tests/test_lib/test_taskLabel.py
@@ -149,6 +149,17 @@ class TestTaskLabel(unittest.TestCase):
                  'request': [123, {'name': 'hostname'}],
                 }, 'restartVerify (hostname)'
             ],
+            [
+                {'method': 'vmExec', 'arch': 'x86_64',
+                 'request': ['name', 'task_info', 'opts'],
+                 }, 'vmExec (name)'
+            ],
+            [
+                {'method': 'winbuild', 'arch': 'x86_64',
+                 'request': ['name', 'source_url', 'target', 'opts'],
+                 }, 'winbuild (target, :source_url)'
+            ],
+
         ]
 
         for input, output in test_data:

