harness-drone/handler/api/repos/chown.go

54 lines
1.3 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +00:00
// 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 (
"net/http"
2019-02-20 18:34:26 +00:00
"github.com/drone/drone/core"
2019-02-19 23:56:41 +00:00
"github.com/drone/drone/handler/api/render"
"github.com/drone/drone/handler/api/request"
"github.com/drone/drone/logger"
"github.com/go-chi/chi"
)
// HandleChown returns an http.HandlerFunc that processes http
// requests to chown the repository to the currently authenticated user.
func HandleChown(repos core.RepositoryStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
owner = chi.URLParam(r, "owner")
name = chi.URLParam(r, "name")
)
repo, err := repos.FindName(r.Context(), owner, name)
if err != nil {
render.NotFound(w, err)
logger.FromRequest(r).
WithError(err).
WithField("namespace", owner).
WithField("name", name).
Debugln("api: repository not found")
return
}
user, _ := request.UserFrom(r.Context())
repo.UserID = user.ID
err = repos.Update(r.Context(), repo)
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).
WithError(err).
WithField("namespace", owner).
WithField("name", name).
Debugln("api: cannot chown repository")
} else {
render.JSON(w, repo, 200)
}
}
}