Skip to content

Commit

Permalink
Ensure correct group is gotten from gitlab search
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
  • Loading branch information
somtochiama committed Jan 14, 2021
1 parent 787b365 commit 095cc0e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 36 deletions.
1 change: 0 additions & 1 deletion git/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import "context"
// Provider is the interface that a git provider should implement
type Provider interface {
CreateRepository(ctx context.Context, r *Repository) (bool, error)
GetRepositoryOwner(ctx context.Context, owner string) (string, error)
DeleteRepository(ctx context.Context, r *Repository) error
AddTeam(ctx context.Context, r *Repository, name, permission string) (bool, error)
AddDeployKey(ctx context.Context, r *Repository, key, keyName string) (bool, error)
Expand Down
6 changes: 0 additions & 6 deletions git/provider_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ func (p *GithubProvider) CreateRepository(ctx context.Context, r *Repository) (b
return false, nil
}

// GetRepositoryOwner returns the actual path owner. This is need for Gitlab where the name of a group might differ
// from its path
func (p *GithubProvider) GetRepositoryOwner(ctx context.Context, token string, owner string) (string, error) {
return owner, nil
}

// AddTeam returns false if the team is already assigned to the repository
func (p *GithubProvider) AddTeam(ctx context.Context, r *Repository, name, permission string) (bool, error) {
gh, err := p.newClient(r)
Expand Down
59 changes: 30 additions & 29 deletions git/provider_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,6 @@ func (p *GitLabProvider) CreateRepository(ctx context.Context, r *Repository) (b
return true, nil
}

// GetRepositoryOwner returns the actual path owner. This is need for Gitlab where the name of a group might differ
// from its path
func (p *GitLabProvider) GetRepositoryOwner(ctx context.Context, token string, owner string) (string, error) {
gl, err := gitlab.NewClient(token)
if err != nil {
return "", fmt.Errorf("client error: %w", err)
}

groupName := strings.Split(owner, "/")[0]
lgo := &gitlab.ListGroupsOptions{
Search: gitlab.String(groupName),
MinAccessLevel: gitlab.AccessLevel(gitlab.GuestPermissions),
}
groups, _, err := gl.Groups.ListGroups(lgo, gitlab.WithContext(ctx))
if err != nil {
return "", fmt.Errorf("failed to list groups, error: %w", err)
}

if len(groups) == 0 {
return "", fmt.Errorf("failed to find group named '%s'", groupName)
}

return groups[0].Path, nil
}

// AddTeam returns false if the team is already assigned to the repository
func (p *GitLabProvider) AddTeam(ctx context.Context, r *Repository, name, permission string) (bool, error) {
return false, nil
Expand Down Expand Up @@ -194,15 +169,20 @@ func (p *GitLabProvider) getProjects(ctx context.Context, gl *gitlab.Client, r *
Search: gitlab.String(groupAndSubGroups[0]),
MinAccessLevel: gitlab.AccessLevel(gitlab.GuestPermissions),
}

groups, _, err := gl.Groups.ListGroups(lgo, gitlab.WithContext(ctx))
if err != nil {
return nil, nil, fmt.Errorf("failed to list groups, error: %w", err)
}

if len(groups) == 0 {
group := findGroupByName(groups, groupAndSubGroups[0])
if len(groups) == 0 || group == nil {
return nil, nil, fmt.Errorf("failed to find group named '%s'", r.Owner)
}
gid = &groups[0].ID
gid = &group.ID

groupAndSubGroups[0] = group.Path
r.Owner = strings.Join(groupAndSubGroups, "/")

if len(groupAndSubGroups) > 1 {
lastSubGroup := groupAndSubGroups[len(groupAndSubGroups)-1]
Expand All @@ -211,15 +191,16 @@ func (p *GitLabProvider) getProjects(ctx context.Context, gl *gitlab.Client, r *
MinAccessLevel: gitlab.AccessLevel(gitlab.GuestPermissions),
}
subGroups, _, err := gl.Groups.ListDescendantGroups(*gid, ldgo, gitlab.WithContext(ctx))
subGroup := findGroupByName(subGroups, lastSubGroup)
if err != nil {
return nil, nil, fmt.Errorf("failed to list subgroups, error: %w", err)
}

if len(subGroups) == 0 {
if len(subGroups) == 0 || subGroup == nil {
return nil, nil, fmt.Errorf("failed to list subgroups named '%s'", lastSubGroup)
}

gid = &subGroups[0].ID
gid = &subGroup.ID
}

lpo := &gitlab.ListGroupProjectsOptions{
Expand Down Expand Up @@ -255,3 +236,23 @@ func (p *GitLabProvider) getProjects(ctx context.Context, gl *gitlab.Client, r *

return gid, projects, nil
}

func findGroupByName(groups []*gitlab.Group, name string) *gitlab.Group {
for _, group := range groups {
if group.Name == name {
return group
}
}

return nil
}

func findGroupByPath(groups []*gitlab.Group, path string) *gitlab.Group {
for _, group := range groups {
if group.Path == path {
return group
}
}

return nil
}

0 comments on commit 095cc0e

Please sign in to comment.