Skip to content

Commit 563e9f3

Browse files
authored
test: add system tests for creating comment and finding PR by label (#2292)
Towards #300 --------- Signed-off-by: Jeff Ching <[email protected]>
1 parent 7d0520b commit 563e9f3

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

internal/github/github.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ func hasLabel(pr *PullRequest, labelName string) bool {
306306

307307
// FindMergedPullRequestsWithPendingReleaseLabel finds all merged pull requests with the "release:pending" label.
308308
func (c *Client) FindMergedPullRequestsWithPendingReleaseLabel(ctx context.Context, owner, repo string) ([]*PullRequest, error) {
309+
return c.FindMergedPullRequestsWithLabel(ctx, owner, repo, "release:pending")
310+
}
311+
312+
// FindMergedPullRequestsWithLabel finds all merged pull requests with the "release:pending" label.
313+
func (c *Client) FindMergedPullRequestsWithLabel(ctx context.Context, owner, repo, label string) ([]*PullRequest, error) {
309314
var allPRs []*PullRequest
310315
opt := &github.PullRequestListOptions{
311316
State: "closed",
@@ -319,7 +324,7 @@ func (c *Client) FindMergedPullRequestsWithPendingReleaseLabel(ctx context.Conte
319324
return nil, err
320325
}
321326
for _, pr := range prs {
322-
if !pr.GetMergedAt().IsZero() && hasLabel(pr, "release:pending") {
327+
if !pr.GetMergedAt().IsZero() && hasLabel(pr, label) {
323328
allPRs = append(allPRs, pr)
324329
}
325330
}

system_test.go

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ func TestPullRequestSystem(t *testing.T) {
102102
// Clone a repo
103103
// Create a commit and push
104104
// Create a pull request
105-
// Add a label to the issue
105+
// Add a label to the pull request
106106
// Fetch labels for the issue and verify
107107
// Replace the issue labels
108+
// Fetch labels for the issue and verify
109+
// Add a comment
108110
// Search for the pull request
109111
// Fetch the pull request
110112
// Close the pull request
@@ -232,6 +234,12 @@ func TestPullRequestSystem(t *testing.T) {
232234
t.Fatalf("GetLabels() mismatch (-want + got):\n%s", diff)
233235
}
234236

237+
// Add a comment
238+
err = client.CreateIssueComment(t.Context(), createdPullRequest.Number, "some comment body")
239+
if err != nil {
240+
t.Fatalf("unexpected error in CreateIssueComment() %s", err)
241+
}
242+
235243
// Search for pull requests (this may take a bit of time, so try 5 times)
236244
found := false
237245
for i := 0; i < 5; i++ {
@@ -241,11 +249,22 @@ func TestPullRequestSystem(t *testing.T) {
241249
}
242250
for _, pullRequest := range foundPullRequests {
243251
// Look for the PR we created
244-
if *pullRequest.Number == createdPullRequest.Number {
252+
if pullRequest.Number != nil && *pullRequest.Number == createdPullRequest.Number {
245253
found = true
254+
255+
// Expect that we found a comment
256+
if pullRequest.Comments == nil {
257+
t.Fatal("pull request comments not set")
258+
}
259+
if *pullRequest.Comments == 0 {
260+
t.Fatalf("Expected to have created a comment on the pull request.")
261+
}
246262
break
247263
}
248264
}
265+
if found {
266+
break
267+
}
249268
delay := time.Duration(2 * time.Second)
250269
t.Logf("Retrying in %v...\n", delay)
251270
time.Sleep(delay)
@@ -265,14 +284,85 @@ func TestPullRequestSystem(t *testing.T) {
265284
if err != nil {
266285
t.Fatalf("unexpected error in GetPullRequest() %s", err)
267286
}
287+
if foundPullRequest.Number == nil {
288+
t.Fatal("pull request number not set")
289+
}
268290
if diff := cmp.Diff(*foundPullRequest.Number, createdPullRequest.Number); diff != "" {
269291
t.Fatalf("pull request number mismatch (-want + got):\n%s", diff)
270292
}
293+
if foundPullRequest.State == nil {
294+
t.Fatal("pull request state not set")
295+
}
271296
if diff := cmp.Diff(*foundPullRequest.State, "closed"); diff != "" {
272297
t.Fatalf("pull request state mismatch (-want + got):\n%s", diff)
273298
}
274299
}
275300

301+
func TestFindMergedPullRequest(t *testing.T) {
302+
if testToken == "" {
303+
t.Skip("TEST_GITHUB_TOKEN not set, skipping GitHub integration test")
304+
}
305+
repoName := "https://github.com/googleapis/librarian"
306+
repo, err := github.ParseRemote(repoName)
307+
if err != nil {
308+
t.Fatalf("unexpected error in ParseRemote() %s", err)
309+
}
310+
311+
for _, test := range []struct {
312+
name string
313+
label string
314+
want int
315+
wantErr bool
316+
wantErrSubstr string
317+
}{
318+
{
319+
name: "existing label",
320+
label: "cla: yes",
321+
want: 2210,
322+
wantErr: false,
323+
},
324+
{
325+
name: "non-existing label",
326+
label: "non-existing-label",
327+
want: 0,
328+
wantErr: false,
329+
},
330+
} {
331+
t.Run(test.name, func(t *testing.T) {
332+
client := github.NewClient(testToken, repo)
333+
prs, err := client.FindMergedPullRequestsWithLabel(t.Context(), repo.Owner, repo.Name, test.label)
334+
if test.wantErr {
335+
if err == nil {
336+
t.Fatalf("FindMergedPullRequestsWithLabel() err = nil, want error containing %q", test.wantErrSubstr)
337+
} else if !strings.Contains(err.Error(), test.wantErrSubstr) {
338+
t.Errorf("FindMergedPullRequestsWithLabel() err = %v, want error containing %q", err, test.wantErrSubstr)
339+
}
340+
return
341+
}
342+
if err != nil {
343+
t.Errorf("FindMergedPullRequestsWithLabel() err = %v, want nil", err)
344+
}
345+
if test.want == 0 {
346+
// expect to not find any
347+
if len(prs) > 0 {
348+
t.Fatalf("FindMergedPullRequestWithLabel() expected to not find any PRs, found %d", len(prs))
349+
}
350+
} else {
351+
found := false
352+
for _, pr := range prs {
353+
t.Logf("Found PR %d", *pr.Number)
354+
if pr.Number != nil && *pr.Number == test.want {
355+
found = true
356+
}
357+
}
358+
if !found {
359+
t.Fatalf("FindMergedPullRequestsWithLabel() expected to find PR #%d", test.want)
360+
}
361+
}
362+
})
363+
}
364+
}
365+
276366
func TestCreateTag(t *testing.T) {
277367
if testToken == "" {
278368
t.Skip("TEST_GITHUB_TOKEN not set, skipping GitHub integration test")

0 commit comments

Comments
 (0)