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.

repo_hooks.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if models.IsErrWebhookNotExist(err) {
  116. ctx.Error(404)
  117. } else {
  118. ctx.APIError(500, "GetWebhookById", err)
  119. }
  120. return
  121. }
  122. if form.Config != nil {
  123. if url, ok := form.Config["url"]; ok {
  124. w.URL = url
  125. }
  126. if ct, ok := form.Config["content_type"]; ok {
  127. if !models.IsValidHookContentType(ct) {
  128. ctx.APIError(422, "", "Invalid content type")
  129. return
  130. }
  131. w.ContentType = models.ToHookContentType(ct)
  132. }
  133. if w.HookTaskType == models.SLACK {
  134. if channel, ok := form.Config["channel"]; ok {
  135. meta, err := json.Marshal(&models.SlackMeta{
  136. Channel: channel,
  137. Username: form.Config["username"],
  138. IconURL: form.Config["icon_url"],
  139. Color: form.Config["color"],
  140. })
  141. if err != nil {
  142. ctx.APIError(500, "slack: JSON marshal failed", err)
  143. return
  144. }
  145. w.Meta = string(meta)
  146. }
  147. }
  148. }
  149. // Update events
  150. if len(form.Events) == 0 {
  151. form.Events = []string{"push"}
  152. }
  153. w.PushOnly = false
  154. w.SendEverything = false
  155. w.ChooseEvents = true
  156. w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE))
  157. w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH))
  158. if err = w.UpdateEvent(); err != nil {
  159. ctx.APIError(500, "UpdateEvent", err)
  160. return
  161. }
  162. if form.Active != nil {
  163. w.IsActive = *form.Active
  164. }
  165. if err := models.UpdateWebhook(w); err != nil {
  166. ctx.APIError(500, "UpdateWebhook", err)
  167. return
  168. }
  169. ctx.JSON(200, ToApiHook(ctx.Repo.RepoLink, w))
  170. }