Exemple #1
0
 def postCommit(args):
     #applies the autoPush hook
     autoPush = args["--autopush"]
     if autoPush.lower().strip() != "false":
         try:
             git.push("-u origin HEAD")
         except git.GrapeGitError:
             pass
         autoPush = True
     else:
         autoPush = False
     #applies the cascade hook
     print("GRAPE: checking for cascades...")
     cascadeDict = grapeConfig.GrapeConfigParser.parseConfigPairList(args["--cascade"])
     if cascadeDict:
         currentBranch = git.currentBranch()
         while currentBranch in cascadeDict:
             source = currentBranch
             target = cascadeDict[source]
             fastForward = False
             print("GRAPE: Cascading commit from %s to %s..." % (source, target))
             if git.branchUpToDateWith(source, target):
                 fastForward = True
                 print("GRAPE: should be a fastforward cascade...")
             git.checkout("%s" % target)
             git.merge("%s -m 'Cascade from %s to %s'" % (source, source, target))
             # we need to kick off the next one if it was a fast forward merge.
             # otherwise, another post-commit hook should be called from the merge commit.
             if fastForward:
                 if autoPush:
                     git.push("origin %s" % target)
                     print("GRAPE: auto push done")
                 currentBranch = target
             else:
                 currentBranch = None
Exemple #2
0
def createNewBranches(repo='', branch='', args={}):
    project = repo
    checkoutargs = args["checkout"]
    with utility.cd(project):
        utility.printMsg("Creating new branch %s in %s." % (branch, project))
        git.checkout(checkoutargs+" -b "+branch)
        git.push("-u origin %s" % branch)
    return True
Exemple #3
0
def createNewBranches(repo='', branch='', args={}):
    project = repo
    checkoutargs = args["checkout"]
    with utility.cd(project):
        utility.printMsg("Creating new branch %s in %s." % (branch, project))
        git.checkout(checkoutargs + " -b " + branch)
        git.push("-u origin %s" % branch)
    return True
Exemple #4
0
def detachThenForceDeleteBranch(repo='', branch='master', args=None):
    with utility.cd(repo):
        utility.printMsg(
            "*** WARNING ***: Detaching in order to delete %s in %s. You will be in a headless state."
            % (branch, repo))
        git.checkout("--detach HEAD")
        git.branch("-D %s" % branch)
        if "origin/%s" % branch in git.remoteBranches():
            git.push("--delete origin %s" % branch, throwOnFail=False)
Exemple #5
0
def deleteBranch(repo='', branch='master', args=None):
    force = args[0]
    forceStr = "-D" if force is True else "-d"
    with utility.cd(repo):
        utility.printMsg("deleting %s in %s..." % (branch, repo))
        git.branch("%s %s" % (forceStr, branch))
        if "origin/%s" % branch in git.branch("-r"):
            try:
                git.push("--delete origin %s" % branch, throwOnFail=True)
            except git.GrapeGitError as e:
                if "remote ref does not exist" in e.gitOutput:
                    pass
Exemple #6
0
def createBranch(repo="unknown", branch="master", args=[]):
    branchPoint = branch
    fullBranch = args
    with utility.cd(repo):
        utility.printMsg("creating and switching to %s in %s" % (fullBranch, repo))
        try:
            git.checkout("-b %s %s " % (fullBranch, branchPoint))
        except git.GrapeGitError as e:
            print "%s:%s" % (repo, e.gitOutput)
            utility.printMsg("WARNING: %s in %s will not be pushed." % (fullBranch, repo))
            return
        utility.printMsg("pushing %s to origin in %s" % (fullBranch, repo))
        try:
            git.push("-u origin %s" % fullBranch)
        except git.GrapeGitError as e:
            print "%s:  %s" % (repo, e.gitOutput)
            return
Exemple #7
0
def createBranch(repo="unknown", branch="master", args=[]):
    branchPoint = branch
    fullBranch = args
    with utility.cd(repo):
        utility.printMsg("creating and switching to %s in %s" %
                         (fullBranch, repo))
        try:
            git.checkout("-b %s %s " % (fullBranch, branchPoint))
        except git.GrapeGitError as e:
            print "%s:%s" % (repo, e.gitOutput)
            utility.printMsg("WARNING: %s in %s will not be pushed." %
                             (fullBranch, repo))
            return
        utility.printMsg("pushing %s to origin in %s" % (fullBranch, repo))
        try:
            git.push("-u origin %s" % fullBranch)
        except git.GrapeGitError as e:
            print "%s:  %s" % (repo, e.gitOutput)
            return
Exemple #8
0
 def postCommit(args):
     #applies the autoPush hook
     autoPush = args["--autopush"]
     if autoPush.lower().strip() != "false":
         try:
             git.push("-u origin HEAD")
         except git.GrapeGitError:
             pass
         autoPush = True
     else:
         autoPush = False
     #applies the cascade hook
     print("GRAPE: checking for cascades...")
     cascadeDict = grapeConfig.GrapeConfigParser.parseConfigPairList(
         args["--cascade"])
     if cascadeDict:
         currentBranch = git.currentBranch()
         while currentBranch in cascadeDict:
             source = currentBranch
             target = cascadeDict[source]
             fastForward = False
             print("GRAPE: Cascading commit from %s to %s..." %
                   (source, target))
             if git.branchUpToDateWith(source, target):
                 fastForward = True
                 print("GRAPE: should be a fastforward cascade...")
             git.checkout("%s" % target)
             git.merge("%s -m 'Cascade from %s to %s'" %
                       (source, source, target))
             # we need to kick off the next one if it was a fast forward merge.
             # otherwise, another post-commit hook should be called from the merge commit.
             if fastForward:
                 if autoPush:
                     git.push("origin %s" % target)
                     print("GRAPE: auto push done")
                 currentBranch = target
             else:
                 currentBranch = None
Exemple #9
0
    def execute(self, args):
        """
        A fair chunk of this stuff relies on stashy's wrapping of the STASH REST API, which is posted at
        https://developer.atlassian.com/static/rest/stash/2.12.1/stash-rest.html
        """
        config = grapeConfig.grapeConfig()
        name = args["--user"]
        if not name:
            name = utility.getUserName()
            
        utility.printMsg("Logging onto %s" % args["--bitbucketURL"])
        if args["--test"]:
            bitbucket = Atlassian.TestAtlassian(name)
        else:
            verify = True if args["--verifySSL"].lower() == "true" else False
            bitbucket = Atlassian.Atlassian(name, url=args["--bitbucketURL"], verify=verify)

        # default project (outer level project)
        project_name = args["--project"]
        
        # default repo (outer level repo)
        repo_name = args["--repo"]

        # determine source branch and target branch
        branch = args["--source"]
        if not branch:
            branch = git.currentBranch()

        # make sure we are in the outer level repo before we push
        wsDir = utility.workspaceDir()
        os.chdir(wsDir)

        #ensure branch is pushed
        utility.printMsg("Pushing %s to bitbucket..." % branch)
        git.push("origin %s" % branch)
        #target branch for outer level repo
        target_branch = args["--target"]
        if not target_branch:
            target_branch = config.getPublicBranchFor(branch)        
        # load pull request from Bitbucket if it already exists
        wsRepo =  bitbucket.project(project_name).repo(repo_name)
        existingOuterLevelRequest = getReposPullRequest(wsRepo, branch, target_branch, args)  

        # determine pull request title
        title = args["--title"]
        if existingOuterLevelRequest is not None and not title:
            title = existingOuterLevelRequest.title()

        
        #determine pull request URL
        outerLevelURL = None
        if existingOuterLevelRequest:
            outerLevelURL = existingOuterLevelRequest.link()
        
        # determine pull request description
        descr = self.parseDescriptionArgs(args)

        if not descr and existingOuterLevelRequest:
            descr = existingOuterLevelRequest.description()

    
        # determine pull request reviewers
        reviewers = self.parseReviewerArgs(args)
        if reviewers is None and existingOuterLevelRequest is not None:
            reviewers = [r[0] for r in existingOuterLevelRequest.reviewers()]

        # if we're in append mode, only append what was asked for:
        if args["--append"] or args["--prepend"]:
            title = args["--title"]
            descr = self.parseDescriptionArgs(args)
            reviewers = self.parseReviewerArgs(args)
            
        ##  Submodule Repos
        missing = utility.getModifiedInactiveSubmodules(target_branch, branch)
        if missing:
            utility.printMsg("The following submodules that you've modified are not currently present in your workspace.\n"
                             "You should activate them using grape uv  and then call grape review again. If you haven't modified "
                             "these submodules, you may need to do a grape md to proceed.")
            utility.printMsg(','.join(missing))
            return False        
        pullRequestLinks = {}
        if not args["--norecurse"] and (args["--recurse"] or config.getboolean("workspace", "manageSubmodules")):
            
            modifiedSubmodules = git.getModifiedSubmodules(target_branch, branch)
            submoduleBranchMappings = config.getMapping("workspace", "submoduleTopicPrefixMappings")
                        
            for submodule in modifiedSubmodules:
                if not submodule:
                    continue
                # push branch
                os.chdir(submodule)
                utility.printMsg("Pushing %s to bitbucket..." % branch)
                git.push("origin %s" % branch)
                os.chdir(wsDir)
                repo = bitbucket.repoFromWorkspaceRepoPath(submodule, 
                                                         isSubmodule=True)
                # determine branch prefix
                prefix = branch.split('/')[0]
                sub_target_branch = submoduleBranchMappings[prefix]
                
                prevSubDescr = getReposPullRequestDescription(repo, branch, 
                                                             sub_target_branch, 
                                                             args)
                #amend the subproject pull request description with the link to the outer pull request
                subDescr = addLinkToDescription(descr, outerLevelURL, True)
                if args["--prepend"] or args["--append"]:
                    subDescr = descr
                newRequest = postPullRequest(repo, title, branch, sub_target_branch, subDescr, reviewers, args)
                if newRequest:
                    pullRequestLinks[newRequest.link()] = True
                else:
                    # if a pull request could not be generated, just add a link to browse the branch
                    pullRequestLinks["%s%s/browse?at=%s" % (bitbucket.rzbitbucketURL,
                                                            repo.repo.url(),
                                                            urllib.quote_plus("refs/heads/%s" % branch))] = False
        
        ## NESTED SUBPROJECT REPOS 
        nestedProjects = grapeConfig.GrapeConfigParser.getAllModifiedNestedSubprojects(target_branch)
        nestedProjectPrefixes = grapeConfig.GrapeConfigParser.getAllModifiedNestedSubprojectPrefixes(target_branch)
        
        for proj, prefix in zip(nestedProjects, nestedProjectPrefixes):
            with utility.cd(prefix):
                git.push("origin %s" % branch)
            repo = bitbucket.repoFromWorkspaceRepoPath(proj, isSubmodule=False, isNested=True)
            
            newRequest = postPullRequest(repo, title, branch, target_branch,descr, reviewers, args)
            if newRequest:
                pullRequestLinks[newRequest.link()] = True
            else:
                # if a pull request could not be generated, just add a link to browse the branch
                pullRequestLinks["%s%s/browse?at=%s" % (bitbucket.rzbitbucketURL,
                                                        repo.repo.url(),
                                                        urllib.quote_plus("refs/heads/%s" % branch))] = False
            

        ## OUTER LEVEL REPO
        # load the repo level REST resource
        if not args["--subprojectsOnly"]:
            if not git.hasBranch(branch):
                utility.printMsg("Top level repository does not have a branch %s, not generating a Pull Request" % (branch))
                return True
            if git.branchUpToDateWith(target_branch, branch):
                utility.printMsg("%s up to date with %s, not generating a Pull Request in Top Level repo" % (target_branch, branch))
                return True
            
                
            repo_name = args["--repo"]
            repo = bitbucket.repoFromWorkspaceRepoPath(wsDir, topLevelRepo=repo_name, topLevelProject=project_name)
            utility.printMsg("Posting pull request to %s,%s" % (project_name, repo_name))
            request = postPullRequest(repo, title, branch, target_branch, descr, reviewers, args)
            updatedDescription = request.description()
            for link in pullRequestLinks:
                updatedDescription = addLinkToDescription(updatedDescription, link, pullRequestLinks[link])

            if updatedDescription != request.description(): 
                request = postPullRequest(repo, title, branch, target_branch, 
                                         updatedDescription, 
                                         reviewers, 
                                         args)
                       
            utility.printMsg("Request generated/updated:\n\n%s" % request)
        return True
Exemple #10
0
def push(repo='', branch='master'):
    with utility.cd(repo):
        utility.printMsg("Pushing %s in %s..." % (branch, repo))
        git.push("-u origin %s" % branch, throwOnFail=True)
Exemple #11
0
def push(repo='', branch='master'):
    with utility.cd(repo):
        utility.printMsg("Pushing %s in %s..." % (branch, repo))
        git.push("-u origin %s" % branch, throwOnFail=True)
Exemple #12
0
def cleanupPush(repo='', branch='', args='none'):
    with utility.cd(repo):
        utility.printMsg("Attempting push of local %s in %s" % (branch, repo))
        git.push("origin %s" % branch)
Exemple #13
0
def cleanupPush(repo='', branch='', args='none'):
    with utility.cd(repo):
        utility.printMsg("Attempting push of local %s in %s" % (branch, repo))
        git.push("origin %s" % branch)                           
Exemple #14
0
    def execute(self, args):
        """
        A fair chunk of this stuff relies on stashy's wrapping of the STASH REST API, which is posted at
        https://developer.atlassian.com/static/rest/stash/2.12.1/stash-rest.html
        """
        config = grapeConfig.grapeConfig()
        name = args["--user"]
        if not name:
            name = utility.getUserName()

        utility.printMsg("Logging onto %s" % args["--bitbucketURL"])
        if args["--test"]:
            bitbucket = Atlassian.TestAtlassian(name)
        else:
            verify = True if args["--verifySSL"].lower() == "true" else False
            bitbucket = Atlassian.Atlassian(name,
                                            url=args["--bitbucketURL"],
                                            verify=verify)

        # default project (outer level project)
        project_name = args["--project"]

        # default repo (outer level repo)
        repo_name = args["--repo"]

        # determine source branch and target branch
        branch = args["--source"]
        if not branch:
            branch = git.currentBranch()

        # make sure we are in the outer level repo before we push
        wsDir = utility.workspaceDir()
        os.chdir(wsDir)

        #ensure branch is pushed
        utility.printMsg("Pushing %s to bitbucket..." % branch)
        git.push("origin %s" % branch)
        #target branch for outer level repo
        target_branch = args["--target"]
        if not target_branch:
            target_branch = config.getPublicBranchFor(branch)
        # load pull request from Bitbucket if it already exists
        wsRepo = bitbucket.project(project_name).repo(repo_name)
        existingOuterLevelRequest = getReposPullRequest(
            wsRepo, branch, target_branch, args)

        # determine pull request title
        title = args["--title"]
        if existingOuterLevelRequest is not None and not title:
            title = existingOuterLevelRequest.title()

        #determine pull request URL
        outerLevelURL = None
        if existingOuterLevelRequest:
            outerLevelURL = existingOuterLevelRequest.link()

        # determine pull request description
        descr = self.parseDescriptionArgs(args)

        if not descr and existingOuterLevelRequest:
            descr = existingOuterLevelRequest.description()

        # determine pull request reviewers
        reviewers = self.parseReviewerArgs(args)
        if reviewers is None and existingOuterLevelRequest is not None:
            reviewers = [r[0] for r in existingOuterLevelRequest.reviewers()]

        # if we're in append mode, only append what was asked for:
        if args["--append"] or args["--prepend"]:
            title = args["--title"]
            descr = self.parseDescriptionArgs(args)
            reviewers = self.parseReviewerArgs(args)

        ##  Submodule Repos
        missing = utility.getModifiedInactiveSubmodules(target_branch, branch)
        if missing:
            utility.printMsg(
                "The following submodules that you've modified are not currently present in your workspace.\n"
                "You should activate them using grape uv  and then call grape review again. If you haven't modified "
                "these submodules, you may need to do a grape md to proceed.")
            utility.printMsg(','.join(missing))
            return False
        pullRequestLinks = {}
        if not args["--norecurse"] and (args["--recurse"] or config.getboolean(
                "workspace", "manageSubmodules")):

            modifiedSubmodules = git.getModifiedSubmodules(
                target_branch, branch)
            submoduleBranchMappings = config.getMapping(
                "workspace", "submoduleTopicPrefixMappings")

            for submodule in modifiedSubmodules:
                if not submodule:
                    continue
                # push branch
                os.chdir(submodule)
                utility.printMsg("Pushing %s to bitbucket..." % branch)
                git.push("origin %s" % branch)
                os.chdir(wsDir)
                repo = bitbucket.repoFromWorkspaceRepoPath(submodule,
                                                           isSubmodule=True)
                # determine branch prefix
                prefix = branch.split('/')[0]
                sub_target_branch = submoduleBranchMappings[prefix]

                prevSubDescr = getReposPullRequestDescription(
                    repo, branch, sub_target_branch, args)
                #amend the subproject pull request description with the link to the outer pull request
                subDescr = addLinkToDescription(descr, outerLevelURL, True)
                if args["--prepend"] or args["--append"]:
                    subDescr = descr
                newRequest = postPullRequest(repo, title, branch,
                                             sub_target_branch, subDescr,
                                             reviewers, args)
                if newRequest:
                    pullRequestLinks[newRequest.link()] = True
                else:
                    # if a pull request could not be generated, just add a link to browse the branch
                    pullRequestLinks[
                        "%s%s/browse?at=%s" %
                        (bitbucket.rzbitbucketURL, repo.repo.url(),
                         urllib.quote_plus("refs/heads/%s" % branch))] = False

        ## NESTED SUBPROJECT REPOS
        nestedProjects = grapeConfig.GrapeConfigParser.getAllModifiedNestedSubprojects(
            target_branch)
        nestedProjectPrefixes = grapeConfig.GrapeConfigParser.getAllModifiedNestedSubprojectPrefixes(
            target_branch)

        for proj, prefix in zip(nestedProjects, nestedProjectPrefixes):
            with utility.cd(prefix):
                git.push("origin %s" % branch)
            repo = bitbucket.repoFromWorkspaceRepoPath(proj,
                                                       isSubmodule=False,
                                                       isNested=True)

            newRequest = postPullRequest(repo, title, branch, target_branch,
                                         descr, reviewers, args)
            if newRequest:
                pullRequestLinks[newRequest.link()] = True
            else:
                # if a pull request could not be generated, just add a link to browse the branch
                pullRequestLinks["%s%s/browse?at=%s" %
                                 (bitbucket.rzbitbucketURL, repo.repo.url(),
                                  urllib.quote_plus(
                                      "refs/heads/%s" % branch))] = False

        ## OUTER LEVEL REPO
        # load the repo level REST resource
        if not args["--subprojectsOnly"]:
            if not git.hasBranch(branch):
                utility.printMsg(
                    "Top level repository does not have a branch %s, not generating a Pull Request"
                    % (branch))
                return True
            if git.branchUpToDateWith(target_branch, branch):
                utility.printMsg(
                    "%s up to date with %s, not generating a Pull Request in Top Level repo"
                    % (target_branch, branch))
                return True

            repo_name = args["--repo"]
            repo = bitbucket.repoFromWorkspaceRepoPath(
                wsDir, topLevelRepo=repo_name, topLevelProject=project_name)
            utility.printMsg("Posting pull request to %s,%s" %
                             (project_name, repo_name))
            request = postPullRequest(repo, title, branch, target_branch,
                                      descr, reviewers, args)
            updatedDescription = request.description()
            for link in pullRequestLinks:
                updatedDescription = addLinkToDescription(
                    updatedDescription, link, pullRequestLinks[link])

            if updatedDescription != request.description():
                request = postPullRequest(repo, title, branch, target_branch,
                                          updatedDescription, reviewers, args)

            utility.printMsg("Request generated/updated:\n\n%s" % request)
        return True