diff --git a/git/provider.go b/git/provider.go index 8108f7da..22acc693 100644 --- a/git/provider.go +++ b/git/provider.go @@ -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) diff --git a/git/provider_github.go b/git/provider_github.go index 87315bdd..a5b317d6 100644 --- a/git/provider_github.go +++ b/git/provider_github.go @@ -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) diff --git a/git/provider_gitlab.go b/git/provider_gitlab.go index c28200ba..ef7ad341 100644 --- a/git/provider_gitlab.go +++ b/git/provider_gitlab.go @@ -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 @@ -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] @@ -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{ @@ -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 +}