hooks.go 4.3 KB

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