CI: Properly run gitlint in CI 41/67641/4
authorAndrew Grimberg <agrimberg@linuxfoundation.org>
Sat, 8 May 2021 14:40:45 +0000 (07:40 -0700)
committerAndrew Grimberg <agrimberg@linuxfoundation.org>
Sat, 8 May 2021 15:19:41 +0000 (08:19 -0700)
When running pre-commit in a CI system the COMMIT_EDITMSG does not
(normally) get created as that is an artifact of editing the commit
message. If the file doesn't exist then gitlint will skip which makes it
possible for pre-commit checks that should fail, to pass.

Since we want tox to run in a consistent manner both locally and in CI
we need to play around a little with how we are checking the commit
message.

Change-Id: I3990aa9846dc8479cadaad5025e45f863623bf86
Signed-off-by: Andrew Grimberg <agrimberg@linuxfoundation.org>
tox.ini

diff --git a/tox.ini b/tox.ini
index 0da5583..f008b29 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -51,11 +51,30 @@ commands = lftools license check-dir -r '.+' shell
 
 [testenv:pre-commit]
 basepython = python3
+allowlist_externals =
+    /bin/sh
 deps =
     pre-commit
+passenv = HOME
 commands =
     pre-commit run --all-files --show-diff-on-failure
-    pre-commit run gitlint --hook-stage commit-msg --commit-msg-filename .git/COMMIT_EDITMSG
+    /bin/sh -c 'if ! git config --get user.name > /dev/null; then \
+        git config --global --add user.name "CI"; \
+        touch .git/REMOVE_USERNAME; fi'
+    /bin/sh -c 'if ! git config --get user.email > /dev/null; then \
+        git config --global --add user.email "ci@example.org"; \
+        touch .git/REMOVE_USEREMAIL; fi'
+    /bin/sh -c "if [ -f .git/COMMIT_EDITMSG ]; then \
+        cp .git/COMMIT_EDITMSG .git/COMMIT_MSGTOX; else \
+        git log HEAD -n1 | tail -n +5 | cut -c 5- > .git/COMMIT_MSGTOX; fi"
+    pre-commit run gitlint --hook-stage commit-msg --commit-msg-filename .git/COMMIT_MSGTOX
+    /bin/sh -c "rm -f .git/COMMIT_MSGTOX"
+    /bin/sh -c "if [ -f .git/REMOVE_USERNAME ]; then \
+        git config --global --unset user.name; \
+        rm -f .git/REMOVE_USERNAME; fi"
+    /bin/sh -c "if [ -f .git/REMOVE_USEREMAIL ]; then \
+        git config --global --unset user.email; \
+        rm -f .git/REMOVE_USEREMAIL; fi"
 
 #########
 # Tools #