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.

repo_hooks.go 5.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 v1
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "github.com/Unknwon/com"
  9. api "github.com/gogits/go-gogs-client"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/middleware"
  13. )
  14. // ToApiHook converts webhook to API format.
  15. func ToApiHook(repoLink string, w *models.Webhook) *api.Hook {
  16. config := map[string]string{
  17. "url": w.URL,
  18. "content_type": w.ContentType.Name(),
  19. }
  20. if w.HookTaskType == models.SLACK {
  21. s := w.GetSlackHook()
  22. config["channel"] = s.Channel
  23. config["username"] = s.Username
  24. config["icon_url"] = s.IconURL
  25. config["color"] = s.Color
  26. }
  27. return &api.Hook{
  28. ID: w.ID,
  29. Type: w.HookTaskType.Name(),
  30. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  31. Active: w.IsActive,
  32. Config: config,
  33. Events: w.EventsArray(),
  34. Updated: w.Updated,
  35. Created: w.Created,
  36. }
  37. }
  38. // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks
  39. func ListRepoHooks(ctx *middleware.Context) {
  40. hooks, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.ID)
  41. if err != nil {
  42. ctx.JSON(500, &base.ApiJsonErr{"GetWebhooksByRepoId: " + err.Error(), base.DOC_URL})
  43. return
  44. }
  45. apiHooks := make([]*api.Hook, len(hooks))
  46. for i := range hooks {
  47. apiHooks[i] = ToApiHook(ctx.Repo.RepoLink, hooks[i])
  48. }
  49. ctx.JSON(200, &apiHooks)
  50. }
  51. // https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
  52. func CreateRepoHook(ctx *middleware.Context, form api.CreateHookOption) {
  53. if !models.IsValidHookTaskType(form.Type) {
  54. ctx.JSON(422, &base.ApiJsonErr{"invalid hook type", base.DOC_URL})
  55. return
  56. }
  57. for _, name := range []string{"url", "content_type"} {
  58. if _, ok := form.Config[name]; !ok {
  59. ctx.JSON(422, &base.ApiJsonErr{"missing config option: " + name, base.DOC_URL})
  60. return
  61. }
  62. }
  63. if !models.IsValidHookContentType(form.Config["content_type"]) {
  64. ctx.JSON(422, &base.ApiJsonErr{"invalid content type", base.DOC_URL})
  65. return
  66. }
  67. w := &models.Webhook{
  68. RepoID: ctx.Repo.Repository.ID,
  69. URL: form.Config["url"],
  70. ContentType: models.ToHookContentType(form.Config["content_type"]),
  71. Secret: form.Config["secret"],
  72. HookEvent: &models.HookEvent{
  73. ChooseEvents: true,
  74. HookEvents: models.HookEvents{
  75. Create: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)),
  76. Push: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)),
  77. },
  78. },
  79. IsActive: form.Active,
  80. HookTaskType: models.ToHookTaskType(form.Type),
  81. }
  82. if w.HookTaskType == models.SLACK {
  83. channel, ok := form.Config["channel"]
  84. if !ok {
  85. ctx.JSON(422, &base.ApiJsonErr{"missing config option: channel", base.DOC_URL})
  86. return
  87. }
  88. meta, err := json.Marshal(&models.SlackMeta{
  89. Channel: channel,
  90. Username: form.Config["username"],
  91. IconURL: form.Config["icon_url"],
  92. Color: form.Config["color"],
  93. })
  94. if err != nil {
  95. ctx.JSON(500, &base.ApiJsonErr{"slack: JSON marshal failed: " + err.Error(), base.DOC_URL})
  96. return
  97. }
  98. w.Meta = string(meta)
  99. }
  100. if err := w.UpdateEvent(); err != nil {
  101. ctx.JSON(500, &base.ApiJsonErr{"UpdateEvent: " + err.Error(), base.DOC_URL})
  102. return
  103. } else if err := models.CreateWebhook(w); err != nil {
  104. ctx.JSON(500, &base.ApiJsonErr{"CreateWebhook: " + err.Error(), base.DOC_URL})
  105. return
  106. }
  107. ctx.JSON(201, ToApiHook(ctx.Repo.RepoLink, w))
  108. }
  109. // https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
  110. func EditRepoHook(ctx *middleware.Context, form api.EditHookOption) {
  111. w, err := models.GetWebhookByID(ctx.ParamsInt64(":id"))
  112. if err != nil {
  113. ctx.JSON(500, &base.ApiJsonErr{"GetWebhookById: " + err.Error(), base.DOC_URL})
  114. return
  115. }
  116. if form.Config != nil {
  117. if url, ok := form.Config["url"]; ok {
  118. w.URL = url
  119. }
  120. if ct, ok := form.Config["content_type"]; ok {
  121. if !models.IsValidHookContentType(ct) {
  122. ctx.JSON(422, &base.ApiJsonErr{"invalid content type", base.DOC_URL})
  123. return
  124. }
  125. w.ContentType = models.ToHookContentType(ct)
  126. }
  127. if w.HookTaskType == models.SLACK {
  128. if channel, ok := form.Config["channel"]; ok {
  129. meta, err := json.Marshal(&models.SlackMeta{
  130. Channel: channel,
  131. Username: form.Config["username"],
  132. IconURL: form.Config["icon_url"],
  133. Color: form.Config["color"],
  134. })
  135. if err != nil {
  136. ctx.JSON(500, &base.ApiJsonErr{"slack: JSON marshal failed: " + err.Error(), base.DOC_URL})
  137. return
  138. }
  139. w.Meta = string(meta)
  140. }
  141. }
  142. }
  143. // Update events
  144. w.PushOnly = false
  145. w.SendEverything = false
  146. w.ChooseEvents = true
  147. w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE))
  148. w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH))
  149. if err = w.UpdateEvent(); err != nil {
  150. ctx.JSON(500, &base.ApiJsonErr{"UpdateEvent: " + err.Error(), base.DOC_URL})
  151. return
  152. }
  153. if form.Active != nil {
  154. w.IsActive = *form.Active
  155. }
  156. if err := models.UpdateWebhook(w); err != nil {
  157. ctx.JSON(500, &base.ApiJsonErr{"UpdateWebhook: " + err.Error(), base.DOC_URL})
  158. return
  159. }
  160. ctx.JSON(200, ToApiHook(ctx.Repo.RepoLink, w))
  161. }