github.com/SAP/jenkins-library@v1.362.0/cmd/githubCreatePullRequest_test.go (about)

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