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.

hook.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "encoding/json"
  7. "github.com/Unknwon/com"
  8. api "code.gitea.io/sdk/gitea"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/routers/api/v1/convert"
  12. )
  13. // ListHooks list all hooks of a repository
  14. // see https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks
  15. func ListHooks(ctx *context.APIContext) {
  16. hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  17. if err != nil {
  18. ctx.Error(500, "GetWebhooksByRepoID", err)
  19. return
  20. }
  21. apiHooks := make([]*api.Hook, len(hooks))
  22. for i := range hooks {
  23. apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
  24. }
  25. ctx.JSON(200, &apiHooks)
  26. }
  27. // CreateHook create a hook for a repository
  28. // see https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
  29. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  30. if !models.IsValidHookTaskType(form.Type) {
  31. ctx.Error(422, "", "Invalid hook type")
  32. return
  33. }
  34. for _, name := range []string{"url", "content_type"} {
  35. if _, ok := form.Config[name]; !ok {
  36. ctx.Error(422, "", "Missing config option: "+name)
  37. return
  38. }
  39. }
  40. if !models.IsValidHookContentType(form.Config["content_type"]) {
  41. ctx.Error(422, "", "Invalid content type")
  42. return
  43. }
  44. if len(form.Events) == 0 {
  45. form.Events = []string{"push"}
  46. }
  47. w := &models.Webhook{
  48. RepoID: ctx.Repo.Repository.ID,
  49. URL: form.Config["url"],
  50. ContentType: models.ToHookContentType(form.Config["content_type"]),
  51. Secret: form.Config["secret"],
  52. HookEvent: &models.HookEvent{
  53. ChooseEvents: true,
  54. HookEvents: models.HookEvents{
  55. Create: com.IsSliceContainsStr(form.Events, string(models.HookEventCreate)),
  56. Push: com.IsSliceContainsStr(form.Events, string(models.HookEventPush)),
  57. PullRequest: com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest)),
  58. },
  59. },
  60. IsActive: form.Active,
  61. HookTaskType: models.ToHookTaskType(form.Type),
  62. }
  63. if w.HookTaskType == models.SLACK {
  64. channel, ok := form.Config["channel"]
  65. if !ok {
  66. ctx.Error(422, "", "Missing config option: channel")
  67. return
  68. }
  69. meta, err := json.Marshal(&models.SlackMeta{
  70. Channel: channel,
  71. Username: form.Config["username"],
  72. IconURL: form.Config["icon_url"],
  73. Color: form.Config["color"],
  74. })
  75. if err != nil {
  76. ctx.Error(500, "slack: JSON marshal failed", err)
  77. return
  78. }
  79. w.Meta = string(meta)
  80. }
  81. if err := w.UpdateEvent(); err != nil {
  82. ctx.Error(500, "UpdateEvent", err)
  83. return
  84. } else if err := models.CreateWebhook(w); err != nil {
  85. ctx.Error(500, "CreateWebhook", err)
  86. return
  87. }
  88. ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w))
  89. }
  90. // EditHook modify a hook of a repository
  91. // see https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
  92. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  93. hookID := ctx.ParamsInt64(":id")
  94. w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
  95. if err != nil {
  96. if models.IsErrWebhookNotExist(err) {
  97. ctx.Status(404)
  98. } else {
  99. ctx.Error(500, "GetWebhookByID", err)
  100. }
  101. return
  102. }
  103. if form.Config != nil {
  104. if url, ok := form.Config["url"]; ok {
  105. w.URL = url
  106. }
  107. if ct, ok := form.Config["content_type"]; ok {
  108. if !models.IsValidHookContentType(ct) {
  109. ctx.Error(422, "", "Invalid content type")
  110. return
  111. }
  112. w.ContentType = models.ToHookContentType(ct)
  113. }
  114. if w.HookTaskType == models.SLACK {
  115. if channel, ok := form.Config["channel"]; ok {
  116. meta, err := json.Marshal(&models.SlackMeta{
  117. Channel: channel,
  118. Username: form.Config["username"],
  119. IconURL: form.Config["icon_url"],
  120. Color: form.Config["color"],
  121. })
  122. if err != nil {
  123. ctx.Error(500, "slack: JSON marshal failed", err)
  124. return
  125. }
  126. w.Meta = string(meta)
  127. }
  128. }
  129. }
  130. // Update events
  131. if len(form.Events) == 0 {
  132. form.Events = []string{"push"}
  133. }
  134. w.PushOnly = false
  135. w.SendEverything = false
  136. w.ChooseEvents = true
  137. w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
  138. w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
  139. w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
  140. if err = w.UpdateEvent(); err != nil {
  141. ctx.Error(500, "UpdateEvent", err)
  142. return
  143. }
  144. if form.Active != nil {
  145. w.IsActive = *form.Active
  146. }
  147. if err := models.UpdateWebhook(w); err != nil {
  148. ctx.Error(500, "UpdateWebhook", err)
  149. return
  150. }
  151. updated, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
  152. if err != nil {
  153. ctx.Error(500, "GetWebhookByRepoID", err)
  154. return
  155. }
  156. ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, updated))
  157. }
  158. // DeleteHook delete a hook of a repository
  159. func DeleteHook(ctx *context.APIContext) {
  160. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  161. ctx.Error(500, "DeleteWebhookByRepoID", err)
  162. return
  163. }
  164. ctx.Status(204)
  165. }