Skip to content

Commit daf15fd

Browse files
Allow usage with GithubEnterprise (#131)
1 parent 2abdb35 commit daf15fd

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func (c *Config) GetClient() (*github.Client, error) {
101101

102102
client := github.NewClient(paginator)
103103

104+
if c.ApiUrl != nil && c.ApiUrl.String() != "https://api.github.com" {
105+
// Don't need to validate error as it's checked in envconfig
106+
client, _ = client.WithEnterpriseURLs(c.ApiUrl.String(), c.ApiUrl.String())
107+
}
108+
104109
if c.GithubToken != "" {
105110
client = client.WithAuthToken(c.GithubToken)
106111
}

config/config_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,56 @@ func TestConfig(t *testing.T) {
142142
})
143143
}
144144
}
145+
146+
func TestGetClient(t *testing.T) {
147+
tests := []struct {
148+
name string
149+
envVars map[string]string
150+
expectedHost string
151+
expectedPath string
152+
expectedErr error
153+
}{
154+
{
155+
name: "default config",
156+
expectedHost: "api.github.com",
157+
expectedPath: "/",
158+
expectedErr: nil,
159+
},
160+
{
161+
name: "non-default config",
162+
envVars: map[string]string{
163+
"METRICS_PATH": "/otherendpoint",
164+
"LISTEN_PORT": "1111",
165+
"LOG_LEVEL": "DEBUG",
166+
"API_URL": "https://example.com",
167+
"REPOS": "repo1, repo2",
168+
"ORGS": "org1,org2 ",
169+
"USERS": " user1, user2 ",
170+
"GITHUB_RESULTS_PER_PAGE": "50",
171+
"GITHUB_TOKEN": "token",
172+
"GITHUB_RATE_LIMIT_ENABLED": "false",
173+
"FETCH_REPO_RELEASES_ENABLED": "false",
174+
"FETCH_ORGS_CONCURRENCY": "2",
175+
"FETCH_ORG_REPOS_CONCURRENCY": "3",
176+
"FETCH_REPOS_CONCURRENCY": "4",
177+
"FETCH_USERS_CONCURRENCY": "5",
178+
},
179+
expectedHost: "example.com",
180+
expectedPath: "/api/v3/",
181+
expectedErr: nil,
182+
},
183+
}
184+
for _, tt := range tests {
185+
t.Run(tt.name, func(t *testing.T) {
186+
for k, v := range tt.envVars {
187+
t.Setenv(k, v)
188+
}
189+
190+
cfg, _ := Init()
191+
client, _ := cfg.GetClient()
192+
193+
assert.Equal(t, client.BaseURL.Host, tt.expectedHost)
194+
assert.Equal(t, client.BaseURL.Path, tt.expectedPath)
195+
})
196+
}
197+
}

0 commit comments

Comments
 (0)