2019-02-21 15:08:30 +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 (
|
2019-07-25 08:46:02 +00:00
|
|
|
"context"
|
2019-02-21 15:08:30 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-03-14 13:36:18 +00:00
|
|
|
"net/url"
|
2019-04-30 15:09:26 +00:00
|
|
|
"path"
|
2019-02-21 15:08:30 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/db"
|
|
|
|
"agola.io/agola/internal/services/configstore/action"
|
|
|
|
"agola.io/agola/internal/services/configstore/readdb"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
csapitypes "agola.io/agola/services/configstore/api/types"
|
|
|
|
"agola.io/agola/services/configstore/types"
|
2019-02-21 15:08:30 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func projectResponse(ctx context.Context, readDB *readdb.ReadDB, project *types.Project) (*csapitypes.Project, error) {
|
2019-07-25 08:46:02 +00:00
|
|
|
r, err := projectsResponse(ctx, readDB, []*types.Project{project})
|
2019-04-30 15:09:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r[0], nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func projectsResponse(ctx context.Context, readDB *readdb.ReadDB, projects []*types.Project) ([]*csapitypes.Project, error) {
|
|
|
|
resProjects := make([]*csapitypes.Project, len(projects))
|
2019-04-30 15:09:26 +00:00
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
err := readDB.Do(ctx, func(tx *db.Tx) error {
|
2019-04-30 15:09:26 +00:00
|
|
|
for i, project := range projects {
|
|
|
|
pp, err := readDB.GetPath(tx, project.Parent.Type, project.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.GetProjectOwnerID(tx, project)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
// calculate global visibility
|
|
|
|
visibility, err := getGlobalVisibility(readDB, tx, project.Visibility, &project.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
|
2019-07-31 13:39:07 +00:00
|
|
|
resProjects[i] = &csapitypes.Project{
|
2019-04-30 15:09:26 +00:00
|
|
|
Project: project,
|
2019-05-03 21:17:07 +00:00
|
|
|
OwnerType: ownerType,
|
|
|
|
OwnerID: ownerID,
|
2019-04-30 15:09:26 +00:00
|
|
|
Path: path.Join(pp, project.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 resProjects, err
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
func getGlobalVisibility(readDB *readdb.ReadDB, tx *db.Tx, curVisibility types.Visibility, parent *types.Parent) (types.Visibility, error) {
|
|
|
|
curParent := parent
|
|
|
|
if curVisibility == types.VisibilityPrivate {
|
|
|
|
return curVisibility, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for curParent.Type == types.ConfigTypeProjectGroup {
|
|
|
|
projectGroup, err := readDB.GetProjectGroupByID(tx, curParent.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if projectGroup.Visibility == types.VisibilityPrivate {
|
2019-05-14 08:56:17 +00:00
|
|
|
return types.VisibilityPrivate, nil
|
2019-04-30 15:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
curParent = &projectGroup.Parent
|
|
|
|
}
|
|
|
|
|
2019-05-14 08:56:17 +00:00
|
|
|
// check parent visibility
|
|
|
|
if curParent.Type == types.ConfigTypeOrg {
|
|
|
|
org, err := readDB.GetOrg(tx, curParent.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if org.Visibility == types.VisibilityPrivate {
|
|
|
|
return types.VisibilityPrivate, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
return curVisibility, nil
|
|
|
|
}
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
type ProjectHandler struct {
|
2019-02-21 15:08:30 +00:00
|
|
|
log *zap.SugaredLogger
|
2019-09-24 13:19:17 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:19:17 +00:00
|
|
|
func NewProjectHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *ProjectHandler {
|
|
|
|
return &ProjectHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 14:52:35 +00:00
|
|
|
func (h *ProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-25 08:46:02 +00:00
|
|
|
ctx := r.Context()
|
2019-02-21 15:08:30 +00:00
|
|
|
vars := mux.Vars(r)
|
2019-03-14 13:36:18 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
2019-02-21 15:08:30 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:19:17 +00:00
|
|
|
project, err := h.ah.GetProject(ctx, projectRef)
|
|
|
|
if httpError(w, err) {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
resProject, err := projectResponse(ctx, h.readDB, project)
|
2019-04-30 15:09:26 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusOK, resProject); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateProjectHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewCreateProjectHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *CreateProjectHandler {
|
|
|
|
return &CreateProjectHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req types.Project
|
|
|
|
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-02-21 15:08:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
project, err := h.ah.CreateProject(ctx, &req)
|
2019-03-14 08:34:05 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
2019-05-09 13:33:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
resProject, err := projectResponse(ctx, h.readDB, project)
|
2019-05-09 13:33:57 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, resProject); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateProjectHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
|
|
|
readDB *readdb.ReadDB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUpdateProjectHandler(logger *zap.Logger, ah *action.ActionHandler, readDB *readdb.ReadDB) *UpdateProjectHandler {
|
|
|
|
return &UpdateProjectHandler{log: logger.Sugar(), ah: ah, readDB: readDB}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UpdateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var project *types.Project
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&project); err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
areq := &action.UpdateProjectRequest{
|
|
|
|
ProjectRef: projectRef,
|
|
|
|
Project: project,
|
|
|
|
}
|
|
|
|
project, err = h.ah.UpdateProject(ctx, areq)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 08:46:02 +00:00
|
|
|
resProject, err := projectResponse(ctx, h.readDB, project)
|
2019-04-30 15:09:26 +00:00
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusCreated, resProject); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteProjectHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:35:25 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
func NewDeleteProjectHandler(logger *zap.Logger, ah *action.ActionHandler) *DeleteProjectHandler {
|
|
|
|
return &DeleteProjectHandler{log: logger.Sugar(), ah: ah}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2019-03-14 13:36:18 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
|
|
|
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-02-21 15:08:30 +00:00
|
|
|
|
2019-05-03 21:35:25 +00:00
|
|
|
err = h.ah.DeleteProject(ctx, projectRef)
|
2019-03-14 08:34:05 +00:00
|
|
|
if httpError(w, err) {
|
2019-02-21 15:08:30 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-04-08 10:29:25 +00:00
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
2019-02-21 15:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultProjectsLimit = 10
|
|
|
|
MaxProjectsLimit = 20
|
|
|
|
)
|