Skip to content

Commit fc5f744

Browse files
authored
fix(github): use scm-base-url for clients (#189)
1 parent f7f9c0f commit fc5f744

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

analyze/analyze.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (a *Analyzer) AnalyzeOrg(ctx context.Context, org string, numberOfGoroutine
8080
log.Debug().Err(err).Msgf("Failed to get provider version for %s", provider)
8181
}
8282

83-
log.Debug().Msgf("Provider: %s, Version: %s", provider, providerVersion)
83+
log.Debug().Msgf("Provider: %s, Version: %s, BaseURL: %s", provider, providerVersion, a.ScmClient.GetProviderBaseURL())
8484

8585
log.Debug().Msgf("Fetching list of repositories for organization: %s on %s", org, provider)
8686
orgReposBatches := a.ScmClient.GetOrgRepos(ctx, org)
@@ -177,7 +177,7 @@ func (a *Analyzer) AnalyzeRepo(ctx context.Context, repoString string, ref strin
177177
log.Debug().Err(err).Msgf("Failed to get provider version for %s", provider)
178178
}
179179

180-
log.Debug().Msgf("Provider: %s, Version: %s", provider, providerVersion)
180+
log.Debug().Msgf("Provider: %s, Version: %s, BaseURL: %s", provider, providerVersion, a.ScmClient.GetProviderBaseURL())
181181

182182
pkgsupplyClient := pkgsupply.NewStaticClient()
183183
inventory := scanner.NewInventory(a.Opa, pkgsupplyClient, provider, providerVersion)
@@ -225,7 +225,7 @@ func (a *Analyzer) AnalyzeLocalRepo(ctx context.Context, repoPath string) error
225225
log.Debug().Err(err).Msgf("Failed to get provider version for %s", provider)
226226
}
227227

228-
log.Debug().Msgf("Provider: %s, Version: %s", provider, providerVersion)
228+
log.Debug().Msgf("Provider: %s, Version: %s, BaseURL: %s", provider, providerVersion, a.ScmClient.GetProviderBaseURL())
229229

230230
pkgsupplyClient := pkgsupply.NewStaticClient()
231231
inventory := scanner.NewInventory(a.Opa, pkgsupplyClient, provider, providerVersion)

providers/github/client.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ import (
1919
)
2020

2121
const GitHub string = "github"
22+
const defaultDomain string = "github.com"
2223

2324
func NewGithubSCMClient(ctx context.Context, baseURL string, token string) (*ScmClient, error) {
24-
client, err := NewClient(ctx, token)
25+
domain := defaultDomain
26+
if baseURL != "" {
27+
domain = baseURL
28+
}
29+
30+
client, err := NewClient(ctx, token, domain)
2531
if err != nil {
2632
return nil, err
2733
}
2834

29-
domain := "github.com"
30-
if baseURL != "" {
31-
domain = baseURL
32-
}
3335
return &ScmClient{
3436
client: client,
3537
baseURL: domain,
@@ -131,19 +133,35 @@ type Client struct {
131133
Token string
132134
}
133135

134-
func NewClient(ctx context.Context, token string) (*Client, error) {
136+
func NewClient(ctx context.Context, token string, domain string) (*Client, error) {
135137
rateLimiter, err := github_ratelimit.NewRateLimitWaiterClient(nil)
136138
if err != nil {
137139
return nil, err
138140
}
139-
restClient := github.NewClient(rateLimiter).WithAuthToken(token)
140141

141-
src := oauth2.StaticTokenSource(
142-
&oauth2.Token{AccessToken: token},
142+
var (
143+
// REST client
144+
restClient = github.NewClient(rateLimiter).WithAuthToken(token)
145+
// GraphQL client
146+
src = oauth2.StaticTokenSource(
147+
&oauth2.Token{AccessToken: token},
148+
)
149+
httpClient = oauth2.NewClient(ctx, src)
150+
graphQLClient *githubv4.Client
143151
)
144-
httpClient := oauth2.NewClient(ctx, src)
145152

146-
graphQLClient := githubv4.NewClient(httpClient)
153+
if domain == defaultDomain {
154+
graphQLClient = githubv4.NewClient(httpClient)
155+
} else {
156+
baseURL := fmt.Sprintf("https://%s/", domain)
157+
restClient, err = restClient.WithEnterpriseURLs(baseURL, baseURL)
158+
if err != nil {
159+
return nil, err
160+
}
161+
162+
graphQLClient = githubv4.NewEnterpriseClient(baseURL+"api/graphql", httpClient)
163+
}
164+
147165
return &Client{
148166
restClient: restClient,
149167
graphQLClient: graphQLClient,

0 commit comments

Comments
 (0)