summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/repo
diff options
context:
space:
mode:
authorEthan Koenig <ethantkoenig@gmail.com>2016-12-06 23:36:28 -0500
committerKim "BKC" Carlbäcker <kim.carlbacker@gmail.com>2016-12-25 14:51:57 +0100
commit9847b38518fe19e0c764e92c51875443b3741e79 (patch)
tree52509b6813d3b9714a420220af52d697e364ed3b /routers/api/v1/repo
parentfa3abc22c06843bb27089e8c0f7aebf0f5d83fcc (diff)
downloadgitea-9847b38518fe19e0c764e92c51875443b3741e79.tar.gz
gitea-9847b38518fe19e0c764e92c51875443b3741e79.zip
Organization webhook API endpoints
Diffstat (limited to 'routers/api/v1/repo')
-rw-r--r--routers/api/v1/repo/hook.go148
1 files changed, 15 insertions, 133 deletions
diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go
index 1299f13923..51f64e3452 100644
--- a/routers/api/v1/repo/hook.go
+++ b/routers/api/v1/repo/hook.go
@@ -5,15 +5,12 @@
package repo
import (
- "encoding/json"
-
- "github.com/Unknwon/com"
-
api "code.gitea.io/sdk/gitea"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/routers/api/v1/convert"
+ "code.gitea.io/gitea/routers/api/v1/utils"
)
// ListHooks list all hooks of a repository
@@ -32,146 +29,31 @@ func ListHooks(ctx *context.APIContext) {
ctx.JSON(200, &apiHooks)
}
-// CreateHook create a hook for a repository
-// see https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
-func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
- if !models.IsValidHookTaskType(form.Type) {
- ctx.Error(422, "", "Invalid hook type")
- return
- }
- for _, name := range []string{"url", "content_type"} {
- if _, ok := form.Config[name]; !ok {
- ctx.Error(422, "", "Missing config option: "+name)
- return
- }
- }
- if !models.IsValidHookContentType(form.Config["content_type"]) {
- ctx.Error(422, "", "Invalid content type")
+// GetHook get a repo's hook by id
+func GetHook(ctx *context.APIContext) {
+ repo := ctx.Repo
+ hookID := ctx.ParamsInt64(":id")
+ hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
+ if err != nil {
return
}
+ ctx.JSON(200, convert.ToHook(repo.RepoLink, hook))
+}
- if len(form.Events) == 0 {
- form.Events = []string{"push"}
- }
- w := &models.Webhook{
- RepoID: ctx.Repo.Repository.ID,
- URL: form.Config["url"],
- ContentType: models.ToHookContentType(form.Config["content_type"]),
- Secret: form.Config["secret"],
- HookEvent: &models.HookEvent{
- ChooseEvents: true,
- HookEvents: models.HookEvents{
- Create: com.IsSliceContainsStr(form.Events, string(models.HookEventCreate)),
- Push: com.IsSliceContainsStr(form.Events, string(models.HookEventPush)),
- PullRequest: com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest)),
- },
- },
- IsActive: form.Active,
- HookTaskType: models.ToHookTaskType(form.Type),
- }
- if w.HookTaskType == models.SLACK {
- channel, ok := form.Config["channel"]
- if !ok {
- ctx.Error(422, "", "Missing config option: channel")
- return
- }
- meta, err := json.Marshal(&models.SlackMeta{
- Channel: channel,
- Username: form.Config["username"],
- IconURL: form.Config["icon_url"],
- Color: form.Config["color"],
- })
- if err != nil {
- ctx.Error(500, "slack: JSON marshal failed", err)
- return
- }
- w.Meta = string(meta)
- }
-
- if err := w.UpdateEvent(); err != nil {
- ctx.Error(500, "UpdateEvent", err)
- return
- } else if err := models.CreateWebhook(w); err != nil {
- ctx.Error(500, "CreateWebhook", err)
+// CreateHook create a hook for a repository
+// see https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
+func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
+ if !utils.CheckCreateHookOption(ctx, &form) {
return
}
-
- ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w))
+ utils.AddRepoHook(ctx, &form)
}
// EditHook modify a hook of a repository
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
func EditHook(ctx *context.APIContext, form api.EditHookOption) {
hookID := ctx.ParamsInt64(":id")
- w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
- if err != nil {
- if models.IsErrWebhookNotExist(err) {
- ctx.Status(404)
- } else {
- ctx.Error(500, "GetWebhookByID", err)
- }
- return
- }
-
- if form.Config != nil {
- if url, ok := form.Config["url"]; ok {
- w.URL = url
- }
- if ct, ok := form.Config["content_type"]; ok {
- if !models.IsValidHookContentType(ct) {
- ctx.Error(422, "", "Invalid content type")
- return
- }
- w.ContentType = models.ToHookContentType(ct)
- }
-
- if w.HookTaskType == models.SLACK {
- if channel, ok := form.Config["channel"]; ok {
- meta, err := json.Marshal(&models.SlackMeta{
- Channel: channel,
- Username: form.Config["username"],
- IconURL: form.Config["icon_url"],
- Color: form.Config["color"],
- })
- if err != nil {
- ctx.Error(500, "slack: JSON marshal failed", err)
- return
- }
- w.Meta = string(meta)
- }
- }
- }
-
- // Update events
- if len(form.Events) == 0 {
- form.Events = []string{"push"}
- }
- w.PushOnly = false
- w.SendEverything = false
- w.ChooseEvents = true
- w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
- w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
- w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
- if err = w.UpdateEvent(); err != nil {
- ctx.Error(500, "UpdateEvent", err)
- return
- }
-
- if form.Active != nil {
- w.IsActive = *form.Active
- }
-
- if err := models.UpdateWebhook(w); err != nil {
- ctx.Error(500, "UpdateWebhook", err)
- return
- }
-
- updated, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
- if err != nil {
- ctx.Error(500, "GetWebhookByRepoID", err)
- return
- }
- ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, updated))
+ utils.EditRepoHook(ctx, &form, hookID)
}
// DeleteHook delete a hook of a repository