You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.7 KiB

9 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/routers/api/v1/convert"
  10. "code.gitea.io/gitea/routers/api/v1/utils"
  11. )
  12. // ListHooks list all hooks of a repository
  13. func ListHooks(ctx *context.APIContext) {
  14. // swagger:route GET /repos/{username}/{reponame}/hooks repository repoListHooks
  15. //
  16. // Produces:
  17. // - application/json
  18. //
  19. // Responses:
  20. // 200: HookList
  21. // 500: error
  22. hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  23. if err != nil {
  24. ctx.Error(500, "GetWebhooksByRepoID", err)
  25. return
  26. }
  27. apiHooks := make([]*api.Hook, len(hooks))
  28. for i := range hooks {
  29. apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
  30. }
  31. ctx.JSON(200, &apiHooks)
  32. }
  33. // GetHook get a repo's hook by id
  34. func GetHook(ctx *context.APIContext) {
  35. repo := ctx.Repo
  36. hookID := ctx.ParamsInt64(":id")
  37. hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
  38. if err != nil {
  39. return
  40. }
  41. ctx.JSON(200, convert.ToHook(repo.RepoLink, hook))
  42. }
  43. // CreateHook create a hook for a repository
  44. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  45. // swagger:route POST /repos/{username}/{reponame}/hooks repository repoCreateHook
  46. //
  47. // Consumes:
  48. // - application/json
  49. //
  50. // Produces:
  51. // - application/json
  52. //
  53. // Responses:
  54. // 200: Hook
  55. // 422: validationError
  56. // 500: error
  57. if !utils.CheckCreateHookOption(ctx, &form) {
  58. return
  59. }
  60. utils.AddRepoHook(ctx, &form)
  61. }
  62. // EditHook modify a hook of a repository
  63. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  64. // swagger:route PATCH /repos/{username}/{reponame}/hooks/{id} repository repoEditHook
  65. //
  66. // Produces:
  67. // - application/json
  68. //
  69. // Responses:
  70. // 200: Hook
  71. // 422: validationError
  72. // 500: error
  73. hookID := ctx.ParamsInt64(":id")
  74. utils.EditRepoHook(ctx, &form, hookID)
  75. }
  76. // DeleteHook delete a hook of a repository
  77. func DeleteHook(ctx *context.APIContext) {
  78. // swagger:route DELETE /repos/{username}/{reponame}/hooks/{id} repository repoDeleteHook
  79. //
  80. // Produces:
  81. // - application/json
  82. //
  83. // Responses:
  84. // 204: empty
  85. // 404: notFound
  86. // 500: error
  87. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  88. if models.IsErrWebhookNotExist(err) {
  89. ctx.Status(404)
  90. } else {
  91. ctx.Error(500, "DeleteWebhookByRepoID", err)
  92. }
  93. return
  94. }
  95. ctx.Status(204)
  96. }