github.com/xgoffin/jenkins-library@v1.154.0/cmd/githubCreatePullRequest_test.go (about) 1 package cmd 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "testing" 8 9 "github.com/google/go-github/v32/github" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 type ghPRMock struct { 14 pullrequest *github.NewPullRequest 15 prError error 16 owner string 17 repo string 18 } 19 20 func (g *ghPRMock) Create(ctx context.Context, owner string, repo string, pull *github.NewPullRequest) (*github.PullRequest, *github.Response, error) { 21 g.pullrequest = pull 22 g.owner = owner 23 g.repo = repo 24 prNumber := 1 25 head := github.PullRequestBranch{Ref: pull.Head} 26 base := github.PullRequestBranch{Ref: pull.Base} 27 pr := github.PullRequest{Number: &prNumber, Title: pull.Title, Head: &head, Base: &base, Body: pull.Body} 28 29 ghRes := github.Response{Response: &http.Response{Status: "200"}} 30 if g.prError != nil { 31 ghRes.Status = "401" 32 } 33 return &pr, &ghRes, g.prError 34 } 35 36 type ghIssueMock struct { 37 issueRequest *github.IssueRequest 38 issueError error 39 owner string 40 repo string 41 number int 42 } 43 44 func (g *ghIssueMock) Edit(ctx context.Context, owner string, repo string, number int, issue *github.IssueRequest) (*github.Issue, *github.Response, error) { 45 g.issueRequest = issue 46 g.owner = owner 47 g.repo = repo 48 g.number = number 49 labels := []*github.Label{} 50 for _, l := range *issue.Labels { 51 labels = append(labels, &github.Label{Name: &l}) 52 } 53 54 assignees := []*github.User{} 55 for _, a := range *issue.Assignees { 56 assignees = append(assignees, &github.User{Login: &a}) 57 } 58 59 updatedIssue := github.Issue{Number: &number, Labels: labels, Assignees: assignees} 60 61 ghRes := github.Response{Response: &http.Response{Status: "200"}} 62 if g.issueError != nil { 63 ghRes.Status = "401" 64 } 65 66 return &updatedIssue, &ghRes, g.issueError 67 } 68 69 func TestRunGithubCreatePullRequest(t *testing.T) { 70 ctx := context.Background() 71 72 myGithubPROptions := githubCreatePullRequestOptions{ 73 Owner: "TEST", 74 Repository: "test", 75 Title: "Test Title", 76 Body: "This is the test body.", 77 Head: "head/test", 78 Base: "base/test", 79 Labels: []string{"Test1", "Test2"}, 80 Assignees: []string{"User1", "User2"}, 81 } 82 83 t.Run("Success", func(t *testing.T) { 84 ghPRService := ghPRMock{} 85 ghIssueService := ghIssueMock{} 86 87 err := runGithubCreatePullRequest(ctx, &myGithubPROptions, &ghPRService, &ghIssueService) 88 assert.NoError(t, err, "Error occurred but none expected.") 89 90 assert.Equal(t, myGithubPROptions.Owner, ghPRService.owner, "Owner not passed correctly") 91 assert.Equal(t, myGithubPROptions.Repository, ghPRService.repo, "Repository not passed correctly") 92 assert.Equal(t, myGithubPROptions.Title, ghPRService.pullrequest.GetTitle(), "Title not passed correctly") 93 assert.Equal(t, myGithubPROptions.Body, ghPRService.pullrequest.GetBody(), "Body not passed correctly") 94 assert.Equal(t, myGithubPROptions.Head, ghPRService.pullrequest.GetHead(), "Head not passed correctly") 95 assert.Equal(t, myGithubPROptions.Base, ghPRService.pullrequest.GetBase(), "Base not passed correctly") 96 97 assert.Equal(t, myGithubPROptions.Owner, ghIssueService.owner, "Owner not passed correctly") 98 assert.Equal(t, myGithubPROptions.Repository, ghIssueService.repo, "Repository not passed correctly") 99 assert.Equal(t, myGithubPROptions.Labels, ghIssueService.issueRequest.GetLabels(), "Labels not passed correctly") 100 assert.Equal(t, myGithubPROptions.Assignees, ghIssueService.issueRequest.GetAssignees(), "Assignees not passed correctly") 101 assert.Equal(t, 1, ghIssueService.number, "PR number not passed correctly") 102 }) 103 104 t.Run("Create error", func(t *testing.T) { 105 ghPRService := ghPRMock{prError: fmt.Errorf("Authentication failed")} 106 ghIssueService := ghIssueMock{} 107 108 err := runGithubCreatePullRequest(ctx, &myGithubPROptions, &ghPRService, &ghIssueService) 109 assert.EqualError(t, err, "Error occurred when creating pull request: Authentication failed", "Wrong error returned") 110 111 }) 112 113 t.Run("Edit error", func(t *testing.T) { 114 ghPRService := ghPRMock{} 115 ghIssueService := ghIssueMock{issueError: fmt.Errorf("Authentication failed")} 116 117 err := runGithubCreatePullRequest(ctx, &myGithubPROptions, &ghPRService, &ghIssueService) 118 assert.EqualError(t, err, "Error occurred when editing pull request: Authentication failed", "Wrong error returned") 119 }) 120 }