Repository: cekit/cekit
Pull request #299 (2018-08-28 07:44:22)

Custom commit message for dist-git
Added new option `--build-osbs-commit-msg` which you can use to
specify custom commit message for dist-git

dbecvarik: fixes #224 
goldmann: Probably a mention in the docs would be useful, WDYT?
dbecvarik: its mentioned https://github.com/cekit/cekit/pull/299/files#diff-f9476ebc9a3c20aef98265a9b3fe9357R32

goldmann: Ah, sorry!
dbecvarik: nps, thanks :)

Patch:
diff --git a/cekit/builders/osbs.py b/cekit/builders/osbs.py
index 1e56ea1..8a01986 100644
--- a/cekit/builders/osbs.py
+++ b/cekit/builders/osbs.py
@@ -14,7 +14,10 @@ logger = logging.getLogger('cekit')
 class OSBSBuilder(Builder):
     """Class representing OSBS builder."""
 
-    def __init__(self, build_engine, target, params={}):
+    def __init__(self, build_engine, target, params=None):
+        if not params:
+            params = {}
+        self._commit_msg = params.get('commit_msg')
         self._user = params.get('user')
         self._nowait = params.get('nowait', False)
         self._release = params.get('release', False)
@@ -147,7 +150,7 @@ class OSBSBuilder(Builder):
             self.update_lookaside_cache()
 
             if self.dist_git.stage_modified():
-                self.dist_git.commit()
+                self.dist_git.commit(self._commit_msg)
                 self.dist_git.push()
             else:
                 logger.info("No changes made to the code, committing skipped")
@@ -249,14 +252,15 @@ class DistGit(object):
             # we probably do not care about non existing files and other errors here
             subprocess.call(["git", "add", "--all", d])
 
-    def commit(self):
-        commit_msg = "Sync"
+    def commit(self, commit_msg):
+        if not commit_msg:
+            commit_msg = "Sync"
 
-        if self.source_repo_name:
-            commit_msg += " with %s" % self.source_repo_name
+            if self.source_repo_name:
+                commit_msg += " with %s" % self.source_repo_name
 
-        if self.source_repo_commit:
-            commit_msg += ", commit %s" % self.source_repo_commit
+            if self.source_repo_commit:
+                commit_msg += ", commit %s" % self.source_repo_commit
 
         # Commit the change
         logger.info("Commiting with message: '%s'" % commit_msg)
diff --git a/cekit/cli.py b/cekit/cli.py
index 64cea29..a243f61 100644
--- a/cekit/cli.py
+++ b/cekit/cli.py
@@ -113,6 +113,10 @@ class Cekit(object):
                                  dest='build_osbs_target',
                                  help='overrides the default rhpkg target')
 
+        build_group.add_argument('--build-osbs-commit-msg',
+                                 dest='build_osbs_commit_msg',
+                                 help='commit message for dist-git')
+
         build_group.add_argument('--build-tech-preview',
                                  action='store_true',
                                  help='perform tech preview build')
@@ -242,7 +246,8 @@ class Cekit(object):
                           'tags': self.args.tags,
                           'pull': self.args.build_pull,
                           'redhat': tools.cfg['common']['redhat'],
-                          'target': self.args.build_osbs_target
+                          'target': self.args.build_osbs_target,
+                          'commit_msg': self.args.build_osbs_commit_msg
                           }
 
                 builder = Builder(self.args.build_engine,
diff --git a/completion/bash/cekit b/completion/bash/cekit
index fa4ce76..2398fe7 100755
--- a/completion/bash/cekit
+++ b/completion/bash/cekit
@@ -22,6 +22,7 @@ _cekit_build_options()
     options+='--build-osbs-nowait '
     options+='--build-osbs-stage '
     options+='--build-osbs-target '
+    options+='--build-osbs-commit-msg '
     options+='--build-tech-preview '
     echo "$options"
 }
diff --git a/completion/zsh/_cekit b/completion/zsh/_cekit
index 03bffc5..cb33cc1 100644
--- a/completion/zsh/_cekit
+++ b/completion/zsh/_cekit
@@ -68,6 +68,7 @@ _cekit_build() {
         "--build-osbs-nowait[run rhpkg container build with --nowait option]" \
         "--build-osbs-stage[use rhpkg-stage instead of rhpkg]" \
         "--build-osbs-target[overrides the default rhpkg target]" \
+	"--build-osbs-commit-msg[commit message for dist-git]" \
         "--build-tech-preview[perform tech preview build]"
 }
 
diff --git a/docs/build.rst b/docs/build.rst
index 237264e..74eca71 100644
--- a/docs/build.rst
+++ b/docs/build.rst
@@ -29,6 +29,7 @@ You can execute an container image build by running:
 * ``--build-osbs-release`` [#f2]_ -- perform a OSBS release build
 * ``--build-osbs-user`` -- alternative user passed to `rhpkg --user`
 * ``--build-osbs-target`` -- overrides the default ``rhpkg`` target
+* ``--build-osbs-commit-msg`` -- custom commit message for dist-git
 * ``--build-osbs-nowait`` -- run `rhpkg container-build` with `--nowait` option specified
 * ``--build-tech-preview`` [#f2]_ -- updates image descriptor ``name`` key to contain ``-tech-preview`` suffix in family part of the image name
   


Test patch:
diff --git a/tests/test_builder.py b/tests/test_builder.py
index 43af310..dec7c0a 100644
--- a/tests/test_builder.py
+++ b/tests/test_builder.py
@@ -31,6 +31,16 @@ def test_osbs_builder_use_rhpkg_staget(mocker):
     assert builder._rhpkg == 'rhpkg-stage'
 
 
+def test_osbs_builder_custom_commit_msg(mocker):
+    mocker.patch.object(subprocess, 'check_output')
+
+    params = {'stage': True,
+              'commit_msg': 'foo'}
+    builder = Builder('osbs', 'tmp', params)
+
+    assert builder._commit_msg == 'foo'
+
+
 def test_osbs_builder_nowait(mocker):
     mocker.patch.object(subprocess, 'check_output')