make PR workflow helpers more robust #300

Merged
6543 merged 8 commits from noerw/tea:improve-pr-clean into master 2020-12-17 14:00:17 +00:00
2 changed files with 23 additions and 22 deletions
Showing only changes of commit 5d64af557c - Show all commits

View File

@ -47,33 +47,28 @@ func (r TeaRepo) TeaCheckout(branchName string) error {
return tree.Checkout(&git.CheckoutOptions{Branch: localBranchRefName})
}
// TeaDeleteBranch removes the given branch locally, and if `remoteBranch` is
// not empty deletes it at it's remote repo.
func (r TeaRepo) TeaDeleteBranch(branch *git_config.Branch, remoteBranch string, auth git_transport.AuthMethod) error {
// TeaDeleteLocalBranch removes the given branch locally
func (r TeaRepo) TeaDeleteLocalBranch(branch *git_config.Branch) error {
err := r.DeleteBranch(branch.Name)
// if the branch is not found that's ok, as .git/config may have no entry if
// no remote tracking branch is configured for it (eg push without -u flag)
if err != nil && err.Error() != "branch not found" {
return err
}
err = r.Storer.RemoveReference(git_plumbing.NewBranchReferenceName(branch.Name))
if err != nil {
return err
}
return r.Storer.RemoveReference(git_plumbing.NewBranchReferenceName(branch.Name))
}
if remoteBranch != "" {
// delete remote branch via git protocol:
// an empty source in the refspec means remote deletion to git 🙃
refspec := fmt.Sprintf(":%s", git_plumbing.NewBranchReferenceName(remoteBranch))
err = r.Push(&git.PushOptions{
RemoteName: branch.Remote,
RefSpecs: []git_config.RefSpec{git_config.RefSpec(refspec)},
Prune: true,
Auth: auth,
})
}
return err
// TeaDeleteRemoteBranch removes the given branch on the given remote via git protocol
func (r TeaRepo) TeaDeleteRemoteBranch(remoteName, remoteBranch string, auth git_transport.AuthMethod) error {
// delete remote branch via git protocol:
// an empty source in the refspec means remote deletion to git 🙃
refspec := fmt.Sprintf(":%s", git_plumbing.NewBranchReferenceName(remoteBranch))
return r.Push(&git.PushOptions{
RemoteName: remoteName,
RefSpecs: []git_config.RefSpec{git_config.RefSpec(refspec)},
Prune: true,
Auth: auth,
})
}
// TeaFindBranchBySha returns a branch that is at the the given SHA and syncs to the

View File

@ -81,14 +81,20 @@ call me again with the --ignore-sha flag`, remoteBranch)
}
// remove local & remote branch
fmt.Printf("Deleting local branch %s and remote branch %s\n", branch.Name, remoteBranch)
fmt.Printf("Deleting local branch %s\n", branch.Name)
url, err := r.TeaRemoteURL(branch.Remote)
if err != nil {
return err
}
err = r.TeaDeleteLocalBranch(branch)
if err != nil {
return err
}
fmt.Printf("Deleting remote branch %s\n", remoteBranch)
auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback)
if err != nil {
return err
}
return r.TeaDeleteBranch(branch, remoteBranch, auth)
return r.TeaDeleteRemoteBranch(branch.Remote, remoteBranch, auth)
}