From 3c811c15b3de9d11ac15dab2ed53857ae42dc05c Mon Sep 17 00:00:00 2001 From: shane Date: Fri, 2 Aug 2019 15:05:46 -0500 Subject: [PATCH] Add support for enterprise repos to github plugin (#6194) --- plugins/inputs/github/README.md | 3 +++ plugins/inputs/github/github.go | 21 ++++++++++++++++----- plugins/inputs/github/github_test.go | 13 +++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/github/README.md b/plugins/inputs/github/README.md index 29eddf25d..7227b167d 100644 --- a/plugins/inputs/github/README.md +++ b/plugins/inputs/github/README.md @@ -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" diff --git a/plugins/inputs/github/github.go b/plugins/inputs/github/github.go index 906c99a20..5c9682e4a 100644 --- a/plugins/inputs/github/github.go +++ b/plugins/inputs/github/github.go @@ -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 } diff --git a/plugins/inputs/github/github_test.go b/plugins/inputs/github/github_test.go index 33abc1c3e..3c346b7f8 100644 --- a/plugins/inputs/github/github_test.go +++ b/plugins/inputs/github/github_test.go @@ -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