110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||
|
// Use of this source code is governed by the Drone Non-Commercial License
|
||
|
// that can be found in the LICENSE file.
|
||
|
|
||
|
package repos
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/drone/drone/handler/api/render"
|
||
|
"github.com/drone/drone/handler/api/request"
|
||
|
"github.com/drone/drone/logger"
|
||
|
"github.com/drone/drone/core"
|
||
|
|
||
|
"github.com/go-chi/chi"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
repositoryInput struct {
|
||
|
Visibility *string `json:"visibility"`
|
||
|
Config *string `json:"config_path"`
|
||
|
Trusted *bool `json:"trusted"`
|
||
|
Protected *bool `json:"protected"`
|
||
|
Timeout *int64 `json:"timeout"`
|
||
|
Counter *int64 `json:"counter"`
|
||
|
}
|
||
|
)
|
||
|
|
||
|
// HandleUpdate returns an http.HandlerFunc that processes http
|
||
|
// requests to update the repository details.
|
||
|
func HandleUpdate(repos core.RepositoryStore) http.HandlerFunc {
|
||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
var (
|
||
|
owner = chi.URLParam(r, "owner")
|
||
|
name = chi.URLParam(r, "name")
|
||
|
slug = owner + "/" + name
|
||
|
)
|
||
|
user, _ := request.UserFrom(r.Context())
|
||
|
|
||
|
repo, err := repos.FindName(r.Context(), owner, name)
|
||
|
if err != nil {
|
||
|
render.NotFound(w, err)
|
||
|
logger.FromRequest(r).
|
||
|
WithError(err).
|
||
|
WithField("repository", slug).
|
||
|
Debugln("api: repository not found")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
in := new(repositoryInput)
|
||
|
err = json.NewDecoder(r.Body).Decode(in)
|
||
|
if err != nil {
|
||
|
render.BadRequest(w, err)
|
||
|
logger.FromRequest(r).
|
||
|
WithError(err).
|
||
|
WithField("repository", slug).
|
||
|
Debugln("api: cannot unmarshal json input")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if in.Visibility != nil {
|
||
|
repo.Visibility = *in.Visibility
|
||
|
}
|
||
|
if in.Config != nil {
|
||
|
repo.Config = *in.Config
|
||
|
}
|
||
|
if in.Protected != nil {
|
||
|
repo.Protected = *in.Protected
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// system administrator only
|
||
|
//
|
||
|
if user != nil && user.Admin {
|
||
|
if in.Trusted != nil {
|
||
|
repo.Trusted = *in.Trusted
|
||
|
}
|
||
|
if in.Timeout != nil {
|
||
|
repo.Timeout = *in.Timeout
|
||
|
}
|
||
|
if in.Counter != nil {
|
||
|
repo.Counter = *in.Counter
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// // right now the only repository field that a user
|
||
|
// // can update is the visibility field.
|
||
|
// if govalidator.IsIn(in.Visibility,
|
||
|
// core.VisibilityInternal,
|
||
|
// core.VisibilityPrivate,
|
||
|
// core.VisibilityPublic,
|
||
|
// ) {
|
||
|
// repo.Visibility = in.Visibility
|
||
|
// }
|
||
|
|
||
|
err = repos.Update(r.Context(), repo)
|
||
|
if err != nil {
|
||
|
render.InternalError(w, err)
|
||
|
logger.FromRequest(r).
|
||
|
WithError(err).
|
||
|
WithField("repository", slug).
|
||
|
Warnln("api: cannot update repository")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
render.JSON(w, repo, 200)
|
||
|
}
|
||
|
}
|