hook.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/models/errors"
  10. "gitote/gitote/pkg/context"
  11. "gitote/gitote/routes/api/v1/convert"
  12. "github.com/json-iterator/go"
  13. "gitlab.com/gitote/com"
  14. api "gitlab.com/gitote/go-gitote-client"
  15. )
  16. // ListHooks list all hooks of a repository
  17. func ListHooks(c *context.APIContext) {
  18. hooks, err := models.GetWebhooksByRepoID(c.Repo.Repository.ID)
  19. if err != nil {
  20. c.Error(500, "GetWebhooksByRepoID", err)
  21. return
  22. }
  23. apiHooks := make([]*api.Hook, len(hooks))
  24. for i := range hooks {
  25. apiHooks[i] = convert.ToHook(c.Repo.RepoLink, hooks[i])
  26. }
  27. c.JSON(200, &apiHooks)
  28. }
  29. // CreateHook create a hook for a repository
  30. func CreateHook(c *context.APIContext, form api.CreateHookOption) {
  31. if !models.IsValidHookTaskType(form.Type) {
  32. c.Error(422, "", "Invalid hook type")
  33. return
  34. }
  35. for _, name := range []string{"url", "content_type"} {
  36. if _, ok := form.Config[name]; !ok {
  37. c.Error(422, "", "Missing config option: "+name)
  38. return
  39. }
  40. }
  41. if !models.IsValidHookContentType(form.Config["content_type"]) {
  42. c.Error(422, "", "Invalid content type")
  43. return
  44. }
  45. if len(form.Events) == 0 {
  46. form.Events = []string{"push"}
  47. }
  48. w := &models.Webhook{
  49. RepoID: c.Repo.Repository.ID,
  50. URL: form.Config["url"],
  51. ContentType: models.ToHookContentType(form.Config["content_type"]),
  52. Secret: form.Config["secret"],
  53. HookEvent: &models.HookEvent{
  54. ChooseEvents: true,
  55. HookEvents: models.HookEvents{
  56. Create: com.IsSliceContainsStr(form.Events, string(models.HookEventCreate)),
  57. Delete: com.IsSliceContainsStr(form.Events, string(models.HookEventDelete)),
  58. Fork: com.IsSliceContainsStr(form.Events, string(models.HookEventFork)),
  59. Push: com.IsSliceContainsStr(form.Events, string(models.HookEventPush)),
  60. Issues: com.IsSliceContainsStr(form.Events, string(models.HookEventIssues)),
  61. IssueComment: com.IsSliceContainsStr(form.Events, string(models.HookEventIssueComment)),
  62. PullRequest: com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest)),
  63. Release: com.IsSliceContainsStr(form.Events, string(models.HookEventRelease)),
  64. },
  65. },
  66. IsActive: form.Active,
  67. HookTaskType: models.ToHookTaskType(form.Type),
  68. }
  69. if w.HookTaskType == models.SLACK {
  70. channel, ok := form.Config["channel"]
  71. if !ok {
  72. c.Error(422, "", "Missing config option: channel")
  73. return
  74. }
  75. meta, err := jsoniter.Marshal(&models.SlackMeta{
  76. Channel: channel,
  77. Username: form.Config["username"],
  78. IconURL: form.Config["icon_url"],
  79. Color: form.Config["color"],
  80. })
  81. if err != nil {
  82. c.Error(500, "slack: JSON marshal failed", err)
  83. return
  84. }
  85. w.Meta = string(meta)
  86. }
  87. if err := w.UpdateEvent(); err != nil {
  88. c.Error(500, "UpdateEvent", err)
  89. return
  90. } else if err := models.CreateWebhook(w); err != nil {
  91. c.Error(500, "CreateWebhook", err)
  92. return
  93. }
  94. c.JSON(201, convert.ToHook(c.Repo.RepoLink, w))
  95. }
  96. // EditHook modify a hook of a repository
  97. func EditHook(c *context.APIContext, form api.EditHookOption) {
  98. w, err := models.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  99. if err != nil {
  100. if errors.IsWebhookNotExist(err) {
  101. c.Status(404)
  102. } else {
  103. c.Error(500, "GetWebhookOfRepoByID", err)
  104. }
  105. return
  106. }
  107. if form.Config != nil {
  108. if url, ok := form.Config["url"]; ok {
  109. w.URL = url
  110. }
  111. if ct, ok := form.Config["content_type"]; ok {
  112. if !models.IsValidHookContentType(ct) {
  113. c.Error(422, "", "Invalid content type")
  114. return
  115. }
  116. w.ContentType = models.ToHookContentType(ct)
  117. }
  118. if w.HookTaskType == models.SLACK {
  119. if channel, ok := form.Config["channel"]; ok {
  120. meta, err := jsoniter.Marshal(&models.SlackMeta{
  121. Channel: channel,
  122. Username: form.Config["username"],
  123. IconURL: form.Config["icon_url"],
  124. Color: form.Config["color"],
  125. })
  126. if err != nil {
  127. c.Error(500, "slack: JSON marshal failed", err)
  128. return
  129. }
  130. w.Meta = string(meta)
  131. }
  132. }
  133. }
  134. // Update events
  135. if len(form.Events) == 0 {
  136. form.Events = []string{"push"}
  137. }
  138. w.PushOnly = false
  139. w.SendEverything = false
  140. w.ChooseEvents = true
  141. w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
  142. w.Delete = com.IsSliceContainsStr(form.Events, string(models.HookEventDelete))
  143. w.Fork = com.IsSliceContainsStr(form.Events, string(models.HookEventFork))
  144. w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
  145. w.Issues = com.IsSliceContainsStr(form.Events, string(models.HookEventIssues))
  146. w.IssueComment = com.IsSliceContainsStr(form.Events, string(models.HookEventIssueComment))
  147. w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
  148. w.Release = com.IsSliceContainsStr(form.Events, string(models.HookEventRelease))
  149. if err = w.UpdateEvent(); err != nil {
  150. c.Error(500, "UpdateEvent", err)
  151. return
  152. }
  153. if form.Active != nil {
  154. w.IsActive = *form.Active
  155. }
  156. if err := models.UpdateWebhook(w); err != nil {
  157. c.Error(500, "UpdateWebhook", err)
  158. return
  159. }
  160. c.JSON(200, convert.ToHook(c.Repo.RepoLink, w))
  161. }
  162. // DeleteHook delete a hook of a repository
  163. func DeleteHook(c *context.APIContext) {
  164. if err := models.DeleteWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  165. c.Error(500, "DeleteWebhookByRepoID", err)
  166. return
  167. }
  168. c.Status(204)
  169. }