hook.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2014 The Gogs 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 repo
  5. import (
  6. "net/http"
  7. jsoniter "github.com/json-iterator/go"
  8. "github.com/pkg/errors"
  9. "github.com/unknwon/com"
  10. api "github.com/gogs/go-gogs-client"
  11. "gogs.io/gogs/internal/context"
  12. "gogs.io/gogs/internal/db"
  13. "gogs.io/gogs/internal/route/api/v1/convert"
  14. )
  15. // https://github.com/gogs/go-gogs-client/wiki/Repositories#list-hooks
  16. func ListHooks(c *context.APIContext) {
  17. hooks, err := db.GetWebhooksByRepoID(c.Repo.Repository.ID)
  18. if err != nil {
  19. c.Errorf(err, "get webhooks by repository ID")
  20. return
  21. }
  22. apiHooks := make([]*api.Hook, len(hooks))
  23. for i := range hooks {
  24. apiHooks[i] = convert.ToHook(c.Repo.RepoLink, hooks[i])
  25. }
  26. c.JSONSuccess(&apiHooks)
  27. }
  28. // https://github.com/gogs/go-gogs-client/wiki/Repositories#create-a-hook
  29. func CreateHook(c *context.APIContext, form api.CreateHookOption) {
  30. if !db.IsValidHookTaskType(form.Type) {
  31. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid hook type."))
  32. return
  33. }
  34. for _, name := range []string{"url", "content_type"} {
  35. if _, ok := form.Config[name]; !ok {
  36. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Missing config option: "+name))
  37. return
  38. }
  39. }
  40. if !db.IsValidHookContentType(form.Config["content_type"]) {
  41. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid content type."))
  42. return
  43. }
  44. if len(form.Events) == 0 {
  45. form.Events = []string{"push"}
  46. }
  47. w := &db.Webhook{
  48. RepoID: c.Repo.Repository.ID,
  49. URL: form.Config["url"],
  50. ContentType: db.ToHookContentType(form.Config["content_type"]),
  51. Secret: form.Config["secret"],
  52. HookEvent: &db.HookEvent{
  53. ChooseEvents: true,
  54. HookEvents: db.HookEvents{
  55. Create: com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_CREATE)),
  56. Delete: com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_DELETE)),
  57. Fork: com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_FORK)),
  58. Push: com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_PUSH)),
  59. Issues: com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_ISSUES)),
  60. IssueComment: com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_ISSUE_COMMENT)),
  61. PullRequest: com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_PULL_REQUEST)),
  62. Release: com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_RELEASE)),
  63. },
  64. },
  65. IsActive: form.Active,
  66. HookTaskType: db.ToHookTaskType(form.Type),
  67. }
  68. if w.HookTaskType == db.SLACK {
  69. channel, ok := form.Config["channel"]
  70. if !ok {
  71. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Missing config option: channel"))
  72. return
  73. }
  74. meta, err := jsoniter.Marshal(&db.SlackMeta{
  75. Channel: channel,
  76. Username: form.Config["username"],
  77. IconURL: form.Config["icon_url"],
  78. Color: form.Config["color"],
  79. })
  80. if err != nil {
  81. c.Errorf(err, "marshal JSON")
  82. return
  83. }
  84. w.Meta = string(meta)
  85. }
  86. if err := w.UpdateEvent(); err != nil {
  87. c.Errorf(err, "update event")
  88. return
  89. } else if err := db.CreateWebhook(w); err != nil {
  90. c.Errorf(err, "create webhook")
  91. return
  92. }
  93. c.JSON(http.StatusCreated, convert.ToHook(c.Repo.RepoLink, w))
  94. }
  95. // https://github.com/gogs/go-gogs-client/wiki/Repositories#edit-a-hook
  96. func EditHook(c *context.APIContext, form api.EditHookOption) {
  97. w, err := db.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  98. if err != nil {
  99. c.NotFoundOrError(err, "get webhook of repository by ID")
  100. return
  101. }
  102. if form.Config != nil {
  103. if url, ok := form.Config["url"]; ok {
  104. w.URL = url
  105. }
  106. if ct, ok := form.Config["content_type"]; ok {
  107. if !db.IsValidHookContentType(ct) {
  108. c.ErrorStatus(http.StatusUnprocessableEntity, errors.New("Invalid content type."))
  109. return
  110. }
  111. w.ContentType = db.ToHookContentType(ct)
  112. }
  113. if w.HookTaskType == db.SLACK {
  114. if channel, ok := form.Config["channel"]; ok {
  115. meta, err := jsoniter.Marshal(&db.SlackMeta{
  116. Channel: channel,
  117. Username: form.Config["username"],
  118. IconURL: form.Config["icon_url"],
  119. Color: form.Config["color"],
  120. })
  121. if err != nil {
  122. c.Errorf(err, "marshal JSON")
  123. return
  124. }
  125. w.Meta = string(meta)
  126. }
  127. }
  128. }
  129. // Update events
  130. if len(form.Events) == 0 {
  131. form.Events = []string{"push"}
  132. }
  133. w.PushOnly = false
  134. w.SendEverything = false
  135. w.ChooseEvents = true
  136. w.Create = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_CREATE))
  137. w.Delete = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_DELETE))
  138. w.Fork = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_FORK))
  139. w.Push = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_PUSH))
  140. w.Issues = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_ISSUES))
  141. w.IssueComment = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_ISSUE_COMMENT))
  142. w.PullRequest = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_PULL_REQUEST))
  143. w.Release = com.IsSliceContainsStr(form.Events, string(db.HOOK_EVENT_RELEASE))
  144. if err = w.UpdateEvent(); err != nil {
  145. c.Errorf(err, "update event")
  146. return
  147. }
  148. if form.Active != nil {
  149. w.IsActive = *form.Active
  150. }
  151. if err := db.UpdateWebhook(w); err != nil {
  152. c.Errorf(err, "update webhook")
  153. return
  154. }
  155. c.JSONSuccess(convert.ToHook(c.Repo.RepoLink, w))
  156. }
  157. func DeleteHook(c *context.APIContext) {
  158. if err := db.DeleteWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
  159. c.Errorf(err, "delete webhook of repository by ID")
  160. return
  161. }
  162. c.NoContent()
  163. }