hook.go 5.5 KB

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