Skip to content

Commit bf0989c

Browse files
authored
Merge pull request #784 from bmeneg/fix-groupsearch
project_create: fix group lookup and allow custom remote name
2 parents 8af151c + ce14486 commit bf0989c

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

cmd/project_create.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var projectCreateCmd = &cobra.Command{
2424
Example: heredoc.Doc(`
2525
lab project create myproject
2626
lab project create myproject -n "new proj"
27+
lab project create myproject -r myupstream
2728
lab project create -g mygroup myproject
2829
lab project create mygroup/myproject -n "new proj"
2930
lab project create myproject --http
@@ -34,9 +35,10 @@ var projectCreateCmd = &cobra.Command{
3435
PersistentPreRun: labPersistentPreRun,
3536
Run: func(cmd *cobra.Command, args []string) {
3637
var (
37-
name, _ = cmd.Flags().GetString("name")
38-
desc, _ = cmd.Flags().GetString("description")
39-
group, _ = cmd.Flags().GetString("group")
38+
name, _ = cmd.Flags().GetString("name")
39+
desc, _ = cmd.Flags().GetString("description")
40+
group, _ = cmd.Flags().GetString("group")
41+
remote, _ = cmd.Flags().GetString("remote")
4042
)
4143

4244
g, path := determineNamespacePath(args, name)
@@ -88,27 +90,34 @@ var projectCreateCmd = &cobra.Command{
8890
if err != nil {
8991
log.Fatal(err)
9092
}
91-
if git.InsideGitRepo() {
92-
urlToRepo := labURLToRepo(p)
93-
err = git.RemoteAdd("origin", urlToRepo, ".")
94-
if err != nil {
95-
log.Fatal(err)
93+
94+
if remote != "" {
95+
if git.InsideGitRepo() {
96+
urlToRepo := labURLToRepo(p)
97+
err = git.RemoteAdd(remote, urlToRepo, ".")
98+
if err != nil {
99+
log.Fatal(err)
100+
}
101+
} else {
102+
log.Warnf("outside of a git repo. remote '%s' not added\n", remote)
96103
}
97104
}
105+
98106
fmt.Println(strings.TrimSuffix(p.HTTPURLToRepo, ".git"))
99107
},
100108
}
101109

102110
func determineNamespacePath(args []string, name string) (string, string) {
103-
var path string
104111
if len(args) > 0 {
105112
ps := strings.Split(args[0], "/")
106113
if len(ps) == 1 {
107114
return "", ps[0]
108115
}
109116
return strings.Join(ps[:len(ps)-1], "/"), ps[len(ps)-1]
110117
}
111-
if path == "" && name == "" && git.InsideGitRepo() {
118+
119+
var path string
120+
if name == "" && git.InsideGitRepo() {
112121
wd, err := git.WorkingDir()
113122
if err != nil {
114123
log.Fatal(err)
@@ -123,6 +132,7 @@ func init() {
123132
projectCreateCmd.Flags().StringP("name", "n", "", "name of the new project")
124133
projectCreateCmd.Flags().StringP("group", "g", "", "group name (also known as namespace)")
125134
projectCreateCmd.Flags().StringP("description", "d", "", "description of the new project")
135+
projectCreateCmd.Flags().StringP("remote", "r", "", "add remote referring to the new project")
126136
projectCreateCmd.Flags().BoolVarP(&private, "private", "p", false, "make project private: visible only to project members")
127137
projectCreateCmd.Flags().BoolVar(&public, "public", false, "make project public: visible without any authentication")
128138
projectCreateCmd.Flags().BoolVar(&internal, "internal", false, "make project internal: visible to any authenticated user (default)")

cmd/project_create_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Test_projectCreateCmd(t *testing.T) {
2222
os.Remove(filepath.Join(repo, ".git/config"))
2323

2424
t.Run("create", func(t *testing.T) {
25-
cmd := exec.Command(labBinaryPath, "project", "create", "-p")
25+
cmd := exec.Command(labBinaryPath, "project", "create", "-p", "-r", "origin")
2626
cmd.Dir = repo
2727

2828
b, err := cmd.CombinedOutput()

internal/gitlab/gitlab.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ var (
3434
// ErrActionRepeated is returned when a GitLab action is executed again. For example
3535
// this can be returned when an MR is approved twice.
3636
ErrActionRepeated = errors.New("GitLab action repeated")
37-
// ErrGroupNotFound is returned when a GitLab group cannot be found.
38-
ErrGroupNotFound = errors.New("GitLab group not found")
3937
// ErrNotModified is returned when adding an already existing item to a Todo list
4038
ErrNotModified = errors.New("Not Modified")
4139
// ErrProjectNotFound is returned when a GitLab project cannot be found.
@@ -1119,38 +1117,37 @@ func (s jobSorter) Less(i, j int) bool {
11191117

11201118
// GroupSearch searches for a namespace on GitLab
11211119
func GroupSearch(query string) (*gitlab.Group, error) {
1120+
query = strings.TrimSpace(query)
11221121
if query == "" {
1123-
return nil, errors.New("query is empty")
1122+
return nil, errors.New("invalid group query")
11241123
}
1124+
11251125
groups := strings.Split(query, "/")
1126-
list, _, err := lab.Groups.SearchGroup(groups[0])
1126+
list, _, err := lab.Groups.SearchGroup(groups[len(groups)-1])
11271127
if err != nil {
11281128
return nil, err
11291129
}
1130-
// SearchGroup doesn't return error if group isn't found. We need to do
1131-
// it ourselves.
11321130
if len(list) == 0 {
1133-
return nil, ErrGroupNotFound
1131+
return nil, fmt.Errorf("group '%s' not found", query)
11341132
}
1135-
// if we found a group and we aren't looking for a subgroup
1136-
if len(list) > 0 && len(groups) == 1 {
1133+
if len(list) == 1 {
11371134
return list[0], nil
11381135
}
1139-
list, _, err = lab.Groups.ListDescendantGroups(list[0].ID, &gitlab.ListDescendantGroupsOptions{
1140-
Search: gitlab.String(groups[len(groups)-1]),
1141-
})
1142-
if err != nil {
1143-
return nil, err
1144-
}
11451136

1146-
for _, g := range list {
1147-
fmt.Println(g.FullPath)
1148-
if g.FullPath == query {
1149-
return g, nil
1137+
for _, group := range list {
1138+
fullName := strings.TrimSpace(group.FullName)
1139+
if group.FullPath == query || fullName == query {
1140+
return group, nil
11501141
}
11511142
}
11521143

1153-
return nil, errors.Errorf("Group '%s' not found", query)
1144+
msg := fmt.Sprintf("found multiple groups with ambiguous name:\n")
1145+
for _, group := range list {
1146+
msg += fmt.Sprintf("\t%s\n", group.FullPath)
1147+
}
1148+
msg += fmt.Sprintf("use one of the above path options\n")
1149+
1150+
return nil, errors.New(msg)
11541151
}
11551152

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

0 commit comments

Comments
 (0)