67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
package gitearemote
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
|
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
|
|
)
|
|
|
|
func (r *GiteaRemote) ReposToProjects(repos []*gitea.Repository) []*projects.Project {
|
|
pList := make([]*projects.Project, len(repos))
|
|
for i, repo := range repos {
|
|
langs, _ := r.GetRepoLanguages(repo)
|
|
path := strings.Split(repo.FullName, "/")
|
|
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: path[len(path)-1],
|
|
AvatarURL: repo.AvatarURL,
|
|
PathWithNamespace: strings.Join(path, "/"),
|
|
LastActivityAt: repo.Updated,
|
|
Remote: r.info.Host,
|
|
Languages: langs,
|
|
}
|
|
pList[i] = project
|
|
}
|
|
return pList
|
|
}
|
|
|
|
func (r *GiteaRemote) GetRepoLanguages(repo *gitea.Repository) (*projects.ProjectLanguages, error) {
|
|
pLangs := projects.NewProjectLanguages()
|
|
|
|
langs, resp, err := r.api.GetRepoLanguages(repo.Owner.Email, repo.FullName)
|
|
// TODO remove debug
|
|
fmt.Printf("Langs: %#v, Resp: %#v, Err: %s\n", langs, resp, err)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for k, v := range langs {
|
|
pLangs.AddLanguage(&projects.ProjectLanguage{Name: k, Percentage: float32(v)})
|
|
}
|
|
|
|
return pLangs, nil
|
|
}
|
|
|
|
func (r *GiteaRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
|
|
var projects int
|
|
|
|
_, resp, err := r.api.SearchRepos(gitea.SearchRepoOptions{ListOptions: gitea.ListOptions{PageSize: 1}})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return -1
|
|
}
|
|
|
|
projects, _ = strconv.Atoi(resp.Header.Get("X-Total-Count"))
|
|
return projects
|
|
}
|