Organization webhook API endpoints
This commit is contained in:
parent
fa3abc22c0
commit
9847b38518
4 changed files with 316 additions and 134 deletions
|
@ -266,7 +266,8 @@ func RegisterRoutes(m *macaron.Macaron) {
|
|||
m.Group("/hooks", func() {
|
||||
m.Combo("").Get(repo.ListHooks).
|
||||
Post(bind(api.CreateHookOption{}), repo.CreateHook)
|
||||
m.Combo("/:id").Patch(bind(api.EditHookOption{}), repo.EditHook).
|
||||
m.Combo("/:id").Get(repo.GetHook).
|
||||
Patch(bind(api.EditHookOption{}), repo.EditHook).
|
||||
Delete(repo.DeleteHook)
|
||||
})
|
||||
m.Put("/collaborators/:collaborator", bind(api.AddCollaboratorOption{}), repo.AddCollaborator)
|
||||
|
@ -343,6 +344,13 @@ func RegisterRoutes(m *macaron.Macaron) {
|
|||
m.Group("/orgs/:orgname", func() {
|
||||
m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
|
||||
m.Combo("/teams").Get(org.ListTeams)
|
||||
m.Group("/hooks", func() {
|
||||
m.Combo("").Get(org.ListHooks).
|
||||
Post(bind(api.CreateHookOption{}), org.CreateHook)
|
||||
m.Combo("/:id").Get(org.GetHook).
|
||||
Patch(bind(api.EditHookOption{}), org.EditHook).
|
||||
Delete(org.DeleteHook)
|
||||
})
|
||||
}, orgAssignment(true))
|
||||
|
||||
m.Any("/*", func(ctx *context.Context) {
|
||||
|
|
65
routers/api/v1/org/hook.go
Normal file
65
routers/api/v1/org/hook.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
// Copyright 2016 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package org
|
||||
|
||||
import (
|
||||
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 an organziation's webhooks
|
||||
func ListHooks(ctx *context.APIContext) {
|
||||
org := ctx.Org.Organization
|
||||
orgHooks, err := models.GetWebhooksByOrgID(org.ID)
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetWebhooksByOrgID", err)
|
||||
return
|
||||
}
|
||||
hooks := make([]*api.Hook, len(orgHooks))
|
||||
for i, hook := range orgHooks {
|
||||
hooks[i] = convert.ToHook(org.HomeLink(), hook)
|
||||
}
|
||||
ctx.JSON(200, hooks)
|
||||
}
|
||||
|
||||
// GetHook get an organization's hook by id
|
||||
func GetHook(ctx *context.APIContext) {
|
||||
org := ctx.Org.Organization
|
||||
hookID := ctx.ParamsInt64(":id")
|
||||
hook, err := utils.GetOrgHook(ctx, org.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx.JSON(200, convert.ToHook(org.HomeLink(), hook))
|
||||
}
|
||||
|
||||
// CreateHook create a hook for an organization
|
||||
func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
|
||||
if !utils.CheckCreateHookOption(ctx, &form) {
|
||||
return
|
||||
}
|
||||
utils.AddOrgHook(ctx, &form)
|
||||
}
|
||||
|
||||
// EditHook modify a hook of a repository
|
||||
func EditHook(ctx *context.APIContext, form api.EditHookOption) {
|
||||
hookID := ctx.ParamsInt64(":id")
|
||||
utils.EditOrgHook(ctx, &form, hookID)
|
||||
}
|
||||
|
||||
// DeleteHook delete a hook of an organization
|
||||
func DeleteHook(ctx *context.APIContext) {
|
||||
org := ctx.Org.Organization
|
||||
hookID := ctx.ParamsInt64(":id")
|
||||
if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
|
||||
ctx.Error(500, "DeleteWebhookByOrgID", err)
|
||||
return
|
||||
}
|
||||
ctx.Status(204)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
// 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")
|
||||
if !utils.CheckCreateHookOption(ctx, &form) {
|
||||
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")
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
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
|
||||
|
|
227
routers/api/v1/utils/hook.go
Normal file
227
routers/api/v1/utils/hook.go
Normal file
|
@ -0,0 +1,227 @@
|
|||
// Copyright 2016 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
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"
|
||||
"encoding/json"
|
||||
"github.com/Unknwon/com"
|
||||
)
|
||||
|
||||
// GetOrgHook get an organization's webhook. If there is an error, write to
|
||||
// `ctx` accordingly and return the error
|
||||
func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*models.Webhook, error) {
|
||||
w, err := models.GetWebhookByOrgID(orgID, hookID)
|
||||
if err != nil {
|
||||
if models.IsErrWebhookNotExist(err) {
|
||||
ctx.Status(404)
|
||||
} else {
|
||||
ctx.Error(500, "GetWebhookByOrgID", err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
|
||||
// GetRepoHook get a repo's webhook. If there is an error, write to `ctx`
|
||||
// accordingly and return the error
|
||||
func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*models.Webhook, error) {
|
||||
w, err := models.GetWebhookByRepoID(repoID, hookID)
|
||||
if err != nil {
|
||||
if models.IsErrWebhookNotExist(err) {
|
||||
ctx.Status(404)
|
||||
} else {
|
||||
ctx.Error(500, "GetWebhookByID", err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
|
||||
// CheckCreateHookOption check if a CreateHookOption form is valid. If invalid,
|
||||
// write the appropriate error to `ctx`. Return whether the form is valid
|
||||
func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
|
||||
if !models.IsValidHookTaskType(form.Type) {
|
||||
ctx.Error(422, "", "Invalid hook type")
|
||||
return false
|
||||
}
|
||||
for _, name := range []string{"url", "content_type"} {
|
||||
if _, ok := form.Config[name]; !ok {
|
||||
ctx.Error(422, "", "Missing config option: "+name)
|
||||
return false
|
||||
}
|
||||
}
|
||||
if !models.IsValidHookContentType(form.Config["content_type"]) {
|
||||
ctx.Error(422, "", "Invalid content type")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// AddOrgHook add a hook to an organization. Writes to `ctx` accordingly
|
||||
func AddOrgHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
||||
org := ctx.Org.Organization
|
||||
hook, ok := addHook(ctx, form, org.ID, 0)
|
||||
if ok {
|
||||
ctx.JSON(200, convert.ToHook(org.HomeLink(), hook))
|
||||
}
|
||||
}
|
||||
|
||||
// AddRepoHook add a hook to a repo. Writes to `ctx` accordingly
|
||||
func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
||||
repo := ctx.Repo
|
||||
hook, ok := addHook(ctx, form, 0, repo.Repository.ID)
|
||||
if ok {
|
||||
ctx.JSON(200, convert.ToHook(repo.RepoLink, hook))
|
||||
}
|
||||
}
|
||||
|
||||
// addHook add the hook specified by `form`, `orgID` and `repoID`. If there is
|
||||
// an error, write to `ctx` accordingly. Return (webhook, ok)
|
||||
func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID int64) (*models.Webhook, bool) {
|
||||
if len(form.Events) == 0 {
|
||||
form.Events = []string{"push"}
|
||||
}
|
||||
w := &models.Webhook{
|
||||
OrgID: orgID,
|
||||
RepoID: repoID,
|
||||
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 nil, false
|
||||
}
|
||||
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 nil, false
|
||||
}
|
||||
w.Meta = string(meta)
|
||||
}
|
||||
|
||||
if err := w.UpdateEvent(); err != nil {
|
||||
ctx.Error(500, "UpdateEvent", err)
|
||||
return nil, false
|
||||
} else if err := models.CreateWebhook(w); err != nil {
|
||||
ctx.Error(500, "CreateWebhook", err)
|
||||
return nil, false
|
||||
}
|
||||
return w, true
|
||||
}
|
||||
|
||||
// EditOrgHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
|
||||
func EditOrgHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
|
||||
org := ctx.Org.Organization
|
||||
hook, err := GetOrgHook(ctx, org.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !editHook(ctx, form, hook) {
|
||||
return
|
||||
}
|
||||
updated, err := GetOrgHook(ctx, org.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx.JSON(200, convert.ToHook(org.HomeLink(), updated))
|
||||
}
|
||||
|
||||
// EditRepoHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
|
||||
func EditRepoHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
|
||||
repo := ctx.Repo
|
||||
hook, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !editHook(ctx, form, hook) {
|
||||
return
|
||||
}
|
||||
updated, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx.JSON(200, convert.ToHook(repo.RepoLink, updated))
|
||||
}
|
||||
|
||||
// editHook edit the webhook `w` according to `form`. If an error occurs, write
|
||||
// to `ctx` accordingly and return the error. Return whether successful
|
||||
func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webhook) bool {
|
||||
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 false
|
||||
}
|
||||
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 false
|
||||
}
|
||||
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 false
|
||||
}
|
||||
|
||||
if form.Active != nil {
|
||||
w.IsActive = *form.Active
|
||||
}
|
||||
|
||||
if err := models.UpdateWebhook(w); err != nil {
|
||||
ctx.Error(500, "UpdateWebhook", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
Loading…
Reference in a new issue