harness-drone/handler/api/repos/chown.go
2019-02-20 10:34:26 -08:00

53 lines
1.3 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 (
"net/http"
"github.com/drone/drone/core"
"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)
}
}
}