From 25820024cd8ef29e06ef207d8f234f95c8d1d5f7 Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Tue, 16 Jan 2024 15:46:32 -0500 Subject: [PATCH] Gitea remote support --- internal/remotes/gitea/gitea_api.go | 67 ++++++++++++-------------- internal/remotes/gitea/gitea_stream.go | 50 +++++++++++++++++++ 2 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 internal/remotes/gitea/gitea_stream.go diff --git a/internal/remotes/gitea/gitea_api.go b/internal/remotes/gitea/gitea_api.go index c9fbf0b..cdc97cc 100644 --- a/internal/remotes/gitea/gitea_api.go +++ b/internal/remotes/gitea/gitea_api.go @@ -1,16 +1,43 @@ package gitearemote import ( - "errors" "fmt" "strconv" + "strings" "code.gitea.io/sdk/gitea" - "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load" + "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects" "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" ) -const giteaReposPerPage = 20 +func (r *GiteaRemote) ReposToProjects(repos []*gitea.Repository) []*projects.Project { + pList := make([]*projects.Project, len(repos)) + for i, repo := range repos { + path := strings.Split(repo.FullName, "/") + var repoPath []string + if len(path) > 1 { + repoPath = path[:len(path)-1] + } else { + repoPath = path + } + project := &projects.Project{ + ID: int(repo.ID), + Description: repo.Description, + SSHURLToRepo: repo.SSHURL, + HTTPURLToRepo: repo.CloneURL, + WebURL: repo.HTMLURL, + Name: repo.Name, + NameWithNamespace: repo.FullName, + Path: strings.Join(repoPath, "/"), + AvatarURL: repo.AvatarURL, + PathWithNamespace: strings.Join(path, "/"), + LastActivityAt: repo.Updated, + Remote: r.info.Host, + } + pList[i] = project + } + return pList +} func (r *GiteaRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int { var projects int @@ -24,37 +51,3 @@ func (r *GiteaRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int { projects, _ = strconv.Atoi(resp.Header.Get("X-Total-Count")) return projects } - -func (r *GiteaRemote) StreamProjects(pi *load.ProgressInfo, opts *remote.RemoteQueryOpts) { - defer close(pi.ProgressChan) - defer close(pi.ProjectsChan) - - // Get projects. TODO support concurrency - giteaListOpts := gitea.ListOptions{Page: 1, PageSize: giteaReposPerPage} - for { - repos, resp, err := r.api.ListMyRepos(gitea.ListReposOptions{ListOptions: giteaListOpts}) - - fmt.Printf("Gitea PrevPage %d, NextPage %d, Pages %d\n", resp.PrevPage, resp.NextPage, resp.LastPage) - - if err != nil { - pi.ErrorChan <- err - break - } else if len(repos) < 1 { - pi.ErrorChan <- errors.New("No gitea repos found") - break - } - - // TODO finish implementing project load support - for _, repo := range repos { - fmt.Printf("Repo: %#v\n", repo) - } - - if resp.LastPage == resp.NextPage { - break - } - - giteaListOpts.Page = resp.NextPage - } - - pi.DoneChan <- nil -} diff --git a/internal/remotes/gitea/gitea_stream.go b/internal/remotes/gitea/gitea_stream.go new file mode 100644 index 0000000..fd26b44 --- /dev/null +++ b/internal/remotes/gitea/gitea_stream.go @@ -0,0 +1,50 @@ +package gitearemote + +import ( + "errors" + "fmt" + + "code.gitea.io/sdk/gitea" + "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/load" + "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote" +) + +const giteaReposPerPage = 20 + +func (r *GiteaRemote) StreamProjects(pi *load.ProgressInfo, opts *remote.RemoteQueryOpts) { + defer close(pi.ProgressChan) + defer close(pi.ProjectsChan) + + // Get projects. TODO support concurrency + giteaListOpts := gitea.ListOptions{Page: 1, PageSize: giteaReposPerPage} + for { + repos, resp, err := r.api.ListMyRepos(gitea.ListReposOptions{ListOptions: giteaListOpts}) + + fmt.Printf("Gitea PrevPage %d, NextPage %d, Pages %d\n", resp.PrevPage, resp.NextPage, resp.LastPage) + + if err != nil { + pi.ErrorChan <- err + break + } else if len(repos) < 1 { + pi.ErrorChan <- errors.New("No gitea repos found") + break + } + + // Write updates to channels + pi.ProjectsChan <- r.ReposToProjects(repos) + pi.ProgressChan <- load.Progress{ + Page: giteaListOpts.Page, + Pages: resp.LastPage, + Projects: len(repos), + TotalProjects: pi.NumProjects, + } + + if resp.LastPage == resp.NextPage { + break + } + + giteaListOpts.Page = resp.NextPage + } + + pi.DoneChan <- nil +}