Skip to content

Commit

Permalink
(project create) support creates in subgroups and nested subgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
zaquestion authored and bmeneg committed Dec 22, 2020
1 parent 22b09ca commit 45198d3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
9 changes: 2 additions & 7 deletions cmd/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,11 @@ lab project create -g mygroup myproject # mygroup/myproject named myproject`,

var namespaceID *int
if group != "" {
list, err := lab.GroupSearch(group)
groupObj, err := lab.GroupSearch(group)
if err != nil {
log.Fatal(err)
}

if len(list) == 0 {
log.Fatalf("no namespace found with such name: %s", group)
}

namespaceID = &list[0].ID
namespaceID = &groupObj.ID
}

// set the default visibility
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad/go.mod h1:Hy8o65+MXnS6EwGElrSRjUzQDLXreJlzYLlWiHtt8hM=
github.com/xanzy/go-gitlab v0.33.1-0.20200713191942-71ea998bed24 h1:WmX8fdQSu32qQpsPZ6/h90HK930NhUmoh+yLDItvmYw=
github.com/xanzy/go-gitlab v0.33.1-0.20200713191942-71ea998bed24/go.mod h1:sPLojNBn68fMUWSxIJtdVVIP8uSBYqesTfDUseX11Ug=
github.com/xanzy/go-gitlab v0.38.1 h1:st5/Ag4h8CqVfp3LpOWW0Jd4jYHTGETwu0KksYDPnYE=
Expand Down Expand Up @@ -716,6 +717,7 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200930213115-e57f6d466a48/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
33 changes: 24 additions & 9 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,13 +937,34 @@ func (s JobSorter) Less(i, j int) bool {
}

// GroupSearch searches for a namespace on GitLab
func GroupSearch(query string) ([]*gitlab.Group, error) {
list, _, err := lab.Groups.SearchGroup(query)
func GroupSearch(query string) (*gitlab.Group, error) {
if query == "" {
return nil, errors.New("query is empty")
}
groups := strings.Split(query, "/")
list, _, err := lab.Groups.SearchGroup(groups[0])
if err != nil {
return nil, err
}
// if we found a group and we aren't looking for a subgroup
if len(list) > 0 && len(groups) == 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
}

return list, nil
for _, g := range list {
fmt.Println(g.FullPath)
if g.FullPath == query {
return g, nil
}
}

return nil, errors.Errorf("Group '%s' not found", query)
}

// CIJobs returns a list of jobs in the pipeline with given id. The jobs are
Expand Down Expand Up @@ -1082,12 +1103,6 @@ func UserIDFromUsername(username string) (int, error) {
return us[0].ID, nil
}

// Labels converts a []string into a non-nil *gitlab.Labels.
func Labels(labels []string) *gitlab.Labels {
l := gitlab.Labels(labels)
return &l
}

// AddMRDiscussionNote adds a note to an existing MR discussion on GitLab
func AddMRDiscussionNote(project string, mrNum int, discussionID string, body string) (string, error) {
p, err := FindProject(project)
Expand Down

0 comments on commit 45198d3

Please sign in to comment.