2014-11-13 13:02:18 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-12-05 03:46:42 +05:30
|
|
|
package repo
|
2014-11-13 13:02:18 +05:30
|
|
|
|
|
|
|
import (
|
2016-11-11 15:09:44 +05:30
|
|
|
api "code.gitea.io/sdk/gitea"
|
2014-11-15 03:41:30 +05:30
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/routers/api/v1/convert"
|
2016-12-07 10:06:28 +05:30
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2014-11-13 13:02:18 +05:30
|
|
|
)
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// ListHooks list all hooks of a repository
|
2016-03-14 04:19:16 +05:30
|
|
|
func ListHooks(ctx *context.APIContext) {
|
2017-08-21 16:43:47 +05:30
|
|
|
// swagger:route GET /repos/{username}/{reponame}/hooks repository repoListHooks
|
2017-05-02 19:05:59 +05:30
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// - application/json
|
|
|
|
//
|
|
|
|
// Responses:
|
2017-08-21 16:43:47 +05:30
|
|
|
// 200: HookList
|
2017-05-02 19:05:59 +05:30
|
|
|
// 500: error
|
|
|
|
|
2015-11-19 07:51:47 +05:30
|
|
|
hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
|
2014-11-13 13:02:18 +05:30
|
|
|
if err != nil {
|
2016-03-14 04:19:16 +05:30
|
|
|
ctx.Error(500, "GetWebhooksByRepoID", err)
|
2014-11-13 13:02:18 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-15 03:41:30 +05:30
|
|
|
apiHooks := make([]*api.Hook, len(hooks))
|
2014-11-13 13:02:18 +05:30
|
|
|
for i := range hooks {
|
2016-03-14 08:50:22 +05:30
|
|
|
apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
|
2014-11-13 13:02:18 +05:30
|
|
|
}
|
|
|
|
ctx.JSON(200, &apiHooks)
|
|
|
|
}
|
2014-11-13 23:27:00 +05:30
|
|
|
|
2016-12-07 10:06:28 +05:30
|
|
|
// 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 {
|
2014-11-13 23:27:00 +05:30
|
|
|
return
|
|
|
|
}
|
2016-12-07 10:06:28 +05:30
|
|
|
ctx.JSON(200, convert.ToHook(repo.RepoLink, hook))
|
|
|
|
}
|
2014-11-13 23:27:00 +05:30
|
|
|
|
2016-12-07 10:06:28 +05:30
|
|
|
// CreateHook create a hook for a repository
|
|
|
|
func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
|
2017-08-21 16:43:47 +05:30
|
|
|
// swagger:route POST /repos/{username}/{reponame}/hooks repository repoCreateHook
|
2017-05-02 19:05:59 +05:30
|
|
|
//
|
|
|
|
// Consumes:
|
|
|
|
// - application/json
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// - application/json
|
|
|
|
//
|
|
|
|
// Responses:
|
2017-08-21 16:43:47 +05:30
|
|
|
// 200: Hook
|
2017-05-02 19:05:59 +05:30
|
|
|
// 422: validationError
|
|
|
|
// 500: error
|
|
|
|
|
2016-12-07 10:06:28 +05:30
|
|
|
if !utils.CheckCreateHookOption(ctx, &form) {
|
2014-11-13 23:27:00 +05:30
|
|
|
return
|
|
|
|
}
|
2016-12-07 10:06:28 +05:30
|
|
|
utils.AddRepoHook(ctx, &form)
|
2014-11-13 23:27:00 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// EditHook modify a hook of a repository
|
2016-03-14 04:19:16 +05:30
|
|
|
func EditHook(ctx *context.APIContext, form api.EditHookOption) {
|
2017-08-21 16:43:47 +05:30
|
|
|
// swagger:route PATCH /repos/{username}/{reponame}/hooks/{id} repository repoEditHook
|
2017-05-02 19:05:59 +05:30
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// - application/json
|
|
|
|
//
|
|
|
|
// Responses:
|
2017-08-21 16:43:47 +05:30
|
|
|
// 200: Hook
|
2017-05-02 19:05:59 +05:30
|
|
|
// 422: validationError
|
|
|
|
// 500: error
|
|
|
|
|
2016-12-08 02:55:29 +05:30
|
|
|
hookID := ctx.ParamsInt64(":id")
|
2016-12-07 10:06:28 +05:30
|
|
|
utils.EditRepoHook(ctx, &form, hookID)
|
2014-11-13 23:27:00 +05:30
|
|
|
}
|
2016-07-17 06:03:59 +05:30
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// DeleteHook delete a hook of a repository
|
2016-07-17 06:03:59 +05:30
|
|
|
func DeleteHook(ctx *context.APIContext) {
|
2017-08-21 16:43:47 +05:30
|
|
|
// swagger:route DELETE /repos/{username}/{reponame}/hooks/{id} repository repoDeleteHook
|
2017-05-02 19:05:59 +05:30
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// - application/json
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 204: empty
|
|
|
|
// 404: notFound
|
|
|
|
// 500: error
|
|
|
|
|
2016-07-17 06:03:59 +05:30
|
|
|
if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
|
2017-01-14 07:44:48 +05:30
|
|
|
if models.IsErrWebhookNotExist(err) {
|
|
|
|
ctx.Status(404)
|
|
|
|
} else {
|
|
|
|
ctx.Error(500, "DeleteWebhookByRepoID", err)
|
|
|
|
}
|
2016-07-17 06:03:59 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Status(204)
|
|
|
|
}
|