Repository: RightBrain-Networks/auto-semver

Issue:
Semver fails with double quotes in a commit
**Version of Auto-Semver:**

0.6.2

**Environment:**

docker

**Issue description:**

Having a double quote anywhere in the commit message will cause versioning to fail. For example:

this will work:

```
[klucas@klucas-wks ui-build-config] (master)$ git log -1
commit 081c914e0d5446aa6eaf8254a9ddb5cc127875fc (HEAD -> master, origin/master, origin/HEAD)
Merge: 9de95d8 7a67747
Author: Kevin Lucas <user@example.com>
Date:   Fri Oct 23 14:14:59 2020 +0000

    Merge branch 'bugfix/PSC-251-remove-polyfill' into 'master'
    
    Arbitrary change
    
    See merge request features/ui-build-config!15
```

But this will fail:

```
[klucas@klucas-wks ui-build-config] (master)$ git log -1
commit 49860371514800ddf853b887ef73c8d584fb4800 (HEAD -> master, origin/master, origin/HEAD)
Merge: 8dba406 431a34b
Author: Kevin Lucas <user@example.com>
Date:   Fri Oct 23 16:04:55 2020 +0000

    Merge branch 'bugfix/PSC-251-remove-polyfill' into 'master'
    
    Arbitrary "change"
    
    See merge request features/ui-build-config!17
```

**Steps to reproduce:**

Create a commit on another branch that has a double quote in the commit message. Merge the branch with the master branch in such a way that commit messages from the merged branch are included in the merge commit (fast-forward summery merges on GitLab) and try to version:

```
(app-root) semver -n --debug
2020-10-23 16:05:25,179 - INFO - Main branch is master
2020-10-23 16:05:25,180 - ERROR - No merge found
```

Discussion:

Patch:
diff --git a/Jenkinsfile b/Jenkinsfile
index cbbd435..10aee15 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,4 +1,4 @@
-library('pipeline-library')
+library('pipeline-library@bugfix/durable-task-workaround')
 
 pipeline {
   options { timestamps() }
@@ -18,7 +18,7 @@ pipeline {
     stage('Self Version') {
       steps {
         withCredentials([usernamePassword(credentialsId: env.DOCKER_CREDENTIALS, usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
-          sh("docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}")
+          sh("docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD")
         }
         runAutoSemver("rightbrainnetworks/auto-semver:${SELF_SEMVER_TAG}")
       }
@@ -59,6 +59,7 @@ pipeline {
       agent {
           docker {
               image "rightbrainnetworks/auto-semver:${env.VERSION}"
+              args "--privileged"
           }
       }
       steps
@@ -85,7 +86,7 @@ pipeline {
           // Authenticate & push to DockerHub
           withCredentials([usernamePassword(credentialsId: env.DOCKER_CREDENTIALS, usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
             sh("""
-              docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
+              docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
               docker push rightbrainnetworks/auto-semver:${env.VERSION}
               """)
           }
diff --git a/semver/__init__.py b/semver/__init__.py
index da6a610..e0075ee 100644
--- a/semver/__init__.py
+++ b/semver/__init__.py
@@ -1,6 +1,8 @@
 import argparse
 import re
 import subprocess
+import sys
+import traceback
 from enum import IntEnum
 from semver.utils import get_tag_version
 from semver.logger import logging, logger, console_logger
@@ -59,7 +61,7 @@ class SemVer(object):
         message = str(p.stdout.read())
         branch = b.stdout.read().decode('utf-8').rstrip()
         logger.info('Main branch is ' + branch)
-        matches = GET_COMMIT_MESSAGE.search(message)
+        matches = GET_COMMIT_MESSAGE.search(message.replace('\\n','\n').replace('\\',''))
         if matches:
             if str(matches.group(4)) == branch:
                 self.merged_branch = matches.group(2)
@@ -111,7 +113,7 @@ class SemVer(object):
             config_file = file.read()
 
         # version repo
-        logger.debug("Running bumpversion of type: " + self.version_type)
+        logger.debug("Running bumpversion of type: " + str(self.version_type.name))
         bump_version(get_tag_version(), self.version_type)
         return self
 
@@ -146,20 +148,22 @@ class SemVer(object):
         return self
 
 def main():
-    try:
-        parser = argparse.ArgumentParser(description='Bump Semantic Version.')
-        parser.add_argument('-n','--no-push', help='Do not try to push', action='store_false', dest='push')
-        parser.add_argument('-g','--global-user', help='Set git user at a global level, helps in jenkins', action='store_true', dest='global_user')
-        parser.add_argument('-D', '--debug', help='Sets logging level to DEBUG', action='store_true', dest='debug', default=False)
-        args = parser.parse_args()
-
+    parser = argparse.ArgumentParser(description='Bump Semantic Version.')
+    parser.add_argument('-n','--no-push', help='Do not try to push', action='store_false', dest='push')
+    parser.add_argument('-g','--global-user', help='Set git user at a global level, helps in jenkins', action='store_true', dest='global_user')
+    parser.add_argument('-D', '--debug', help='Sets logging level to DEBUG', action='store_true', dest='debug', default=False)
+    args = parser.parse_args()
 
-        if args.debug:
-            console_logger.setLevel(logging.DEBUG)
 
+    if args.debug:
+        console_logger.setLevel(logging.DEBUG)
+    try:
         SemVer(global_user=args.global_user).run(push=args.push)
     except Exception as e:
         logger.error(e)
+        if args.debug:
+            tb = sys.exc_info()[2]
+            traceback.print_tb(tb)
         if e == NO_MERGE_FOUND:
             exit(1)
         elif e == NOT_MAIN_BRANCH:


Test patch:
diff --git a/semver/tests.py b/semver/tests.py
index 6383cd0..63025fa 100644
--- a/semver/tests.py
+++ b/semver/tests.py
@@ -158,6 +158,13 @@ class TestGetCommitMessageRegex(unittest.TestCase):
     def test_non_merge_message(self):
         matches = GET_COMMIT_MESSAGE.search("Example unrelated commit message that should get 0 matches")
         self.assertEqual(matches, None)
+    def test_gitlab_merge_with_double_quotes(self):
+        matches = GET_COMMIT_MESSAGE.search("Merge branch 'branch' into 'master'\n\n\"Message in quotes!\"")
+        if matches:
+            self.assertEqual(matches.group(4), "master")
+            self.assertEqual(matches.group(2), "branch")
+        else:
+            self.assertTrue(False)
 
 class TestVersionBumping(unittest.TestCase):
     def test_patch_bump(self):