Use context in requests

This commit is contained in:
Ryan McGuire 2024-01-16 21:32:46 -05:00
parent 868183f012
commit 7e2ff1198b
2 changed files with 29 additions and 12 deletions

View File

@ -1,14 +1,33 @@
package gitlabremote package gitlabremote
import ( import (
"time"
"github.com/pterm/pterm" "github.com/pterm/pterm"
"github.com/xanzy/go-gitlab" "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/projects"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" "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) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -20,7 +39,7 @@ func (r *GitlabRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
listOpts.PerPage = 1 listOpts.PerPage = 1
listOpts.Page = 1 listOpts.Page = 1
listOpts.Simple = gitlab.Ptr[bool](true) 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 { if err != nil {
pterm.Error.Printfln("Failed getting number of GitLab projects: %s", err) pterm.Error.Printfln("Failed getting number of GitLab projects: %s", err)
return -1 return -1
@ -35,7 +54,7 @@ func (r *GitlabRemote) ListProjects(opts *gitlab.ListProjectsOptions) (
pList := make([]*projects.Project, 0) pList := make([]*projects.Project, 0)
projects, resp, err := r.api.Projects.ListProjects( projects, resp, err := r.api.Projects.ListProjects(
opts, opts,
gitlab.WithContext(r.info.Ctx), r.GetDefaultRequestOptions()...,
) )
if err == nil { if err == nil {
pList = append(pList, r.handleProjects(projects)...) 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 // A nil return indicates an API error or GitLab doesn't know what
// language the project uses. // language the project uses.
func (r *GitlabRemote) GetProjectLanguages(project *gitlab.Project) *projects.ProjectLanguages { 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 { if e != nil {
pterm.Error.Printfln("Failed requesting project languages: %s", e.Error()) pterm.Error.Printfln("Failed requesting project languages: %s", e.Error())
return nil return nil
@ -97,3 +116,9 @@ func (r *GitlabRemote) GetProjectLanguages(project *gitlab.Project) *projects.Pr
return &pLangs return &pLangs
} }
func (r *GitlabRemote) GetDefaultRequestOptions() []gitlab.RequestOptionFunc {
requestOpts := make([]gitlab.RequestOptionFunc, 1)
requestOpts[0] = gitlab.WithContext(r.GetInfo().Ctx)
return requestOpts
}

View File

@ -17,14 +17,6 @@ const (
projectsPerGoroutine = 200 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) { func (r *GitlabRemote) StreamProjects(pi *load.ProgressInfo, opts *remote.RemoteQueryOpts) {
defer close(pi.ProgressChan) defer close(pi.ProgressChan)
defer close(pi.ProjectsChan) defer close(pi.ProjectsChan)