git-project-manager/internal/remotes/github/github_stream.go

63 lines
1.4 KiB
Go
Raw Normal View History

2024-01-18 16:09:54 +00:00
package githubremote
import (
"errors"
"github.com/google/go-github/v58/github"
2024-12-19 19:55:49 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/load"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/remote"
2024-01-18 16:09:54 +00:00
)
const githubReposPerPage = 20
func (r *GithubRemote) StreamProjects(pi *load.ProgressInfo, opts *remote.RemoteQueryOpts) {
defer close(pi.ProgressChan)
defer close(pi.ProjectsChan)
// Get projects. TODO support concurrency
githubListOpts := github.ListOptions{
Page: 1,
PerPage: githubReposPerPage,
}
var githubRepoType string
if opts.OwnerOnly {
githubRepoType = "owner"
} else {
githubRepoType = "all"
}
for {
repos, resp, err := r.api.Repositories.ListByAuthenticatedUser(
r.info.Context(),
&github.RepositoryListByAuthenticatedUserOptions{
Type: githubRepoType,
ListOptions: githubListOpts,
},
)
if err != nil {
pi.ErrorChan <- err
break
} else if len(repos) < 1 {
pi.ErrorChan <- errors.New("No github repos found")
break
}
// Write updates to channels
pi.ProjectsChan <- r.ReposToProjects(repos)
pi.ProgressChan <- load.Progress{
Page: githubListOpts.Page,
Pages: resp.LastPage,
Projects: len(repos),
TotalProjects: pi.NumProjects,
}
if resp.NextPage == 0 {
break
}
githubListOpts.Page = resp.NextPage
}
pi.DoneChan <- nil
}