From c88b0a3298425add47c7684cb4f36705d81cf266 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Wed, 2 Oct 2013 15:36:25 +0800 Subject: [PATCH] Better extracting of MDL from commit messages. Fixed #42 --- lib/commands/push.py | 19 +++++++++++++++---- lib/moodle.py | 4 ++-- lib/tools.py | 9 +++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/commands/push.py b/lib/commands/push.py index 0f1d398..3bb07ce 100644 --- a/lib/commands/push.py +++ b/lib/commands/push.py @@ -23,8 +23,10 @@ http://github.com/FMCorz/mdk """ import logging +import re from lib import tools, jira from lib.command import Command +from lib.tools import getMDLFromCommitMessage class PushCommand(Command): @@ -114,14 +116,23 @@ class PushCommand(Command): parsedbranch = tools.parseBranch(branch, self.C.get('wording.branchRegex')) if parsedbranch or branch != M.get('stablebranch'): message = M.git().messages(count=1)[0] - mdl = message.split(' ')[0] + + mdl = getMDLFromCommitMessage(message) + if parsedbranch: branchmdl = 'MDL-%s' % (parsedbranch['issue']) else: branchmdl = branch - if mdl != branchmdl: - print 'The MDL number in the last commit does not match the branch being pushed to.' - print 'Branch: %s vs. commit: %s' % (branchmdl, mdl) + + if not mdl or mdl != branchmdl: + if not mdl: + print 'The MDL number could not be found in the commit message.' + print 'Commit: %s' % (message) + + elif mdl != branchmdl: + print 'The MDL number in the last commit does not match the branch being pushed to.' + print 'Branch: \'%s\' vs. commit: \'%s\'' % (branchmdl, mdl) + answer = tools.question('Are you sure you want to continue?', default='n') if answer.lower()[0] != 'y': print 'Exiting...' diff --git a/lib/moodle.py b/lib/moodle.py index 81d3b4c..eb8919b 100644 --- a/lib/moodle.py +++ b/lib/moodle.py @@ -27,7 +27,7 @@ import re import logging import shutil -from tools import mkdir, process, parseBranch +from tools import getMDLFromCommitMessage, mkdir, process, parseBranch from db import DB from config import Conf from git import Git, GitException @@ -258,7 +258,7 @@ class Moodle(object): # Looping over the last commits to find the commit messages that match the MDL-12345. candidate = None for commit in commits: - match = commit.strip().lower().startswith(issue.lower()) + match = getMDLFromCommitMessage(commit) == issue if not candidate and not match: # The first commit does not match a hash, let's ignore this method. break diff --git a/lib/tools.py b/lib/tools.py index d517358..6762b7e 100644 --- a/lib/tools.py +++ b/lib/tools.py @@ -73,6 +73,15 @@ def chmodRecursive(path, chmod): os.chmod(file, chmod) +def getMDLFromCommitMessage(message): + """Return the MDL-12345 number from a commit message""" + mdl = None + match = re.match(r'MDL(-|_)([0-9]+)', message, re.I) + if match: + mdl = 'MDL-%s' % (match.group(2)) + return mdl + + def get_current_user(): """Attempt to get the currently logged in user""" username = 'root' -- 2.11.0