github.com/jaylevin/jenkins-library@v1.230.4/pkg/github/github_test.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"regexp"
     8  	"testing"
     9  
    10  	"github.com/google/go-github/v32/github"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type ghCreateIssueMock struct {
    15  	issue      *github.IssueRequest
    16  	issueID    int64
    17  	issueError error
    18  	owner      string
    19  	repo       string
    20  	number     int
    21  	assignees  []string
    22  }
    23  
    24  func (g *ghCreateIssueMock) Create(ctx context.Context, owner string, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error) {
    25  	g.issue = issue
    26  	g.owner = owner
    27  	g.repo = repo
    28  	g.assignees = *issue.Assignees
    29  
    30  	issueResponse := github.Issue{ID: &g.issueID, Title: issue.Title, Body: issue.Body}
    31  
    32  	ghRes := github.Response{Response: &http.Response{Status: "200"}}
    33  	if g.issueError != nil {
    34  		ghRes.Status = "401"
    35  	}
    36  
    37  	return &issueResponse, &ghRes, g.issueError
    38  }
    39  
    40  type ghSearchIssuesMock struct {
    41  	issueID            int64
    42  	issueNumber        int
    43  	issueTitle         string
    44  	issueBody          string
    45  	issuesSearchResult *github.IssuesSearchResult
    46  	issuesSearchError  error
    47  }
    48  
    49  func (g *ghSearchIssuesMock) Issues(ctx context.Context, query string, opts *github.SearchOptions) (*github.IssuesSearchResult, *github.Response, error) {
    50  
    51  	regex := regexp.MustCompile(`.*in:title (?P<Title>(.*))`)
    52  	matches := regex.FindStringSubmatch(query)
    53  
    54  	g.issueTitle = matches[1]
    55  
    56  	issues := []*github.Issue{
    57  		{
    58  			ID:     &g.issueID,
    59  			Number: &g.issueNumber,
    60  			Title:  &g.issueTitle,
    61  			Body:   &g.issueBody,
    62  		},
    63  	}
    64  
    65  	total := len(issues)
    66  	incompleteResults := false
    67  
    68  	g.issuesSearchResult = &github.IssuesSearchResult{
    69  		Issues:            issues,
    70  		Total:             &total,
    71  		IncompleteResults: &incompleteResults,
    72  	}
    73  
    74  	ghRes := github.Response{Response: &http.Response{Status: "200"}}
    75  	if g.issuesSearchError != nil {
    76  		ghRes.Status = "401"
    77  	}
    78  
    79  	return g.issuesSearchResult, &ghRes, g.issuesSearchError
    80  }
    81  
    82  type ghCreateCommentMock struct {
    83  	issueComment      *github.IssueComment
    84  	issueCommentError error
    85  }
    86  
    87  func (g *ghCreateCommentMock) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
    88  	g.issueComment = comment
    89  	ghRes := github.Response{Response: &http.Response{Status: "200"}}
    90  	if g.issueCommentError != nil {
    91  		ghRes.Status = "401"
    92  	}
    93  	return g.issueComment, &ghRes, g.issueCommentError
    94  }
    95  
    96  func TestRunGithubCreateIssue(t *testing.T) {
    97  	ctx := context.Background()
    98  	t.Parallel()
    99  
   100  	t.Run("Success", func(t *testing.T) {
   101  		// init
   102  		ghCreateIssueService := ghCreateIssueMock{
   103  			issueID: 1,
   104  		}
   105  		ghSearchIssuesMock := ghSearchIssuesMock{
   106  			issueID: 1,
   107  		}
   108  		ghCreateCommentMock := ghCreateCommentMock{}
   109  		config := CreateIssueOptions{
   110  			Owner:      "TEST",
   111  			Repository: "test",
   112  			Body:       []byte("This is my test body"),
   113  			Title:      "This is my title",
   114  			Assignees:  []string{"userIdOne", "userIdTwo"},
   115  		}
   116  
   117  		// test
   118  		err := createIssueLocal(ctx, &config, &ghCreateIssueService, &ghSearchIssuesMock, &ghCreateCommentMock)
   119  
   120  		// assert
   121  		assert.NoError(t, err)
   122  		assert.Equal(t, config.Owner, ghCreateIssueService.owner)
   123  		assert.Equal(t, config.Repository, ghCreateIssueService.repo)
   124  		assert.Equal(t, "This is my test body", ghCreateIssueService.issue.GetBody())
   125  		assert.Equal(t, config.Title, ghCreateIssueService.issue.GetTitle())
   126  		assert.Equal(t, config.Assignees, ghCreateIssueService.issue.GetAssignees())
   127  		assert.Nil(t, ghSearchIssuesMock.issuesSearchResult)
   128  		assert.Nil(t, ghCreateCommentMock.issueComment)
   129  	})
   130  
   131  	t.Run("Success update existing", func(t *testing.T) {
   132  		// init
   133  		ghSearchIssuesMock := ghSearchIssuesMock{
   134  			issueID: 1,
   135  		}
   136  		ghCreateCommentMock := ghCreateCommentMock{}
   137  		config := CreateIssueOptions{
   138  			Owner:          "TEST",
   139  			Repository:     "test",
   140  			Body:           []byte("This is my test body"),
   141  			Title:          "This is my title",
   142  			Assignees:      []string{"userIdOne", "userIdTwo"},
   143  			UpdateExisting: true,
   144  		}
   145  
   146  		// test
   147  		err := createIssueLocal(ctx, &config, nil, &ghSearchIssuesMock, &ghCreateCommentMock)
   148  
   149  		// assert
   150  		assert.NoError(t, err)
   151  		assert.NotNil(t, ghSearchIssuesMock.issuesSearchResult)
   152  		assert.NotNil(t, ghCreateCommentMock.issueComment)
   153  		assert.Equal(t, config.Title, ghSearchIssuesMock.issueTitle)
   154  		assert.Equal(t, config.Title, *ghSearchIssuesMock.issuesSearchResult.Issues[0].Title)
   155  		assert.Equal(t, "This is my test body", ghCreateCommentMock.issueComment.GetBody())
   156  	})
   157  
   158  	t.Run("Empty body", func(t *testing.T) {
   159  		// init
   160  		ghCreateIssueService := ghCreateIssueMock{
   161  			issueID: 1,
   162  		}
   163  		ghSearchIssuesMock := ghSearchIssuesMock{
   164  			issueID: 1,
   165  		}
   166  		ghCreateCommentMock := ghCreateCommentMock{}
   167  		config := CreateIssueOptions{
   168  			Owner:          "TEST",
   169  			Repository:     "test",
   170  			Body:           []byte(""),
   171  			Title:          "This is my title",
   172  			Assignees:      []string{"userIdOne", "userIdTwo"},
   173  			UpdateExisting: true,
   174  		}
   175  
   176  		// test
   177  		err := createIssueLocal(ctx, &config, &ghCreateIssueService, &ghSearchIssuesMock, &ghCreateCommentMock)
   178  
   179  		// assert
   180  		assert.NoError(t, err)
   181  		assert.NotNil(t, ghSearchIssuesMock.issuesSearchResult)
   182  		assert.NotNil(t, ghCreateCommentMock.issueComment)
   183  		assert.Equal(t, config.Title, ghSearchIssuesMock.issueTitle)
   184  		assert.Equal(t, config.Title, *ghSearchIssuesMock.issuesSearchResult.Issues[0].Title)
   185  		assert.Equal(t, "", ghCreateCommentMock.issueComment.GetBody())
   186  	})
   187  
   188  	t.Run("Create error", func(t *testing.T) {
   189  		// init
   190  		ghCreateIssueService := ghCreateIssueMock{
   191  			issueError: fmt.Errorf("error creating issue"),
   192  		}
   193  		config := CreateIssueOptions{
   194  			Body: []byte("test content"),
   195  		}
   196  
   197  		// test
   198  		err := createIssueLocal(ctx, &config, &ghCreateIssueService, nil, nil)
   199  
   200  		// assert
   201  		assert.EqualError(t, err, "error occurred when creating issue: error creating issue")
   202  	})
   203  }