You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

hook.go 4.5 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "encoding/json"
  7. "github.com/Unknwon/com"
  8. api "github.com/gogits/go-gogs-client"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/context"
  11. "github.com/gogits/gogs/routers/api/v1/convert"
  12. )
  13. // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks
  14. func ListHooks(ctx *context.APIContext) {
  15. hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  16. if err != nil {
  17. ctx.Error(500, "GetWebhooksByRepoID", err)
  18. return
  19. }
  20. apiHooks := make([]*api.Hook, len(hooks))
  21. for i := range hooks {
  22. apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
  23. }
  24. ctx.JSON(200, &apiHooks)
  25. }
  26. // https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
  27. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  28. if !models.IsValidHookTaskType(form.Type) {
  29. ctx.Error(422, "", "Invalid hook type")
  30. return
  31. }
  32. for _, name := range []string{"url", "content_type"} {
  33. if _, ok := form.Config[name]; !ok {
  34. ctx.Error(422, "", "Missing config option: "+name)
  35. return
  36. }
  37. }
  38. if !models.IsValidHookContentType(form.Config["content_type"]) {
  39. ctx.Error(422, "", "Invalid content type")
  40. return
  41. }
  42. if len(form.Events) == 0 {
  43. form.Events = []string{"push"}
  44. }
  45. w := &models.Webhook{
  46. RepoID: ctx.Repo.Repository.ID,
  47. URL: form.Config["url"],
  48. ContentType: models.ToHookContentType(form.Config["content_type"]),
  49. Secret: form.Config["secret"],
  50. HookEvent: &models.HookEvent{
  51. ChooseEvents: true,
  52. HookEvents: models.HookEvents{
  53. Create: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)),
  54. Push: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)),
  55. },
  56. },
  57. IsActive: form.Active,
  58. HookTaskType: models.ToHookTaskType(form.Type),
  59. }
  60. if w.HookTaskType == models.SLACK {
  61. channel, ok := form.Config["channel"]
  62. if !ok {
  63. ctx.Error(422, "", "Missing config option: channel")
  64. return
  65. }
  66. meta, err := json.Marshal(&models.SlackMeta{
  67. Channel: channel,
  68. Username: form.Config["username"],
  69. IconURL: form.Config["icon_url"],
  70. Color: form.Config["color"],
  71. })
  72. if err != nil {
  73. ctx.Error(500, "slack: JSON marshal failed", err)
  74. return
  75. }
  76. w.Meta = string(meta)
  77. }
  78. if err := w.UpdateEvent(); err != nil {
  79. ctx.Error(500, "UpdateEvent", err)
  80. return
  81. } else if err := models.CreateWebhook(w); err != nil {
  82. ctx.Error(500, "CreateWebhook", err)
  83. return
  84. }
  85. ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w))
  86. }
  87. func DeleteHook(ctx *context.APIContext) {
  88. if err := models.DeleteWebhook(ctx.ParamsInt64(":id")); err != nil {
  89. ctx.Error(500, "DeleteWebhook", err)
  90. return
  91. }
  92. ctx.Status(204)
  93. }
  94. // https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
  95. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  96. w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  97. if err != nil {
  98. if models.IsErrWebhookNotExist(err) {
  99. ctx.Status(404)
  100. } else {
  101. ctx.Error(500, "GetWebhookByID", err)
  102. }
  103. return
  104. }
  105. if form.Config != nil {
  106. if url, ok := form.Config["url"]; ok {
  107. w.URL = url
  108. }
  109. if ct, ok := form.Config["content_type"]; ok {
  110. if !models.IsValidHookContentType(ct) {
  111. ctx.Error(422, "", "Invalid content type")
  112. return
  113. }
  114. w.ContentType = models.ToHookContentType(ct)
  115. }
  116. if w.HookTaskType == models.SLACK {
  117. if channel, ok := form.Config["channel"]; ok {
  118. meta, err := json.Marshal(&models.SlackMeta{
  119. Channel: 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
  127. }
  128. w.Meta = string(meta)
  129. }
  130. }
  131. }
  132. // Update events
  133. if len(form.Events) == 0 {
  134. form.Events = []string{"push"}
  135. }
  136. w.PushOnly = false
  137. w.SendEverything = false
  138. w.ChooseEvents = true
  139. w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE))
  140. w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH))
  141. if err = w.UpdateEvent(); err != nil {
  142. ctx.Error(500, "UpdateEvent", err)
  143. return
  144. }
  145. if form.Active != nil {
  146. w.IsActive = *form.Active
  147. }
  148. if err := models.UpdateWebhook(w); err != nil {
  149. ctx.Error(500, "UpdateWebhook", err)
  150. return
  151. }
  152. ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, w))
  153. }