@@ -4,13 +4,15 @@ import (
44 "context"
55 "errors"
66 "fmt"
7+ "net/http"
78 "strings"
89 "time"
910
1011 "sigs.k8s.io/controller-runtime/pkg/client"
1112
1213 log "github.com/sirupsen/logrus"
1314
15+ "github.com/argoproj/argo-cd/v3/applicationset/services"
1416 "github.com/argoproj/argo-cd/v3/applicationset/services/github_app_auth"
1517 "github.com/argoproj/argo-cd/v3/applicationset/services/scm_provider"
1618 "github.com/argoproj/argo-cd/v3/applicationset/utils"
@@ -31,20 +33,22 @@ type SCMProviderGenerator struct {
3133 SCMConfig
3234}
3335type SCMConfig struct {
34- scmRootCAPath string
35- allowedSCMProviders []string
36- enableSCMProviders bool
37- GitHubApps github_app_auth.Credentials
38- tokenRefStrictMode bool
36+ scmRootCAPath string
37+ allowedSCMProviders []string
38+ enableSCMProviders bool
39+ enableGitHubAPIMetrics bool
40+ GitHubApps github_app_auth.Credentials
41+ tokenRefStrictMode bool
3942}
4043
41- func NewSCMConfig (scmRootCAPath string , allowedSCMProviders []string , enableSCMProviders bool , gitHubApps github_app_auth.Credentials , tokenRefStrictMode bool ) SCMConfig {
44+ func NewSCMConfig (scmRootCAPath string , allowedSCMProviders []string , enableSCMProviders bool , enableGitHubAPIMetrics bool , gitHubApps github_app_auth.Credentials , tokenRefStrictMode bool ) SCMConfig {
4245 return SCMConfig {
43- scmRootCAPath : scmRootCAPath ,
44- allowedSCMProviders : allowedSCMProviders ,
45- enableSCMProviders : enableSCMProviders ,
46- GitHubApps : gitHubApps ,
47- tokenRefStrictMode : tokenRefStrictMode ,
46+ scmRootCAPath : scmRootCAPath ,
47+ allowedSCMProviders : allowedSCMProviders ,
48+ enableSCMProviders : enableSCMProviders ,
49+ enableGitHubAPIMetrics : enableGitHubAPIMetrics ,
50+ GitHubApps : gitHubApps ,
51+ tokenRefStrictMode : tokenRefStrictMode ,
4852 }
4953}
5054
@@ -273,23 +277,36 @@ func (g *SCMProviderGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha
273277}
274278
275279func (g * SCMProviderGenerator ) githubProvider (ctx context.Context , github * argoprojiov1alpha1.SCMProviderGeneratorGithub , applicationSetInfo * argoprojiov1alpha1.ApplicationSet ) (scm_provider.SCMProviderService , error ) {
280+ var metricsCtx * services.MetricsContext
281+ var httpClient * http.Client
282+
283+ if g .enableGitHubAPIMetrics {
284+ metricsCtx = & services.MetricsContext {
285+ AppSetNamespace : applicationSetInfo .Namespace ,
286+ AppSetName : applicationSetInfo .Name ,
287+ }
288+ httpClient = services .NewGitHubMetricsClient (metricsCtx )
289+ }
290+
276291 if github .AppSecretName != "" {
277292 auth , err := g .GitHubApps .GetAuthSecret (ctx , github .AppSecretName )
278293 if err != nil {
279294 return nil , fmt .Errorf ("error fetching Github app secret: %w" , err )
280295 }
281296
282- return scm_provider .NewGithubAppProviderFor (
283- * auth ,
284- github .Organization ,
285- github .API ,
286- github .AllBranches ,
287- )
297+ if g .enableGitHubAPIMetrics {
298+ return scm_provider .NewGithubAppProviderFor (* auth , github .Organization , github .API , github .AllBranches , httpClient )
299+ }
300+ return scm_provider .NewGithubAppProviderFor (* auth , github .Organization , github .API , github .AllBranches )
288301 }
289302
290303 token , err := utils .GetSecretRef (ctx , g .client , github .TokenRef , applicationSetInfo .Namespace , g .tokenRefStrictMode )
291304 if err != nil {
292305 return nil , fmt .Errorf ("error fetching Github token: %w" , err )
293306 }
307+
308+ if g .enableGitHubAPIMetrics {
309+ return scm_provider .NewGithubProvider (github .Organization , token , github .API , github .AllBranches , httpClient )
310+ }
294311 return scm_provider .NewGithubProvider (github .Organization , token , github .API , github .AllBranches )
295312}
0 commit comments