From 7c7e2b604349e1879ec0c3b1b217004392af83cb Mon Sep 17 00:00:00 2001
From: Aditya Bisoi <abisoi@redhat.com>
Date: May 25 2021 11:46:40 +0000
Subject: Fix rpkg container-build ignoring values when same argument is specified multiple times


JIRA: RHELCMP-4112
Resolves: pagure #537

Signed-off-by: Aditya Bisoi <abisoi@redhat.com>

---

diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py
index aa38980..c9e3114 100644
--- a/pyrpkg/cli.py
+++ b/pyrpkg/cli.py
@@ -1591,12 +1591,13 @@ class cliClient(object):
 
         parser.add_argument(
             '--arches',
-            action='store',
+            action='append',
             nargs='*',
             help='Limit a scratch or a isolated build to an arch. May have multiple arches.')
 
         parser.add_argument(
             '--repo-url',
+            action='append',
             metavar="URL",
             help='URLs of yum repo files',
             nargs='+')
@@ -1630,6 +1631,7 @@ class cliClient(object):
 
         self.container_build_parser.add_argument(
             '--compose-id',
+            action='append',
             dest='compose_ids',
             metavar='COMPOSE_ID',
             type=int,
@@ -1637,6 +1639,7 @@ class cliClient(object):
             nargs='+')
         self.container_build_parser.add_argument(
             '--replace-dependency',
+            action='append',
             metavar="PKG_MANAGER:NAME:VERSION[:NEW_NAME]",
             help='Cachito dependency replacement',
             nargs='+')
@@ -2035,6 +2038,16 @@ class cliClient(object):
             self.cmd._target = self.args.target
             target_override = True
 
+        # Group similar argparse arguments into single list
+        def group_arguments(arg):
+            return sum((item for item in arg), [])
+
+        if self.args.repo_url:
+            self.args.repo_url = group_arguments(self.args.repo_url)
+
+        if self.args.arches:
+            self.args.arches = group_arguments(self.args.arches)
+
         if self.args.release:
             self.log.warning('using --release with container-build will cause git_branch to be '
                              'overridden, and if the specified commit is not present in the '
@@ -2053,6 +2066,12 @@ class cliClient(object):
                 "yum_repourls": self.args.repo_url}
 
         if not flatpak:
+            if self.args.compose_ids:
+                self.args.compose_ids = group_arguments(self.args.compose_ids)
+
+            if self.args.replace_dependency:
+                self.args.replace_dependency = group_arguments(self.args.replace_dependency)
+
             if self.args.compose_ids and self.args.signing_intent:
                 raise rpkgError("argument --compose-id: not allowed with argument"
                                 " --signing-intent")
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 8421564..0fda4f1 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -229,6 +229,71 @@ class TestContainerBuildWithKoji(CliTestCase):
             flatpak=False
         )
 
+    def test_mutiple_arguments_repo_url(self):
+        cli_cmd = ['rpkg', '--path', self.cloned_repo_path, 'container-build',
+                   '--target', 'f34-container-candidate', '--scratch',
+                   '--repo-url', 'a', 'b', '--repo-url', 'c']
+
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+            cli.container_build_koji()
+
+        self.assertEqual('f34-container-candidate', cli.cmd._target)
+        self.mock_container_build_koji.assert_called_once_with(
+            True,
+            opts={
+                'scratch': True,
+                'quiet': False,
+                'release': None,
+                'isolated': False,
+                'koji_parent_build': None,
+                'yum_repourls': ['a', 'b', 'c'],
+                'dependency_replacements': None,
+                'git_branch': 'eng-rhel-7',
+                'arches': None,
+                'signing_intent': None,
+                'compose_ids': None,
+                'skip_build': False
+            },
+            kojiprofile='koji',
+            build_client=utils.build_client,
+            koji_task_watcher=koji_cli.lib.watch_tasks,
+            nowait=False,
+            flatpak=False
+        )
+
+    def test_mutiple_arguments_compose_ids(self):
+        cli_cmd = ['rpkg', '--path', self.cloned_repo_path, 'container-build',
+                   '--target', 'f34-container-candidate', '--scratch',
+                   '--compose-id', '1', '2', '--compose-id', '3']
+
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+            cli.container_build_koji()
+
+        self.mock_container_build_koji.assert_called_once_with(
+            True,
+            opts={
+                'scratch': True,
+                'quiet': False,
+                'release': None,
+                'isolated': False,
+                'koji_parent_build': None,
+                'yum_repourls': None,
+                'dependency_replacements': None,
+                'git_branch': 'eng-rhel-7',
+                'arches': None,
+                'signing_intent': None,
+                'compose_ids': [1, 2, 3],
+                'skip_build': False
+            },
+            kojiprofile='koji',
+            build_client=utils.build_client,
+            koji_task_watcher=koji_cli.lib.watch_tasks,
+            nowait=False,
+            flatpak=False
+        )
+
     def test_isolated(self):
         cli_cmd = ['rpkg', '--path', self.cloned_repo_path, 'container-build',
                    '--isolated', '--build-release', '99']

