github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/github/github.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"strings"
     7  	"time"
     8  
     9  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    10  	"github.com/google/go-github/v45/github"
    11  	"github.com/pkg/errors"
    12  	"golang.org/x/oauth2"
    13  )
    14  
    15  type githubCreateIssueService interface {
    16  	Create(ctx context.Context, owner string, repo string, issue *github.IssueRequest) (*github.Issue, *github.Response, error)
    17  }
    18  
    19  type githubSearchIssuesService interface {
    20  	Issues(ctx context.Context, query string, opts *github.SearchOptions) (*github.IssuesSearchResult, *github.Response, error)
    21  }
    22  
    23  type githubCreateCommentService interface {
    24  	CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
    25  }
    26  
    27  type ClientBuilder struct {
    28  	token        string // GitHub token, required
    29  	baseURL      string // GitHub API URL, required
    30  	uploadURL    string // Base URL for uploading files, optional
    31  	timeout      time.Duration
    32  	maxRetries   int
    33  	trustedCerts []string // Trusted TLS certificates, optional
    34  }
    35  
    36  func NewClientBuilder(token, baseURL string) *ClientBuilder {
    37  	if !strings.HasSuffix(baseURL, "/") {
    38  		baseURL += "/"
    39  	}
    40  
    41  	return &ClientBuilder{
    42  		token:        token,
    43  		baseURL:      baseURL,
    44  		uploadURL:    "",
    45  		timeout:      0,
    46  		maxRetries:   0,
    47  		trustedCerts: nil,
    48  	}
    49  }
    50  
    51  func (b *ClientBuilder) WithTrustedCerts(trustedCerts []string) *ClientBuilder {
    52  	b.trustedCerts = trustedCerts
    53  	return b
    54  }
    55  
    56  func (b *ClientBuilder) WithUploadURL(uploadURL string) *ClientBuilder {
    57  	if !strings.HasSuffix(uploadURL, "/") {
    58  		uploadURL += "/"
    59  	}
    60  
    61  	b.uploadURL = uploadURL
    62  	return b
    63  }
    64  
    65  func (b *ClientBuilder) WithTimeout(timeout time.Duration) *ClientBuilder {
    66  	b.timeout = timeout
    67  	return b
    68  }
    69  
    70  func (b *ClientBuilder) WithMaxRetries(maxRetries int) *ClientBuilder {
    71  	b.maxRetries = maxRetries
    72  	return b
    73  }
    74  
    75  func (b *ClientBuilder) Build() (context.Context, *github.Client, error) {
    76  	baseURL, err := url.Parse(b.baseURL)
    77  	if err != nil {
    78  		return nil, nil, errors.Wrap(err, "failed to parse baseURL")
    79  	}
    80  
    81  	uploadURL, err := url.Parse(b.uploadURL)
    82  	if err != nil {
    83  		return nil, nil, errors.Wrap(err, "failed to parse uploadURL")
    84  	}
    85  
    86  	if b.timeout == 0 {
    87  		b.timeout = 30 * time.Second
    88  	}
    89  
    90  	if b.maxRetries == 0 {
    91  		b.maxRetries = 5
    92  	}
    93  
    94  	piperHttp := piperhttp.Client{}
    95  	piperHttp.SetOptions(piperhttp.ClientOptions{
    96  		TrustedCerts:             b.trustedCerts,
    97  		DoLogRequestBodyOnDebug:  true,
    98  		DoLogResponseBodyOnDebug: true,
    99  		TransportTimeout:         b.timeout,
   100  		MaxRetries:               b.maxRetries,
   101  	})
   102  	ctx := context.WithValue(context.Background(), oauth2.HTTPClient, piperHttp.StandardClient())
   103  	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: b.token, TokenType: "Bearer"})
   104  
   105  	client := github.NewClient(oauth2.NewClient(ctx, tokenSource))
   106  	client.BaseURL = baseURL
   107  	client.UploadURL = uploadURL
   108  	return ctx, client, nil
   109  }