You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-11-06 09:09:19 +02:00
* feat(githubCreateIssue): add updateExisting flag (#3193) * run go generate again Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
@@ -17,18 +17,26 @@ type githubCreateIssueService interface {
|
||||
Create(ctx context.Context, owner string, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error)
|
||||
}
|
||||
|
||||
type githubSearchIssuesService interface {
|
||||
Issues(ctx context.Context, query string, opts *github.SearchOptions) (*github.IssuesSearchResult, *github.Response, error)
|
||||
}
|
||||
|
||||
type githubCreateCommentService interface {
|
||||
CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
|
||||
}
|
||||
|
||||
func githubCreateIssue(config githubCreateIssueOptions, telemetryData *telemetry.CustomData) {
|
||||
ctx, client, err := piperGithub.NewClient(config.Token, config.APIURL, "")
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Fatal("Failed to get GitHub client")
|
||||
}
|
||||
err = runGithubCreateIssue(ctx, &config, telemetryData, client.Issues, ioutil.ReadFile)
|
||||
err = runGithubCreateIssue(ctx, &config, telemetryData, client.Issues, client.Search, client.Issues, ioutil.ReadFile)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Fatal("Failed to comment on issue")
|
||||
}
|
||||
}
|
||||
|
||||
func runGithubCreateIssue(ctx context.Context, config *githubCreateIssueOptions, _ *telemetry.CustomData, ghCreateIssueService githubCreateIssueService, readFile func(string) ([]byte, error)) error {
|
||||
func runGithubCreateIssue(ctx context.Context, config *githubCreateIssueOptions, _ *telemetry.CustomData, ghCreateIssueService githubCreateIssueService, ghSearchIssuesService githubSearchIssuesService, ghCreateCommentService githubCreateCommentService, readFile func(string) ([]byte, error)) error {
|
||||
|
||||
if len(config.Body)+len(config.BodyFilePath) == 0 {
|
||||
return fmt.Errorf("either parameter `body` or parameter `bodyFilePath` is required")
|
||||
@@ -55,6 +63,37 @@ func runGithubCreateIssue(ctx context.Context, config *githubCreateIssueOptions,
|
||||
issue.Assignees = &[]string{}
|
||||
}
|
||||
|
||||
var existingIssue *github.Issue = nil
|
||||
|
||||
if config.UpdateExisting {
|
||||
queryString := fmt.Sprintf("is:open is:issue repo:%v/%v in:title %v", config.Owner, config.Repository, config.Title)
|
||||
searchResult, resp, err := ghSearchIssuesService.Issues(ctx, queryString, nil)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
log.Entry().Errorf("GitHub response code %v", resp.Status)
|
||||
}
|
||||
return errors.Wrap(err, "error occurred when looking for existing issue")
|
||||
} else {
|
||||
for _, value := range searchResult.Issues {
|
||||
if value != nil && *value.Title == config.Title {
|
||||
existingIssue = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if existingIssue != nil {
|
||||
comment := &github.IssueComment{Body: issue.Body}
|
||||
_, resp, err := ghCreateCommentService.CreateComment(ctx, config.Owner, config.Repository, *existingIssue.Number, comment)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
log.Entry().Errorf("GitHub response code %v", resp.Status)
|
||||
}
|
||||
return errors.Wrap(err, "error occurred when looking for existing issue")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if existingIssue == nil {
|
||||
newIssue, resp, err := ghCreateIssueService.Create(ctx, config.Owner, config.Repository, &issue)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
@@ -63,6 +102,7 @@ func runGithubCreateIssue(ctx context.Context, config *githubCreateIssueOptions,
|
||||
return errors.Wrap(err, "error occurred when creating issue")
|
||||
}
|
||||
log.Entry().Debugf("New issue created: %v", newIssue)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ type githubCreateIssueOptions struct {
|
||||
Owner string `json:"owner,omitempty"`
|
||||
Repository string `json:"repository,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
UpdateExisting bool `json:"updateExisting,omitempty"`
|
||||
Token string `json:"token,omitempty"`
|
||||
}
|
||||
|
||||
@@ -120,6 +121,7 @@ func addGithubCreateIssueFlags(cmd *cobra.Command, stepConfig *githubCreateIssue
|
||||
cmd.Flags().StringVar(&stepConfig.Owner, "owner", os.Getenv("PIPER_owner"), "Name of the GitHub organization.")
|
||||
cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Name of the GitHub repository.")
|
||||
cmd.Flags().StringVar(&stepConfig.Title, "title", os.Getenv("PIPER_title"), "Defines the title for the Issue.")
|
||||
cmd.Flags().BoolVar(&stepConfig.UpdateExisting, "updateExisting", false, "Whether to update an existing open issue with the same title by adding a comment instead of creating a new one.")
|
||||
cmd.Flags().StringVar(&stepConfig.Token, "token", os.Getenv("PIPER_token"), "GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line.")
|
||||
|
||||
cmd.MarkFlagRequired("apiUrl")
|
||||
@@ -216,6 +218,15 @@ func githubCreateIssueMetadata() config.StepData {
|
||||
Aliases: []config.Alias{},
|
||||
Default: os.Getenv("PIPER_title"),
|
||||
},
|
||||
{
|
||||
Name: "updateExisting",
|
||||
ResourceRef: []config.ResourceReference{},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "bool",
|
||||
Mandatory: false,
|
||||
Aliases: []config.Alias{},
|
||||
Default: false,
|
||||
},
|
||||
{
|
||||
Name: "token",
|
||||
ResourceRef: []config.ResourceReference{
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/mock"
|
||||
@@ -38,6 +39,62 @@ func (g *ghCreateIssueMock) Create(ctx context.Context, owner string, repo strin
|
||||
return &issueResponse, &ghRes, g.issueError
|
||||
}
|
||||
|
||||
type ghSearchIssuesMock struct {
|
||||
issueID int64
|
||||
issueNumber int
|
||||
issueTitle string
|
||||
issueBody string
|
||||
issuesSearchResult *github.IssuesSearchResult
|
||||
issuesSearchError error
|
||||
}
|
||||
|
||||
func (g *ghSearchIssuesMock) Issues(ctx context.Context, query string, opts *github.SearchOptions) (*github.IssuesSearchResult, *github.Response, error) {
|
||||
|
||||
regex := regexp.MustCompile(`.*in:title (?P<Title>(.*))`)
|
||||
matches := regex.FindStringSubmatch(query)
|
||||
|
||||
g.issueTitle = matches[1]
|
||||
|
||||
issues := []*github.Issue{
|
||||
{
|
||||
ID: &g.issueID,
|
||||
Number: &g.issueNumber,
|
||||
Title: &g.issueTitle,
|
||||
Body: &g.issueBody,
|
||||
},
|
||||
}
|
||||
|
||||
total := len(issues)
|
||||
incompleteResults := false
|
||||
|
||||
g.issuesSearchResult = &github.IssuesSearchResult{
|
||||
Issues: issues,
|
||||
Total: &total,
|
||||
IncompleteResults: &incompleteResults,
|
||||
}
|
||||
|
||||
ghRes := github.Response{Response: &http.Response{Status: "200"}}
|
||||
if g.issuesSearchError != nil {
|
||||
ghRes.Status = "401"
|
||||
}
|
||||
|
||||
return g.issuesSearchResult, &ghRes, g.issuesSearchError
|
||||
}
|
||||
|
||||
type ghCreateCommentMock struct {
|
||||
issueComment *github.IssueComment
|
||||
issueCommentError error
|
||||
}
|
||||
|
||||
func (g *ghCreateCommentMock) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
|
||||
g.issueComment = comment
|
||||
ghRes := github.Response{Response: &http.Response{Status: "200"}}
|
||||
if g.issueCommentError != nil {
|
||||
ghRes.Status = "401"
|
||||
}
|
||||
return g.issueComment, &ghRes, g.issueCommentError
|
||||
}
|
||||
|
||||
func TestRunGithubCreateIssue(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
t.Parallel()
|
||||
@@ -48,6 +105,10 @@ func TestRunGithubCreateIssue(t *testing.T) {
|
||||
ghCreateIssueService := ghCreateIssueMock{
|
||||
issueID: 1,
|
||||
}
|
||||
ghSearchIssuesMock := ghSearchIssuesMock{
|
||||
issueID: 1,
|
||||
}
|
||||
ghCreateCommentMock := ghCreateCommentMock{}
|
||||
config := githubCreateIssueOptions{
|
||||
Owner: "TEST",
|
||||
Repository: "test",
|
||||
@@ -57,7 +118,7 @@ func TestRunGithubCreateIssue(t *testing.T) {
|
||||
}
|
||||
|
||||
// test
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead)
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, &ghSearchIssuesMock, &ghCreateCommentMock, filesMock.FileRead)
|
||||
|
||||
// assert
|
||||
assert.NoError(t, err)
|
||||
@@ -66,6 +127,8 @@ func TestRunGithubCreateIssue(t *testing.T) {
|
||||
assert.Equal(t, config.Body, ghCreateIssueService.issue.GetBody())
|
||||
assert.Equal(t, config.Title, ghCreateIssueService.issue.GetTitle())
|
||||
assert.Equal(t, config.Assignees, ghCreateIssueService.issue.GetAssignees())
|
||||
assert.Nil(t, ghSearchIssuesMock.issuesSearchResult)
|
||||
assert.Nil(t, ghCreateCommentMock.issueComment)
|
||||
})
|
||||
|
||||
t.Run("Success - body from file", func(t *testing.T) {
|
||||
@@ -83,7 +146,7 @@ func TestRunGithubCreateIssue(t *testing.T) {
|
||||
}
|
||||
|
||||
// test
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead)
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, nil, nil, filesMock.FileRead)
|
||||
|
||||
// assert
|
||||
assert.NoError(t, err)
|
||||
@@ -94,6 +157,34 @@ func TestRunGithubCreateIssue(t *testing.T) {
|
||||
assert.Empty(t, ghCreateIssueService.issue.GetAssignees())
|
||||
})
|
||||
|
||||
t.Run("Success update existing", func(t *testing.T) {
|
||||
// init
|
||||
filesMock := mock.FilesMock{}
|
||||
ghSearchIssuesMock := ghSearchIssuesMock{
|
||||
issueID: 1,
|
||||
}
|
||||
ghCreateCommentMock := ghCreateCommentMock{}
|
||||
config := githubCreateIssueOptions{
|
||||
Owner: "TEST",
|
||||
Repository: "test",
|
||||
Body: "This is my test body",
|
||||
Title: "This is my title",
|
||||
Assignees: []string{"userIdOne", "userIdTwo"},
|
||||
UpdateExisting: true,
|
||||
}
|
||||
|
||||
// test
|
||||
err := runGithubCreateIssue(ctx, &config, nil, nil, &ghSearchIssuesMock, &ghCreateCommentMock, filesMock.FileRead)
|
||||
|
||||
// assert
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, ghSearchIssuesMock.issuesSearchResult)
|
||||
assert.NotNil(t, ghCreateCommentMock.issueComment)
|
||||
assert.Equal(t, config.Title, ghSearchIssuesMock.issueTitle)
|
||||
assert.Equal(t, config.Title, *ghSearchIssuesMock.issuesSearchResult.Issues[0].Title)
|
||||
assert.Equal(t, config.Body, ghCreateCommentMock.issueComment.GetBody())
|
||||
})
|
||||
|
||||
t.Run("Error", func(t *testing.T) {
|
||||
// init
|
||||
filesMock := mock.FilesMock{}
|
||||
@@ -105,7 +196,7 @@ func TestRunGithubCreateIssue(t *testing.T) {
|
||||
}
|
||||
|
||||
// test
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead)
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, nil, nil, filesMock.FileRead)
|
||||
|
||||
// assert
|
||||
assert.EqualError(t, err, "error occurred when creating issue: error creating issue")
|
||||
@@ -115,10 +206,12 @@ func TestRunGithubCreateIssue(t *testing.T) {
|
||||
// init
|
||||
filesMock := mock.FilesMock{}
|
||||
ghCreateIssueService := ghCreateIssueMock{}
|
||||
ghSearchIssuesMock := ghSearchIssuesMock{}
|
||||
ghCreateCommentMock := ghCreateCommentMock{}
|
||||
config := githubCreateIssueOptions{}
|
||||
|
||||
// test
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead)
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, &ghSearchIssuesMock, &ghCreateCommentMock, filesMock.FileRead)
|
||||
|
||||
// assert
|
||||
assert.EqualError(t, err, "either parameter `body` or parameter `bodyFilePath` is required")
|
||||
@@ -133,7 +226,7 @@ func TestRunGithubCreateIssue(t *testing.T) {
|
||||
}
|
||||
|
||||
// test
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead)
|
||||
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, nil, nil, filesMock.FileRead)
|
||||
|
||||
// assert
|
||||
assert.Contains(t, fmt.Sprint(err), "failed to read file 'test.md'")
|
||||
|
||||
@@ -81,6 +81,15 @@ spec:
|
||||
- STEPS
|
||||
type: string
|
||||
mandatory: true
|
||||
- name: updateExisting
|
||||
description: Whether to update an existing open issue with the same title by adding a comment instead of creating a new one.
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
||||
type: bool
|
||||
mandatory: false
|
||||
default: false
|
||||
- name: token
|
||||
aliases:
|
||||
- name: githubToken
|
||||
|
||||
Reference in New Issue
Block a user