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 api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-04-30 15:09:26 +00:00
|
|
|
"path"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-03-14 13:36:18 +00:00
|
|
|
"github.com/sorintlab/agola/internal/db"
|
2019-05-03 21:35:25 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/configstore/action"
|
2019-03-14 13:36:18 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/configstore/readdb"
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
// ProjectGroup augments types.ProjectGroup with dynamic data
|
|
|
|
type ProjectGroup struct {
|
|
|
|
*types.ProjectGroup
|
|
|
|
|
|
|
|
// dynamic data
|
2019-05-03 21:17:07 +00:00
|
|
|
OwnerType types.ConfigType
|
|
|
|
OwnerID string
|
2019-04-30 15:09:26 +00:00
|
|
|
Path string
|
|
|
|
ParentPath string
|
|
|
|
GlobalVisibility types.Visibility
|
2019-04-30 15:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func projectGroupResponse(readDB *readdb.ReadDB, projectGroup *types.ProjectGroup) (*ProjectGroup, error) {
|
|
|
|
r, err := projectGroupsResponse(readDB, []*types.ProjectGroup{projectGroup})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func projectGroupsResponse(readDB *readdb.ReadDB, projectGroups []*types.ProjectGroup) ([]*ProjectGroup, error) {
|
|
|
|
resProjectGroups := make([]*ProjectGroup, len(projectGroups))
|
|
|
|
|
|
|
|
err := readDB.Do(func(tx *db.Tx) error {
|
|
|
|
for i, projectGroup := range projectGroups {
|
|
|
|
pp, err := readDB.GetPath(tx, projectGroup.Parent.Type, projectGroup.Parent.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-30 15:09:26 +00:00
|
|
|
|
2019-05-03 21:17:07 +00:00
|
|
|
ownerType, ownerID, err := readDB.GetProjectGroupOwnerID(tx, projectGroup)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
// calculate global visibility
|
|
|
|
visibility, err := getGlobalVisibility(readDB, tx, projectGroup.Visibility, &projectGroup.Parent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
// we calculate the path here from parent path since the db could not yet be
|
|
|
|
// updated on create
|
|
|
|
resProjectGroups[i] = &ProjectGroup{
|
2019-04-30 15:09:26 +00:00
|
|
|
ProjectGroup: projectGroup,
|
2019-05-03 21:17:07 +00:00
|
|
|
OwnerType: ownerType,
|
|
|
|
OwnerID: ownerID,
|
2019-04-30 15:09:26 +00:00
|
|
|
Path: path.Join(pp, projectGroup.Name),
|
|
|
|
ParentPath: pp,
|
|
|
|
GlobalVisibility: visibility,
|
2019-04-30 15:09:26 +00:00
|
|
|
}
|
2019-04-30 15:09:26 +00:00
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return resProjectGroups, err
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
type ProjectGroupHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectGroupHandler(logger *zap.Logger, readDB *readdb.ReadDB) *ProjectGroupHandler {
|
|
|
|
return &ProjectGroupHandler{log: logger.Sugar(), readDB: readDB}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var projectGroup *types.ProjectGroup
|
|
|
|
err = h.readDB.Do(func(tx *db.Tx) error {
|
|
|
|
var err error
|
|
|
|
projectGroup, err = h.readDB.GetProjectGroup(tx, projectGroupRef)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
httpError(w, err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if projectGroup == nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrNotFound(errors.Errorf("project group %q doesn't exist", projectGroupRef)))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
resProjectGroup, err := projectGroupResponse(h.readDB, projectGroup)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusOK, resProjectGroup); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupProjectsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-12 22:21:10 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:21:10 +00:00
|
|
|
func NewProjectGroupProjectsHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *ProjectGroupProjectsHandler {
|
|
|
|
return &ProjectGroupProjectsHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-05-12 22:21:10 +00:00
|
|
|
ctx := r.Context()
|
2019-03-14 13:36:18 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:21:10 +00:00
|
|
|
projects, err := h.ah.GetProjectGroupProjects(ctx, projectGroupRef)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
resProjects, err := projectsResponse(h.readDB, projects)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusOK, resProjects); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupSubgroupsHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-12 22:21:10 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:21:10 +00:00
|
|
|
func NewProjectGroupSubgroupsHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *ProjectGroupSubgroupsHandler {
|
|
|
|
return &ProjectGroupSubgroupsHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupSubgroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-05-12 22:21:10 +00:00
|
|
|
ctx := r.Context()
|
2019-03-14 13:36:18 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:21:10 +00:00
|
|
|
projectGroups, err := h.ah.GetProjectGroupSubgroups(ctx, projectGroupRef)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
resProjectGroups, err := projectGroupsResponse(h.readDB, projectGroups)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusOK, resProjectGroups); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateProjectGroupHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewCreateProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *CreateProjectGroupHandler {
|
|
|
|
return &CreateProjectGroupHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req types.ProjectGroup
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
projectGroup, err := h.ah.CreateProjectGroup(ctx, &req)
|
2019-03-14 13:36:18 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
resProjectGroup, err := projectGroupResponse(h.readDB, projectGroup)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, resProjectGroup); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-12 22:23:57 +00:00
|
|
|
|
|
|
|
type DeleteProjectGroupHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDeleteProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteProjectGroupHandler {
|
|
|
|
return &DeleteProjectGroupHandler{log: logger.Sugar(), ah: ah}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.ah.DeleteProjectGroup(ctx, projectGroupRef)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|