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-07-01 09:40:20 +00:00
"agola.io/agola/internal/datamanager"
"agola.io/agola/internal/db"
"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-23 09:23:14 +00:00
errors "golang.org/x/xerrors"
2019-05-03 10:47:22 +00:00
)
2019-05-09 13:32:27 +00:00
func ( h * ActionHandler ) ValidateProject ( ctx context . Context , project * types . Project ) error {
2019-05-03 10:47:22 +00:00
if project . Name == "" {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "project name required" ) )
2019-05-03 10:47:22 +00:00
}
if ! util . ValidateName ( project . Name ) {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "invalid project name %q" , project . Name ) )
2019-05-03 10:47:22 +00:00
}
if project . Parent . ID == "" {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "project parent id required" ) )
2019-05-03 10:47:22 +00:00
}
if project . Parent . Type != types . ConfigTypeProjectGroup {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "invalid project parent type %q" , project . Parent . Type ) )
2019-05-03 10:47:22 +00:00
}
if ! types . IsValidVisibility ( project . Visibility ) {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "invalid project visibility" ) )
2019-05-03 10:47:22 +00:00
}
if ! types . IsValidRemoteRepositoryConfigType ( project . RemoteRepositoryConfigType ) {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "invalid project remote repository config type %q" , project . RemoteRepositoryConfigType ) )
2019-05-03 10:47:22 +00:00
}
if project . RemoteRepositoryConfigType == types . RemoteRepositoryConfigTypeRemoteSource {
if project . RemoteSourceID == "" {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "empty remote source id" ) )
2019-05-03 10:47:22 +00:00
}
if project . LinkedAccountID == "" {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "empty linked account id" ) )
2019-05-03 10:47:22 +00:00
}
if project . RepositoryID == "" {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "empty remote repository id" ) )
2019-05-03 10:47:22 +00:00
}
if project . RepositoryPath == "" {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "empty remote repository path" ) )
2019-05-03 10:47:22 +00:00
}
}
2019-05-09 13:32:27 +00:00
return nil
}
2019-09-24 13:19:17 +00:00
func ( h * ActionHandler ) GetProject ( ctx context . Context , projectRef string ) ( * types . Project , error ) {
var project * types . Project
err := h . readDB . Do ( ctx , func ( tx * db . Tx ) error {
var err error
project , err = h . readDB . GetProject ( tx , projectRef )
return err
} )
if err != nil {
return nil , err
}
if project == nil {
2022-02-21 11:19:55 +00:00
return nil , util . NewAPIError ( util . ErrNotExist , errors . Errorf ( "project %q doesn't exist" , projectRef ) )
2019-09-24 13:19:17 +00:00
}
return project , nil
}
2019-05-09 13:32:27 +00:00
func ( h * ActionHandler ) CreateProject ( ctx context . Context , project * types . Project ) ( * types . Project , error ) {
if err := h . ValidateProject ( ctx , project ) ; err != nil {
return nil , err
}
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 10:47:22 +00:00
var err error
2019-05-03 21:35:25 +00:00
group , err := h . readDB . GetProjectGroup ( tx , project . Parent . ID )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
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" , project . Parent . ID ) )
2019-05-03 10:47:22 +00:00
}
project . Parent . ID = group . ID
2019-05-03 21:35:25 +00:00
groupPath , err := h . readDB . GetProjectGroupPath ( tx , group )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
pp := path . Join ( groupPath , project . Name )
// changegroup is the project 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 {
return err
}
// check duplicate project name
2019-05-03 21:35:25 +00:00
p , err := h . readDB . GetProjectByName ( tx , project . Parent . ID , project . Name )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
if p != nil {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "project with name %q, path %q already exists" , p . Name , pp ) )
2019-05-03 10:47:22 +00:00
}
if project . RemoteRepositoryConfigType == types . RemoteRepositoryConfigTypeRemoteSource {
// check that the linked account matches the remote source
2019-05-03 21:35:25 +00:00
user , err := h . readDB . GetUserByLinkedAccount ( tx , project . LinkedAccountID )
2019-05-03 10:47:22 +00:00
if err != nil {
2019-05-23 09:23:14 +00:00
return errors . Errorf ( "failed to get user with linked account id %q: %w" , project . LinkedAccountID , err )
2019-05-03 10:47:22 +00:00
}
if user == nil {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "user for linked account %q doesn't exist" , project . LinkedAccountID ) )
2019-05-03 10:47:22 +00:00
}
la , ok := user . LinkedAccounts [ project . LinkedAccountID ]
if ! ok {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "linked account id %q for user %q doesn't exist" , project . LinkedAccountID , user . Name ) )
2019-05-03 10:47:22 +00:00
}
if la . RemoteSourceID != project . RemoteSourceID {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "linked account id %q remote source %q different than project remote source %q" , project . LinkedAccountID , la . RemoteSourceID , project . RemoteSourceID ) )
2019-05-03 10:47:22 +00:00
}
}
return nil
} )
if err != nil {
return nil , err
}
2022-02-21 08:40:18 +00:00
project . ID = uuid . Must ( uuid . NewV4 ( ) ) . String ( )
2019-05-03 10:47:22 +00:00
project . Parent . Type = types . ConfigTypeProjectGroup
2019-05-07 16:29:31 +00:00
// generate the Secret and the WebhookSecret
2022-02-21 08:40:18 +00:00
project . Secret = util . EncodeSha1Hex ( uuid . Must ( uuid . NewV4 ( ) ) . String ( ) )
project . WebhookSecret = util . EncodeSha1Hex ( uuid . Must ( uuid . NewV4 ( ) ) . String ( ) )
2019-05-03 10:47:22 +00:00
pcj , err := json . Marshal ( project )
if err != nil {
2019-05-23 09:23:14 +00:00
return nil , errors . Errorf ( "failed to marshal project: %w" , err )
2019-05-03 10:47:22 +00:00
}
actions := [ ] * datamanager . Action {
{
ActionType : datamanager . ActionTypePut ,
DataType : string ( types . ConfigTypeProject ) ,
ID : project . ID ,
Data : pcj ,
} ,
}
2019-05-03 21:35:25 +00:00
_ , err = h . dm . WriteWal ( ctx , actions , cgt )
2019-05-03 10:47:22 +00:00
return project , err
}
2019-05-09 13:33:57 +00:00
type UpdateProjectRequest struct {
ProjectRef string
Project * types . Project
}
func ( h * ActionHandler ) UpdateProject ( ctx context . Context , req * UpdateProjectRequest ) ( * types . Project , error ) {
if err := h . ValidateProject ( ctx , req . Project ) ; err != nil {
return nil , err
}
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-09 13:33:57 +00:00
var err error
// check project exists
p , err := h . readDB . GetProject ( tx , req . ProjectRef )
if err != nil {
return err
}
if p == nil {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "project with ref %q doesn't exist" , req . ProjectRef ) )
2019-05-09 13:33:57 +00:00
}
// check that the project.ID matches
if p . ID != req . Project . ID {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "project with ref %q has a different id" , req . ProjectRef ) )
2019-05-09 13:33:57 +00:00
}
// check parent project group exists
group , err := h . readDB . GetProjectGroup ( tx , req . Project . Parent . ID )
if err != nil {
return err
}
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 . Project . Parent . ID ) )
2019-05-09 13:33:57 +00:00
}
2019-09-24 13:19:17 +00:00
req . Project . Parent . ID = group . ID
2019-05-09 13:33:57 +00:00
2019-07-08 16:12:42 +00:00
groupPath , err := h . readDB . GetProjectGroupPath ( tx , group )
2019-05-09 13:33:57 +00:00
if err != nil {
return err
}
2019-07-08 16:12:42 +00:00
pp := path . Join ( groupPath , req . Project . Name )
2019-05-09 13:33:57 +00:00
2019-09-24 13:19:17 +00:00
if p . Name != req . Project . Name || p . Parent . ID != req . Project . Parent . ID {
2019-07-08 16:12:42 +00:00
// check duplicate project name
ap , err := h . readDB . GetProjectByName ( tx , req . Project . Parent . ID , req . Project . Name )
if err != nil {
return err
}
if ap != nil {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "project with name %q, path %q already exists" , req . Project . Name , pp ) )
2019-07-08 16:12:42 +00:00
}
2019-05-09 13:33:57 +00:00
}
// changegroup is the project path. Use "projectpath" prefix as it must
// cover both projects and projectgroups
cgNames := [ ] string { util . EncodeSha256Hex ( "projectpath-" + pp ) }
2019-09-24 13:19:17 +00:00
// add new projectpath
if p . Parent . ID != req . Project . Parent . ID {
// get old parent project group
curGroup , err := h . readDB . GetProjectGroup ( tx , p . Parent . ID )
if err != nil {
return err
}
if curGroup == nil {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "project group with id %q doesn't exist" , p . Parent . ID ) )
2019-09-24 13:19:17 +00:00
}
curGroupPath , err := h . readDB . GetProjectGroupPath ( tx , curGroup )
if err != nil {
return err
}
pp := path . Join ( curGroupPath , req . Project . Name )
cgNames = append ( cgNames , util . EncodeSha256Hex ( "projectpath-" + pp ) )
}
2019-05-09 13:33:57 +00:00
cgt , err = h . readDB . GetChangeGroupsUpdateTokens ( tx , cgNames )
if err != nil {
return err
}
if req . Project . RemoteRepositoryConfigType == types . RemoteRepositoryConfigTypeRemoteSource {
// check that the linked account matches the remote source
user , err := h . readDB . GetUserByLinkedAccount ( tx , req . Project . LinkedAccountID )
if err != nil {
2019-05-23 09:23:14 +00:00
return errors . Errorf ( "failed to get user with linked account id %q: %w" , req . Project . LinkedAccountID , err )
2019-05-09 13:33:57 +00:00
}
if user == nil {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "user for linked account %q doesn't exist" , req . Project . LinkedAccountID ) )
2019-05-09 13:33:57 +00:00
}
la , ok := user . LinkedAccounts [ req . Project . LinkedAccountID ]
if ! ok {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "linked account id %q for user %q doesn't exist" , req . Project . LinkedAccountID , user . Name ) )
2019-05-09 13:33:57 +00:00
}
if la . RemoteSourceID != req . Project . RemoteSourceID {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "linked account id %q remote source %q different than project remote source %q" , req . Project . LinkedAccountID , la . RemoteSourceID , req . Project . RemoteSourceID ) )
2019-05-09 13:33:57 +00:00
}
}
return nil
} )
if err != nil {
return nil , err
}
pcj , err := json . Marshal ( req . Project )
if err != nil {
2019-05-23 09:23:14 +00:00
return nil , errors . Errorf ( "failed to marshal project: %w" , err )
2019-05-09 13:33:57 +00:00
}
actions := [ ] * datamanager . Action {
{
ActionType : datamanager . ActionTypePut ,
DataType : string ( types . ConfigTypeProject ) ,
ID : req . Project . ID ,
Data : pcj ,
} ,
}
_ , err = h . dm . WriteWal ( ctx , actions , cgt )
return req . Project , err
}
2019-05-03 21:35:25 +00:00
func ( h * ActionHandler ) DeleteProject ( ctx context . Context , projectRef string ) error {
2019-05-03 10:47:22 +00:00
var project * types . Project
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 10:47:22 +00:00
var err error
// check project existance
2019-05-03 21:35:25 +00:00
project , err = h . readDB . GetProject ( tx , projectRef )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
if project == nil {
2022-02-21 11:19:55 +00:00
return util . NewAPIError ( util . ErrBadRequest , errors . Errorf ( "project %q doesn't exist" , projectRef ) )
2019-05-03 10:47:22 +00:00
}
// changegroup is the project id.
cgNames := [ ] string { util . EncodeSha256Hex ( project . ID ) }
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 {
return err
}
return nil
} )
if err != nil {
return err
}
2019-05-12 22:23:57 +00:00
// TODO(sgotti) implement childs garbage collection
2019-05-03 10:47:22 +00:00
actions := [ ] * datamanager . Action {
{
ActionType : datamanager . ActionTypeDelete ,
DataType : string ( types . ConfigTypeProject ) ,
ID : project . ID ,
} ,
}
2019-05-03 21:35:25 +00:00
_ , err = h . dm . WriteWal ( ctx , actions , cgt )
2019-05-03 10:47:22 +00:00
return err
}