|
3 | 3 | package e2e_test
|
4 | 4 |
|
5 | 5 | import (
|
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "github.com/google/go-github/github" |
| 9 | + "github.com/stretchr/testify/assert" |
6 | 10 | "io/ioutil"
|
7 | 11 | "os"
|
8 | 12 | "os/exec"
|
9 | 13 | "path/filepath"
|
| 14 | + "strconv" |
10 | 15 | "strings"
|
11 | 16 | "testing"
|
12 | 17 | "time"
|
13 | 18 |
|
14 |
| - "github.com/stretchr/testify/assert" |
15 | 19 | "github.com/stretchr/testify/require"
|
16 | 20 | resource "github.com/telia-oss/github-pr-resource"
|
17 | 21 | )
|
@@ -172,11 +176,11 @@ func TestCheckE2E(t *testing.T) {
|
172 | 176 |
|
173 | 177 | for _, tc := range tests {
|
174 | 178 | t.Run(tc.description, func(t *testing.T) {
|
175 |
| - github, err := resource.NewGithubClient(&tc.source) |
| 179 | + githubClient, err := resource.NewGithubClient(&tc.source) |
176 | 180 | require.NoError(t, err)
|
177 | 181 |
|
178 | 182 | input := resource.CheckRequest{Source: tc.source, Version: tc.version}
|
179 |
| - output, err := resource.Check(input, github) |
| 183 | + output, err := resource.Check(input, githubClient) |
180 | 184 |
|
181 | 185 | if assert.NoError(t, err) {
|
182 | 186 | assert.Equal(t, tc.expected, output)
|
@@ -380,15 +384,15 @@ func TestGetAndPutE2E(t *testing.T) {
|
380 | 384 | require.NoError(t, err)
|
381 | 385 | defer os.RemoveAll(dir)
|
382 | 386 |
|
383 |
| - github, err := resource.NewGithubClient(&tc.source) |
| 387 | + githubClient, err := resource.NewGithubClient(&tc.source) |
384 | 388 | require.NoError(t, err)
|
385 | 389 |
|
386 | 390 | git, err := resource.NewGitClient(&tc.source, dir, ioutil.Discard)
|
387 | 391 | require.NoError(t, err)
|
388 | 392 |
|
389 | 393 | // Get (output and files)
|
390 | 394 | getRequest := resource.GetRequest{Source: tc.source, Version: tc.version, Params: tc.getParameters}
|
391 |
| - getOutput, err := resource.Get(getRequest, github, git, dir) |
| 395 | + getOutput, err := resource.Get(getRequest, githubClient, git, dir) |
392 | 396 |
|
393 | 397 | require.NoError(t, err)
|
394 | 398 | assert.Equal(t, tc.version, getOutput.Version)
|
@@ -423,14 +427,139 @@ func TestGetAndPutE2E(t *testing.T) {
|
423 | 427 |
|
424 | 428 | // Put
|
425 | 429 | putRequest := resource.PutRequest{Source: tc.source, Params: tc.putParameters}
|
426 |
| - putOutput, err := resource.Put(putRequest, github, dir) |
| 430 | + putOutput, err := resource.Put(putRequest, githubClient, dir) |
427 | 431 |
|
428 | 432 | require.NoError(t, err)
|
429 | 433 | assert.Equal(t, tc.version, putOutput.Version)
|
430 | 434 | })
|
431 | 435 | }
|
432 | 436 | }
|
433 | 437 |
|
| 438 | +func TestPutCommentsE2E(t *testing.T) { |
| 439 | + owner := "rcoy-v" |
| 440 | + repo := "github-pr-resource-e2e" |
| 441 | + |
| 442 | + tests := []struct { |
| 443 | + description, branch string |
| 444 | + source resource.Source |
| 445 | + getParams resource.GetParameters |
| 446 | + putParameters resource.PutParameters |
| 447 | + previousComments, expectedComments []string |
| 448 | + }{ |
| 449 | + { |
| 450 | + description: "delete previous comments removes old comments and makes new one", |
| 451 | + branch: "delete-previous-comments-remove-old-add-new", |
| 452 | + source: resource.Source{ |
| 453 | + Repository: fmt.Sprintf("%s/%s", owner, repo), |
| 454 | + V3Endpoint: "https://api.github.com/", |
| 455 | + V4Endpoint: "https://api.github.com/graphql", |
| 456 | + AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"), |
| 457 | + }, |
| 458 | + getParams: resource.GetParameters{}, |
| 459 | + putParameters: resource.PutParameters{ |
| 460 | + Comment: "new comment", |
| 461 | + DeletePreviousComments: true, |
| 462 | + }, |
| 463 | + previousComments: []string{"old comment"}, |
| 464 | + expectedComments: []string{ |
| 465 | + "new comment", |
| 466 | + }, |
| 467 | + }, |
| 468 | + { |
| 469 | + description: "delete previous comments removes all comments when no new comment", |
| 470 | + branch: "delete-previous-comments-remove-old", |
| 471 | + source: resource.Source{ |
| 472 | + Repository: fmt.Sprintf("%s/%s", owner, repo), |
| 473 | + V3Endpoint: "https://api.github.com/", |
| 474 | + V4Endpoint: "https://api.github.com/graphql", |
| 475 | + AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"), |
| 476 | + }, |
| 477 | + getParams: resource.GetParameters{}, |
| 478 | + putParameters: resource.PutParameters{ |
| 479 | + DeletePreviousComments: true, |
| 480 | + }, |
| 481 | + previousComments: []string{"old comment"}, |
| 482 | + expectedComments: []string{}, |
| 483 | + }, |
| 484 | + { |
| 485 | + description: "delete previous comments should not delete comments when false", |
| 486 | + branch: "delete-previous-comments-false", |
| 487 | + source: resource.Source{ |
| 488 | + Repository: fmt.Sprintf("%s/%s", owner, repo), |
| 489 | + V3Endpoint: "https://api.github.com/", |
| 490 | + V4Endpoint: "https://api.github.com/graphql", |
| 491 | + AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"), |
| 492 | + }, |
| 493 | + getParams: resource.GetParameters{}, |
| 494 | + putParameters: resource.PutParameters{ |
| 495 | + Comment: "new comment", |
| 496 | + DeletePreviousComments: false, |
| 497 | + }, |
| 498 | + previousComments: []string{"should not delete"}, |
| 499 | + expectedComments: []string{ |
| 500 | + "should not delete", |
| 501 | + "new comment", |
| 502 | + }, |
| 503 | + }, |
| 504 | + } |
| 505 | + |
| 506 | + for _, tc := range tests { |
| 507 | + t.Run(tc.description, func(t *testing.T) { |
| 508 | + dir, err := ioutil.TempDir("", "github-pr-resource") |
| 509 | + require.NoError(t, err) |
| 510 | + defer os.RemoveAll(dir) |
| 511 | + |
| 512 | + githubClient, err := resource.NewGithubClient(&tc.source) |
| 513 | + require.NoError(t, err) |
| 514 | + |
| 515 | + git, err := resource.NewGitClient(&tc.source, dir, ioutil.Discard) |
| 516 | + require.NoError(t, err) |
| 517 | + |
| 518 | + pullRequest, _, err := githubClient.V3.PullRequests.Create(context.TODO(), owner, repo, &github.NewPullRequest{ |
| 519 | + Title: github.String(tc.description), |
| 520 | + Base: github.String("master"), |
| 521 | + Head: github.String(fmt.Sprintf("%s:%s", owner, tc.branch)), |
| 522 | + }) |
| 523 | + require.NoError(t, err) |
| 524 | + |
| 525 | + for _, comment := range tc.previousComments { |
| 526 | + _, _, err = githubClient.V3.Issues.CreateComment(context.TODO(), owner, repo, pullRequest.GetNumber(), &github.IssueComment{ |
| 527 | + Body: github.String(comment), |
| 528 | + }) |
| 529 | + require.NoError(t, err) |
| 530 | + } |
| 531 | + |
| 532 | + getRequest := resource.GetRequest{Source: tc.source, Version: resource.Version{ |
| 533 | + PR: strconv.Itoa(pullRequest.GetNumber()), |
| 534 | + Commit: pullRequest.GetHead().GetSHA(), |
| 535 | + }, Params: tc.getParams} |
| 536 | + _, err = resource.Get(getRequest, githubClient, git, dir) |
| 537 | + require.NoError(t, err) |
| 538 | + |
| 539 | + putRequest := resource.PutRequest{ |
| 540 | + Source: tc.source, |
| 541 | + Params: tc.putParameters, |
| 542 | + } |
| 543 | + |
| 544 | + _, err = resource.Put(putRequest, githubClient, dir) |
| 545 | + require.NoError(t, err) |
| 546 | + |
| 547 | + comments, _, err := githubClient.V3.Issues.ListComments(context.TODO(), owner, repo, pullRequest.GetNumber(), nil) |
| 548 | + require.NoError(t, err) |
| 549 | + |
| 550 | + require.Len(t, comments, len(tc.expectedComments)) |
| 551 | + for index, comment := range comments { |
| 552 | + require.Equal(t, tc.expectedComments[index], comment.GetBody()) |
| 553 | + } |
| 554 | + |
| 555 | + _, _, err = githubClient.V3.PullRequests.Edit(context.TODO(), owner, repo, pullRequest.GetNumber(), &github.PullRequest{ |
| 556 | + State: github.String("closed"), |
| 557 | + }) |
| 558 | + require.NoError(t, err) |
| 559 | + }) |
| 560 | + } |
| 561 | +} |
| 562 | + |
434 | 563 | func gitHistory(t *testing.T, directory string) map[int]string {
|
435 | 564 | cmd := exec.Command("git", "log", "--oneline", "--pretty=format:%s")
|
436 | 565 | cmd.Dir = directory
|
|
0 commit comments