@@ -16,6 +16,8 @@ package librarian
1616
1717import (
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