diff --git a/internal/remotes/gitlab/gitlab_api.go b/internal/remotes/gitlab/gitlab_api.go index c929f55..16b80cd 100644 --- a/internal/remotes/gitlab/gitlab_api.go +++ b/internal/remotes/gitlab/gitlab_api.go @@ -1,14 +1,33 @@ package gitlabremote import ( + "time" + "github.com/pterm/pterm" "github.com/xanzy/go-gitlab" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" ) +var DefaultListOpts = &gitlab.ListProjectsOptions{ + ListOptions: gitlab.ListOptions{ + PerPage: projectsPerPage, + Page: 1, + }, + Archived: gitlab.Ptr[bool](false), +} + +var ( + defApiWaitMin time.Duration = 2 * time.Second + defApiWaitMax time.Duration = 5 * time.Second +) + func NewGitlabApi(info *remote.RemoteInfo) (*gitlab.Client, error) { - client, err := gitlab.NewClient(info.Token, gitlab.WithBaseURL(info.Host)) + client, err := gitlab.NewClient( + info.Token, + gitlab.WithBaseURL(info.Host), + gitlab.WithCustomRetryWaitMinMax(defApiWaitMin, defApiWaitMax), + ) if err != nil { return nil, err } @@ -20,7 +39,7 @@ func (r *GitlabRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int { listOpts.PerPage = 1 listOpts.Page = 1 listOpts.Simple = gitlab.Ptr[bool](true) - _, resp, err := r.api.Projects.ListProjects(&listOpts) + _, resp, err := r.api.Projects.ListProjects(&listOpts, r.GetDefaultRequestOptions()...) if err != nil { pterm.Error.Printfln("Failed getting number of GitLab projects: %s", err) return -1 @@ -35,7 +54,7 @@ func (r *GitlabRemote) ListProjects(opts *gitlab.ListProjectsOptions) ( pList := make([]*projects.Project, 0) projects, resp, err := r.api.Projects.ListProjects( opts, - gitlab.WithContext(r.info.Ctx), + r.GetDefaultRequestOptions()..., ) if err == nil { pList = append(pList, r.handleProjects(projects)...) @@ -77,7 +96,7 @@ func (r *GitlabRemote) handleProjects(gitProjects []*gitlab.Project) []*projects // A nil return indicates an API error or GitLab doesn't know what // language the project uses. func (r *GitlabRemote) GetProjectLanguages(project *gitlab.Project) *projects.ProjectLanguages { - l, _, e := r.api.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(r.info.Ctx)) + l, _, e := r.api.Projects.GetProjectLanguages(project.ID, r.GetDefaultRequestOptions()...) if e != nil { pterm.Error.Printfln("Failed requesting project languages: %s", e.Error()) return nil @@ -97,3 +116,9 @@ func (r *GitlabRemote) GetProjectLanguages(project *gitlab.Project) *projects.Pr return &pLangs } + +func (r *GitlabRemote) GetDefaultRequestOptions() []gitlab.RequestOptionFunc { + requestOpts := make([]gitlab.RequestOptionFunc, 1) + requestOpts[0] = gitlab.WithContext(r.GetInfo().Ctx) + return requestOpts +} diff --git a/internal/remotes/gitlab/gitlab_strean.go b/internal/remotes/gitlab/gitlab_strean.go index 587a311..43b730d 100644 --- a/internal/remotes/gitlab/gitlab_strean.go +++ b/internal/remotes/gitlab/gitlab_strean.go @@ -17,14 +17,6 @@ const ( projectsPerGoroutine = 200 ) -var DefaultListOpts = &gitlab.ListProjectsOptions{ - ListOptions: gitlab.ListOptions{ - PerPage: projectsPerPage, - Page: 1, - }, - Archived: gitlab.Ptr[bool](false), -} - func (r *GitlabRemote) StreamProjects(pi *load.ProgressInfo, opts *remote.RemoteQueryOpts) { defer close(pi.ProgressChan) defer close(pi.ProjectsChan)