package gitearemote import ( "fmt" "math" "strconv" "strings" "code.gitea.io/sdk/gitea" "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects" "gitea.libretechconsulting.com/rmcguire/git-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) owner, name := GetOwnerRepo(repo.FullName) project := &projects.Project{ ID: int(repo.ID), Owner: owner, Description: repo.Description, SSHURLToRepo: repo.SSHURL, HTTPURLToRepo: repo.CloneURL, WebURL: repo.HTMLURL, Name: repo.Name, NameWithNamespace: repo.FullName, Path: name, AvatarURL: repo.AvatarURL, PathWithNamespace: repo.FullName, 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() owner, repoName := GetOwnerRepo(repo.FullName) langs, _, err := r.api.GetRepoLanguages(owner, repoName) if err != nil { return nil, err } var totalLines int64 for _, v := range langs { totalLines += v } for k, v := range langs { pcnt := float64(v) / float64(totalLines) * 100 pcnt = math.Round(pcnt*100) / 100 pLangs.AddLanguage(&projects.ProjectLanguage{Name: k, Percentage: float32(pcnt)}) } return pLangs, nil } // Simply returns the owner (user or org) of a repo // and its name in strings func GetOwnerRepo(fullName string) (string, string) { var owner, repo string parts := strings.Split(fullName, "/") if len(parts) > 1 { owner = parts[0] repo = parts[1] } return owner, repo } func (r *GiteaRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int { var projects int r.setClient() _, 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 }