configstore: update CreateProject action

* move validation to ValidateProject function
* remove wrong project group check
This commit is contained in:
Simone Gotti 2019-05-09 15:32:27 +02:00
parent 92de7591da
commit 79c1a60a36
1 changed files with 18 additions and 19 deletions

View File

@ -28,39 +28,46 @@ import (
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
) )
func (h *ActionHandler) CreateProject(ctx context.Context, project *types.Project) (*types.Project, error) { func (h *ActionHandler) ValidateProject(ctx context.Context, project *types.Project) error {
if project.Name == "" { if project.Name == "" {
return nil, util.NewErrBadRequest(errors.Errorf("project name required")) return util.NewErrBadRequest(errors.Errorf("project name required"))
} }
if !util.ValidateName(project.Name) { if !util.ValidateName(project.Name) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid project name %q", project.Name)) return util.NewErrBadRequest(errors.Errorf("invalid project name %q", project.Name))
} }
if project.Parent.ID == "" { if project.Parent.ID == "" {
return nil, util.NewErrBadRequest(errors.Errorf("project parent id required")) return util.NewErrBadRequest(errors.Errorf("project parent id required"))
} }
if project.Parent.Type != types.ConfigTypeProjectGroup { if project.Parent.Type != types.ConfigTypeProjectGroup {
return nil, util.NewErrBadRequest(errors.Errorf("invalid project parent type %q", project.Parent.Type)) return util.NewErrBadRequest(errors.Errorf("invalid project parent type %q", project.Parent.Type))
} }
if !types.IsValidVisibility(project.Visibility) { if !types.IsValidVisibility(project.Visibility) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid project visibility")) return util.NewErrBadRequest(errors.Errorf("invalid project visibility"))
} }
if !types.IsValidRemoteRepositoryConfigType(project.RemoteRepositoryConfigType) { if !types.IsValidRemoteRepositoryConfigType(project.RemoteRepositoryConfigType) {
return nil, util.NewErrBadRequest(errors.Errorf("invalid project remote repository config type %q", project.RemoteRepositoryConfigType)) return util.NewErrBadRequest(errors.Errorf("invalid project remote repository config type %q", project.RemoteRepositoryConfigType))
} }
if project.RemoteRepositoryConfigType == types.RemoteRepositoryConfigTypeRemoteSource { if project.RemoteRepositoryConfigType == types.RemoteRepositoryConfigTypeRemoteSource {
if project.RemoteSourceID == "" { if project.RemoteSourceID == "" {
return nil, util.NewErrBadRequest(errors.Errorf("empty remote source id")) return util.NewErrBadRequest(errors.Errorf("empty remote source id"))
} }
if project.LinkedAccountID == "" { if project.LinkedAccountID == "" {
return nil, util.NewErrBadRequest(errors.Errorf("empty linked account id")) return util.NewErrBadRequest(errors.Errorf("empty linked account id"))
} }
if project.RepositoryID == "" { if project.RepositoryID == "" {
return nil, util.NewErrBadRequest(errors.Errorf("empty remote repository id")) return util.NewErrBadRequest(errors.Errorf("empty remote repository id"))
} }
if project.RepositoryPath == "" { if project.RepositoryPath == "" {
return nil, util.NewErrBadRequest(errors.Errorf("empty remote repository path")) return util.NewErrBadRequest(errors.Errorf("empty remote repository path"))
} }
} }
return nil
}
func (h *ActionHandler) CreateProject(ctx context.Context, project *types.Project) (*types.Project, error) {
if err := h.ValidateProject(ctx, project); err != nil {
return nil, err
}
var cgt *datamanager.ChangeGroupsUpdateToken var cgt *datamanager.ChangeGroupsUpdateToken
@ -98,14 +105,6 @@ func (h *ActionHandler) CreateProject(ctx context.Context, project *types.Projec
if p != nil { if p != nil {
return util.NewErrBadRequest(errors.Errorf("project with name %q, path %q already exists", p.Name, pp)) return util.NewErrBadRequest(errors.Errorf("project with name %q, path %q already exists", p.Name, pp))
} }
// check duplicate project group name
pg, err := h.readDB.GetProjectGroupByName(tx, project.Parent.ID, project.Name)
if err != nil {
return err
}
if pg != nil {
return util.NewErrBadRequest(errors.Errorf("project group with name %q, path %q already exists", pg.Name, pp))
}
if project.RemoteRepositoryConfigType == types.RemoteRepositoryConfigTypeRemoteSource { if project.RemoteRepositoryConfigType == types.RemoteRepositoryConfigTypeRemoteSource {
// check that the linked account matches the remote source // check that the linked account matches the remote source