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.

274 lines
9.7 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. "encoding/json"
  7. "net/http"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/convert"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/webhook"
  14. "code.gitea.io/gitea/routers/utils"
  15. "github.com/unknwon/com"
  16. )
  17. // GetOrgHook get an organization's webhook. If there is an error, write to
  18. // `ctx` accordingly and return the error
  19. func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*models.Webhook, error) {
  20. w, err := models.GetWebhookByOrgID(orgID, hookID)
  21. if err != nil {
  22. if models.IsErrWebhookNotExist(err) {
  23. ctx.NotFound()
  24. } else {
  25. ctx.Error(http.StatusInternalServerError, "GetWebhookByOrgID", err)
  26. }
  27. return nil, err
  28. }
  29. return w, nil
  30. }
  31. // GetRepoHook get a repo's webhook. If there is an error, write to `ctx`
  32. // accordingly and return the error
  33. func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*models.Webhook, error) {
  34. w, err := models.GetWebhookByRepoID(repoID, hookID)
  35. if err != nil {
  36. if models.IsErrWebhookNotExist(err) {
  37. ctx.NotFound()
  38. } else {
  39. ctx.Error(http.StatusInternalServerError, "GetWebhookByID", err)
  40. }
  41. return nil, err
  42. }
  43. return w, nil
  44. }
  45. // CheckCreateHookOption check if a CreateHookOption form is valid. If invalid,
  46. // write the appropriate error to `ctx`. Return whether the form is valid
  47. func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
  48. if !models.IsValidHookTaskType(form.Type) {
  49. ctx.Error(http.StatusUnprocessableEntity, "", "Invalid hook type")
  50. return false
  51. }
  52. for _, name := range []string{"url", "content_type"} {
  53. if _, ok := form.Config[name]; !ok {
  54. ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: "+name)
  55. return false
  56. }
  57. }
  58. if !models.IsValidHookContentType(form.Config["content_type"]) {
  59. ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
  60. return false
  61. }
  62. return true
  63. }
  64. // AddOrgHook add a hook to an organization. Writes to `ctx` accordingly
  65. func AddOrgHook(ctx *context.APIContext, form *api.CreateHookOption) {
  66. org := ctx.Org.Organization
  67. hook, ok := addHook(ctx, form, org.ID, 0)
  68. if ok {
  69. ctx.JSON(http.StatusCreated, convert.ToHook(org.HomeLink(), hook))
  70. }
  71. }
  72. // AddRepoHook add a hook to a repo. Writes to `ctx` accordingly
  73. func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
  74. repo := ctx.Repo
  75. hook, ok := addHook(ctx, form, 0, repo.Repository.ID)
  76. if ok {
  77. ctx.JSON(http.StatusCreated, convert.ToHook(repo.RepoLink, hook))
  78. }
  79. }
  80. func issuesHook(events []string, event string) bool {
  81. return com.IsSliceContainsStr(events, event) || com.IsSliceContainsStr(events, string(models.HookEventIssues))
  82. }
  83. func pullHook(events []string, event string) bool {
  84. return com.IsSliceContainsStr(events, event) || com.IsSliceContainsStr(events, string(models.HookEventPullRequest))
  85. }
  86. // addHook add the hook specified by `form`, `orgID` and `repoID`. If there is
  87. // an error, write to `ctx` accordingly. Return (webhook, ok)
  88. func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID int64) (*models.Webhook, bool) {
  89. if len(form.Events) == 0 {
  90. form.Events = []string{"push"}
  91. }
  92. w := &models.Webhook{
  93. OrgID: orgID,
  94. RepoID: repoID,
  95. URL: form.Config["url"],
  96. ContentType: models.ToHookContentType(form.Config["content_type"]),
  97. Secret: form.Config["secret"],
  98. HTTPMethod: "POST",
  99. HookEvent: &models.HookEvent{
  100. ChooseEvents: true,
  101. HookEvents: models.HookEvents{
  102. Create: com.IsSliceContainsStr(form.Events, string(models.HookEventCreate)),
  103. Delete: com.IsSliceContainsStr(form.Events, string(models.HookEventDelete)),
  104. Fork: com.IsSliceContainsStr(form.Events, string(models.HookEventFork)),
  105. Issues: issuesHook(form.Events, "issues_only"),
  106. IssueAssign: issuesHook(form.Events, string(models.HookEventIssueAssign)),
  107. IssueLabel: issuesHook(form.Events, string(models.HookEventIssueLabel)),
  108. IssueMilestone: issuesHook(form.Events, string(models.HookEventIssueMilestone)),
  109. IssueComment: issuesHook(form.Events, string(models.HookEventIssueComment)),
  110. Push: com.IsSliceContainsStr(form.Events, string(models.HookEventPush)),
  111. PullRequest: pullHook(form.Events, "pull_request_only"),
  112. PullRequestAssign: pullHook(form.Events, string(models.HookEventPullRequestAssign)),
  113. PullRequestLabel: pullHook(form.Events, string(models.HookEventPullRequestLabel)),
  114. PullRequestMilestone: pullHook(form.Events, string(models.HookEventPullRequestMilestone)),
  115. PullRequestComment: pullHook(form.Events, string(models.HookEventPullRequestComment)),
  116. PullRequestReview: pullHook(form.Events, "pull_request_review"),
  117. PullRequestSync: pullHook(form.Events, string(models.HookEventPullRequestSync)),
  118. Repository: com.IsSliceContainsStr(form.Events, string(models.HookEventRepository)),
  119. Release: com.IsSliceContainsStr(form.Events, string(models.HookEventRelease)),
  120. },
  121. BranchFilter: form.BranchFilter,
  122. },
  123. IsActive: form.Active,
  124. HookTaskType: models.ToHookTaskType(form.Type),
  125. }
  126. if w.HookTaskType == models.SLACK {
  127. channel, ok := form.Config["channel"]
  128. if !ok {
  129. ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel")
  130. return nil, false
  131. }
  132. if !utils.IsValidSlackChannel(channel) {
  133. ctx.Error(http.StatusBadRequest, "", "Invalid slack channel name")
  134. return nil, false
  135. }
  136. meta, err := json.Marshal(&webhook.SlackMeta{
  137. Channel: strings.TrimSpace(channel),
  138. Username: form.Config["username"],
  139. IconURL: form.Config["icon_url"],
  140. Color: form.Config["color"],
  141. })
  142. if err != nil {
  143. ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
  144. return nil, false
  145. }
  146. w.Meta = string(meta)
  147. }
  148. if err := w.UpdateEvent(); err != nil {
  149. ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
  150. return nil, false
  151. } else if err := models.CreateWebhook(w); err != nil {
  152. ctx.Error(http.StatusInternalServerError, "CreateWebhook", err)
  153. return nil, false
  154. }
  155. return w, true
  156. }
  157. // EditOrgHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
  158. func EditOrgHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
  159. org := ctx.Org.Organization
  160. hook, err := GetOrgHook(ctx, org.ID, hookID)
  161. if err != nil {
  162. return
  163. }
  164. if !editHook(ctx, form, hook) {
  165. return
  166. }
  167. updated, err := GetOrgHook(ctx, org.ID, hookID)
  168. if err != nil {
  169. return
  170. }
  171. ctx.JSON(http.StatusOK, convert.ToHook(org.HomeLink(), updated))
  172. }
  173. // EditRepoHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
  174. func EditRepoHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
  175. repo := ctx.Repo
  176. hook, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
  177. if err != nil {
  178. return
  179. }
  180. if !editHook(ctx, form, hook) {
  181. return
  182. }
  183. updated, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
  184. if err != nil {
  185. return
  186. }
  187. ctx.JSON(http.StatusOK, convert.ToHook(repo.RepoLink, updated))
  188. }
  189. // editHook edit the webhook `w` according to `form`. If an error occurs, write
  190. // to `ctx` accordingly and return the error. Return whether successful
  191. func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webhook) bool {
  192. if form.Config != nil {
  193. if url, ok := form.Config["url"]; ok {
  194. w.URL = url
  195. }
  196. if ct, ok := form.Config["content_type"]; ok {
  197. if !models.IsValidHookContentType(ct) {
  198. ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
  199. return false
  200. }
  201. w.ContentType = models.ToHookContentType(ct)
  202. }
  203. if w.HookTaskType == models.SLACK {
  204. if channel, ok := form.Config["channel"]; ok {
  205. meta, err := json.Marshal(&webhook.SlackMeta{
  206. Channel: channel,
  207. Username: form.Config["username"],
  208. IconURL: form.Config["icon_url"],
  209. Color: form.Config["color"],
  210. })
  211. if err != nil {
  212. ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
  213. return false
  214. }
  215. w.Meta = string(meta)
  216. }
  217. }
  218. }
  219. // Update events
  220. if len(form.Events) == 0 {
  221. form.Events = []string{"push"}
  222. }
  223. w.PushOnly = false
  224. w.SendEverything = false
  225. w.ChooseEvents = true
  226. w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
  227. w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
  228. w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
  229. w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
  230. w.Delete = com.IsSliceContainsStr(form.Events, string(models.HookEventDelete))
  231. w.Fork = com.IsSliceContainsStr(form.Events, string(models.HookEventFork))
  232. w.Issues = com.IsSliceContainsStr(form.Events, string(models.HookEventIssues))
  233. w.IssueComment = com.IsSliceContainsStr(form.Events, string(models.HookEventIssueComment))
  234. w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
  235. w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
  236. w.Repository = com.IsSliceContainsStr(form.Events, string(models.HookEventRepository))
  237. w.Release = com.IsSliceContainsStr(form.Events, string(models.HookEventRelease))
  238. w.BranchFilter = form.BranchFilter
  239. if err := w.UpdateEvent(); err != nil {
  240. ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
  241. return false
  242. }
  243. if form.Active != nil {
  244. w.IsActive = *form.Active
  245. }
  246. if err := models.UpdateWebhook(w); err != nil {
  247. ctx.Error(http.StatusInternalServerError, "UpdateWebhook", err)
  248. return false
  249. }
  250. return true
  251. }