configstore: resolve also org and user paths

This commit is contained in:
Simone Gotti 2019-05-03 13:48:19 +02:00
parent 2215aaebfa
commit c7585a6152
2 changed files with 26 additions and 14 deletions

View File

@ -116,23 +116,15 @@ func (r *ReadDB) GetProjectGroupPath(tx *db.Tx, group *types.ProjectGroup) (stri
rootGroupID := groups[0].ParentID
switch rootGroupType {
case types.ConfigTypeOrg:
org, err := r.GetOrg(tx, rootGroupID)
if err != nil {
return "", errors.Wrapf(err, "failed to get org %q", rootGroupID)
}
if org == nil {
return "", errors.Errorf("cannot find org with id %q", rootGroupID)
}
p = path.Join("org", org.Name)
fallthrough
case types.ConfigTypeUser:
user, err := r.GetUser(tx, rootGroupID)
var err error
p, err = r.GetPath(tx, rootGroupType, rootGroupID)
if err != nil {
return "", errors.Wrapf(err, "failed to get user %q", rootGroupID)
return "", err
}
if user == nil {
return "", errors.Errorf("cannot find user with id %q", rootGroupID)
}
p = path.Join("user", user.Name)
default:
return "", errors.Errorf("invalid root group type %q", rootGroupType)
}
for _, group := range groups {

View File

@ -15,6 +15,8 @@
package readdb
import (
"path"
"github.com/pkg/errors"
"github.com/sorintlab/agola/internal/db"
"github.com/sorintlab/agola/internal/services/types"
@ -75,6 +77,24 @@ func (r *ReadDB) GetPath(tx *db.Tx, configType types.ConfigType, id string) (str
if err != nil {
return "", err
}
case types.ConfigTypeOrg:
org, err := r.GetOrg(tx, id)
if err != nil {
return "", errors.Wrapf(err, "failed to get org %q", id)
}
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 {
return "", errors.Wrapf(err, "failed to get user %q", id)
}
if user == nil {
return "", errors.Errorf("cannot find user with id %q", id)
}
p = path.Join("user", user.Name)
default:
return "", errors.Errorf("config type %q doesn't provide a path", configType)
}