2019-03-14 13:36:18 +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.
|
|
|
|
|
|
|
|
package readdb
|
|
|
|
|
|
|
|
import (
|
2019-05-03 11:48:19 +00:00
|
|
|
"path"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/db"
|
2019-07-31 13:39:07 +00:00
|
|
|
"agola.io/agola/services/configstore/types"
|
2022-02-21 11:19:55 +00:00
|
|
|
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-14 13:36:18 +00:00
|
|
|
)
|
|
|
|
|
2019-04-30 15:06:44 +00:00
|
|
|
func (r *ReadDB) GetPath(tx *db.Tx, configType types.ConfigType, id string) (string, error) {
|
2019-03-14 13:36:18 +00:00
|
|
|
var p string
|
2019-04-30 15:06:44 +00:00
|
|
|
switch configType {
|
2019-03-14 13:36:18 +00:00
|
|
|
case types.ConfigTypeProjectGroup:
|
2019-04-30 15:06:44 +00:00
|
|
|
projectGroup, err := r.GetProjectGroup(tx, id)
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if projectGroup == nil {
|
2019-04-30 15:06:44 +00:00
|
|
|
return "", errors.Errorf("projectgroup with id %q doesn't exist", id)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
p, err = r.GetProjectGroupPath(tx, projectGroup)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
case types.ConfigTypeProject:
|
2019-04-30 15:06:44 +00:00
|
|
|
project, err := r.GetProject(tx, id)
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if project == nil {
|
2019-04-30 15:06:44 +00:00
|
|
|
return "", errors.Errorf("project with id %q doesn't exist", id)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
p, err = r.GetProjectPath(tx, project)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-05-03 11:48:19 +00:00
|
|
|
case types.ConfigTypeOrg:
|
|
|
|
org, err := r.GetOrg(tx, id)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return "", errors.Errorf("failed to get org %q: %w", id, err)
|
2019-05-03 11:48:19 +00:00
|
|
|
}
|
|
|
|
if org == nil {
|
|
|
|
return "", errors.Errorf("cannot find org with id %q", id)
|
|
|
|
}
|
|
|
|
p = path.Join("org", org.Name)
|
|
|
|
case types.ConfigTypeUser:
|
|
|
|
user, err := r.GetUser(tx, id)
|
|
|
|
if err != nil {
|
2019-05-23 09:23:14 +00:00
|
|
|
return "", errors.Errorf("failed to get user %q: %w", id, err)
|
2019-05-03 11:48:19 +00:00
|
|
|
}
|
|
|
|
if user == nil {
|
|
|
|
return "", errors.Errorf("cannot find user with id %q", id)
|
|
|
|
}
|
|
|
|
p = path.Join("user", user.Name)
|
2019-04-30 15:06:44 +00:00
|
|
|
default:
|
|
|
|
return "", errors.Errorf("config type %q doesn't provide a path", configType)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|