From 20325648079ba7260d58c6edf94e01d74f1eb958 Mon Sep 17 00:00:00 2001 From: Fred Date: Tue, 30 Apr 2013 19:54:06 +0800 Subject: [PATCH] Backport and Rebase can update the tracker git info --- extra/bash_completion | 4 ++-- lib/commands/backport.py | 14 +++++++++++++- lib/commands/push.py | 37 ++----------------------------------- lib/commands/rebase.py | 12 ++++++++++++ lib/moodle.py | 42 +++++++++++++++++++++++++++++++++++++++++- 5 files changed, 70 insertions(+), 39 deletions(-) diff --git a/extra/bash_completion b/extra/bash_completion index 0aedb1c..2efbac0 100644 --- a/extra/bash_completion +++ b/extra/bash_completion @@ -60,7 +60,7 @@ function _moodle() { ;; backport) if [[ "$CUR" == -* ]]; then - OPTS="--branch --remote --integration --dont-push --dont-fetch --push --push-to --force-push --versions" + OPTS="--branch --remote --integration --dont-push --dont-fetch --push --push-to --force-push --versions --update-tracker" else OPTS="-- $($BIN info -ln)" fi @@ -156,7 +156,7 @@ function _moodle() { ;; rebase) if [[ "$PREV" == "rebase" || "$PREV" == "-p" || "$PREV" == "--push" || "$PREV" == "-f" || "$PREV" == "--force-push" ]]; then - OPTS="--issues --suffix --versions --push --remote --force-push" + OPTS="--issues --suffix --versions --push --remote --force-push --update-tracker" if [[ "$CUR" != -* ]]; then OPTS="$OPTS $($BIN info -ln)" fi diff --git a/lib/commands/backport.py b/lib/commands/backport.py index 7256788..df3e0c6 100644 --- a/lib/commands/backport.py +++ b/lib/commands/backport.py @@ -65,7 +65,7 @@ class BackportCommand(Command): } ), ( - ['-t', '--push-to'], + ['--push-to'], { 'dest': 'pushremote', 'help': 'the remote to push the branch to. Default is %s.' % self.C.get('myRemote'), @@ -73,6 +73,14 @@ class BackportCommand(Command): } ), ( + ['-t', '--update-tracker'], + { + 'action': 'store_true', + 'dest': 'updatetracker', + 'help': 'to use with --push, also add the diff information to the tracker issue' + } + ), + ( ['-v', '--versions'], { 'choices': [str(x) for x in range(13, int(self.C.get('masterBranch')))] + ['master'], @@ -205,6 +213,10 @@ class BackportCommand(Command): stashPop(stash) continue + # Update the tracker + if args.updatetracker: + M2.updateTrackerGitInfo(branch=newbranch) + stashPop(stash) logging.info('Instance %s successfully patched!' % name) diff --git a/lib/commands/push.py b/lib/commands/push.py index f6f3474..9c8ffef 100644 --- a/lib/commands/push.py +++ b/lib/commands/push.py @@ -114,42 +114,9 @@ class PushCommand(Command): if result[0] != 0: raise Exception(result[2]) + # Update the tracker if args.updatetracker: - # Getting issue number - # Parsing the branch - parsedbranch = tools.parseBranch(branch, self.C.get('wording.branchRegex')) - if not parsedbranch: - raise Exception('Could not extract issue number from %s' % branch) - issue = 'MDL-%s' % (parsedbranch['issue']) - - version = parsedbranch['version'] - - # Get the jira config - repositoryurl = self.C.get('repositoryUrl') - diffurltemplate = self.C.get('diffUrlTemplate') - stablebranch = M.get('stablebranch') - upstreamremote = self.C.get('upstreamRemote') - # Get the hash of the last upstream commit - ref = '%s/%s' % (upstreamremote, stablebranch) - headcommit = M.git().hashes(ref=ref, limit=1)[0] - - J = jira.Jira() - diffurl = diffurltemplate.replace('%branch%', branch).replace('%stablebranch%', stablebranch).replace('%headcommit%', headcommit) - - fieldrepositoryurl = self.C.get('tracker.fieldnames.repositoryurl') - fieldbranch = self.C.get('tracker.fieldnames.%s.branch' % version) - fielddiffurl = self.C.get('tracker.fieldnames.%s.diffurl' % version) - - if not (fieldrepositoryurl or fieldbranch or fielddiffurl): - logging.error('Cannot set tracker fields for this version (%s) as the field names are not configured in the config file.', version) - - else: - logging.info('Setting tracker fields: \n\t%s: %s \n\t%s: %s \n\t%s: %s\n' % (fieldrepositoryurl, repositoryurl, - fieldbranch, branch, - fielddiffurl, diffurl)) - J.setCustomFields(issue, {fieldrepositoryurl: repositoryurl, - fieldbranch: branch, - fielddiffurl: diffurl}) + M.updateTrackerGitInfo(branch=branch) # Pushing stable branch if args.includestable: diff --git a/lib/commands/rebase.py b/lib/commands/rebase.py index fe7ca6a..2e236a8 100644 --- a/lib/commands/rebase.py +++ b/lib/commands/rebase.py @@ -66,6 +66,14 @@ class RebaseCommand(Command): } ), ( + ['-t', '--update-tracker'], + { + 'action': 'store_true', + 'dest': 'updatetracker', + 'help': 'to use with --push, also add the diff information to the tracker issue' + } + ), + ( ['-r', '--remote'], { 'default': self.C.get('myRemote'), @@ -168,6 +176,10 @@ class RebaseCommand(Command): logging.debug(result[2]) continue + # Update the tracker + if args.updatetracker: + M.updateTrackerGitInfo(branch=branch) + # Stash pop if not stash[1].startswith('No local changes'): pop = M.git().stash(command='pop') diff --git a/lib/moodle.py b/lib/moodle.py index f9ad091..0e120f5 100644 --- a/lib/moodle.py +++ b/lib/moodle.py @@ -28,11 +28,12 @@ import logging import shutil import subprocess -from tools import process +from tools import process, parseBranch from db import DB from config import Conf from git import Git from exceptions import ScriptNotFound, InstallException +from jira import Jira C = Conf() @@ -601,6 +602,45 @@ class Moodle(object): self.removeConfig(name) self.addConfig(name, value) + def updateTrackerGitInfo(self, branch=None): + """Updates the git info on the tracker issue""" + + if branch == None: + branch = self.currentBranch() + if branch == 'HEAD': + raise Exception('Cannot update the tracker when on detached branch') + + # Parsing the branch + parsedbranch = parseBranch(branch, C.get('wording.branchRegex')) + if not parsedbranch: + raise Exception('Could not extract issue number from %s' % branch) + issue = 'MDL-%s' % (parsedbranch['issue']) + version = parsedbranch['version'] + + # Get the jira config + repositoryurl = C.get('repositoryUrl') + diffurltemplate = C.get('diffUrlTemplate') + stablebranch = self.get('stablebranch') + upstreamremote = C.get('upstreamRemote') + + # Get the hash of the last upstream commit + ref = '%s/%s' % (upstreamremote, stablebranch) + headcommit = self.git().hashes(ref=ref, limit=1)[0] + + J = Jira() + diffurl = diffurltemplate.replace('%branch%', branch).replace('%stablebranch%', stablebranch).replace('%headcommit%', headcommit) + + fieldrepositoryurl = C.get('tracker.fieldnames.repositoryurl') + fieldbranch = C.get('tracker.fieldnames.%s.branch' % version) + fielddiffurl = C.get('tracker.fieldnames.%s.diffurl' % version) + + if not (fieldrepositoryurl or fieldbranch or fielddiffurl): + logging.error('Cannot set tracker fields for this version (%s). The field names are not set in the config file.', version) + else: + logging.info('Setting tracker fields: \n\t%s: %s \n\t%s: %s \n\t%s: %s' % + (fieldrepositoryurl, repositoryurl, fieldbranch, branch, fielddiffurl, diffurl)) + J.setCustomFields(issue, {fieldrepositoryurl: repositoryurl, fieldbranch: branch, fielddiffurl: diffurl}) + def upgrade(self, nocheckout=False): """Calls the upgrade script""" if not self.isInstalled(): -- 2.11.0