From 9b330865c2efaa1a66dd34ba8f09942cce6dd934 Mon Sep 17 00:00:00 2001
From: Lubomír Sedlář <lsedlar@redhat.com>
Date: Mar 30 2016 12:28:39 +0000
Subject: Fix fedpkg retire with namespace


Fixes: rhbz#1321462
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>

---

diff --git a/src/fedpkg/cli.py b/src/fedpkg/cli.py
index 803a4a4..a97fe4f 100644
--- a/src/fedpkg/cli.py
+++ b/src/fedpkg/cli.py
@@ -60,6 +60,9 @@ class fedpkgClient(cliClient):
     # Target functions go here
     def retire(self):
         try:
+            module_name = self.cmd.module_name
+            ns_module_name = self.cmd.ns_module_name
+            namespace = ns_module_name.split(module_name)[0].rstrip('/')
             # Skip if package is already retired to allow to retire only in
             # pkgdb
             if os.path.isfile(os.path.join(self.cmd.path, 'dead.package')):
@@ -70,18 +73,10 @@ class fedpkgClient(cliClient):
                 self.cmd.retire(self.args.reason)
             self.push()
 
-            # get module name from git, because pyrpkg gets it from SPEC,
-            # which is deleted at this point
-            cmd = ['git', 'config', '--get', 'remote.origin.url']
-            module_name = subprocess.check_output(cmd, cwd=self.cmd.path)
-            module_name = \
-                module_name.strip().split(self.cmd.gitbaseurl %
-                                          {'user': self.cmd.user,
-                                           'module': ''})[1]
             branch = self.cmd.branch_merge
             pkgdb = pkgdb2client.PkgDB(
                 login_callback=pkgdb2client.ask_password)
-            pkgdb.retire_packages(module_name, branch)
+            pkgdb.retire_packages(module_name, branch, namespace=namespace)
         except Exception as e:
             self.log.error('Could not retire package: %s' % e)
             sys.exit(1)
diff --git a/test/fedpkg-test.conf b/test/fedpkg-test.conf
new file mode 100644
index 0000000..34d12bf
--- /dev/null
+++ b/test/fedpkg-test.conf
@@ -0,0 +1,10 @@
+[fedpkg]
+distgit_namespaced = True
+lookaside = http://pkgs.example.com/repo/pkgs
+lookasidehash = md5
+lookaside_cgi = https://pkgs.example.com/repo/pkgs/upload.cgi
+gitbaseurl = ssh://%(user)s@pkgs.example.com/%(module)s
+anongiturl = git://pkgs.example.com/%(module)s
+branchre = f\d$|f\d\d$|el\d$|olpc\d$|master$
+kojiconfig = /etc/koji.conf
+build_client = koji
diff --git a/test/test_retire.py b/test/test_retire.py
new file mode 100644
index 0000000..bfc8d10
--- /dev/null
+++ b/test/test_retire.py
@@ -0,0 +1,90 @@
+# -*- coding: utf-8 -*-
+
+import os
+import shutil
+import unittest
+import mock
+import ConfigParser
+import tempfile
+import subprocess
+
+from fedpkg.cli import fedpkgClient
+
+
+TEST_CONFIG = os.path.join(os.path.dirname(__file__), 'fedpkg-test.conf')
+
+
+class RetireTestCase(unittest.TestCase):
+    def setUp(self):
+        self.tmpdir = tempfile.mkdtemp()
+        self.log = mock.Mock()
+
+    def tearDown(self):
+        shutil.rmtree(self.tmpdir)
+
+    def _setup_repo(self, origin):
+        subprocess.check_call(
+            ['git', 'init'],
+            cwd=self.tmpdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        subprocess.check_call(
+            ['git', 'remote', 'add', 'origin', origin],
+            cwd=self.tmpdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        subprocess.check_call(
+            ['touch', 'fedpkg.spec'],
+            cwd=self.tmpdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        subprocess.check_call(
+            ['git', 'add', '.'],
+            cwd=self.tmpdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        subprocess.check_call(
+            ['git', 'commit', '-m', 'Initial commit'],
+            cwd=self.tmpdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+    def _get_latest_commit(self):
+        return subprocess.check_output(
+            ['git', 'log', '-n', '1', '--pretty=%s'], cwd=self.tmpdir).strip()
+
+    def _fake_client(self, args):
+        config = ConfigParser.SafeConfigParser()
+        config.read(TEST_CONFIG)
+        with mock.patch('sys.argv', new=args):
+            client = fedpkgClient(config)
+            client.do_imports(site='fedpkg')
+            client.setupLogging(self.log)
+
+            client.parse_cmdline()
+            client.args.path = self.tmpdir
+            client.cmd.push = mock.Mock()
+        return client
+
+    def assertRetired(self, reason):
+        self.assertTrue(os.path.isfile(os.path.join(self.tmpdir,
+                                                    'dead.package')))
+        self.assertFalse(os.path.isfile(os.path.join(self.tmpdir,
+                                                     'fedpkg.spec')))
+        self.assertEqual(self._get_latest_commit(), reason)
+
+    @mock.patch('pkgdb2client.PkgDB')
+    def test_retire_with_namespace(self, PkgDB):
+        self._setup_repo('ssh://git@pkgs.example.com/rpms/fedpkg')
+        args = ['fedpkg', '--dist=master', 'retire', 'my reason']
+
+        client = self._fake_client(args)
+        client.retire()
+
+        self.assertRetired('my reason')
+        self.assertEqual(client.cmd.push.call_args_list, [mock.call()])
+        self.assertEqual(PkgDB.return_value.retire_packages.call_args_list,
+                         [mock.call('fedpkg', 'master', namespace='rpms')])
+
+    @mock.patch('pkgdb2client.PkgDB')
+    def test_retire_without_namespace(self, PkgDB):
+        self._setup_repo('ssh://git@pkgs.example.com/fedpkg')
+        args = ['fedpkg', '--dist=master', 'retire', 'my reason']
+
+        client = self._fake_client(args)
+        client.retire()
+
+        self.assertRetired('my reason')
+        self.assertEqual(client.cmd.push.call_args_list, [mock.call()])
+        self.assertEqual(PkgDB.return_value.retire_packages.call_args_list,
+                         [mock.call('fedpkg', 'master', namespace='rpms')])

