Add GitHub support
This commit is contained in:
72
internal/remotes/github/github_api.go
Normal file
72
internal/remotes/github/github_api.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package githubremote
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/google/go-github/v58/github"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/projects"
|
||||
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user