Add support for enterprise repos to github plugin (#6194)

This commit is contained in:
shane 2019-08-02 15:05:46 -05:00 committed by Daniel Nelson
parent 78d3b86581
commit 3c811c15b3
3 changed files with 32 additions and 5 deletions

View File

@ -14,6 +14,9 @@ alternative method for collecting repository information.
## Github API access token. Unauthenticated requests are limited to 60 per hour.
# access_token = ""
## Github API enterprise url. Github Enterprise accounts must specify their base url.
# enterprise_base_url = ""
## Timeout for HTTP requests.
# http_timeout = "5s"

View File

@ -18,10 +18,11 @@ import (
// GitHub - plugin main structure
type GitHub struct {
Repositories []string `toml:"repositories"`
AccessToken string `toml:"access_token"`
HTTPTimeout internal.Duration `toml:"http_timeout"`
githubClient *github.Client
Repositories []string `toml:"repositories"`
AccessToken string `toml:"access_token"`
EnterpriseBaseURL string `toml:"enterprise_base_url"`
HTTPTimeout internal.Duration `toml:"http_timeout"`
githubClient *github.Client
obfusticatedToken string
@ -36,6 +37,9 @@ const sampleConfig = `
## Github API access token. Unauthenticated requests are limited to 60 per hour.
# access_token = ""
## Github API enterprise url. Github Enterprise accounts must specify their base url.
# enterprise_base_url = ""
## Timeout for HTTP requests.
# http_timeout = "5s"
@ -71,9 +75,16 @@ func (g *GitHub) createGitHubClient(ctx context.Context) (*github.Client, error)
g.obfusticatedToken = g.AccessToken[0:4] + "..." + g.AccessToken[len(g.AccessToken)-3:]
return github.NewClient(oauthClient), nil
return g.newGithubClient(oauthClient)
}
return g.newGithubClient(httpClient)
}
func (g *GitHub) newGithubClient(httpClient *http.Client) (*github.Client, error) {
if g.EnterpriseBaseURL != "" {
return github.NewEnterpriseClient(g.EnterpriseBaseURL, "", httpClient)
}
return github.NewClient(httpClient), nil
}

View File

@ -1,6 +1,7 @@
package github
import (
"net/http"
"reflect"
"testing"
@ -8,6 +9,18 @@ import (
"github.com/stretchr/testify/require"
)
func TestNewGithubClient(t *testing.T) {
httpClient := &http.Client{}
g := &GitHub{}
client, err := g.newGithubClient(httpClient)
require.Nil(t, err)
require.Contains(t, client.BaseURL.String(), "api.github.com")
g.EnterpriseBaseURL = "api.example.com/"
enterpriseClient, err := g.newGithubClient(httpClient)
require.Nil(t, err)
require.Contains(t, enterpriseClient.BaseURL.String(), "api.example.com")
}
func TestSplitRepositoryNameWithWorkingExample(t *testing.T) {
var validRepositoryNames = []struct {
fullName string