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 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2016 The Gitea 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 utils
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/routers/api/v1/convert"
  10. "encoding/json"
  11. "github.com/Unknwon/com"
  12. "net/http"
  13. )
  14. // GetOrgHook get an organization's webhook. If there is an error, write to
  15. // `ctx` accordingly and return the error
  16. func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*models.Webhook, error) {
  17. w, err := models.GetWebhookByOrgID(orgID, hookID)
  18. if err != nil {
  19. if models.IsErrWebhookNotExist(err) {
  20. ctx.Status(404)
  21. } else {
  22. ctx.Error(500, "GetWebhookByOrgID", err)
  23. }
  24. return nil, err
  25. }
  26. return w, nil
  27. }
  28. // GetRepoHook get a repo's webhook. If there is an error, write to `ctx`
  29. // accordingly and return the error
  30. func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*models.Webhook, error) {
  31. w, err := models.GetWebhookByRepoID(repoID, hookID)
  32. if err != nil {
  33. if models.IsErrWebhookNotExist(err) {
  34. ctx.Status(404)
  35. } else {
  36. ctx.Error(500, "GetWebhookByID", err)
  37. }
  38. return nil, err
  39. }
  40. return w, nil
  41. }
  42. // CheckCreateHookOption check if a CreateHookOption form is valid. If invalid,
  43. // write the appropriate error to `ctx`. Return whether the form is valid
  44. func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
  45. if !models.IsValidHookTaskType(form.Type) {
  46. ctx.Error(422, "", "Invalid hook type")
  47. return false
  48. }
  49. for _, name := range []string{"url", "content_type"} {
  50. if _, ok := form.Config[name]; !ok {
  51. ctx.Error(422, "", "Missing config option: "+name)
  52. return false
  53. }
  54. }
  55. if !models.IsValidHookContentType(form.Config["content_type"]) {
  56. ctx.Error(422, "", "Invalid content type")
  57. return false
  58. }
  59. return true
  60. }
  61. // AddOrgHook add a hook to an organization. Writes to `ctx` accordingly
  62. func AddOrgHook(ctx *context.APIContext, form *api.CreateHookOption) {
  63. org := ctx.Org.Organization
  64. hook, ok := addHook(ctx, form, org.ID, 0)
  65. if ok {
  66. ctx.JSON(http.StatusCreated, convert.ToHook(org.HomeLink(), hook))
  67. }
  68. }
  69. // AddRepoHook add a hook to a repo. Writes to `ctx` accordingly
  70. func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
  71. repo := ctx.Repo
  72. hook, ok := addHook(ctx, form, 0, repo.Repository.ID)
  73. if ok {
  74. ctx.JSON(http.StatusCreated, convert.ToHook(repo.RepoLink, hook))
  75. }
  76. }
  77. // addHook add the hook specified by `form`, `orgID` and `repoID`. If there is
  78. // an error, write to `ctx` accordingly. Return (webhook, ok)
  79. func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID int64) (*models.Webhook, bool) {
  80. if len(form.Events) == 0 {
  81. form.Events = []string{"push"}
  82. }
  83. w := &models.Webhook{
  84. OrgID: orgID,
  85. RepoID: repoID,
  86. URL: form.Config["url"],
  87. ContentType: models.ToHookContentType(form.Config["content_type"]),
  88. Secret: form.Config["secret"],
  89. HookEvent: &models.HookEvent{
  90. ChooseEvents: true,
  91. HookEvents: models.HookEvents{
  92. Create: com.IsSliceContainsStr(form.Events, string(models.HookEventCreate)),
  93. Push: com.IsSliceContainsStr(form.Events, string(models.HookEventPush)),
  94. PullRequest: com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest)),
  95. },
  96. },
  97. IsActive: form.Active,
  98. HookTaskType: models.ToHookTaskType(form.Type),
  99. }
  100. if w.HookTaskType == models.SLACK {
  101. channel, ok := form.Config["channel"]
  102. if !ok {
  103. ctx.Error(422, "", "Missing config option: channel")
  104. return nil, false
  105. }
  106. meta, err := json.Marshal(&models.SlackMeta{
  107. Channel: channel,
  108. Username: form.Config["username"],
  109. IconURL: form.Config["icon_url"],
  110. Color: form.Config["color"],
  111. })
  112. if err != nil {
  113. ctx.Error(500, "slack: JSON marshal failed", err)
  114. return nil, false
  115. }
  116. w.Meta = string(meta)
  117. }
  118. if err := w.UpdateEvent(); err != nil {
  119. ctx.Error(500, "UpdateEvent", err)
  120. return nil, false
  121. } else if err := models.CreateWebhook(w); err != nil {
  122. ctx.Error(500, "CreateWebhook", err)
  123. return nil, false
  124. }
  125. return w, true
  126. }
  127. // EditOrgHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
  128. func EditOrgHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
  129. org := ctx.Org.Organization
  130. hook, err := GetOrgHook(ctx, org.ID, hookID)
  131. if err != nil {
  132. return
  133. }
  134. if !editHook(ctx, form, hook) {
  135. return
  136. }
  137. updated, err := GetOrgHook(ctx, org.ID, hookID)
  138. if err != nil {
  139. return
  140. }
  141. ctx.JSON(200, convert.ToHook(org.HomeLink(), updated))
  142. }
  143. // EditRepoHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
  144. func EditRepoHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
  145. repo := ctx.Repo
  146. hook, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
  147. if err != nil {
  148. return
  149. }
  150. if !editHook(ctx, form, hook) {
  151. return
  152. }
  153. updated, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
  154. if err != nil {
  155. return
  156. }
  157. ctx.JSON(200, convert.ToHook(repo.RepoLink, updated))
  158. }
  159. // editHook edit the webhook `w` according to `form`. If an error occurs, write
  160. // to `ctx` accordingly and return the error. Return whether successful
  161. func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webhook) bool {
  162. if form.Config != nil {
  163. if url, ok := form.Config["url"]; ok {
  164. w.URL = url
  165. }
  166. if ct, ok := form.Config["content_type"]; ok {
  167. if !models.IsValidHookContentType(ct) {
  168. ctx.Error(422, "", "Invalid content type")
  169. return false
  170. }
  171. w.ContentType = models.ToHookContentType(ct)
  172. }
  173. if w.HookTaskType == models.SLACK {
  174. if channel, ok := form.Config["channel"]; ok {
  175. meta, err := json.Marshal(&models.SlackMeta{
  176. Channel: channel,
  177. Username: form.Config["username"],
  178. IconURL: form.Config["icon_url"],
  179. Color: form.Config["color"],
  180. })
  181. if err != nil {
  182. ctx.Error(500, "slack: JSON marshal failed", err)
  183. return false
  184. }
  185. w.Meta = string(meta)
  186. }
  187. }
  188. }
  189. // Update events
  190. if len(form.Events) == 0 {
  191. form.Events = []string{"push"}
  192. }
  193. w.PushOnly = false
  194. w.SendEverything = false
  195. w.ChooseEvents = true
  196. w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
  197. w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
  198. w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
  199. if err := w.UpdateEvent(); err != nil {
  200. ctx.Error(500, "UpdateEvent", err)
  201. return false
  202. }
  203. if form.Active != nil {
  204. w.IsActive = *form.Active
  205. }
  206. if err := models.UpdateWebhook(w); err != nil {
  207. ctx.Error(500, "UpdateWebhook", err)
  208. return false
  209. }
  210. return true
  211. }