1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-11-06 09:09:19 +02:00

feat(githubCreateIssue): add updateExisting flag (#3193) (#3200)

* 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:
Maurice Breit
2021-11-08 14:54:39 +01:00
committed by GitHub
parent f1a5b6a918
commit b89f095b53
4 changed files with 174 additions and 21 deletions

View File

@@ -17,18 +17,26 @@ type githubCreateIssueService interface {
Create(ctx context.Context, owner string, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error) 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) { func githubCreateIssue(config githubCreateIssueOptions, telemetryData *telemetry.CustomData) {
ctx, client, err := piperGithub.NewClient(config.Token, config.APIURL, "") ctx, client, err := piperGithub.NewClient(config.Token, config.APIURL, "")
if err != nil { if err != nil {
log.Entry().WithError(err).Fatal("Failed to get GitHub client") 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 { if err != nil {
log.Entry().WithError(err).Fatal("Failed to comment on issue") 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 { if len(config.Body)+len(config.BodyFilePath) == 0 {
return fmt.Errorf("either parameter `body` or parameter `bodyFilePath` is required") return fmt.Errorf("either parameter `body` or parameter `bodyFilePath` is required")
@@ -55,14 +63,46 @@ func runGithubCreateIssue(ctx context.Context, config *githubCreateIssueOptions,
issue.Assignees = &[]string{} issue.Assignees = &[]string{}
} }
newIssue, resp, err := ghCreateIssueService.Create(ctx, config.Owner, config.Repository, &issue) var existingIssue *github.Issue = nil
if err != nil {
if resp != nil { if config.UpdateExisting {
log.Entry().Errorf("GitHub response code %v", resp.Status) 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")
}
} }
return errors.Wrap(err, "error occurred when creating issue")
} }
log.Entry().Debugf("New issue created: %v", newIssue)
if existingIssue == nil {
newIssue, resp, err := ghCreateIssueService.Create(ctx, config.Owner, config.Repository, &issue)
if err != nil {
if resp != nil {
log.Entry().Errorf("GitHub response code %v", resp.Status)
}
return errors.Wrap(err, "error occurred when creating issue")
}
log.Entry().Debugf("New issue created: %v", newIssue)
}
return nil return nil
} }

View File

@@ -16,14 +16,15 @@ import (
) )
type githubCreateIssueOptions struct { type githubCreateIssueOptions struct {
APIURL string `json:"apiUrl,omitempty"` APIURL string `json:"apiUrl,omitempty"`
Assignees []string `json:"assignees,omitempty"` Assignees []string `json:"assignees,omitempty"`
Body string `json:"body,omitempty"` Body string `json:"body,omitempty"`
BodyFilePath string `json:"bodyFilePath,omitempty"` BodyFilePath string `json:"bodyFilePath,omitempty"`
Owner string `json:"owner,omitempty"` Owner string `json:"owner,omitempty"`
Repository string `json:"repository,omitempty"` Repository string `json:"repository,omitempty"`
Title string `json:"title,omitempty"` Title string `json:"title,omitempty"`
Token string `json:"token,omitempty"` UpdateExisting bool `json:"updateExisting,omitempty"`
Token string `json:"token,omitempty"`
} }
// GithubCreateIssueCommand Create a new GitHub issue. // GithubCreateIssueCommand Create a new GitHub issue.
@@ -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.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.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().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.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") cmd.MarkFlagRequired("apiUrl")
@@ -216,6 +218,15 @@ func githubCreateIssueMetadata() config.StepData {
Aliases: []config.Alias{}, Aliases: []config.Alias{},
Default: os.Getenv("PIPER_title"), 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", Name: "token",
ResourceRef: []config.ResourceReference{ ResourceRef: []config.ResourceReference{

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"regexp"
"testing" "testing"
"github.com/SAP/jenkins-library/pkg/mock" "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 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) { func TestRunGithubCreateIssue(t *testing.T) {
ctx := context.Background() ctx := context.Background()
t.Parallel() t.Parallel()
@@ -48,6 +105,10 @@ func TestRunGithubCreateIssue(t *testing.T) {
ghCreateIssueService := ghCreateIssueMock{ ghCreateIssueService := ghCreateIssueMock{
issueID: 1, issueID: 1,
} }
ghSearchIssuesMock := ghSearchIssuesMock{
issueID: 1,
}
ghCreateCommentMock := ghCreateCommentMock{}
config := githubCreateIssueOptions{ config := githubCreateIssueOptions{
Owner: "TEST", Owner: "TEST",
Repository: "test", Repository: "test",
@@ -57,7 +118,7 @@ func TestRunGithubCreateIssue(t *testing.T) {
} }
// test // test
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead) err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, &ghSearchIssuesMock, &ghCreateCommentMock, filesMock.FileRead)
// assert // assert
assert.NoError(t, err) 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.Body, ghCreateIssueService.issue.GetBody())
assert.Equal(t, config.Title, ghCreateIssueService.issue.GetTitle()) assert.Equal(t, config.Title, ghCreateIssueService.issue.GetTitle())
assert.Equal(t, config.Assignees, ghCreateIssueService.issue.GetAssignees()) 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) { t.Run("Success - body from file", func(t *testing.T) {
@@ -83,7 +146,7 @@ func TestRunGithubCreateIssue(t *testing.T) {
} }
// test // test
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead) err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, nil, nil, filesMock.FileRead)
// assert // assert
assert.NoError(t, err) assert.NoError(t, err)
@@ -94,6 +157,34 @@ func TestRunGithubCreateIssue(t *testing.T) {
assert.Empty(t, ghCreateIssueService.issue.GetAssignees()) 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) { t.Run("Error", func(t *testing.T) {
// init // init
filesMock := mock.FilesMock{} filesMock := mock.FilesMock{}
@@ -105,7 +196,7 @@ func TestRunGithubCreateIssue(t *testing.T) {
} }
// test // test
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead) err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, nil, nil, filesMock.FileRead)
// assert // assert
assert.EqualError(t, err, "error occurred when creating issue: error creating issue") assert.EqualError(t, err, "error occurred when creating issue: error creating issue")
@@ -115,10 +206,12 @@ func TestRunGithubCreateIssue(t *testing.T) {
// init // init
filesMock := mock.FilesMock{} filesMock := mock.FilesMock{}
ghCreateIssueService := ghCreateIssueMock{} ghCreateIssueService := ghCreateIssueMock{}
ghSearchIssuesMock := ghSearchIssuesMock{}
ghCreateCommentMock := ghCreateCommentMock{}
config := githubCreateIssueOptions{} config := githubCreateIssueOptions{}
// test // test
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead) err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, &ghSearchIssuesMock, &ghCreateCommentMock, filesMock.FileRead)
// assert // assert
assert.EqualError(t, err, "either parameter `body` or parameter `bodyFilePath` is required") assert.EqualError(t, err, "either parameter `body` or parameter `bodyFilePath` is required")
@@ -133,7 +226,7 @@ func TestRunGithubCreateIssue(t *testing.T) {
} }
// test // test
err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, filesMock.FileRead) err := runGithubCreateIssue(ctx, &config, nil, &ghCreateIssueService, nil, nil, filesMock.FileRead)
// assert // assert
assert.Contains(t, fmt.Sprint(err), "failed to read file 'test.md'") assert.Contains(t, fmt.Sprint(err), "failed to read file 'test.md'")

View File

@@ -81,6 +81,15 @@ spec:
- STEPS - STEPS
type: string type: string
mandatory: true 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 - name: token
aliases: aliases:
- name: githubToken - name: githubToken