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.

hook.go 8.2KB

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