hook.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/context"
  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 *context.APIContext) {
  15. hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  16. if err != nil {
  17. ctx.Error(500, "GetWebhooksByRepoID", err)
  18. return
  19. }
  20. apiHooks := make([]*api.Hook, len(hooks))
  21. for i := range hooks {
  22. apiHooks[i] = convert.ToHook(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 *context.APIContext, form api.CreateHookOption) {
  28. if !models.IsValidHookTaskType(form.Type) {
  29. ctx.Error(422, "", "Invalid hook type")
  30. return
  31. }
  32. for _, name := range []string{"url", "content_type"} {
  33. if _, ok := form.Config[name]; !ok {
  34. ctx.Error(422, "", "Missing config option: "+name)
  35. return
  36. }
  37. }
  38. if !models.IsValidHookContentType(form.Config["content_type"]) {
  39. ctx.Error(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. PullRequest: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PULL_REQUEST)),
  56. },
  57. },
  58. IsActive: form.Active,
  59. HookTaskType: models.ToHookTaskType(form.Type),
  60. }
  61. if w.HookTaskType == models.SLACK {
  62. channel, ok := form.Config["channel"]
  63. if !ok {
  64. ctx.Error(422, "", "Missing config option: channel")
  65. return
  66. }
  67. meta, err := json.Marshal(&models.SlackMeta{
  68. Channel: channel,
  69. Username: form.Config["username"],
  70. IconURL: form.Config["icon_url"],
  71. Color: form.Config["color"],
  72. })
  73. if err != nil {
  74. ctx.Error(500, "slack: JSON marshal failed", err)
  75. return
  76. }
  77. w.Meta = string(meta)
  78. }
  79. if err := w.UpdateEvent(); err != nil {
  80. ctx.Error(500, "UpdateEvent", err)
  81. return
  82. } else if err := models.CreateWebhook(w); err != nil {
  83. ctx.Error(500, "CreateWebhook", err)
  84. return
  85. }
  86. ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w))
  87. }
  88. // https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
  89. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  90. w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  91. if err != nil {
  92. if models.IsErrWebhookNotExist(err) {
  93. ctx.Status(404)
  94. } else {
  95. ctx.Error(500, "GetWebhookByID", err)
  96. }
  97. return
  98. }
  99. if form.Config != nil {
  100. if url, ok := form.Config["url"]; ok {
  101. w.URL = url
  102. }
  103. if ct, ok := form.Config["content_type"]; ok {
  104. if !models.IsValidHookContentType(ct) {
  105. ctx.Error(422, "", "Invalid content type")
  106. return
  107. }
  108. w.ContentType = models.ToHookContentType(ct)
  109. }
  110. if w.HookTaskType == models.SLACK {
  111. if channel, ok := form.Config["channel"]; ok {
  112. meta, err := json.Marshal(&models.SlackMeta{
  113. Channel: channel,
  114. Username: form.Config["username"],
  115. IconURL: form.Config["icon_url"],
  116. Color: form.Config["color"],
  117. })
  118. if err != nil {
  119. ctx.Error(500, "slack: JSON marshal failed", err)
  120. return
  121. }
  122. w.Meta = string(meta)
  123. }
  124. }
  125. }
  126. // Update events
  127. if len(form.Events) == 0 {
  128. form.Events = []string{"push"}
  129. }
  130. w.PushOnly = false
  131. w.SendEverything = false
  132. w.ChooseEvents = true
  133. w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE))
  134. w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH))
  135. w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PULL_REQUEST))
  136. if err = w.UpdateEvent(); err != nil {
  137. ctx.Error(500, "UpdateEvent", err)
  138. return
  139. }
  140. if form.Active != nil {
  141. w.IsActive = *form.Active
  142. }
  143. if err := models.UpdateWebhook(w); err != nil {
  144. ctx.Error(500, "UpdateWebhook", err)
  145. return
  146. }
  147. ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, w))
  148. }
  149. func DeleteHook(ctx *context.APIContext) {
  150. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  151. ctx.Error(500, "DeleteWebhookByRepoID", err)
  152. return
  153. }
  154. ctx.Status(204)
  155. }