Skip to content

Commit 234f2a2

Browse files
authored
feat: add DeleteBranch() and ClosePullRequest() (#2232)
test: update system test to clean up branch and close the pull request. Followup to #2210 that cleans up the branch we created and closes the pull request. Towards #300 Signed-off-by: Jeff Ching <[email protected]>
1 parent e5a86a1 commit 234f2a2

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

internal/github/github.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,13 @@ func (c *Client) CreateTag(ctx context.Context, tagName, commitSHA string) error
332332
_, _, err := c.Git.CreateRef(ctx, c.repo.Owner, c.repo.Name, tagRef)
333333
return err
334334
}
335+
336+
// ClosePullRequest closes the pull request specified by pull request number.
337+
func (c *Client) ClosePullRequest(ctx context.Context, number int) error {
338+
slog.Info("Closing pull request", slog.Int("number", number))
339+
state := "closed"
340+
_, _, err := c.PullRequests.Edit(ctx, c.repo.Owner, c.repo.Name, number, &github.PullRequest{
341+
State: &state,
342+
})
343+
return err
344+
}

internal/gitrepo/gitrepo.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Repository interface {
4646
CreateBranchAndCheckout(name string) error
4747
Push(branchName string) error
4848
Restore(paths []string) error
49+
pushRefSpec(refSpec string) error
4950
}
5051

5152
// LocalRepository represents a git repository.
@@ -429,8 +430,19 @@ func (r *LocalRepository) CreateBranchAndCheckout(name string) error {
429430
// Push pushes the local branch to the origin remote.
430431
func (r *LocalRepository) Push(branchName string) error {
431432
// https://stackoverflow.com/a/75727620
432-
refSpec := config.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/heads/%s", branchName, branchName))
433+
refSpec := fmt.Sprintf("+refs/heads/%s:refs/heads/%s", branchName, branchName)
433434
slog.Info("Pushing changes", "branch name", branchName, slog.Any("refspec", refSpec))
435+
return r.pushRefSpec(refSpec)
436+
}
437+
438+
// DeleteBranch deletes a branch on the origin remote.
439+
func (r *LocalRepository) DeleteBranch(branchName string) error {
440+
refSpec := fmt.Sprintf(":refs/heads/%s", branchName)
441+
return r.pushRefSpec(refSpec)
442+
}
443+
444+
func (r *LocalRepository) pushRefSpec(refSpec string) error {
445+
slog.Info("Pushing changes", "refSpec", refSpec)
434446
var auth *httpAuth.BasicAuth
435447
if r.gitPassword != "" {
436448
slog.Info("Authenticating with basic auth")
@@ -443,12 +455,12 @@ func (r *LocalRepository) Push(branchName string) error {
443455
}
444456
if err := r.repo.Push(&git.PushOptions{
445457
RemoteName: "origin",
446-
RefSpecs: []config.RefSpec{refSpec},
458+
RefSpecs: []config.RefSpec{config.RefSpec(refSpec)},
447459
Auth: auth,
448460
}); err != nil {
449461
return err
450462
}
451-
slog.Info("Successfully pushed branch to remote 'origin", "branch", branchName)
463+
slog.Info("Successfully pushed changes", "refSpec", refSpec)
452464
return nil
453465
}
454466

system_test.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package librarian
1616

1717
import (
1818
"context"
19+
"fmt"
20+
"log/slog"
1921
"os"
2022
"path"
2123
"strings"
@@ -105,6 +107,7 @@ func TestPullRequestSystem(t *testing.T) {
105107
// Replace the issue labels
106108
// Search for the pull request
107109
// Fetch the pull request
110+
// Close the pull request
108111
if testToken == "" {
109112
t.Skip("TEST_GITHUB_TOKEN not set, skipping GitHub integration test")
110113
}
@@ -131,7 +134,8 @@ func TestPullRequestSystem(t *testing.T) {
131134
t.Fatalf("unexpected error in ParseRemote() %s", err)
132135
}
133136

134-
branchName := "integration-test"
137+
now := time.Now()
138+
branchName := fmt.Sprintf("integration-test-%s", now.Format("20060102150405"))
135139
err = localRepository.CreateBranchAndCheckout(branchName)
136140
if err != nil {
137141
t.Fatalf("unexpected error in CreateBranchAndCheckout() %s", err)
@@ -155,23 +159,40 @@ func TestPullRequestSystem(t *testing.T) {
155159
t.Fatalf("unexpected error in Push() %s", err)
156160
}
157161

162+
cleanupBranch := func() {
163+
slog.Info("cleaning up created branch", "branch", branchName)
164+
err := localRepository.DeleteBranch(branchName)
165+
if err != nil {
166+
t.Fatalf("unexpected error in DeleteBranch() %s", err)
167+
}
168+
}
169+
defer cleanupBranch()
170+
158171
// Create a pull request
159172
client := github.NewClient(testToken, repo)
160-
metadata, err := client.CreatePullRequest(t.Context(), repo, branchName, "main", "test: integration test", "do not merge")
173+
createdPullRequest, err := client.CreatePullRequest(t.Context(), repo, branchName, "main", "test: integration test", "do not merge")
161174
if err != nil {
162175
t.Fatalf("unexpected error in CreatePullRequest() %s", err)
163176
}
164-
t.Logf("created pull request: %d", metadata.Number)
177+
t.Logf("created pull request: %d", createdPullRequest.Number)
178+
179+
// Ensure we clean up the created PR. The pull request is closed later in the
180+
// test, but this should make sure unless ClosePullRequest() is not working.
181+
cleanupPR := func() {
182+
slog.Info("cleaning up opened pull request")
183+
client.ClosePullRequest(t.Context(), createdPullRequest.Number)
184+
}
185+
defer cleanupPR()
165186

166187
// Add a label to the pull request
167188
labels := []string{"do not merge", "type: cleanup"}
168-
err = client.AddLabelsToIssue(t.Context(), repo, metadata.Number, labels)
189+
err = client.AddLabelsToIssue(t.Context(), repo, createdPullRequest.Number, labels)
169190
if err != nil {
170191
t.Fatalf("unexpected error in AddLabelsToIssue() %s", err)
171192
}
172193

173194
// Get labels and verify
174-
foundLabels, err := client.GetLabels(t.Context(), metadata.Number)
195+
foundLabels, err := client.GetLabels(t.Context(), createdPullRequest.Number)
175196
if err != nil {
176197
t.Fatalf("unexpected error in GetLabels() %s", err)
177198
}
@@ -181,13 +202,13 @@ func TestPullRequestSystem(t *testing.T) {
181202

182203
// Replace labels
183204
labels = []string{"foo", "bar"}
184-
err = client.ReplaceLabels(t.Context(), metadata.Number, labels)
205+
err = client.ReplaceLabels(t.Context(), createdPullRequest.Number, labels)
185206
if err != nil {
186207
t.Fatalf("unexpected error in ReplaceLabels() %s", err)
187208
}
188209

189210
// Get labels and verify
190-
foundLabels, err = client.GetLabels(t.Context(), metadata.Number)
211+
foundLabels, err = client.GetLabels(t.Context(), createdPullRequest.Number)
191212
if err != nil {
192213
t.Fatalf("unexpected error in GetLabels() %s", err)
193214
}
@@ -196,14 +217,14 @@ func TestPullRequestSystem(t *testing.T) {
196217
}
197218

198219
// Add label
199-
err = client.AddLabelsToIssue(t.Context(), repo, metadata.Number, []string{"librarian-test", "asdf"})
220+
err = client.AddLabelsToIssue(t.Context(), repo, createdPullRequest.Number, []string{"librarian-test", "asdf"})
200221
if err != nil {
201222
t.Fatalf("unexpected error in AddLabelsToIssue() %s", err)
202223
}
203224

204225
// Get labels and verify
205226
wantLabels := []string{"foo", "bar", "librarian-test", "asdf"}
206-
foundLabels, err = client.GetLabels(t.Context(), metadata.Number)
227+
foundLabels, err = client.GetLabels(t.Context(), createdPullRequest.Number)
207228
if err != nil {
208229
t.Fatalf("unexpected error in GetLabels() %s", err)
209230
}
@@ -220,7 +241,7 @@ func TestPullRequestSystem(t *testing.T) {
220241
}
221242
for _, pullRequest := range foundPullRequests {
222243
// Look for the PR we created
223-
if *pullRequest.Number == metadata.Number {
244+
if *pullRequest.Number == createdPullRequest.Number {
224245
found = true
225246
break
226247
}
@@ -233,12 +254,21 @@ func TestPullRequestSystem(t *testing.T) {
233254
t.Fatalf("failed to find pull request after 5 attempts")
234255
}
235256

257+
// Close pull request
258+
err = client.ClosePullRequest(t.Context(), createdPullRequest.Number)
259+
if err != nil {
260+
t.Fatalf("unexpected error in ClosePullRequest() %s", err)
261+
}
262+
236263
// Get single pull request
237-
foundPullRequest, err := client.GetPullRequest(t.Context(), metadata.Number)
264+
foundPullRequest, err := client.GetPullRequest(t.Context(), createdPullRequest.Number)
238265
if err != nil {
239266
t.Fatalf("unexpected error in GetPullRequest() %s", err)
240267
}
241-
if diff := cmp.Diff(*foundPullRequest.Number, metadata.Number); diff != "" {
268+
if diff := cmp.Diff(*foundPullRequest.Number, createdPullRequest.Number); diff != "" {
242269
t.Fatalf("pull request number mismatch (-want + got):\n%s", diff)
243270
}
271+
if diff := cmp.Diff(*foundPullRequest.State, "closed"); diff != "" {
272+
t.Fatalf("pull request state mismatch (-want + got):\n%s", diff)
273+
}
244274
}

0 commit comments

Comments
 (0)