Skip to content

Commit

Permalink
create a repository in a group
Browse files Browse the repository at this point in the history
  • Loading branch information
kovetskiy authored and bmeneg committed Dec 22, 2020
1 parent f50f4fc commit 4ffe48e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
26 changes: 23 additions & 3 deletions cmd/project_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,37 @@ lab project create # user/<curr dir> named <curr dir>
# (above only works w/i git repo)
lab project create myproject # user/myproject named myproject
lab project create myproject -n "new proj" # user/myproject named "new proj"
lab project create -n "new proj" # user/new-proj named "new proj"`,
lab project create -n "new proj" # user/new-proj named "new proj"
lab project create -g mygroup myproject # mygroup/myproject named myproject`,
Args: cobra.MaximumNArgs(1),
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
var (
name, _ = cmd.Flags().GetString("name")
desc, _ = cmd.Flags().GetString("description")
name, _ = cmd.Flags().GetString("name")
desc, _ = cmd.Flags().GetString("description")
group, _ = cmd.Flags().GetString("group")
)

path := determinePath(args, name)
if path == "" && name == "" {
log.Fatal("path or name must be set")
}

var namespaceID *int
if group != "" {
list, err := lab.NamespaceSearch(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
}

// set the default visibility
visibility := gitlab.PrivateVisibility

Expand All @@ -60,6 +78,7 @@ lab project create -n "new proj" # user/new-proj named "new proj"`,
}

opts := gitlab.CreateProjectOptions{
NamespaceID: namespaceID,
Path: gitlab.String(path),
Name: gitlab.String(name),
Description: gitlab.String(desc),
Expand Down Expand Up @@ -99,6 +118,7 @@ func determinePath(args []string, name string) string {

func init() {
projectCreateCmd.Flags().StringP("name", "n", "", "name of the new project")
projectCreateCmd.Flags().StringP("group", "g", "", "group name (also known as namespace)")
projectCreateCmd.Flags().StringP("description", "d", "", "description of the new project")
projectCreateCmd.Flags().BoolVarP(&private, "private", "p", false, "make project private: visible only to project members")
projectCreateCmd.Flags().BoolVar(&public, "public", false, "make project public: visible without any authentication")
Expand Down
20 changes: 13 additions & 7 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ import (
"github.com/zaquestion/lab/internal/git"
)

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")
)
// 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 (
lab *gitlab.Client
Expand Down Expand Up @@ -169,9 +167,7 @@ func LoadGitLabTmpl(tmplName string) string {
return strings.TrimSpace(string(tmpl))
}

var (
localProjects map[string]*gitlab.Project = make(map[string]*gitlab.Project)
)
var localProjects map[string]*gitlab.Project = make(map[string]*gitlab.Project)

// GetProject looks up a Gitlab project by ID.
func GetProject(projectID interface{}) (*gitlab.Project, error) {
Expand Down Expand Up @@ -940,6 +936,16 @@ func (s JobSorter) Less(i, j int) bool {
return time.Time(*s.Jobs[i].CreatedAt).Before(time.Time(*s.Jobs[j].CreatedAt))
}

// NamespaceSearch searches for a namespace on GitLab
func NamespaceSearch(query string) ([]*gitlab.Namespace, error) {
list, _, err := lab.Namespaces.SearchNamespace(query)
if err != nil {
return nil, err
}

return list, nil
}

// CIJobs returns a list of jobs in the pipeline with given id. The jobs are
// returned sorted by their CreatedAt time
func CIJobs(pid interface{}, id int) ([]*gitlab.Job, error) {
Expand Down

0 comments on commit 4ffe48e

Please sign in to comment.