From 7a2436cd2c6ca5aeeba9b9b66b3d6a0dbd6a249d Mon Sep 17 00:00:00 2001
From: Ondřej Nosek <onosek@redhat.com>
Date: May 12 2020 22:08:13 +0000
Subject: [PATCH 1/2] Correct flake8 complaints to pass unittests


Signed-off-by: Ondřej Nosek <onosek@redhat.com>

---

diff --git a/fedpkg/cli.py b/fedpkg/cli.py
index 0817306..ae83965 100644
--- a/fedpkg/cli.py
+++ b/fedpkg/cli.py
@@ -323,16 +323,13 @@ class fedpkgClient(cliClient):
 
     def register_request_tests_repo(self):
         help_msg = 'Request a new tests dist-git repository'
-        pagure_section = '{0}.pagure'.format(self.name)
-        pagure_url = config_get_safely(self.config, pagure_section, 'url')
-        pagure_url_parsed = urlparse(pagure_url).netloc
         anongiturl = self.config.get(
             self.name, 'anongiturl', vars={'repo': 'any', 'module': 'any'}
         )
         description = textwrap.dedent('''
             Request a new dist-git repository in tests shared namespace
 
-                {2}/projects/tests/*
+                {1}/projects/tests/*
 
             For more information about tests shared namespace see
 
@@ -348,7 +345,7 @@ class fedpkgClient(cliClient):
 
             Note that the space name needs to reflect the intent of the tests and will
             undergo a manual review.
-        '''.format(self.name, pagure_url_parsed, get_dist_git_url(anongiturl)))
+        '''.format(self.name, get_dist_git_url(anongiturl)))
 
         request_tests_repo_parser = self.subparsers.add_parser(
             'request-tests-repo',
@@ -646,15 +643,15 @@ class fedpkgClient(cliClient):
     # Target functions go here
     def _format_update_clog(self, clog):
         ''' Format clog for the update template. '''
-        lines = [l for l in clog.split('\n') if l]
+        lines = [ln for ln in clog.split('\n') if ln]
         if len(lines) == 0:
             return "- Rebuilt.", ""
         elif len(lines) == 1:
             return lines[0], ""
         log = ["# Changelog:"]
         log.append('# - ' + lines[0])
-        for l in lines[1:]:
-            log.append('# ' + l)
+        for ln in lines[1:]:
+            log.append('# ' + ln)
         log.append('#')
         return lines[0], "\n".join(log)
 
@@ -1241,8 +1238,8 @@ class fedpkgClient(cliClient):
         server_url = self.config.get('{0}.pdc'.format(self.name), 'url')
         releases = get_release_branches(server_url)
 
-        def _join(l):
-            return ' '.join(l)
+        def _join(ln):
+            return ' '.join(ln)
 
         if self.args.show_epel_only:
             print(_join(releases['epel']))

From 7e61dc6b1508131505532411cf4caf63b67f649d Mon Sep 17 00:00:00 2001
From: Ondrej Nosek <onosek@redhat.com>
Date: May 12 2020 22:09:48 +0000
Subject: [PATCH 2/2] Make it easier to opt-out from the playground


When 'fedpkg request-branch' requests for an 'epel8' (or higher),
Fedora SCM will create also 'epel8-playground' branch and place
initial commit containing 'package.cfg' into 'epel8'. This allows
avoiding the creation of the extra 'epel8-playground' branch
and the commit. New argument '--without-playground' was added.

JIRA: RHELCMP-589
Resolves: rhbz#1746314

Signed-off-by: Ondrej Nosek <onosek@redhat.com>

---

diff --git a/fedpkg/cli.py b/fedpkg/cli.py
index ae83965..4a7c599 100644
--- a/fedpkg/cli.py
+++ b/fedpkg/cli.py
@@ -412,7 +412,8 @@ class fedpkgClient(cliClient):
             required=False,
             dest='repo_ns_for_branch',
             choices=self.get_distgit_namespaces(),
-            help='Repository name the new branch is requested for.'
+            help=("Repository name the new branch is requested for. Requires "
+                  "filling the '--repo' argument")
         )
         request_branch_parser.add_argument(
             '--sl', nargs='*',
@@ -436,6 +437,13 @@ class fedpkgClient(cliClient):
             '--all-releases', default=False, action='store_true',
             help='Make a new branch request for every active Fedora release'
         )
+        request_branch_parser.add_argument(
+            '--without-playground', default=False, action='store_true',
+            help=("In case of requesting 'epelX' branch (where X is >7), "
+                  "'epelX-playground' branch will not be automatically "
+                  "requested. Branch will be created in pdc; user has to "
+                  "create git branch manually afterwards.")
+        )
         request_branch_parser.set_defaults(command=self.request_branch)
 
     def register_do_fork(self):
@@ -927,8 +935,10 @@ class fedpkgClient(cliClient):
             active_branch=active_branch,
             repo_name=self.cmd.repo_name,
             ns=self.cmd.ns,
-            no_git_branch=self.args.no_git_branch,
+            # 'without_playground' option also disables creating git branch
+            no_git_branch=self.args.without_playground or self.args.no_git_branch,
             no_auto_module=self.args.no_auto_module,
+            without_playground=self.args.without_playground,
             name=self.name,
             config=self.config,
         )
@@ -936,7 +946,7 @@ class fedpkgClient(cliClient):
     @staticmethod
     def _request_branch(service_levels, all_releases, branch, active_branch,
                         repo_name, ns, no_git_branch, no_auto_module,
-                        name, config):
+                        without_playground, name, config):
         """ Implementation of `request_branch`.
 
         Submits a request for a new branch of a given dist-git repo.
@@ -957,6 +967,8 @@ class fedpkgClient(cliClient):
         :param no_auto_module: A boolean flag.  If True, requests for
             non-standard branches should not automatically result in additional
             requests for matching modules.
+        :param without_playground: A boolean flag. If True, 'epelX-playground'
+            branch will not be requested together with epelX in SCM.
         :param name: A string representing which section of the config should be
             used.  Typically the value of `self.name`.
         :param config: A dict containing the configuration, loaded from file.
@@ -987,7 +999,8 @@ class fedpkgClient(cliClient):
             # Check if the requested branch is an epel branch
             match = re.match(r'^epel(?P<epel_version>\d+)$', branch)
             if match:
-                epel_playground = True
+                # do not request playground branch when without_playground is active
+                epel_playground = not without_playground
                 epel_version = int(match.groupdict()["epel_version"])
 
             if is_epel(branch):
@@ -1093,6 +1106,7 @@ class fedpkgClient(cliClient):
                     ns='modules',
                     no_git_branch=no_git_branch,
                     no_auto_module=True,  # Avoid infinite recursion.
+                    without_playground=without_playground,
                     name=name,
                     config=config,
                 )
diff --git a/test/test_cli.py b/test/test_cli.py
index c22eefd..2367802 100644
--- a/test/test_cli.py
+++ b/test/test_cli.py
@@ -1002,6 +1002,58 @@ class TestRequestBranch(CliTestCase):
                            'fedora-scm-requests/issue/2']))
         self.assertEqual(output, expected_output)
 
+    @patch('requests.get')
+    @patch('requests.post')
+    @patch('fedpkg.cli.get_release_branches')
+    @patch('sys.stdout', new=StringIO())
+    def test_request_epel_branch_override_without_playground(
+        self, mock_grb, mock_request_post, mock_request_get
+    ):
+        """Tests request-epel-branch-override-without-playground
+
+        epel8 branch operation (with 'without_playground' argument) generates
+        only one request. It doesn't request for epel8-playground branch.
+        It won't create git branch either.
+        """
+        mock_grb.return_value = {'fedora': ['f25', 'f26', 'f27'],
+                                 'epel': ['el6', 'epel7', 'epel8']}
+        mock_rv = Mock()
+        mock_rv.ok = True
+        mock_rv.json.return_value = {'issue': {'id': 2}}
+        mock_request_post.return_value = mock_rv
+
+        mock_rv = Mock()
+        mock_rv.ok = True
+        mock_rv.json.return_value = {"arches": [], "packages": {}}
+        mock_request_get.return_value = mock_rv
+        # Checkout the epel7 branch
+        self.run_cmd(['git', 'checkout', 'epel8'], cwd=self.cloned_repo_path)
+
+        cli_cmd = ['fedpkg-stage', '--path', self.cloned_repo_path,
+                   'request-branch', '--repo', 'sudoku', 'epel8',
+                   '--without-playground']
+        cli = self.get_cli(cli_cmd)
+        cli.request_branch()
+
+        expected_issue_content = {
+            'action': 'new_branch',
+            'repo': 'sudoku',
+            'namespace': 'rpms',
+            'branch': 'epel8',
+            'create_git_branch': False
+        }
+        self.assertEqual(len(mock_request_post.call_args_list), 1)
+
+        # Get the data that was submitted to Pagure
+        post_data = mock_request_post.call_args_list[0][1]['data']
+        actual_issue_content = json.loads(json.loads(
+            post_data)['issue_content'].strip('```'))
+        self.assertEqual(expected_issue_content, actual_issue_content)
+        output = sys.stdout.getvalue().strip()
+        expected_output = ('https://pagure.stg.example.com/releng/'
+                           'fedora-scm-requests/issue/2')
+        self.assertEqual(output, expected_output)
+
     @patch('requests.post')
     @patch('fedpkg.cli.get_release_branches')
     @patch('sys.stdout', new=StringIO())

