You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.8 KiB

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