Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organziation webhook API endpoints #372

Merged
merged 2 commits into from
Dec 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
65 changes: 65 additions & 0 deletions routers/api/v1/org/hook.go
Original file line number Diff line number Diff line change
@@ -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)
}
148 changes: 15 additions & 133 deletions routers/api/v1/repo/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading