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"
|
|
|
|
|
|
|
|
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
|
2019-05-03 21:48:49 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/gateway/action"
|
2019-04-30 15:09:26 +00:00
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
2019-04-09 12:53:00 +00:00
|
|
|
"github.com/sorintlab/agola/internal/util"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CreateProjectGroupRequest struct {
|
2019-05-14 15:53:48 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
ParentRef string `json:"parent_ref"`
|
|
|
|
Visibility types.Visibility `json:"visibility"`
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CreateProjectGroupHandler struct {
|
2019-05-11 22:28:11 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 22:28:11 +00:00
|
|
|
func NewCreateProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler) *CreateProjectGroupHandler {
|
|
|
|
return &CreateProjectGroupHandler{log: logger.Sugar(), ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
var req CreateProjectGroupRequest
|
|
|
|
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-04-09 12:53:00 +00:00
|
|
|
userIDVal := ctx.Value("userid")
|
|
|
|
if userIDVal == nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(errors.Errorf("user not authenticated")))
|
2019-03-14 13:36:18 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 12:53:00 +00:00
|
|
|
userID := userIDVal.(string)
|
2019-03-14 13:36:18 +00:00
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
creq := &action.CreateProjectGroupRequest{
|
2019-03-14 13:36:18 +00:00
|
|
|
Name: req.Name,
|
2019-05-05 15:19:23 +00:00
|
|
|
ParentRef: req.ParentRef,
|
2019-04-30 15:09:26 +00:00
|
|
|
Visibility: req.Visibility,
|
2019-03-14 13:36:18 +00:00
|
|
|
CurrentUserID: userID,
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
projectGroup, err := h.ah.CreateProjectGroup(ctx, creq)
|
2019-04-09 12:53:00 +00:00
|
|
|
if httpError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res := createProjectGroupResponse(projectGroup)
|
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:53:48 +00:00
|
|
|
type UpdateProjectGroupRequest struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Visibility types.Visibility `json:"visibility,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateProjectGroupHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUpdateProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler) *UpdateProjectGroupHandler {
|
|
|
|
return &UpdateProjectGroupHandler{log: logger.Sugar(), ah: ah}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *UpdateProjectGroupHandler) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var req UpdateProjectGroupRequest
|
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
areq := &action.UpdateProjectGroupRequest{
|
|
|
|
Name: req.Name,
|
|
|
|
Visibility: req.Visibility,
|
|
|
|
}
|
|
|
|
projectGroup, err := h.ah.UpdateProjectGroup(ctx, projectGroupRef, areq)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectGroupResponse(projectGroup)
|
|
|
|
if err := httpResponse(w, http.StatusCreated, res); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:24:16 +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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusNoContent, nil); err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
type ProjectGroupHandler struct {
|
2019-05-05 12:27:22 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
func NewProjectGroupHandler(logger *zap.Logger, ah *action.ActionHandler) *ProjectGroupHandler {
|
|
|
|
return &ProjectGroupHandler{log: logger.Sugar(), ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
2019-03-14 13:36:18 +00:00
|
|
|
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-05 12:27:22 +00:00
|
|
|
projectGroup, err := h.ah.GetProjectGroup(ctx, projectGroupRef)
|
|
|
|
if httpError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectGroupResponse(projectGroup)
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupProjectsHandler struct {
|
2019-05-05 12:27:22 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
func NewProjectGroupProjectsHandler(logger *zap.Logger, ah *action.ActionHandler) *ProjectGroupProjectsHandler {
|
|
|
|
return &ProjectGroupProjectsHandler{log: logger.Sugar(), ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupProjectsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
2019-03-14 13:36:18 +00:00
|
|
|
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-05 12:27:22 +00:00
|
|
|
csprojects, err := h.ah.GetProjectGroupProjects(ctx, projectGroupRef)
|
|
|
|
if httpError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projects := make([]*ProjectResponse, len(csprojects))
|
|
|
|
for i, p := range csprojects {
|
|
|
|
projects[i] = createProjectResponse(p)
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, projects); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupSubgroupsHandler struct {
|
2019-05-05 12:27:22 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
func NewProjectGroupSubgroupsHandler(logger *zap.Logger, ah *action.ActionHandler) *ProjectGroupSubgroupsHandler {
|
|
|
|
return &ProjectGroupSubgroupsHandler{log: logger.Sugar(), ah: ah}
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectGroupSubgroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectGroupRef, err := url.PathUnescape(vars["projectgroupref"])
|
2019-03-14 13:36:18 +00:00
|
|
|
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-05 12:27:22 +00:00
|
|
|
cssubgroups, err := h.ah.GetProjectGroupSubgroups(ctx, projectGroupRef)
|
|
|
|
if httpError(w, err) {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
subgroups := make([]*ProjectGroupResponse, len(cssubgroups))
|
|
|
|
for i, g := range cssubgroups {
|
|
|
|
subgroups[i] = createProjectGroupResponse(g)
|
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, subgroups); err != nil {
|
2019-03-14 13:36:18 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGroupResponse struct {
|
2019-05-14 15:53:48 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
ParentPath string `json:"parent_path"`
|
|
|
|
Visibility types.Visibility `json:"visibility"`
|
|
|
|
GlobalVisibility string `json:"global_visibility"`
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 15:09:26 +00:00
|
|
|
func createProjectGroupResponse(r *csapi.ProjectGroup) *ProjectGroupResponse {
|
2019-03-14 13:36:18 +00:00
|
|
|
run := &ProjectGroupResponse{
|
2019-04-30 15:09:26 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
|
|
|
Path: r.Path,
|
|
|
|
ParentPath: r.ParentPath,
|
2019-05-12 21:24:48 +00:00
|
|
|
Visibility: r.Visibility,
|
2019-04-30 15:09:26 +00:00
|
|
|
GlobalVisibility: string(r.GlobalVisibility),
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return run
|
|
|
|
}
|