2019-02-21 16:58:25 +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"
|
2019-03-14 13:36:18 +00:00
|
|
|
"net/url"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/action"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
csapitypes "agola.io/agola/services/configstore/api/types"
|
|
|
|
cstypes "agola.io/agola/services/configstore/types"
|
|
|
|
gwapitypes "agola.io/agola/services/gateway/api/types"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-02-21 16:58:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CreateProjectHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-11 22:28:11 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewCreateProjectHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateProjectHandler {
|
|
|
|
return &CreateProjectHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CreateProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.CreateProjectRequest
|
2019-02-21 16:58:25 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
areq := &action.CreateProjectRequest{
|
2019-02-21 16:58:25 +00:00
|
|
|
Name: req.Name,
|
2019-05-05 15:19:23 +00:00
|
|
|
ParentRef: req.ParentRef,
|
2019-07-31 13:39:07 +00:00
|
|
|
Visibility: cstypes.Visibility(req.Visibility),
|
2019-04-03 09:07:54 +00:00
|
|
|
RepoPath: req.RepoPath,
|
2019-02-21 16:58:25 +00:00
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
|
2020-01-25 11:33:53 +00:00
|
|
|
PassVarsToForkedPR: req.PassVarsToForkedPR,
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
project, err := h.ah.CreateProject(ctx, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:08:33 +00:00
|
|
|
res := createProjectResponse(project)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 21:22:18 +00:00
|
|
|
type UpdateProjectHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-12 21:22:18 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewUpdateProjectHandler(log zerolog.Logger, ah *action.ActionHandler) *UpdateProjectHandler {
|
|
|
|
return &UpdateProjectHandler{log: log, ah: ah}
|
2019-05-12 21:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-05-12 21:22:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.UpdateProjectRequest
|
2019-05-12 21:22:18 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-05-12 21:22:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-24 13:19:17 +00:00
|
|
|
var visibility *cstypes.Visibility
|
|
|
|
if req.Visibility != nil {
|
|
|
|
v := cstypes.Visibility(*req.Visibility)
|
|
|
|
visibility = &v
|
|
|
|
}
|
|
|
|
|
2019-05-12 21:22:18 +00:00
|
|
|
areq := &action.UpdateProjectRequest{
|
2020-01-25 11:33:53 +00:00
|
|
|
Name: req.Name,
|
|
|
|
ParentRef: req.ParentRef,
|
|
|
|
Visibility: visibility,
|
|
|
|
PassVarsToForkedPR: req.PassVarsToForkedPR,
|
2019-05-12 21:22:18 +00:00
|
|
|
}
|
|
|
|
project, err := h.ah.UpdateProject(ctx, projectRef, areq)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-12 21:22:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectResponse(project)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-12 21:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type ProjectReconfigHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-11 22:28:11 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewProjectReconfigHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectReconfigHandler {
|
|
|
|
return &ProjectReconfigHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectReconfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
2019-02-28 14:52:35 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
if err := h.ah.ReconfigProject(ctx, projectRef); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, err)
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 13:36:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectUpdateRepoLinkedAccountHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-11 22:28:11 +00:00
|
|
|
ah *action.ActionHandler
|
2019-05-09 13:36:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewProjectUpdateRepoLinkedAccountHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectUpdateRepoLinkedAccountHandler {
|
|
|
|
return &ProjectUpdateRepoLinkedAccountHandler{log: log, ah: ah}
|
2019-05-09 13:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectUpdateRepoLinkedAccountHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-05-09 13:36:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := h.ah.ProjectUpdateRepoLinkedAccount(ctx, projectRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-05-09 13:36:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectResponse(project)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteProjectHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:27:22 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewDeleteProjectHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteProjectHandler {
|
|
|
|
return &DeleteProjectHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *DeleteProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
2019-02-28 14:52:35 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
err = h.ah.DeleteProject(ctx, projectRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-04-08 09:35:45 +00:00
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-05-05 12:27:22 +00:00
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewProjectHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectHandler {
|
|
|
|
return &ProjectHandler{log: log, ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
2019-04-08 07:04:55 +00:00
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-02-28 14:52:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:27:22 +00:00
|
|
|
project, err := h.ah.GetProject(ctx, projectRef)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := createProjectResponse(project)
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func createProjectResponse(r *csapitypes.Project) *gwapitypes.ProjectResponse {
|
|
|
|
res := &gwapitypes.ProjectResponse{
|
2020-01-25 11:33:53 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
|
|
|
Path: r.Path,
|
|
|
|
ParentPath: r.ParentPath,
|
|
|
|
Visibility: gwapitypes.Visibility(r.Visibility),
|
|
|
|
GlobalVisibility: string(r.GlobalVisibility),
|
|
|
|
PassVarsToForkedPR: r.PassVarsToForkedPR,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
return res
|
2019-02-28 14:52:35 +00:00
|
|
|
}
|
2019-06-11 09:08:40 +00:00
|
|
|
|
|
|
|
type ProjectCreateRunHandler struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-06-11 09:08:40 +00:00
|
|
|
ah *action.ActionHandler
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewProjectCreateRunHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectCreateRunHandler {
|
|
|
|
return &ProjectCreateRunHandler{log: log, ah: ah}
|
2019-06-11 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ProjectCreateRunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
projectRef, err := url.PathUnescape(vars["projectref"])
|
|
|
|
if err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-06-11 09:08:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
var req gwapitypes.ProjectCreateRunRequest
|
2019-06-11 09:08:40 +00:00
|
|
|
d := json.NewDecoder(r.Body)
|
|
|
|
if err := d.Decode(&req); err != nil {
|
2022-02-21 11:19:55 +00:00
|
|
|
util.HTTPError(w, util.NewAPIError(util.ErrBadRequest, err))
|
2019-06-11 09:08:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.ah.ProjectCreateRun(ctx, projectRef, req.Branch, req.Tag, req.Ref, req.CommitSHA)
|
2022-02-21 11:19:55 +00:00
|
|
|
if util.HTTPError(w, err) {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-06-11 09:08:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:19:55 +00:00
|
|
|
if err := util.HTTPResponse(w, http.StatusCreated, nil); err != nil {
|
2022-02-21 17:07:58 +00:00
|
|
|
h.log.Err(err).Send()
|
2019-06-11 09:08:40 +00:00
|
|
|
}
|
|
|
|
}
|