repo_hooks.go 5.3 KB

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