-
Notifications
You must be signed in to change notification settings - Fork 104
/
create_hooks.go
45 lines (38 loc) · 1.2 KB
/
create_hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package repo
import (
"net/http"
"strconv"
"github.com/bleenco/abstruse/pkg/gitscm"
"github.com/bleenco/abstruse/pkg/lib"
"github.com/bleenco/abstruse/server/api/middlewares"
"github.com/bleenco/abstruse/server/api/render"
"github.com/bleenco/abstruse/server/core"
"github.com/go-chi/chi"
)
// HandleCreateHooks returns an http.HandlerFunc that writes JSON encoded
// result about creating webhooks on repository to the http response body.
func HandleCreateHooks(repos core.RepositoryStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
claims := middlewares.ClaimsFromCtx(r.Context())
var f gitscm.HookForm
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
render.InternalServerError(w, err.Error())
return
}
defer r.Body.Close()
if err := lib.DecodeJSON(r.Body, &f); err != nil {
render.InternalServerError(w, err.Error())
return
}
if perm := repos.GetPermissions(uint(id), claims.ID); !perm.Write {
render.UnathorizedError(w, "permission denied")
return
}
if err := repos.CreateHook(uint(id), claims.ID, f); err != nil {
render.InternalServerError(w, err.Error())
return
}
render.JSON(w, http.StatusOK, render.Empty{})
}
}