repo_hooks.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. api "github.com/gogits/go-gogs-client"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/middleware"
  11. )
  12. // GET /repos/:username/:reponame/hooks
  13. // https://developer.github.com/v3/repos/hooks/#list-hooks
  14. func ListRepoHooks(ctx *middleware.Context) {
  15. hooks, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.ID)
  16. if err != nil {
  17. ctx.JSON(500, &base.ApiJsonErr{"GetWebhooksByRepoId: " + err.Error(), base.DOC_URL})
  18. return
  19. }
  20. apiHooks := make([]*api.Hook, len(hooks))
  21. for i := range hooks {
  22. h := &api.Hook{
  23. Id: hooks[i].Id,
  24. Type: hooks[i].HookTaskType.Name(),
  25. Active: hooks[i].IsActive,
  26. Config: make(map[string]string),
  27. }
  28. // Currently, onle have push event.
  29. h.Events = []string{"push"}
  30. h.Config["url"] = hooks[i].Url
  31. h.Config["content_type"] = hooks[i].ContentType.Name()
  32. if hooks[i].HookTaskType == models.SLACK {
  33. s := hooks[i].GetSlackHook()
  34. h.Config["channel"] = s.Channel
  35. }
  36. apiHooks[i] = h
  37. }
  38. ctx.JSON(200, &apiHooks)
  39. }
  40. // POST /repos/:username/:reponame/hooks
  41. // https://developer.github.com/v3/repos/hooks/#create-a-hook
  42. func CreateRepoHook(ctx *middleware.Context, form api.CreateHookOption) {
  43. if !models.IsValidHookTaskType(form.Type) {
  44. ctx.JSON(422, &base.ApiJsonErr{"invalid hook type", base.DOC_URL})
  45. return
  46. }
  47. for _, name := range []string{"url", "content_type"} {
  48. if _, ok := form.Config[name]; !ok {
  49. ctx.JSON(422, &base.ApiJsonErr{"missing config option: " + name, base.DOC_URL})
  50. return
  51. }
  52. }
  53. if !models.IsValidHookContentType(form.Config["content_type"]) {
  54. ctx.JSON(422, &base.ApiJsonErr{"invalid content type", base.DOC_URL})
  55. return
  56. }
  57. w := &models.Webhook{
  58. RepoId: ctx.Repo.Repository.ID,
  59. Url: form.Config["url"],
  60. ContentType: models.ToHookContentType(form.Config["content_type"]),
  61. Secret: form.Config["secret"],
  62. HookEvent: &models.HookEvent{
  63. PushOnly: true, // Only support it now.
  64. },
  65. IsActive: form.Active,
  66. HookTaskType: models.ToHookTaskType(form.Type),
  67. }
  68. if w.HookTaskType == models.SLACK {
  69. channel, ok := form.Config["channel"]
  70. if !ok {
  71. ctx.JSON(422, &base.ApiJsonErr{"missing config option: channel", base.DOC_URL})
  72. return
  73. }
  74. meta, err := json.Marshal(&models.Slack{
  75. Channel: channel,
  76. })
  77. if err != nil {
  78. ctx.JSON(500, &base.ApiJsonErr{"slack: JSON marshal failed: " + err.Error(), base.DOC_URL})
  79. return
  80. }
  81. w.Meta = string(meta)
  82. }
  83. if err := w.UpdateEvent(); err != nil {
  84. ctx.JSON(500, &base.ApiJsonErr{"UpdateEvent: " + err.Error(), base.DOC_URL})
  85. return
  86. } else if err := models.CreateWebhook(w); err != nil {
  87. ctx.JSON(500, &base.ApiJsonErr{"CreateWebhook: " + err.Error(), base.DOC_URL})
  88. return
  89. }
  90. apiHook := &api.Hook{
  91. Id: w.Id,
  92. Type: w.HookTaskType.Name(),
  93. Events: []string{"push"},
  94. Active: w.IsActive,
  95. Config: map[string]string{
  96. "url": w.Url,
  97. "content_type": w.ContentType.Name(),
  98. },
  99. }
  100. if w.HookTaskType == models.SLACK {
  101. s := w.GetSlackHook()
  102. apiHook.Config["channel"] = s.Channel
  103. }
  104. ctx.JSON(201, apiHook)
  105. }
  106. // PATCH /repos/:username/:reponame/hooks/:id
  107. // https://developer.github.com/v3/repos/hooks/#edit-a-hook
  108. func EditRepoHook(ctx *middleware.Context, form api.EditHookOption) {
  109. w, err := models.GetWebhookById(ctx.ParamsInt64(":id"))
  110. if err != nil {
  111. ctx.JSON(500, &base.ApiJsonErr{"GetWebhookById: " + err.Error(), base.DOC_URL})
  112. return
  113. }
  114. if form.Config != nil {
  115. if url, ok := form.Config["url"]; ok {
  116. w.Url = url
  117. }
  118. if ct, ok := form.Config["content_type"]; ok {
  119. if !models.IsValidHookContentType(ct) {
  120. ctx.JSON(422, &base.ApiJsonErr{"invalid content type", base.DOC_URL})
  121. return
  122. }
  123. w.ContentType = models.ToHookContentType(ct)
  124. }
  125. if w.HookTaskType == models.SLACK {
  126. if channel, ok := form.Config["channel"]; ok {
  127. meta, err := json.Marshal(&models.Slack{
  128. Channel: channel,
  129. })
  130. if err != nil {
  131. ctx.JSON(500, &base.ApiJsonErr{"slack: JSON marshal failed: " + err.Error(), base.DOC_URL})
  132. return
  133. }
  134. w.Meta = string(meta)
  135. }
  136. }
  137. }
  138. if form.Active != nil {
  139. w.IsActive = *form.Active
  140. }
  141. // FIXME: edit events
  142. if err := models.UpdateWebhook(w); err != nil {
  143. ctx.JSON(500, &base.ApiJsonErr{"UpdateWebhook: " + err.Error(), base.DOC_URL})
  144. return
  145. }
  146. ctx.JSON(200, map[string]interface{}{
  147. "ok": true,
  148. })
  149. }