From 1e4e9147f184fd5e9cffc8832c506ce06e6923e9 Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Tue, 16 Jan 2024 21:32:59 -0500 Subject: [PATCH] Fix Gitea lang detection --- internal/remotes/gitea/gitea_api.go | 35 +++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/internal/remotes/gitea/gitea_api.go b/internal/remotes/gitea/gitea_api.go index 41aef45..b892fc1 100644 --- a/internal/remotes/gitea/gitea_api.go +++ b/internal/remotes/gitea/gitea_api.go @@ -2,6 +2,7 @@ package gitearemote import ( "fmt" + "math" "strconv" "strings" @@ -14,18 +15,19 @@ func (r *GiteaRemote) ReposToProjects(repos []*gitea.Repository) []*projects.Pro pList := make([]*projects.Project, len(repos)) for i, repo := range repos { langs, _ := r.GetRepoLanguages(repo) - path := strings.Split(repo.FullName, "/") + 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: path[len(path)-1], + Path: name, AvatarURL: repo.AvatarURL, - PathWithNamespace: strings.Join(path, "/"), + PathWithNamespace: repo.FullName, LastActivityAt: repo.Updated, Remote: r.info.Host, Languages: langs, @@ -38,20 +40,39 @@ func (r *GiteaRemote) ReposToProjects(repos []*gitea.Repository) []*projects.Pro 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) + 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 { - pLangs.AddLanguage(&projects.ProjectLanguage{Name: k, Percentage: float32(v)}) + 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