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.

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