git-project-manager/internal/remotes/github/github_api.go

73 lines
2.1 KiB
Go
Raw Normal View History

2024-01-18 16:09:54 +00:00
package githubremote
import (
"math"
"github.com/google/go-github/v58/github"
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-18 16:09:54 +00:00
)
func (r *GithubRemote) GetNumProjects(opts *remote.RemoteQueryOpts) int {
var projects int
if opts.OwnerOnly {
projects = int(r.user.GetOwnedPrivateRepos())
} else {
projects += int(r.user.GetTotalPrivateRepos())
projects += r.user.GetPublicRepos()
}
return projects
}
func (r *GithubRemote) ReposToProjects(repos []*github.Repository) []*projects.Project {
pList := make([]*projects.Project, len(repos))
for i, repo := range repos {
var ownerName, avatar string
owner := repo.GetOwner()
if owner != nil {
ownerName = owner.GetName()
avatar = owner.GetAvatarURL()
}
// owner, name := GetOwnerRepo(repo.FullName)
project := &projects.Project{
ID: int(repo.GetID()),
Owner: ownerName,
Description: repo.GetDescription(),
SSHURLToRepo: repo.GetSSHURL(),
HTTPURLToRepo: repo.GetCloneURL(),
WebURL: repo.GetHTMLURL(),
Name: repo.GetName(),
NameWithNamespace: repo.GetFullName(),
Path: repo.GetName(),
AvatarURL: avatar,
PathWithNamespace: repo.GetFullName(),
LastActivityAt: repo.GetPushedAt().Time,
Remote: r.info.Host,
Languages: r.GetRepoLangs(repo),
}
pList[i] = project
}
return pList
}
func (r *GithubRemote) GetRepoLangs(repo *github.Repository) *projects.ProjectLanguages {
languages := projects.NewProjectLanguages()
langs, _, err := r.api.Repositories.ListLanguages(r.info.Context(), r.user.GetName(), repo.GetName())
if err != nil {
var ttlLines int
for _, lines := range langs {
ttlLines += lines
}
for l, n := range langs {
pcnt := float64(n) / float64(ttlLines) * 100
pcnt = math.Round(pcnt*100) / 100
languages.AddLanguage(&projects.ProjectLanguage{
Name: l,
Percentage: float32(pcnt),
})
}
}
return languages
}