fix(internal/releaser): add helper method to lookup for provider accesToken in environment variabels

This commit is contained in:
Felix Wiedmann
2019-06-17 22:30:17 +02:00
parent 065070657c
commit 8efde63865
2 changed files with 23 additions and 2 deletions

View File

@@ -29,16 +29,22 @@ type Client struct {
// New initialize a new GitHubRelease
func New(c *config.GitHubProvider) (*Client, error) {
var err error
if c.AccessToken, err = util.GetAccessToken(GITHUB); err != nil {
return &Client{}, err
}
ctx := context.Background()
httpClient := util.CreateBearerHTTPClient(ctx, c.AccessToken)
var client *github.Client
var err error
baseURL := "https://github.com"
if c.CustomURL == "" {
client = github.NewClient(httpClient)
} else {
client, err = github.NewEnterpriseClient(c.CustomURL, c.CustomURL+"/api/v3/", httpClient)
if client, err = github.NewEnterpriseClient(c.CustomURL, c.CustomURL+"/api/v3/", httpClient); err != nil {
return &Client{}, err
}
baseURL = c.CustomURL
}
return &Client{

View File

@@ -2,7 +2,10 @@ package util
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"golang.org/x/oauth2"
)
@@ -17,3 +20,15 @@ func CreateBearerHTTPClient(ctx context.Context, token string) *http.Client {
return client
}
// GetAccessToken lookup for the providers accesstoken
func GetAccessToken(providerName string) (string, error) {
var token string
var exists bool
envName := fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(providerName))
if token, exists = os.LookupEnv(envName); !exists {
return "", fmt.Errorf("Could not find %s in the enviroment variables. Please check if it is set", envName)
}
return token, nil
}