Fix Gitea lang detection

This commit is contained in:
Ryan McGuire 2024-01-16 21:32:59 -05:00
parent 7e2ff1198b
commit 1e4e9147f1

View File

@ -2,6 +2,7 @@ package gitearemote
import ( import (
"fmt" "fmt"
"math"
"strconv" "strconv"
"strings" "strings"
@ -14,18 +15,19 @@ func (r *GiteaRemote) ReposToProjects(repos []*gitea.Repository) []*projects.Pro
pList := make([]*projects.Project, len(repos)) pList := make([]*projects.Project, len(repos))
for i, repo := range repos { for i, repo := range repos {
langs, _ := r.GetRepoLanguages(repo) langs, _ := r.GetRepoLanguages(repo)
path := strings.Split(repo.FullName, "/") owner, name := GetOwnerRepo(repo.FullName)
project := &projects.Project{ project := &projects.Project{
ID: int(repo.ID), ID: int(repo.ID),
Owner: owner,
Description: repo.Description, Description: repo.Description,
SSHURLToRepo: repo.SSHURL, SSHURLToRepo: repo.SSHURL,
HTTPURLToRepo: repo.CloneURL, HTTPURLToRepo: repo.CloneURL,
WebURL: repo.HTMLURL, WebURL: repo.HTMLURL,
Name: repo.Name, Name: repo.Name,
NameWithNamespace: repo.FullName, NameWithNamespace: repo.FullName,
Path: path[len(path)-1], Path: name,
AvatarURL: repo.AvatarURL, AvatarURL: repo.AvatarURL,
PathWithNamespace: strings.Join(path, "/"), PathWithNamespace: repo.FullName,
LastActivityAt: repo.Updated, LastActivityAt: repo.Updated,
Remote: r.info.Host, Remote: r.info.Host,
Languages: langs, 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) { func (r *GiteaRemote) GetRepoLanguages(repo *gitea.Repository) (*projects.ProjectLanguages, error) {
pLangs := projects.NewProjectLanguages() pLangs := projects.NewProjectLanguages()
langs, resp, err := r.api.GetRepoLanguages(repo.Owner.Email, repo.FullName) owner, repoName := GetOwnerRepo(repo.FullName)
// TODO remove debug langs, _, err := r.api.GetRepoLanguages(owner, repoName)
fmt.Printf("Langs: %#v, Resp: %#v, Err: %s\n", langs, resp, err)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var totalLines int64
for _, v := range langs {
totalLines += v
}
for k, v := range langs { 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 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 { func (r *GiteaRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
var projects int var projects int