@@ -178,6 +178,30 @@ func newGitHubGraphQLClient(config ClientConfig) *RateLimitAwareGraphQLClient {
178178 }
179179}
180180
181+ // getGraphQLClient returns the appropriate GraphQL client and client name based on the client type
182+ func (api * GitHubAPI ) getGraphQLClient (clientType ClientType ) (* RateLimitAwareGraphQLClient , string , error ) {
183+ switch clientType {
184+ case SourceClient :
185+ return api .sourceGraphClient , "source" , nil
186+ case TargetClient :
187+ return api .targetGraphClient , "target" , nil
188+ default :
189+ return nil , "" , fmt .Errorf ("invalid client type" )
190+ }
191+ }
192+
193+ // getRESTClient returns the appropriate REST client and client name based on the client type
194+ func (api * GitHubAPI ) getRESTClient (clientType ClientType ) (* github.Client , string , error ) {
195+ switch clientType {
196+ case SourceClient :
197+ return api .sourceClient , "source" , nil
198+ case TargetClient :
199+ return api .targetClient , "target" , nil
200+ default :
201+ return nil , "" , fmt .Errorf ("invalid client type" )
202+ }
203+ }
204+
181205func (c * RateLimitAwareGraphQLClient ) Query (ctx context.Context , q interface {}, variables map [string ]interface {}) error {
182206 var rateLimitQuery struct {
183207 RateLimit struct {
@@ -227,21 +251,12 @@ func (api *GitHubAPI) GetIssueCount(clientType ClientType, owner, name string) (
227251 "name" : githubv4 .String (name ),
228252 }
229253
230- var client * RateLimitAwareGraphQLClient
231- var clientName string
232-
233- switch clientType {
234- case SourceClient :
235- client = api .sourceGraphClient
236- clientName = "source"
237- case TargetClient :
238- client = api .targetGraphClient
239- clientName = "target"
240- default :
241- return 0 , fmt .Errorf ("invalid client type" )
254+ client , clientName , err := api .getGraphQLClient (clientType )
255+ if err != nil {
256+ return 0 , err
242257 }
243258
244- err : = client .Query (ctx , & query , variables )
259+ err = client .Query (ctx , & query , variables )
245260 if err != nil {
246261 return 0 , fmt .Errorf ("failed to query %s repository issue count: %v" , clientName , err )
247262 }
@@ -281,21 +296,12 @@ func (api *GitHubAPI) GetPRCounts(clientType ClientType, owner, name string) (*P
281296 "name" : githubv4 .String (name ),
282297 }
283298
284- var client * RateLimitAwareGraphQLClient
285- var clientName string
286-
287- switch clientType {
288- case SourceClient :
289- client = api .sourceGraphClient
290- clientName = "source"
291- case TargetClient :
292- client = api .targetGraphClient
293- clientName = "target"
294- default :
295- return nil , fmt .Errorf ("invalid client type" )
299+ client , clientName , err := api .getGraphQLClient (clientType )
300+ if err != nil {
301+ return nil , err
296302 }
297303
298- err : = client .Query (ctx , & query , variables )
304+ err = client .Query (ctx , & query , variables )
299305 if err != nil {
300306 return nil , fmt .Errorf ("failed to query %s repository PR counts: %v" , clientName , err )
301307 }
@@ -330,21 +336,12 @@ func (api *GitHubAPI) GetTagCount(clientType ClientType, owner, name string) (in
330336 "name" : githubv4 .String (name ),
331337 }
332338
333- var client * RateLimitAwareGraphQLClient
334- var clientName string
335-
336- switch clientType {
337- case SourceClient :
338- client = api .sourceGraphClient
339- clientName = "source"
340- case TargetClient :
341- client = api .targetGraphClient
342- clientName = "target"
343- default :
344- return 0 , fmt .Errorf ("invalid client type" )
339+ client , clientName , err := api .getGraphQLClient (clientType )
340+ if err != nil {
341+ return 0 , err
345342 }
346343
347- err : = client .Query (ctx , & query , variables )
344+ err = client .Query (ctx , & query , variables )
348345 if err != nil {
349346 return 0 , fmt .Errorf ("failed to query %s repository tag count: %v" , clientName , err )
350347 }
@@ -370,21 +367,12 @@ func (api *GitHubAPI) GetReleaseCount(clientType ClientType, owner, name string)
370367 "name" : githubv4 .String (name ),
371368 }
372369
373- var client * RateLimitAwareGraphQLClient
374- var clientName string
375-
376- switch clientType {
377- case SourceClient :
378- client = api .sourceGraphClient
379- clientName = "source"
380- case TargetClient :
381- client = api .targetGraphClient
382- clientName = "target"
383- default :
384- return 0 , fmt .Errorf ("invalid client type" )
370+ client , clientName , err := api .getGraphQLClient (clientType )
371+ if err != nil {
372+ return 0 , err
385373 }
386374
387- err : = client .Query (ctx , & query , variables )
375+ err = client .Query (ctx , & query , variables )
388376 if err != nil {
389377 return 0 , fmt .Errorf ("failed to query %s repository release count: %v" , clientName , err )
390378 }
@@ -416,21 +404,12 @@ func (api *GitHubAPI) GetCommitCount(clientType ClientType, owner, name string)
416404 "name" : githubv4 .String (name ),
417405 }
418406
419- var client * RateLimitAwareGraphQLClient
420- var clientName string
421-
422- switch clientType {
423- case SourceClient :
424- client = api .sourceGraphClient
425- clientName = "source"
426- case TargetClient :
427- client = api .targetGraphClient
428- clientName = "target"
429- default :
430- return 0 , fmt .Errorf ("invalid client type" )
407+ client , clientName , err := api .getGraphQLClient (clientType )
408+ if err != nil {
409+ return 0 , err
431410 }
432411
433- err : = client .Query (ctx , & query , variables )
412+ err = client .Query (ctx , & query , variables )
434413 if err != nil {
435414 return 0 , fmt .Errorf ("failed to query %s repository commit count: %v" , clientName , err )
436415 }
@@ -460,21 +439,12 @@ func (api *GitHubAPI) GetLatestCommitHash(clientType ClientType, owner, name str
460439 "name" : githubv4 .String (name ),
461440 }
462441
463- var client * RateLimitAwareGraphQLClient
464- var clientName string
465-
466- switch clientType {
467- case SourceClient :
468- client = api .sourceGraphClient
469- clientName = "source"
470- case TargetClient :
471- client = api .targetGraphClient
472- clientName = "target"
473- default :
474- return "" , fmt .Errorf ("invalid client type" )
442+ client , clientName , err := api .getGraphQLClient (clientType )
443+ if err != nil {
444+ return "" , err
475445 }
476446
477- err : = client .Query (ctx , & query , variables )
447+ err = client .Query (ctx , & query , variables )
478448 if err != nil {
479449 return "" , fmt .Errorf ("failed to query %s repository latest commit hash: %v" , clientName , err )
480450 }
@@ -500,21 +470,12 @@ func (api *GitHubAPI) GetBranchProtectionRulesCount(clientType ClientType, owner
500470 "name" : githubv4 .String (name ),
501471 }
502472
503- var client * RateLimitAwareGraphQLClient
504- var clientName string
505-
506- switch clientType {
507- case SourceClient :
508- client = api .sourceGraphClient
509- clientName = "source"
510- case TargetClient :
511- client = api .targetGraphClient
512- clientName = "target"
513- default :
514- return 0 , fmt .Errorf ("invalid client type" )
473+ client , clientName , err := api .getGraphQLClient (clientType )
474+ if err != nil {
475+ return 0 , err
515476 }
516477
517- err : = client .Query (ctx , & query , variables )
478+ err = client .Query (ctx , & query , variables )
518479 if err != nil {
519480 return 0 , fmt .Errorf ("failed to query %s repository branch protection rules count: %v" , clientName , err )
520481 }
@@ -526,18 +487,9 @@ func (api *GitHubAPI) GetBranchProtectionRulesCount(clientType ClientType, owner
526487func (api * GitHubAPI ) GetWebhookCount (clientType ClientType , owner , name string ) (int , error ) {
527488 ctx := context .Background ()
528489
529- var client * github.Client
530- var clientName string
531-
532- switch clientType {
533- case SourceClient :
534- client = api .sourceClient
535- clientName = "source"
536- case TargetClient :
537- client = api .targetClient
538- clientName = "target"
539- default :
540- return 0 , fmt .Errorf ("invalid client type" )
490+ client , clientName , err := api .getRESTClient (clientType )
491+ if err != nil {
492+ return 0 , err
541493 }
542494
543495 // List all webhooks for the repository
0 commit comments