Skip to content

Commit

Permalink
gitlab: check SearchGroup list length
Browse files Browse the repository at this point in the history
go-gitlab's SearchGroup() doesn't return error in case a group isn't find,
it only returns an empty list. With that, we must make sure we're not
moving that list around.

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Dec 22, 2020
1 parent 45198d3 commit dd21120
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 1 addition & 2 deletions cmd/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ lab project create -g mygroup myproject # mygroup/myproject named myproject`,
log.Fatal("path or name must be set")
}
if g != "" && group != "" {

log.Fatalf("group can be passed by flag or in path not both\n%s", labUsageFormat(cmd))
log.Fatalf("group can be passed by flag or in path, but not both\n%s", labUsageFormat(cmd))
}
if g != "" {
group = g
Expand Down
13 changes: 11 additions & 2 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ import (
"github.com/zaquestion/lab/internal/git"
)

// ErrProjectNotFound is returned when a GitLab project cannot be found.
var ErrProjectNotFound = errors.New("gitlab project not found, verify you have access to the requested resource")
var (
// ErrProjectNotFound is returned when a GitLab project cannot be found.
ErrProjectNotFound = errors.New("gitlab project not found, verify you have access to the requested resource")
// ErrGroupNotFound is returned when a GitLab group cannot be found.
ErrGroupNotFound = errors.New("gitlab group not found")
)

var (
lab *gitlab.Client
Expand Down Expand Up @@ -946,6 +950,11 @@ func GroupSearch(query string) (*gitlab.Group, error) {
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
}
// if we found a group and we aren't looking for a subgroup
if len(list) > 0 && len(groups) == 1 {
return list[0], nil
Expand Down

0 comments on commit dd21120

Please sign in to comment.