From 4f2f8c9d06f176be0d01a6050eb8fb0b38659af2 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 13 Jan 2024 20:01:42 -0600 Subject: [PATCH 1/2] Delete remote branch after closing pull request. --- git/helpers.go | 6 ++++++ spr/spr.go | 1 + 2 files changed, 7 insertions(+) diff --git a/git/helpers.go b/git/helpers.go index a52c638..6b9f631 100644 --- a/git/helpers.go +++ b/git/helpers.go @@ -42,6 +42,12 @@ func GetLocalTopCommit(cfg *config.Config, gitcmd GitInterface) *Commit { return &commits[len(commits)-1] } +func DeleteRemoteBranch(cfg *config.Config, gitcmd GitInterface, branchName string) { + command := fmt.Sprintf("push origin --delete %s", branchName) + err := gitcmd.Git(command, nil) + check(err) +} + // GetLocalCommitStack returns a list of unmerged commits // // the list is ordered with the bottom commit in the stack first diff --git a/spr/spr.go b/spr/spr.go index 7058eeb..ff9373b 100644 --- a/spr/spr.go +++ b/spr/spr.go @@ -328,6 +328,7 @@ func (sd *stackediff) MergePullRequests(ctx context.Context, count *uint) { prToMerge.Number, sd.config.Repo.GitHubHost, sd.config.Repo.GitHubRepoOwner, sd.config.Repo.GitHubRepoName, prToMerge.Number) sd.github.CommentPullRequest(ctx, pr, comment) sd.github.ClosePullRequest(ctx, pr) + git.DeleteRemoteBranch(sd.config, sd.gitcmd, pr.FromBranch) } sd.profiletimer.Step("MergePullRequests::close prs") From 6de52186084e3f0f63ca83be9ac8b2d3388cd346 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Sat, 13 Jan 2024 20:18:43 -0600 Subject: [PATCH 2/2] Tried to add tests --- git/mockgit/mockgit.go | 4 ++++ spr/spr_test.go | 3 +++ 2 files changed, 7 insertions(+) diff --git a/git/mockgit/mockgit.go b/git/mockgit/mockgit.go index 86ba64b..4c20149 100644 --- a/git/mockgit/mockgit.go +++ b/git/mockgit/mockgit.go @@ -69,6 +69,10 @@ func (m *Mock) ExpectFetch() { m.expect("git rebase origin/master --autostash") } +func (m *Mock) ExpectDeleteBranch(branchName string) { + m.expect(fmt.Sprintf("git push origin --delete %s", branchName)) +} + func (m *Mock) ExpectLogAndRespond(commits []*git.Commit) { m.expect("git log --format=medium --no-color origin/master..HEAD").commitRespond(commits) } diff --git a/spr/spr_test.go b/spr/spr_test.go index 727219f..7540358 100644 --- a/spr/spr_test.go +++ b/spr/spr_test.go @@ -306,10 +306,13 @@ func TestSPRBasicFlowFourCommits(t *testing.T) { githubmock.ExpectMergePullRequest(c4, genclient.PullRequestMergeMethod_REBASE) githubmock.ExpectCommentPullRequest(c1) githubmock.ExpectClosePullRequest(c1) + gitmock.ExpectDeleteBranch('???') // Not sure where to find the branch name? githubmock.ExpectCommentPullRequest(c2) githubmock.ExpectClosePullRequest(c2) + gitmock.ExpectDeleteBranch('???') githubmock.ExpectCommentPullRequest(c3) githubmock.ExpectClosePullRequest(c3) + gitmock.ExpectDeleteBranch('???') s.MergePullRequests(ctx, nil) lines = strings.Split(output.String(), "\n") assert.Equal("MERGED 1 : test commit 1", lines[0])