diff --git a/internal/adapter/github/forge.go b/internal/adapter/github/forge.go index 4a3b53c..38e74d3 100644 --- a/internal/adapter/github/forge.go +++ b/internal/adapter/github/forge.go @@ -41,104 +41,10 @@ func (f *Forge) CreateIssue(ctx context.Context, projectID string, title string, func (f *Forge) GetAllProjects(ctx context.Context) ([]*model.Project, error) { projects := make([]*model.Project, 0) - collectUserProjects := func() error { - page := 1 - for { - repos, res, err := f.client.Repositories.ListByAuthenticatedUser(ctx, &github.RepositoryListByAuthenticatedUserOptions{ - Type: "all", - ListOptions: github.ListOptions{ - Page: page, - PerPage: 100, - }, - }) - if err != nil { - if res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized { - slog.ErrorContext(ctx, "could not retrieve user repositories", slog.Any("error", errors.WithStack(err))) - return errors.WithStack(service.ErrForgeNotAvailable) - } - - return errors.WithStack(err) - } - - for _, r := range repos { - if r.ID == nil || r.FullName == nil { - continue - } - - var projectDescription string - if r.Description != nil { - projectDescription = *r.Description - } - - projects = append(projects, &model.Project{ - ID: strconv.FormatInt(*r.ID, 10), - Name: *r.FullName, - Description: projectDescription, - }) - - } - - if res.NextPage == 0 { - return nil - } - - page = res.NextPage - } - } - - collectOrgProjects := func(owner string) error { - page := 1 - for { - repos, res, err := f.client.Repositories.ListByOrg(ctx, owner, &github.RepositoryListByOrgOptions{ - Type: "all", - ListOptions: github.ListOptions{ - Page: page, - PerPage: 100, - }, - }) - if err != nil { - if res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized { - slog.ErrorContext(ctx, "could not retrieve org repositories", slog.String("org", owner), slog.Any("error", errors.WithStack(err))) - return errors.WithStack(service.ErrForgeNotAvailable) - } - - return errors.WithStack(err) - } - - for _, r := range repos { - if r.ID == nil || r.FullName == nil { - continue - } - - var projectDescription string - if r.Description != nil { - projectDescription = *r.Description - } - - projects = append(projects, &model.Project{ - ID: strconv.FormatInt(*r.ID, 10), - Name: *r.FullName, - Description: projectDescription, - }) - - } - - if res.NextPage == 0 { - return nil - } - - page = res.NextPage - } - } - - if err := collectUserProjects(); err != nil { - return nil, errors.WithStack(err) - } - page := 1 for { - orgs, res, err := f.client.Organizations.ListOrgMemberships(ctx, &github.ListOrgMembershipsOptions{ - State: "active", + repos, res, err := f.client.Repositories.ListByAuthenticatedUser(ctx, &github.RepositoryListByAuthenticatedUserOptions{ + Type: "all", ListOptions: github.ListOptions{ Page: page, PerPage: 100, @@ -146,27 +52,37 @@ func (f *Forge) GetAllProjects(ctx context.Context) ([]*model.Project, error) { }) if err != nil { if res.StatusCode == http.StatusForbidden || res.StatusCode == http.StatusUnauthorized { - slog.ErrorContext(ctx, "could not retrieve user organizations", slog.Any("error", errors.WithStack(err))) + slog.ErrorContext(ctx, "could not retrieve user repositories", slog.Any("error", errors.WithStack(err))) return nil, errors.WithStack(service.ErrForgeNotAvailable) } return nil, errors.WithStack(err) } - for _, o := range orgs { - if err := collectOrgProjects(*o.Organization.Login); err != nil { - return nil, errors.WithStack(err) + for _, r := range repos { + if r.ID == nil || r.FullName == nil { + continue } + + var projectDescription string + if r.Description != nil { + projectDescription = *r.Description + } + + projects = append(projects, &model.Project{ + ID: strconv.FormatInt(*r.ID, 10), + Name: *r.FullName, + Description: projectDescription, + }) + } if res.NextPage == 0 { - break + return projects, nil } page = res.NextPage } - - return projects, nil } // GetFile implements port.Forge.