Example #1
0
    def updateTrackerGitInfo(self, branch=None, ref=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')

        # Get the hash of the last upstream commit
        headcommit = None
        logging.info('Searching for the head commit...')
        if ref:
            try:
                headcommit = self.git().hashes(ref=ref, limit=1, format='%h')[0]
            except GitException:
                logging.warning('Could not resolve a head commit using the reference: %s' % (ref))
                headcommit = None

        # No reference was passed, or it was invalid.
        if not headcommit:
            headcommit = self.headcommit(branch)

        # Head commit not resolved
        if not headcommit:
            logging.error('Head commit not resolved, aborting update of tracker fields')
            return False

        logging.debug('Head commit resolved to %s' % (headcommit))

        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 not fieldbranch or not 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  %s: %s \n  %s: %s \n  %s: %s' %
                (fieldrepositoryurl, repositoryurl, fieldbranch, branch, fielddiffurl, diffurl))
            J.setCustomFields(issue, {fieldrepositoryurl: repositoryurl, fieldbranch: branch, fielddiffurl: diffurl})
Example #2
0
    def pushPatch(self, branch=None):
        """Push a patch on the tracker, and remove the previous one"""

        if branch == None:
            branch = self.currentBranch()
            if branch == 'HEAD':
                raise Exception('Cannot create a patch from a 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'])
        headcommit = self.headcommit(branch)

        # Creating a patch file.
        fileName = branch + '.mdk.patch'
        tmpPatchFile = os.path.join(gettempdir(), fileName)
        if self.git().createPatch('%s...%s' % (headcommit, branch), saveTo=tmpPatchFile):

            J = Jira()

            # Checking if file with same name exists.
            existingAttachmentId = None
            existingAttachments = J.getIssue(issue, fields='attachment')
            for existingAttachment in existingAttachments.get('fields', {}).get('attachment', {}):
                if existingAttachment.get('filename') == fileName:
                    # Found an existing attachment with the same name, we keep track of it.
                    existingAttachmentId = existingAttachment.get('id')
                    break

            # Pushing patch to the tracker.
            try:
                logging.info('Uploading %s to the tracker' % (fileName))
                J.upload(issue, tmpPatchFile)
            except JiraException:
                logging.error('Error while uploading the patch to the tracker')
                return False
            else:
                if existingAttachmentId != None:
                    # On success, deleting file that was there before.
                    try:
                        logging.info('Deleting older patch...')
                        J.deleteAttachment(existingAttachmentId)
                    except JiraException:
                        logging.info('Could not delete older attachment')

        else:
            logging.error('Could not create a patch file')
            return False

        return True
Example #3
0
    def headcommit(self, branch=None):
        """Try to resolve the head commit of branch of this instance"""

        if branch == None:
            branch = self.currentBranch()
            if branch == 'HEAD':
                raise Exception('Cannot update the tracker when on detached branch')

        smartSearch = C.get('smartHeadCommitSearch')

        # Parsing the branch
        parsedbranch = parseBranch(branch, C.get('wording.branchRegex'))
        if parsedbranch:
            issue = 'MDL-%s' % (parsedbranch['issue'])
        else:
            logging.debug('Cannot smart resolve using the branch %s' % (branch))
            smartSearch = False

        headcommit = None
        try:
            # Trying to smart guess the last commit needed
            if smartSearch:
                commits = self.git().log(since=branch, count=C.get('smartHeadCommitLimit'), format='%s_____%h').split('\n')[:-1]

                # Looping over the last commits to find the commit messages that match the MDL-12345.
                candidate = None
                for commit in commits:
                    match = getMDLFromCommitMessage(commit) == issue
                    if not candidate and not match:
                        # The first commit does not match a hash, let's ignore this method.
                        break
                    candidate = commit.split('_____')[-1]
                    if not match:
                        # The commit does not match any more, we found it!
                        headcommit = candidate
                        break

            # We could not smart find the last commit, let's use the default mechanism.
            if not headcommit:
                upstreamremote = C.get('upstreamRemote')
                stablebranch = self.get('stablebranch')
                headcommit = self.git().hashes(ref='%s/%s' % (upstreamremote, stablebranch), limit=1, format='%h')[0]

        except GitException:
            logging.warning('Could not resolve the head commit')
            headcommit = False

        return headcommit
Example #4
0
    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})
Example #5
0
    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, format='%h')[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})