git-project-manager/internal/remotes/gitea/gitea_api.go

89 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-01-16 18:23:38 +00:00
package gitearemote
import (
"fmt"
2024-01-17 02:32:59 +00:00
"math"
2024-01-16 19:26:56 +00:00
"strconv"
2024-01-16 20:46:32 +00:00
"strings"
2024-01-16 18:23:38 +00:00
"code.gitea.io/sdk/gitea"
2024-12-19 19:55:49 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/projects"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/remote"
2024-01-16 18:23:38 +00:00
)
2024-01-16 20:46:32 +00:00
func (r *GiteaRemote) ReposToProjects(repos []*gitea.Repository) []*projects.Project {
pList := make([]*projects.Project, len(repos))
for i, repo := range repos {
2024-01-16 21:14:53 +00:00
langs, _ := r.GetRepoLanguages(repo)
2024-01-17 02:32:59 +00:00
owner, name := GetOwnerRepo(repo.FullName)
2024-01-16 20:46:32 +00:00
project := &projects.Project{
ID: int(repo.ID),
2024-01-17 02:32:59 +00:00
Owner: owner,
2024-01-16 20:46:32 +00:00
Description: repo.Description,
SSHURLToRepo: repo.SSHURL,
HTTPURLToRepo: repo.CloneURL,
WebURL: repo.HTMLURL,
Name: repo.Name,
NameWithNamespace: repo.FullName,
2024-01-17 02:32:59 +00:00
Path: name,
2024-01-16 20:46:32 +00:00
AvatarURL: repo.AvatarURL,
2024-01-17 02:32:59 +00:00
PathWithNamespace: repo.FullName,
2024-01-16 20:46:32 +00:00
LastActivityAt: repo.Updated,
Remote: r.info.Host,
2024-01-16 21:14:53 +00:00
Languages: langs,
2024-01-16 20:46:32 +00:00
}
pList[i] = project
}
return pList
}
2024-01-16 18:23:38 +00:00
2024-01-16 21:14:53 +00:00
func (r *GiteaRemote) GetRepoLanguages(repo *gitea.Repository) (*projects.ProjectLanguages, error) {
pLangs := projects.NewProjectLanguages()
2024-01-17 02:32:59 +00:00
owner, repoName := GetOwnerRepo(repo.FullName)
langs, _, err := r.api.GetRepoLanguages(owner, repoName)
2024-01-16 21:14:53 +00:00
if err != nil {
return nil, err
}
2024-01-17 02:32:59 +00:00
var totalLines int64
for _, v := range langs {
totalLines += v
}
2024-01-16 21:14:53 +00:00
for k, v := range langs {
2024-01-17 02:32:59 +00:00
pcnt := float64(v) / float64(totalLines) * 100
pcnt = math.Round(pcnt*100) / 100
pLangs.AddLanguage(&projects.ProjectLanguage{Name: k, Percentage: float32(pcnt)})
2024-01-16 21:14:53 +00:00
}
return pLangs, nil
}
2024-01-17 02:32:59 +00:00
// 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
}
2024-01-16 18:23:38 +00:00
func (r *GiteaRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
var projects int
2024-03-06 21:21:52 +00:00
r.setClient()
2024-01-16 19:32:12 +00:00
2024-01-16 18:23:38 +00:00
_, resp, err := r.api.SearchRepos(gitea.SearchRepoOptions{ListOptions: gitea.ListOptions{PageSize: 1}})
if err != nil {
2024-01-16 19:26:56 +00:00
fmt.Println(err)
2024-01-16 18:23:38 +00:00
return -1
}
2024-01-16 19:26:56 +00:00
projects, _ = strconv.Atoi(resp.Header.Get("X-Total-Count"))
2024-01-16 18:23:38 +00:00
return projects
}