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.1KB

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