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

hook.go 4.9 kB

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