-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(refactor): Switch GitHub actions provider to use github sdk (#4563
) * refactor github package and use builder pattern for client * switch to github package * some renamings * fix panic on uninitialized provider * fix according to review comments --------- Co-authored-by: Gulom Alimov <[email protected]> Co-authored-by: Jordi van Liempt <[email protected]>
- Loading branch information
1 parent
1e99326
commit 3744787
Showing
22 changed files
with
661 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/SAP/jenkins-library/pkg/log" | ||
"github.com/google/go-github/v45/github" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// CreateIssueOptions to configure the creation | ||
type CreateIssueOptions struct { | ||
APIURL string `json:"apiUrl,omitempty"` | ||
Assignees []string `json:"assignees,omitempty"` | ||
Body []byte `json:"body,omitempty"` | ||
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"` | ||
TrustedCerts []string `json:"trustedCerts,omitempty"` | ||
Issue *github.Issue `json:"issue,omitempty"` | ||
} | ||
|
||
func CreateIssue(options *CreateIssueOptions) (*github.Issue, error) { | ||
ctx, client, err := NewClientBuilder(options.Token, options.APIURL).WithTrustedCerts(options.TrustedCerts).Build() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get GitHub client") | ||
} | ||
return createIssueLocal(ctx, options, client.Issues, client.Search, client.Issues) | ||
} | ||
|
||
func createIssueLocal( | ||
ctx context.Context, | ||
options *CreateIssueOptions, | ||
createIssueService githubCreateIssueService, | ||
searchIssuesService githubSearchIssuesService, | ||
createCommentService githubCreateCommentService, | ||
) (*github.Issue, error) { | ||
issue := github.IssueRequest{ | ||
Title: &options.Title, | ||
} | ||
var bodyString string | ||
if len(options.Body) > 0 { | ||
bodyString = string(options.Body) | ||
} else { | ||
bodyString = "" | ||
} | ||
issue.Body = &bodyString | ||
if len(options.Assignees) > 0 { | ||
issue.Assignees = &options.Assignees | ||
} else { | ||
issue.Assignees = &[]string{} | ||
} | ||
|
||
var existingIssue *github.Issue = nil | ||
|
||
if options.UpdateExisting { | ||
existingIssue = options.Issue | ||
if existingIssue == nil { | ||
queryString := fmt.Sprintf("is:open is:issue repo:%v/%v in:title %v", options.Owner, options.Repository, options.Title) | ||
searchResult, resp, err := searchIssuesService.Issues(ctx, queryString, nil) | ||
if err != nil { | ||
if resp != nil { | ||
log.Entry().Errorf("GitHub search issue returned response code %v", resp.Status) | ||
} | ||
return nil, errors.Wrap(err, "error occurred when looking for existing issue") | ||
} else { | ||
for _, value := range searchResult.Issues { | ||
if value != nil && *value.Title == options.Title { | ||
existingIssue = value | ||
} | ||
} | ||
} | ||
} | ||
|
||
if existingIssue != nil { | ||
comment := &github.IssueComment{Body: issue.Body} | ||
_, resp, err := createCommentService.CreateComment(ctx, options.Owner, options.Repository, *existingIssue.Number, comment) | ||
if err != nil { | ||
if resp != nil { | ||
log.Entry().Errorf("GitHub create comment returned response code %v", resp.Status) | ||
} | ||
return nil, errors.Wrap(err, "error occurred when adding comment to existing issue") | ||
} | ||
} | ||
} | ||
|
||
if existingIssue == nil { | ||
newIssue, resp, err := createIssueService.Create(ctx, options.Owner, options.Repository, &issue) | ||
if err != nil { | ||
if resp != nil { | ||
log.Entry().Errorf("GitHub create issue returned response code %v", resp.Status) | ||
} | ||
return nil, errors.Wrap(err, "error occurred when creating issue") | ||
} | ||
log.Entry().Debugf("New issue created: %v", newIssue) | ||
existingIssue = newIssue | ||
} | ||
|
||
return existingIssue, nil | ||
} |
Oops, something went wrong.