本站源代码
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.

257 lines
8.1KB

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