harness-drone/handler/api/repos/disable.go
2019-02-19 15:56:41 -08:00

66 lines
1.6 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/handler/api/render"
"github.com/drone/drone/logger"
"github.com/drone/drone/core"
"github.com/go-chi/chi"
)
// HandleDisable returns an http.HandlerFunc that processes http
// requests to disable a repository in the system.
func HandleDisable(
repos core.RepositoryStore,
sender core.WebhookSender,
) 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
}
repo.Active = false
err = repos.Update(r.Context(), repo)
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).
WithError(err).
WithField("namespace", owner).
WithField("name", name).
Warnln("api: cannot update repository")
return
}
err = sender.Send(r.Context(), &core.WebhookData{
Event: core.WebhookEventRepo,
Action: core.WebhookActionDisabled,
Repo: repo,
})
if err != nil {
logger.FromRequest(r).
WithError(err).
WithField("namespace", owner).
WithField("name", name).
Warnln("api: cannot send webhook")
}
render.JSON(w, repo, 200)
}
}