Skip to content

Commit

Permalink
internal/gitlab: fix GroupSearch() logic
Browse files Browse the repository at this point in the history
The group search logic didn't consider the fact that the GitLab API
returns an alphabetical ordered list of groups within a certain group,
thus when multiple subgroups were nested the value groups[0] and list[0]
used in the code weren't always the expected ones.

With that, instead of looking up for the first group, we look for the
last one, meaning that all subgroups found in user's namespace or
wherever he has access to, is returned from the API. The match then
happens with the group full path or full name, which matches first.

In case the user has access to multiple groups with ambiguous names,
all possible paths are shown and lab's exits.

Signed-off-by: Lucas Zampieri <lzampier@redhat.com>
Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Feb 8, 2022
1 parent 7368ada commit fb2f180
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
5 changes: 3 additions & 2 deletions cmd/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,16 @@ var projectCreateCmd = &cobra.Command{
}

func determineNamespacePath(args []string, name string) (string, string) {
var path string
if len(args) > 0 {
ps := strings.Split(args[0], "/")
if len(ps) == 1 {
return "", ps[0]
}
return strings.Join(ps[:len(ps)-1], "/"), ps[len(ps)-1]
}
if path == "" && name == "" && git.InsideGitRepo() {

var path string
if name == "" && git.InsideGitRepo() {
wd, err := git.WorkingDir()
if err != nil {
log.Fatal(err)
Expand Down
37 changes: 17 additions & 20 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ var (
// ErrActionRepeated is returned when a GitLab action is executed again. For example
// this can be returned when an MR is approved twice.
ErrActionRepeated = errors.New("GitLab action repeated")
// ErrGroupNotFound is returned when a GitLab group cannot be found.
ErrGroupNotFound = errors.New("GitLab group not found")
// ErrNotModified is returned when adding an already existing item to a Todo list
ErrNotModified = errors.New("Not Modified")
// ErrProjectNotFound is returned when a GitLab project cannot be found.
Expand Down Expand Up @@ -1119,38 +1117,37 @@ func (s jobSorter) Less(i, j int) bool {

// GroupSearch searches for a namespace on GitLab
func GroupSearch(query string) (*gitlab.Group, error) {
query = strings.TrimSpace(query)
if query == "" {
return nil, errors.New("query is empty")
return nil, errors.New("invalid group query")
}

groups := strings.Split(query, "/")
list, _, err := lab.Groups.SearchGroup(groups[0])
list, _, err := lab.Groups.SearchGroup(groups[len(groups)-1])
if err != nil {
return nil, err
}
// SearchGroup doesn't return error if group isn't found. We need to do
// it ourselves.
if len(list) == 0 {
return nil, ErrGroupNotFound
return nil, fmt.Errorf("group '%s' not found", query)
}
// if we found a group and we aren't looking for a subgroup
if len(list) > 0 && len(groups) == 1 {
if len(list) == 1 {
return list[0], nil
}
list, _, err = lab.Groups.ListDescendantGroups(list[0].ID, &gitlab.ListDescendantGroupsOptions{
Search: gitlab.String(groups[len(groups)-1]),
})
if err != nil {
return nil, err
}

for _, g := range list {
fmt.Println(g.FullPath)
if g.FullPath == query {
return g, nil
for _, group := range list {
fullName := strings.TrimSpace(group.FullName)
if group.FullPath == query || fullName == query {
return group, nil
}
}

return nil, errors.Errorf("Group '%s' not found", query)
msg := fmt.Sprintf("found multiple groups with ambiguous name:\n")
for _, group := range list {
msg += fmt.Sprintf("\t%s\n", group.FullPath)
}
msg += fmt.Sprintf("use one of the above path options\n")

return nil, errors.New(msg)
}

// CIJobs returns a list of jobs in the pipeline with given id.
Expand Down

0 comments on commit fb2f180

Please sign in to comment.