Skip to content

Commit 815ac8b

Browse files
implemented builtin retry mechanism for gitlab and proper handling of next page
1 parent 87bdc78 commit 815ac8b

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

pkg/sources/gitlab/gitlab.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gitlab
22

33
import (
44
"fmt"
5-
"net/http"
65
"net/url"
76
"os"
87
"path/filepath"
@@ -453,14 +452,24 @@ func (s *Source) newClient() (*gitlab.Client, error) {
453452
// Initialize a new api instance.
454453
switch s.authMethod {
455454
case "OAUTH":
456-
apiClient, err := gitlab.NewOAuthClient(s.token, gitlab.WithBaseURL(s.url))
455+
apiClient, err := gitlab.NewOAuthClient(
456+
s.token,
457+
gitlab.WithBaseURL(s.url),
458+
gitlab.WithCustomRetryWaitMinMax(time.Second, 5*time.Second),
459+
gitlab.WithCustomRetryMax(3),
460+
)
457461
if err != nil {
458462
return nil, fmt.Errorf("could not create Gitlab OAUTH client for %q: %w", s.url, err)
459463
}
460464
return apiClient, nil
461465

462466
case "BASIC_AUTH":
463-
apiClient, err := gitlab.NewBasicAuthClient(s.user, s.password, gitlab.WithBaseURL(s.url))
467+
apiClient, err := gitlab.NewOAuthClient(
468+
s.token,
469+
gitlab.WithBaseURL(s.url),
470+
gitlab.WithCustomRetryWaitMinMax(time.Second, 5*time.Second),
471+
gitlab.WithCustomRetryMax(3),
472+
)
464473
if err != nil {
465474
return nil, fmt.Errorf("could not create Gitlab BASICAUTH client for %q: %w", s.url, err)
466475
}
@@ -473,7 +482,12 @@ func (s *Source) newClient() (*gitlab.Client, error) {
473482
}
474483
fallthrough
475484
case "TOKEN":
476-
apiClient, err := gitlab.NewOAuthClient(s.token, gitlab.WithBaseURL(s.url))
485+
apiClient, err := gitlab.NewOAuthClient(
486+
s.token,
487+
gitlab.WithBaseURL(s.url),
488+
gitlab.WithCustomRetryWaitMinMax(time.Second, 5*time.Second),
489+
gitlab.WithCustomRetryMax(3),
490+
)
477491
if err != nil {
478492
return nil, fmt.Errorf("could not create Gitlab TOKEN client for %q: %w", s.url, err)
479493
}
@@ -695,25 +709,15 @@ func (s *Source) getAllProjectReposV2(
695709
// totalCount tracks the total number of projects processed by this enumeration.
696710
// It includes all projects fetched from the API, even those later skipped by ignore rules.
697711
totalCount := 0
698-
maxRetries := 3
712+
713+
requestOptions := []gitlab.RequestOptionFunc{gitlab.WithContext(ctx)}
699714

700715
// Pagination loop: Continue fetching pages until the API indicates there are no more.
701716
for {
702717
// Fetch a page of projects from the GitLab API using the current query options.
703-
projects, resp, err := apiClient.Projects.ListProjects(projectQueryOptions)
718+
projects, resp, err := apiClient.Projects.ListProjects(projectQueryOptions, requestOptions...)
704719
if err != nil {
705-
if errResp, ok := err.(*gitlab.ErrorResponse); ok {
706-
// check if it is a server side error and we have some retries left
707-
if errResp.Response.StatusCode >= http.StatusInternalServerError && maxRetries > 0 {
708-
maxRetries--
709-
ctx.Logger().V(2).Info("retrying API call",
710-
"retry_attempts_left", maxRetries)
711-
// delay for 2 second between retries
712-
time.Sleep(time.Second * 2)
713-
continue
714-
}
715-
}
716-
err = fmt.Errorf("received error on listing projects, you probably don't have permissions to do that: %w", err)
720+
err = fmt.Errorf("received error on listing projects, you might not have permissions to do that: %w", err)
717721
if err := reporter.UnitErr(ctx, err); err != nil {
718722
return err
719723
}
@@ -771,7 +775,10 @@ func (s *Source) getAllProjectReposV2(
771775
break
772776
}
773777
// Only update the token for the next page if we have a valid, non-empty link.
774-
projectQueryOptions.PageToken = resp.NextLink
778+
requestOptions = []gitlab.RequestOptionFunc{
779+
gitlab.WithContext(ctx),
780+
gitlab.WithKeysetPaginationParameters(resp.NextLink),
781+
}
775782
}
776783

777784
ctx.Logger().Info("Enumerated GitLab projects", "count", totalCount)

0 commit comments

Comments
 (0)