2019-11-04 17:07:30 +02:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-14 12:05:12 +02:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2019-11-04 17:07:30 +02:00
|
|
|
|
2020-09-14 12:05:12 +02:00
|
|
|
"github.com/google/go-github/v32/github"
|
2019-11-04 17:07:30 +02:00
|
|
|
"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)
|
|
|
|
|
2020-09-14 12:05:12 +02:00
|
|
|
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)
|
2019-11-04 17:07:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return ctx, nil, err
|
|
|
|
}
|
2020-09-14 12:05:12 +02:00
|
|
|
|
|
|
|
client := github.NewClient(tc)
|
|
|
|
|
|
|
|
client.BaseURL = baseURL
|
|
|
|
client.UploadURL = uploadTargetURL
|
2019-11-04 17:07:30 +02:00
|
|
|
return ctx, client, nil
|
|
|
|
}
|