gitforgefs/gitlab/client.go

142 lines
3.2 KiB
Go
Raw Normal View History

2020-12-27 03:30:19 +00:00
package gitlab
import (
"fmt"
"github.com/xanzy/go-gitlab"
)
type GroupFetcher interface {
FetchGroup(gid int) (*Group, error)
FetchGroupContent(group *Group) (*GroupContent, error)
2020-12-27 03:30:19 +00:00
}
type GroupContent struct {
Groups map[string]*Group
Repositories map[string]*Repository
}
type Group struct {
ID int
Name string
Path string
Content *GroupContent
2020-12-27 03:30:19 +00:00
}
type Repository struct {
ID int
Name string
Path string
CloneURL string
}
2020-12-29 02:37:18 +00:00
type GitlabClientParam struct {
2020-12-27 03:30:19 +00:00
}
2020-12-29 02:37:18 +00:00
type gitlabClient struct {
GitlabClientParam
client *gitlab.Client
}
func NewClient(gitlabUrl string, gitlabToken string, p GitlabClientParam) (*gitlabClient, error) {
2020-12-27 03:30:19 +00:00
client, err := gitlab.NewClient(
gitlabToken,
gitlab.WithBaseURL(gitlabUrl),
)
if err != nil {
2020-12-29 02:37:18 +00:00
return nil, fmt.Errorf("failed to create gitlab client: %v", err)
2020-12-27 03:30:19 +00:00
}
2020-12-29 02:37:18 +00:00
gitlabClient := &gitlabClient{
client: client,
2020-12-27 03:30:19 +00:00
}
return gitlabClient, nil
}
func NewRepositoryFromGitlabProject(project *gitlab.Project) Repository {
2020-12-27 03:30:19 +00:00
// https://godoc.org/github.com/xanzy/go-gitlab#Project
return Repository{
ID: project.ID,
Name: project.Name,
Path: project.Path,
CloneURL: project.HTTPURLToRepo,
// CloneUrl: project.SSHURLToRepo,
}
}
func NewGroupFromGitlabGroup(group *gitlab.Group) Group {
// https://godoc.org/github.com/xanzy/go-gitlab#Group
return Group{
ID: group.ID,
Name: group.Name,
Path: group.Path,
}
}
2020-12-29 02:37:18 +00:00
func (c gitlabClient) FetchGroup(gid int) (*Group, error) {
gitlabGroup, _, err := c.client.Groups.GetGroup(gid)
if err != nil {
2020-12-29 02:37:18 +00:00
return nil, fmt.Errorf("failed to fetch group with id %v: %v\n", gid, err)
}
group := NewGroupFromGitlabGroup(gitlabGroup)
return &group, nil
}
2020-12-27 03:30:19 +00:00
2020-12-29 02:37:18 +00:00
func (c gitlabClient) FetchGroupContent(group *Group) (*GroupContent, error) {
if group.Content != nil {
return group.Content, nil
}
content := &GroupContent{
Groups: map[string]*Group{},
Repositories: map[string]*Repository{},
}
// List subgroups in path
ListGroupsOpt := &gitlab.ListSubgroupsOptions{
2020-12-27 03:30:19 +00:00
ListOptions: gitlab.ListOptions{
2020-12-29 02:37:18 +00:00
Page: 1,
PerPage: 1000,
2020-12-27 03:30:19 +00:00
}}
for {
2020-12-29 02:37:18 +00:00
gitlabGroups, response, err := c.client.Groups.ListSubgroups(group.ID, ListGroupsOpt)
2020-12-27 03:30:19 +00:00
if err != nil {
2020-12-29 02:37:18 +00:00
return nil, fmt.Errorf("failed to fetch groups in gitlab: %v", err)
2020-12-27 03:30:19 +00:00
}
for _, gitlabGroup := range gitlabGroups {
group := NewGroupFromGitlabGroup(gitlabGroup)
content.Groups[group.Path] = &group
2020-12-27 03:30:19 +00:00
}
if response.CurrentPage >= response.TotalPages {
break
}
// Get the next page
ListGroupsOpt.Page = response.NextPage
2020-12-27 03:30:19 +00:00
}
// List repositories in path
listProjectOpt := &gitlab.ListGroupProjectsOptions{
2020-12-27 03:30:19 +00:00
ListOptions: gitlab.ListOptions{
2020-12-29 02:37:18 +00:00
Page: 1,
PerPage: 1000,
2020-12-27 03:30:19 +00:00
}}
for {
2020-12-29 02:37:18 +00:00
gitlabProjects, response, err := c.client.Groups.ListGroupProjects(group.ID, listProjectOpt)
2020-12-27 03:30:19 +00:00
if err != nil {
2020-12-29 02:37:18 +00:00
return nil, fmt.Errorf("failed to fetch projects in gitlab: %v", err)
2020-12-27 03:30:19 +00:00
}
for _, gitlabProject := range gitlabProjects {
repository := NewRepositoryFromGitlabProject(gitlabProject)
content.Repositories[repository.Path] = &repository
2020-12-27 03:30:19 +00:00
}
if response.CurrentPage >= response.TotalPages {
break
}
// Get the next page
listProjectOpt.Page = response.NextPage
2020-12-27 03:30:19 +00:00
}
group.Content = content
2020-12-27 03:30:19 +00:00
return content, nil
}