Skip to content

Commit 448aaa3

Browse files
shanempopeMathieu Lecarme
authored andcommitted
Add support for enterprise repos to github plugin (influxdata#6194)
1 parent e14249d commit 448aaa3

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

plugins/inputs/github/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ alternative method for collecting repository information.
1414

1515
## Github API access token. Unauthenticated requests are limited to 60 per hour.
1616
# access_token = ""
17+
18+
## Github API enterprise url. Github Enterprise accounts must specify their base url.
19+
# enterprise_base_url = ""
1720

1821
## Timeout for HTTP requests.
1922
# http_timeout = "5s"

plugins/inputs/github/github.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import (
1818

1919
// GitHub - plugin main structure
2020
type GitHub struct {
21-
Repositories []string `toml:"repositories"`
22-
AccessToken string `toml:"access_token"`
23-
HTTPTimeout internal.Duration `toml:"http_timeout"`
24-
githubClient *github.Client
21+
Repositories []string `toml:"repositories"`
22+
AccessToken string `toml:"access_token"`
23+
EnterpriseBaseURL string `toml:"enterprise_base_url"`
24+
HTTPTimeout internal.Duration `toml:"http_timeout"`
25+
githubClient *github.Client
2526

2627
obfusticatedToken string
2728

@@ -36,6 +37,9 @@ const sampleConfig = `
3637
3738
## Github API access token. Unauthenticated requests are limited to 60 per hour.
3839
# access_token = ""
40+
41+
## Github API enterprise url. Github Enterprise accounts must specify their base url.
42+
# enterprise_base_url = ""
3943
4044
## Timeout for HTTP requests.
4145
# http_timeout = "5s"
@@ -71,9 +75,16 @@ func (g *GitHub) createGitHubClient(ctx context.Context) (*github.Client, error)
7175

7276
g.obfusticatedToken = g.AccessToken[0:4] + "..." + g.AccessToken[len(g.AccessToken)-3:]
7377

74-
return github.NewClient(oauthClient), nil
78+
return g.newGithubClient(oauthClient)
7579
}
7680

81+
return g.newGithubClient(httpClient)
82+
}
83+
84+
func (g *GitHub) newGithubClient(httpClient *http.Client) (*github.Client, error) {
85+
if g.EnterpriseBaseURL != "" {
86+
return github.NewEnterpriseClient(g.EnterpriseBaseURL, "", httpClient)
87+
}
7788
return github.NewClient(httpClient), nil
7889
}
7990

plugins/inputs/github/github_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package github
22

33
import (
4+
"net/http"
45
"reflect"
56
"testing"
67

78
gh "github.com/google/go-github/github"
89
"github.com/stretchr/testify/require"
910
)
1011

12+
func TestNewGithubClient(t *testing.T) {
13+
httpClient := &http.Client{}
14+
g := &GitHub{}
15+
client, err := g.newGithubClient(httpClient)
16+
require.Nil(t, err)
17+
require.Contains(t, client.BaseURL.String(), "api.github.com")
18+
g.EnterpriseBaseURL = "api.example.com/"
19+
enterpriseClient, err := g.newGithubClient(httpClient)
20+
require.Nil(t, err)
21+
require.Contains(t, enterpriseClient.BaseURL.String(), "api.example.com")
22+
}
23+
1124
func TestSplitRepositoryNameWithWorkingExample(t *testing.T) {
1225
var validRepositoryNames = []struct {
1326
fullName string

0 commit comments

Comments
 (0)