From 718321298e74f6ba2ce976b38c52718c6580569e Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Mar 19 2020 14:40:55 +0000
Subject: allow to skip SRPM rebuild for scratch builds


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

---

diff --git a/builder/kojid b/builder/kojid
index f95cf5f..c8eea4f 100755
--- a/builder/kojid
+++ b/builder/kojid
@@ -1071,14 +1071,30 @@ class BuildTask(BaseTaskHandler):
             self.tagBuild(build_id, dest_tag)
 
     def getSRPM(self, src, build_tag, repo_id):
-        """Get srpm from src"""
+        """
+        Get srpm from src - it can fetch SCM and build SRPM from there or
+        alternatively get downloaded SRPM (and rebuild it if needed).
+
+        Buildroot has extra.rebuild_srpm field with default value of True. For
+        scratch builds we allow overriding by user, for regular builds only
+        buildtag's option can affect it.
+
+        :param str src:
+           SCM url or filename
+        :param str|dict build_tag:
+           build tag used for re/building srpm
+        :param int repo_id:
+           repo id to be used
+        """
         if isinstance(src, str):
             if SCM.is_scm_url(src):
                 return self.getSRPMFromSCM(src, build_tag, repo_id)
             else:
                 buildconfig = self.session.getBuildConfig(build_tag, event=self.event_id)
-                if buildconfig['extra'].get('rebuild_srpm', True):
-                    # default is to always rebuild
+                rebuild = buildconfig['extra'].get('rebuild_srpm', True)
+                if self.opts.get('scratch') and self.opts.get('rebuild_srpm') is not None:
+                    rebuild = self.opts.get('rebuild_srpm')
+                if rebuild:
                     return self.getSRPMFromSRPM(src, build_tag, repo_id)
                 else:
                     return src
diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py
index c083734..a175f9b 100644
--- a/cli/koji_cli/commands.py
+++ b/cli/koji_cli/commands.py
@@ -458,6 +458,10 @@ def handle_build(options, session, args):
                       help=_("Do not attempt to tag package"))
     parser.add_option("--scratch", action="store_true",
                       help=_("Perform a scratch build"))
+    parser.add_option("--rebuild-srpm", action="store_true", dest="rebuild_srpm",
+                      help=_("Force rebuilding SRPM for scratch build only"))
+    parser.add_option("--no-rebuild-srpm", action="store_false", dest="rebuild_srpm",
+                      help=_("Force not to rebuild srpm for scratch build only"))
     parser.add_option("--wait", action="store_true",
                       help=_("Wait on the build, even if running in the background"))
     parser.add_option("--nowait", action="store_false", dest="wait",
@@ -480,6 +484,8 @@ def handle_build(options, session, args):
     if len(args) != 2:
         parser.error(_("Exactly two arguments (a build target and a SCM URL or srpm file) are "
                        "required"))
+    if build_opts.rebuild_srpm is not None and not build_opts.scratch:
+        parser.error(_("--no-/rebuild-srpm is only allowed for --scratch builds"))
     if build_opts.arch_override and not build_opts.scratch:
         parser.error(_("--arch_override is only allowed for --scratch builds"))
     activate_session(session, options)
@@ -500,7 +506,8 @@ def handle_build(options, session, args):
     opts = {}
     if build_opts.arch_override:
         opts['arch_override'] = koji.parse_arches(build_opts.arch_override)
-    for key in ('skip_tag', 'scratch', 'repo_id', 'fail_fast', 'wait_repo', 'wait_builds'):
+    for key in ('skip_tag', 'scratch', 'repo_id', 'fail_fast', 'wait_repo', 'wait_builds',
+                'rebuild_srpm'):
         val = getattr(build_opts, key)
         if val is not None:
             opts[key] = val
diff --git a/tests/test_cli/test_build.py b/tests/test_cli/test_build.py
index 00459d5..101afd6 100644
--- a/tests/test_cli/test_build.py
+++ b/tests/test_cli/test_build.py
@@ -199,6 +199,8 @@ Options:
   -h, --help            show this help message and exit
   --skip-tag            Do not attempt to tag package
   --scratch             Perform a scratch build
+  --rebuild-srpm        Force rebuilding SRPM for scratch build only
+  --no-rebuild-srpm     Force not to rebuild srpm for scratch build only
   --wait                Wait on the build, even if running in the background
   --nowait              Don't wait on build
   --wait-repo           Wait for the actual buildroot repo of given target

