From 97192c21af97ad66d745b4eab69f735b5004d9ca Mon Sep 17 00:00:00 2001
From: Lubomír Sedlář <lsedlar@redhat.com>
Date: Jul 23 2019 07:09:13 +0000
Subject: Fix rpmdefines for epel playground branches


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

---

diff --git a/fedpkg/__init__.py b/fedpkg/__init__.py
index f66cb12..b4a9fd8 100644
--- a/fedpkg/__init__.py
+++ b/fedpkg/__init__.py
@@ -122,33 +122,54 @@ class Commands(pyrpkg.Commands):
         # Determine runtime environment
         self._runtime_disttag = self._determine_runtime_env()
 
+        m = re.match(
+            r"""
+            (?P<master>master)$                 # Either just master
+            |                                   # or some other branch.
+            (?P<dist>f|el|(?P<epel>epel)|olpc)  # Capture name and store a flag for epel
+            (?P<ver>\d+)
+            (?(epel)(?:-playground)?)           # If we have epel flag, check for
+                                                # optional -playground suffix.
+            $
+            """,
+            self.branch_merge,
+            re.VERBOSE,
+        )
+        if not m:
+            raise pyrpkg.rpkgError(
+                "Could not find the release/dist from branch name "
+                "%s\nPlease specify with --release" % self.branch_merge
+            )
+        groups = m.groupdict()
+        dist = groups.get("master") or groups.get("dist")
+        distval = groups.get("ver")
+
         # We only match the top level branch name exactly.
         # Anything else is too dangerous and --dist should be used
         # This regex works until after Fedora 99.
-        if re.match(r'f\d\d$', self.branch_merge):
-            self._distval = self.branch_merge.split('f')[1]
+        if dist == "f":
+            self._distval = distval
             self._distvar = 'fedora'
             self._disttag = 'fc%s' % self._distval
             self.mockconfig = 'fedora-%s-%s' % (self._distval, self.localarch)
             self.override = 'f%s-override' % self._distval
             self._distunset = 'rhel'
         # Works until RHEL 10
-        elif re.match(r'el\d$', self.branch_merge) or \
-                re.match(r'epel\d$', self.branch_merge):
-            self._distval = self.branch_merge.split('el')[1]
+        elif dist in ("el", "epel"):
+            self._distval = distval
             self._distvar = 'rhel'
             self._disttag = 'el%s' % self._distval
             self.mockconfig = 'epel-%s-%s' % (self._distval, self.localarch)
             self.override = 'epel%s-override' % self._distval
             self._distunset = 'fedora'
-        elif re.match(r'olpc\d$', self.branch_merge):
-            self._distval = self.branch_merge.split('olpc')[1]
+        elif dist == "olpc":
+            self._distval = distval
             self._distvar = 'olpc'
             self._disttag = 'olpc%s' % self._distval
             self.override = 'dist-olpc%s-override' % self._distval
             self._distunset = 'rhel'
         # master
-        elif re.match(r'master$', self.branch_merge):
+        elif dist == "master":
             self._distval = self._findmasterbranch()
             self._distvar = 'fedora'
             self._disttag = 'fc%s' % self._distval
@@ -156,10 +177,6 @@ class Commands(pyrpkg.Commands):
             self.override = None
             self._distunset = 'rhel'
         # If we don't match one of the above, punt
-        else:
-            raise pyrpkg.rpkgError('Could not find the release/dist from branch name '
-                                   '%s\nPlease specify with --release' %
-                                   self.branch_merge)
         self._rpmdefines = ["--define '_sourcedir %s'" % self.path,
                             "--define '_specdir %s'" % self.path,
                             "--define '_builddir %s'" % self.path,
diff --git a/test/test_commands.py b/test/test_commands.py
index 7824f9f..5cf13b1 100644
--- a/test/test_commands.py
+++ b/test/test_commands.py
@@ -246,6 +246,21 @@ class TestLoadRpmDefines(CommandTestCase):
         self.assert_rpmdefines()
 
     @patch('pyrpkg.Commands.branch_merge', new_callable=PropertyMock)
+    def test_load_epel8_playground_dist_tag(self, branch_merge):
+        branch_merge.return_value = 'epel8-playground'
+
+        self.cmd.load_rpmdefines()
+
+        self.assertEqual('8', self.cmd._distval)
+        self.assertEqual('rhel', self.cmd._distvar)
+        self.assertEqual('el8', self.cmd._disttag)
+        self.assertEqual('epel-8-i686', self.cmd.mockconfig)
+        self.assertEqual('epel8-override', self.cmd.override)
+        self.assertTrue(hasattr(self.cmd, '_distunset'))
+
+        self.assert_rpmdefines()
+
+    @patch('pyrpkg.Commands.branch_merge', new_callable=PropertyMock)
     @patch('fedpkg.Commands._findmasterbranch')
     def test_load_master_dist_tag(self, _findmasterbranch, branch_merge):
         _findmasterbranch.return_value = '28'

