2019-05-03 10:47:22 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
package action
|
2019-05-03 10:47:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"path"
|
2019-09-25 11:40:51 +00:00
|
|
|
"strings"
|
2019-05-03 10:47:22 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/datamanager"
|
|
|
|
"agola.io/agola/internal/db"
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
"agola.io/agola/services/configstore/types"
|
2019-05-03 10:47:22 +00:00
|
|
|
|
2022-02-21 08:40:18 +00:00
|
|
|
"github.com/gofrs/uuid"
|
2019-05-03 10:47:22 +00:00
|
|
|
)
|
|
|
|
|
2019-09-25 11:40:51 +00:00
|
|
|
func (h *ActionHandler) GetProjectGroup(ctx context.Context, projectGroupRef string) (*types.ProjectGroup, error) {
|
|
|
|
var projectGroup *types.ProjectGroup
|
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
|
|
|
var err error
|
|
|
|
projectGroup, err = h.readDB.GetProjectGroup(tx, projectGroupRef)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-09-25 11:40:51 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-09-25 11:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if projectGroup == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrNotExist, errors.Errorf("project group %q doesn't exist", projectGroupRef))
|
2019-09-25 11:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return projectGroup, nil
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:21:10 +00:00
|
|
|
func (h *ActionHandler) GetProjectGroupSubgroups(ctx context.Context, projectGroupRef string) ([]*types.ProjectGroup, error) {
|
|
|
|
var projectGroups []*types.ProjectGroup
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-12 22:21:10 +00:00
|
|
|
var err error
|
|
|
|
projectGroup, err := h.readDB.GetProjectGroup(tx, projectGroupRef)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-12 22:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if projectGroup == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrNotExist, errors.Errorf("project group %q doesn't exist", projectGroupRef))
|
2019-05-12 22:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
projectGroups, err = h.readDB.GetProjectGroupSubgroups(tx, projectGroup.ID)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-12 22:21:10 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-12 22:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return projectGroups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) GetProjectGroupProjects(ctx context.Context, projectGroupRef string) ([]*types.Project, error) {
|
|
|
|
var projects []*types.Project
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-12 22:21:10 +00:00
|
|
|
var err error
|
|
|
|
projectGroup, err := h.readDB.GetProjectGroup(tx, projectGroupRef)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-12 22:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if projectGroup == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrNotExist, errors.Errorf("project group %q doesn't exist", projectGroupRef))
|
2019-05-12 22:21:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
projects, err = h.readDB.GetProjectGroupProjects(tx, projectGroup.ID)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-12 22:21:10 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-12 22:21:10 +00:00
|
|
|
}
|
|
|
|
return projects, nil
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
func (h *ActionHandler) ValidateProjectGroup(ctx context.Context, projectGroup *types.ProjectGroup) error {
|
|
|
|
if projectGroup.Parent.Type != types.ConfigTypeProjectGroup &&
|
|
|
|
projectGroup.Parent.Type != types.ConfigTypeOrg &&
|
|
|
|
projectGroup.Parent.Type != types.ConfigTypeUser {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("invalid project group parent type %q", projectGroup.Parent.Type))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if projectGroup.Parent.ID == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group parent id required"))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the project group is a root project group the name must be empty
|
|
|
|
if projectGroup.Parent.Type == types.ConfigTypeOrg ||
|
|
|
|
projectGroup.Parent.Type == types.ConfigTypeUser {
|
|
|
|
if projectGroup.Name != "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group name for root project group must be empty"))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if projectGroup.Name == "" {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group name required"))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
if !util.ValidateName(projectGroup.Name) {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("invalid project group name %q", projectGroup.Name))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if !types.IsValidVisibility(projectGroup.Visibility) {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("invalid project group visibility"))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) CreateProjectGroup(ctx context.Context, projectGroup *types.ProjectGroup) (*types.ProjectGroup, error) {
|
|
|
|
if err := h.ValidateProjectGroup(ctx, projectGroup); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 11:40:51 +00:00
|
|
|
if projectGroup.Parent.Type != types.ConfigTypeProjectGroup {
|
2022-02-21 11:19:55 +00:00
|
|
|
return nil, util.NewAPIError(util.ErrBadRequest, errors.Errorf("wrong project group parent type %q", projectGroup.Parent.Type))
|
2019-09-25 11:40:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 10:47:22 +00:00
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-03 21:35:25 +00:00
|
|
|
parentProjectGroup, err := h.readDB.GetProjectGroup(tx, projectGroup.Parent.ID)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if parentProjectGroup == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group with id %q doesn't exist", projectGroup.Parent.ID))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
2019-09-25 11:40:51 +00:00
|
|
|
// TODO(sgotti) now we are doing a very ugly thing setting the request
|
|
|
|
// projectgroup parent ID that can be both an ID or a ref. Then we are fixing
|
|
|
|
// it to an ID here. Change the request format to avoid this.
|
2019-05-03 10:47:22 +00:00
|
|
|
projectGroup.Parent.ID = parentProjectGroup.ID
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
groupPath, err := h.readDB.GetProjectGroupPath(tx, parentProjectGroup)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
pp := path.Join(groupPath, projectGroup.Name)
|
|
|
|
|
|
|
|
// changegroup is the projectgroup path. Use "projectpath" prefix as it must
|
|
|
|
// cover both projects and projectgroups
|
|
|
|
cgNames := []string{util.EncodeSha256Hex("projectpath-" + pp)}
|
2019-05-03 21:35:25 +00:00
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check duplicate project group name
|
2019-05-03 21:35:25 +00:00
|
|
|
pg, err := h.readDB.GetProjectGroupByName(tx, projectGroup.Parent.ID, projectGroup.Name)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
if pg != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group with name %q, path %q already exists", pg.Name, pp))
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 08:40:18 +00:00
|
|
|
projectGroup.ID = uuid.Must(uuid.NewV4()).String()
|
2019-05-03 10:47:22 +00:00
|
|
|
projectGroup.Parent.Type = types.ConfigTypeProjectGroup
|
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
pgj, err := json.Marshal(projectGroup)
|
2019-05-03 10:47:22 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to marshal projectGroup")
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
|
|
|
actions := []*datamanager.Action{
|
|
|
|
{
|
|
|
|
ActionType: datamanager.ActionTypePut,
|
|
|
|
DataType: string(types.ConfigTypeProjectGroup),
|
|
|
|
ID: projectGroup.ID,
|
2019-05-14 15:53:48 +00:00
|
|
|
Data: pgj,
|
2019-05-03 10:47:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
2022-02-22 14:01:29 +00:00
|
|
|
return projectGroup, errors.WithStack(err)
|
2019-05-03 10:47:22 +00:00
|
|
|
}
|
2019-05-12 22:23:57 +00:00
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
type UpdateProjectGroupRequest struct {
|
|
|
|
ProjectGroupRef string
|
|
|
|
|
|
|
|
ProjectGroup *types.ProjectGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ActionHandler) UpdateProjectGroup(ctx context.Context, req *UpdateProjectGroupRequest) (*types.ProjectGroup, error) {
|
|
|
|
if err := h.ValidateProjectGroup(ctx, req.ProjectGroup); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-14 15:53:48 +00:00
|
|
|
var err error
|
|
|
|
// check project exists
|
|
|
|
pg, err := h.readDB.GetProjectGroup(tx, req.ProjectGroupRef)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
if pg == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group with ref %q doesn't exist", req.ProjectGroupRef))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
2019-09-25 11:40:51 +00:00
|
|
|
// check that the project group ID matches
|
2019-05-14 15:53:48 +00:00
|
|
|
if pg.ID != req.ProjectGroup.ID {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group with ref %q has a different id", req.ProjectGroupRef))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 11:40:51 +00:00
|
|
|
if pg.Parent.Type != req.ProjectGroup.Parent.Type {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("changing project group parent type isn't supported"))
|
2019-09-25 11:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch req.ProjectGroup.Parent.Type {
|
|
|
|
case types.ConfigTypeOrg:
|
|
|
|
fallthrough
|
|
|
|
case types.ConfigTypeUser:
|
|
|
|
// Cannot update root project group parent
|
|
|
|
if pg.Parent.Type != req.ProjectGroup.Parent.Type || pg.Parent.ID != req.ProjectGroup.Parent.ID {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("cannot change root project group parent type or id"))
|
2019-09-25 11:40:51 +00:00
|
|
|
}
|
|
|
|
// if the project group is a root project group force the name to be empty
|
|
|
|
req.ProjectGroup.Name = ""
|
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
case types.ConfigTypeProjectGroup:
|
2019-09-25 11:40:51 +00:00
|
|
|
// check parent exists
|
2019-05-14 15:53:48 +00:00
|
|
|
group, err := h.readDB.GetProjectGroup(tx, req.ProjectGroup.Parent.ID)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
if group == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group with id %q doesn't exist", req.ProjectGroup.Parent.ID))
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
2019-09-25 11:40:51 +00:00
|
|
|
// TODO(sgotti) now we are doing a very ugly thing setting the request
|
|
|
|
// projectgroup parent ID that can be both an ID or a ref. Then we are fixing
|
|
|
|
// it to an ID here. Change the request format to avoid this.
|
|
|
|
req.ProjectGroup.Parent.ID = group.ID
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 11:40:51 +00:00
|
|
|
curPGParentPath, err := h.readDB.GetPath(tx, pg.Parent.Type, pg.Parent.ID)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
2019-09-25 11:40:51 +00:00
|
|
|
curPGP := path.Join(curPGParentPath, pg.Name)
|
2019-05-14 15:53:48 +00:00
|
|
|
|
2019-09-25 11:40:51 +00:00
|
|
|
pgParentPath, err := h.readDB.GetPath(tx, req.ProjectGroup.Parent.Type, req.ProjectGroup.Parent.ID)
|
2019-05-14 15:53:48 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
2019-09-25 11:40:51 +00:00
|
|
|
pgp := path.Join(pgParentPath, req.ProjectGroup.Name)
|
2019-07-08 16:12:42 +00:00
|
|
|
|
2019-09-25 11:40:51 +00:00
|
|
|
if pg.Name != req.ProjectGroup.Name || pg.Parent.ID != req.ProjectGroup.Parent.ID {
|
2019-07-08 16:12:42 +00:00
|
|
|
// check duplicate project group name
|
|
|
|
ap, err := h.readDB.GetProjectGroupByName(tx, req.ProjectGroup.Parent.ID, req.ProjectGroup.Name)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-07-08 16:12:42 +00:00
|
|
|
}
|
|
|
|
if ap != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group with name %q, path %q already exists", req.ProjectGroup.Name, pgp))
|
2019-07-08 16:12:42 +00:00
|
|
|
}
|
2019-09-25 11:40:51 +00:00
|
|
|
// Cannot move inside itself or a child project group
|
|
|
|
if strings.HasPrefix(pgp, curPGP+"/") {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("cannot move project group inside itself or child project group"))
|
2019-09-25 11:40:51 +00:00
|
|
|
}
|
2019-07-08 16:12:42 +00:00
|
|
|
}
|
2019-05-14 15:53:48 +00:00
|
|
|
|
|
|
|
// changegroup is the project group path. Use "projectpath" prefix as it must
|
|
|
|
// cover both projects and projectgroups
|
|
|
|
cgNames := []string{util.EncodeSha256Hex("projectpath-" + pgp)}
|
2019-09-25 11:40:51 +00:00
|
|
|
|
|
|
|
// add new projectpath
|
|
|
|
if pg.Parent.ID != req.ProjectGroup.Parent.ID {
|
|
|
|
cgNames = append(cgNames, util.EncodeSha256Hex("projectpath-"+pgp))
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.WithStack(err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pgj, err := json.Marshal(req.ProjectGroup)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to marshal project")
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
actions := []*datamanager.Action{
|
|
|
|
{
|
|
|
|
ActionType: datamanager.ActionTypePut,
|
|
|
|
DataType: string(types.ConfigTypeProjectGroup),
|
|
|
|
ID: req.ProjectGroup.ID,
|
|
|
|
Data: pgj,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
2022-02-22 14:01:29 +00:00
|
|
|
return req.ProjectGroup, errors.WithStack(err)
|
2019-05-14 15:53:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 22:23:57 +00:00
|
|
|
func (h *ActionHandler) DeleteProjectGroup(ctx context.Context, projectGroupRef string) error {
|
|
|
|
var projectGroup *types.ProjectGroup
|
|
|
|
|
|
|
|
var cgt *datamanager.ChangeGroupsUpdateToken
|
|
|
|
|
|
|
|
// must do all the checks in a single transaction to avoid concurrent changes
|
2019-07-25 08:46:02 +00:00
|
|
|
err := h.readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-05-12 22:23:57 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// check project group existance
|
|
|
|
projectGroup, err = h.readDB.GetProjectGroup(tx, projectGroupRef)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-12 22:23:57 +00:00
|
|
|
}
|
|
|
|
if projectGroup == nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("project group %q doesn't exist", projectGroupRef))
|
2019-05-12 22:23:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 08:13:58 +00:00
|
|
|
// cannot delete root project group
|
|
|
|
if projectGroup.Parent.Type == types.ConfigTypeOrg ||
|
|
|
|
projectGroup.Parent.Type == types.ConfigTypeUser {
|
2022-02-21 11:19:55 +00:00
|
|
|
return util.NewAPIError(util.ErrBadRequest, errors.Errorf("cannot delete root project group"))
|
2019-10-01 08:13:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 22:23:57 +00:00
|
|
|
// changegroup is the project group id.
|
|
|
|
cgNames := []string{util.EncodeSha256Hex(projectGroup.ID)}
|
|
|
|
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, cgNames)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-12 22:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-12 22:23:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(sgotti) implement childs garbage collection
|
|
|
|
actions := []*datamanager.Action{
|
|
|
|
{
|
|
|
|
ActionType: datamanager.ActionTypeDelete,
|
|
|
|
DataType: string(types.ConfigTypeProjectGroup),
|
|
|
|
ID: projectGroup.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = h.dm.WriteWal(ctx, actions, cgt)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-05-12 22:23:57 +00:00
|
|
|
}
|