Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Enterprise repos in Github plugin #6194

Merged
merged 3 commits into from
Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions plugins/inputs/github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 16 additions & 5 deletions plugins/inputs/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if there's a better function name for this...

if g.EnterpriseBaseURL != "" {
return github.NewEnterpriseClient(g.EnterpriseBaseURL, "", httpClient)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "" in the 2nd arg is the Enterprise Upload URL that isn't necessary for the plugin. You can compare this commit to my first commit, but I checked go-github and as far as I can tell it isn't necessary for this plugin.

}
return github.NewClient(httpClient), nil
}

Expand Down
13 changes: 13 additions & 0 deletions plugins/inputs/github/github_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package github

import (
"net/http"
"reflect"
"testing"

gh "github.com/google/go-github/github"
"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
Expand Down