1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/github/github.go

110 lines
2.8 KiB
Go
Raw Normal View History

2019-11-04 17:07:30 +02:00
package github
import (
"context"
"net/url"
"strings"
"time"
2019-11-04 17:07:30 +02:00
feat(whitesourceExecuteScan): GitHub issue creation + SARIF (#3535) * Add GH issue creation + SARIF * Code cleanup * Fix fmt, add debug * Code enhancements * Fix * Added debug info * Rework UA log scan * Fix code * read UA version * Fix nil reference * Extraction * Credentials * Issue creation * Error handling * Fix issue creation * query escape * Query escape 2 * Revert * Test avoid update * HTTP client * Add support for custom TLS certs * Fix code * Fix code 2 * Fix code 3 * Disable cert check * Fix auth * Remove implicit trust * Skip verification * Fix * Fix client * Fix HTTP auth * Fix trusted certs * Trim version * Code * Add token * Added token handling to client * Fix token * Cleanup * Fix token * Token rework * Fix code * Kick out oauth client * Kick out oauth client * Transport wrapping * Token * Simplification * Refactor * Variation * Check * Fix * Debug * Switch client * Variation * Debug * Switch to cert check * Add debug * Parse self * Cleanup * Update resources/metadata/whitesourceExecuteScan.yaml * Add debug * Expose subjects * Patch * Debug * Debug2 * Debug3 * Fix logging response body * Cleanup * Cleanup * Fix request body logging * Cleanup import * Fix import cycle * Cleanup * Fix fmt * Fix NopCloser reference * Regenerate * Reintroduce * Fix test * Fix tests * Correction * Fix error * Code fix * Fix tests * Add tests * Fix code climate issues * Code climate * Code climate again * Code climate again * Fix fmt * Fix fmt 2 Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-02-23 10:30:19 +02:00
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/google/go-github/v45/github"
"github.com/pkg/errors"
2019-11-04 17:07:30 +02:00
"golang.org/x/oauth2"
)
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)
}
type ClientBuilder struct {
token string // GitHub token, required
baseURL string // GitHub API URL, required
uploadURL string // Base URL for uploading files, optional
timeout time.Duration
maxRetries int
trustedCerts []string // Trusted TLS certificates, optional
}
func NewClientBuilder(token, baseURL string) *ClientBuilder {
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
return &ClientBuilder{
token: token,
baseURL: baseURL,
uploadURL: "",
timeout: 0,
maxRetries: 0,
trustedCerts: nil,
}
}
func (b *ClientBuilder) WithTrustedCerts(trustedCerts []string) *ClientBuilder {
b.trustedCerts = trustedCerts
return b
}
func (b *ClientBuilder) WithUploadURL(uploadURL string) *ClientBuilder {
if !strings.HasSuffix(uploadURL, "/") {
uploadURL += "/"
}
b.uploadURL = uploadURL
return b
}
func (b *ClientBuilder) WithTimeout(timeout time.Duration) *ClientBuilder {
b.timeout = timeout
return b
2019-11-04 17:07:30 +02:00
}
func (b *ClientBuilder) WithMaxRetries(maxRetries int) *ClientBuilder {
b.maxRetries = maxRetries
return b
}
func (b *ClientBuilder) Build() (context.Context, *github.Client, error) {
baseURL, err := url.Parse(b.baseURL)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to parse baseURL")
}
uploadURL, err := url.Parse(b.uploadURL)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to parse uploadURL")
}
if b.timeout == 0 {
b.timeout = 30 * time.Second
}
if b.maxRetries == 0 {
b.maxRetries = 5
}
piperHttp := piperhttp.Client{}
piperHttp.SetOptions(piperhttp.ClientOptions{
TrustedCerts: b.trustedCerts,
DoLogRequestBodyOnDebug: true,
DoLogResponseBodyOnDebug: true,
TransportTimeout: b.timeout,
MaxRetries: b.maxRetries,
})
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, piperHttp.StandardClient())
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: b.token, TokenType: "Bearer"})
client := github.NewClient(oauth2.NewClient(ctx, tokenSource))
client.BaseURL = baseURL
client.UploadURL = uploadURL
return ctx, client, nil
}