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.

245 lines
7.8 KiB

  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. "encoding/json"
  8. "net/http"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/routers/api/v1/convert"
  12. "github.com/Unknwon/com"
  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. Delete: com.IsSliceContainsStr(form.Events, string(models.HookEventDelete)),
  94. Fork: com.IsSliceContainsStr(form.Events, string(models.HookEventFork)),
  95. Issues: com.IsSliceContainsStr(form.Events, string(models.HookEventIssues)),
  96. IssueComment: com.IsSliceContainsStr(form.Events, string(models.HookEventIssueComment)),
  97. Push: com.IsSliceContainsStr(form.Events, string(models.HookEventPush)),
  98. PullRequest: com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest)),
  99. Repository: com.IsSliceContainsStr(form.Events, string(models.HookEventRepository)),
  100. Release: com.IsSliceContainsStr(form.Events, string(models.HookEventRelease)),
  101. },
  102. },
  103. IsActive: form.Active,
  104. HookTaskType: models.ToHookTaskType(form.Type),
  105. }
  106. if w.HookTaskType == models.SLACK {
  107. channel, ok := form.Config["channel"]
  108. if !ok {
  109. ctx.Error(422, "", "Missing config option: channel")
  110. return nil, false
  111. }
  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 nil, false
  121. }
  122. w.Meta = string(meta)
  123. }
  124. if err := w.UpdateEvent(); err != nil {
  125. ctx.Error(500, "UpdateEvent", err)
  126. return nil, false
  127. } else if err := models.CreateWebhook(w); err != nil {
  128. ctx.Error(500, "CreateWebhook", err)
  129. return nil, false
  130. }
  131. return w, true
  132. }
  133. // EditOrgHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
  134. func EditOrgHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
  135. org := ctx.Org.Organization
  136. hook, err := GetOrgHook(ctx, org.ID, hookID)
  137. if err != nil {
  138. return
  139. }
  140. if !editHook(ctx, form, hook) {
  141. return
  142. }
  143. updated, err := GetOrgHook(ctx, org.ID, hookID)
  144. if err != nil {
  145. return
  146. }
  147. ctx.JSON(200, convert.ToHook(org.HomeLink(), updated))
  148. }
  149. // EditRepoHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
  150. func EditRepoHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
  151. repo := ctx.Repo
  152. hook, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
  153. if err != nil {
  154. return
  155. }
  156. if !editHook(ctx, form, hook) {
  157. return
  158. }
  159. updated, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
  160. if err != nil {
  161. return
  162. }
  163. ctx.JSON(200, convert.ToHook(repo.RepoLink, updated))
  164. }
  165. // editHook edit the webhook `w` according to `form`. If an error occurs, write
  166. // to `ctx` accordingly and return the error. Return whether successful
  167. func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webhook) bool {
  168. if form.Config != nil {
  169. if url, ok := form.Config["url"]; ok {
  170. w.URL = url
  171. }
  172. if ct, ok := form.Config["content_type"]; ok {
  173. if !models.IsValidHookContentType(ct) {
  174. ctx.Error(422, "", "Invalid content type")
  175. return false
  176. }
  177. w.ContentType = models.ToHookContentType(ct)
  178. }
  179. if w.HookTaskType == models.SLACK {
  180. if channel, ok := form.Config["channel"]; ok {
  181. meta, err := json.Marshal(&models.SlackMeta{
  182. Channel: channel,
  183. Username: form.Config["username"],
  184. IconURL: form.Config["icon_url"],
  185. Color: form.Config["color"],
  186. })
  187. if err != nil {
  188. ctx.Error(500, "slack: JSON marshal failed", err)
  189. return false
  190. }
  191. w.Meta = string(meta)
  192. }
  193. }
  194. }
  195. // Update events
  196. if len(form.Events) == 0 {
  197. form.Events = []string{"push"}
  198. }
  199. w.PushOnly = false
  200. w.SendEverything = false
  201. w.ChooseEvents = true
  202. w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
  203. w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
  204. w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
  205. w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
  206. w.Delete = com.IsSliceContainsStr(form.Events, string(models.HookEventDelete))
  207. w.Fork = com.IsSliceContainsStr(form.Events, string(models.HookEventFork))
  208. w.Issues = com.IsSliceContainsStr(form.Events, string(models.HookEventIssues))
  209. w.IssueComment = com.IsSliceContainsStr(form.Events, string(models.HookEventIssueComment))
  210. w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
  211. w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
  212. w.Repository = com.IsSliceContainsStr(form.Events, string(models.HookEventRepository))
  213. w.Release = com.IsSliceContainsStr(form.Events, string(models.HookEventRelease))
  214. if err := w.UpdateEvent(); err != nil {
  215. ctx.Error(500, "UpdateEvent", err)
  216. return false
  217. }
  218. if form.Active != nil {
  219. w.IsActive = *form.Active
  220. }
  221. if err := models.UpdateWebhook(w); err != nil {
  222. ctx.Error(500, "UpdateWebhook", err)
  223. return false
  224. }
  225. return true
  226. }