mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
eef3bcde60
* add step for GitHub branch protection check * add command to piper command * remove unnecessary parameter * Update resources/metadata/githubbranchprotection.yaml * add groovy part * update generation & go mod tidy * update groovy tests * fix bug with go-github version * Add step to check GitHub branch protection settings * include PR review feedabck Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
42 lines
845 B
Go
42 lines
845 B
Go
package github
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/google/go-github/v32/github"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
//NewClient creates a new GitHub client using an OAuth token for authentication
|
|
func NewClient(token, apiURL, uploadURL string) (context.Context, *github.Client, error) {
|
|
ctx := context.Background()
|
|
ts := oauth2.StaticTokenSource(
|
|
&oauth2.Token{AccessToken: token},
|
|
)
|
|
tc := oauth2.NewClient(ctx, ts)
|
|
|
|
if !strings.HasSuffix(apiURL, "/") {
|
|
apiURL += "/"
|
|
}
|
|
baseURL, err := url.Parse(apiURL)
|
|
if err != nil {
|
|
return ctx, nil, err
|
|
}
|
|
|
|
if !strings.HasSuffix(uploadURL, "/") {
|
|
uploadURL += "/"
|
|
}
|
|
uploadTargetURL, err := url.Parse(uploadURL)
|
|
if err != nil {
|
|
return ctx, nil, err
|
|
}
|
|
|
|
client := github.NewClient(tc)
|
|
|
|
client.BaseURL = baseURL
|
|
client.UploadURL = uploadTargetURL
|
|
return ctx, client, nil
|
|
}
|