Skip to content

Commit

Permalink
Add support for enterprise repos to github plugin (influxdata#6194)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanempope authored and Mathieu Lecarme committed Apr 17, 2020
1 parent e14249d commit 448aaa3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
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) {
if g.EnterpriseBaseURL != "" {
return github.NewEnterpriseClient(g.EnterpriseBaseURL, "", httpClient)
}
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

0 comments on commit 448aaa3

Please sign in to comment.