2019-11-04 17:07:30 +02:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-14 12:05:12 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2023-09-20 11:38:45 +02:00
|
|
|
"time"
|
2019-11-04 17:07:30 +02:00
|
|
|
|
2022-02-23 10:30:19 +02:00
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
2022-08-02 08:26:26 +02:00
|
|
|
"github.com/google/go-github/v45/github"
|
2022-01-21 11:52:17 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-11-04 17:07:30 +02:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
2022-01-21 11:52:17 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
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
|
2022-01-21 11:52:17 +02:00
|
|
|
}
|
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
func NewClientBuilder(token, baseURL string) *ClientBuilder {
|
|
|
|
if !strings.HasSuffix(baseURL, "/") {
|
|
|
|
baseURL += "/"
|
2020-09-14 12:05:12 +02:00
|
|
|
}
|
2023-09-20 11:38:45 +02:00
|
|
|
|
|
|
|
return &ClientBuilder{
|
|
|
|
token: token,
|
|
|
|
baseURL: baseURL,
|
|
|
|
uploadURL: "",
|
|
|
|
timeout: 0,
|
|
|
|
maxRetries: 0,
|
|
|
|
trustedCerts: nil,
|
2020-09-14 12:05:12 +02:00
|
|
|
}
|
2023-09-20 11:38:45 +02:00
|
|
|
}
|
2020-09-14 12:05:12 +02:00
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
func (b *ClientBuilder) WithTrustedCerts(trustedCerts []string) *ClientBuilder {
|
|
|
|
b.trustedCerts = trustedCerts
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ClientBuilder) WithUploadURL(uploadURL string) *ClientBuilder {
|
2020-09-14 12:05:12 +02:00
|
|
|
if !strings.HasSuffix(uploadURL, "/") {
|
|
|
|
uploadURL += "/"
|
|
|
|
}
|
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
b.uploadURL = uploadURL
|
|
|
|
return b
|
|
|
|
}
|
2020-09-14 12:05:12 +02:00
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
func (b *ClientBuilder) WithTimeout(timeout time.Duration) *ClientBuilder {
|
|
|
|
b.timeout = timeout
|
|
|
|
return b
|
2019-11-04 17:07:30 +02:00
|
|
|
}
|
2022-01-21 11:52:17 +02:00
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
func (b *ClientBuilder) WithMaxRetries(maxRetries int) *ClientBuilder {
|
|
|
|
b.maxRetries = maxRetries
|
|
|
|
return b
|
2022-01-21 11:52:17 +02:00
|
|
|
}
|
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
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")
|
2022-01-21 11:52:17 +02:00
|
|
|
}
|
2023-09-20 11:38:45 +02:00
|
|
|
|
|
|
|
uploadURL, err := url.Parse(b.uploadURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "failed to parse uploadURL")
|
2022-01-21 11:52:17 +02:00
|
|
|
}
|
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
if b.timeout == 0 {
|
|
|
|
b.timeout = 30 * time.Second
|
2022-01-21 11:52:17 +02:00
|
|
|
}
|
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
if b.maxRetries == 0 {
|
|
|
|
b.maxRetries = 5
|
2022-01-21 11:52:17 +02:00
|
|
|
}
|
|
|
|
|
2023-09-20 11:38:45 +02:00
|
|
|
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
|
2022-01-21 11:52:17 +02:00
|
|
|
}
|