repo_hooks.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. type CreateRepoHookForm struct {
  41. Type string `json:"type" binding:"Required"`
  42. Config map[string]string `json:"config" binding:"Required"`
  43. Active bool `json:"active"`
  44. }
  45. // POST /repos/:username/:reponame/hooks
  46. // https://developer.github.com/v3/repos/hooks/#create-a-hook
  47. func CreateRepoHook(ctx *middleware.Context, form CreateRepoHookForm) {
  48. if !models.IsValidHookTaskType(form.Type) {
  49. ctx.JSON(422, &base.ApiJsonErr{"invalid hook type", base.DOC_URL})
  50. return
  51. }
  52. for _, name := range []string{"url", "content_type"} {
  53. if _, ok := form.Config[name]; !ok {
  54. ctx.JSON(422, &base.ApiJsonErr{"missing config option: " + name, base.DOC_URL})
  55. return
  56. }
  57. }
  58. if !models.IsValidHookContentType(form.Config["content_type"]) {
  59. ctx.JSON(422, &base.ApiJsonErr{"invalid content type", base.DOC_URL})
  60. return
  61. }
  62. w := &models.Webhook{
  63. RepoId: ctx.Repo.Repository.Id,
  64. Url: form.Config["url"],
  65. ContentType: models.ToHookContentType(form.Config["content_type"]),
  66. Secret: form.Config["secret"],
  67. HookEvent: &models.HookEvent{
  68. PushOnly: true, // Only support it now.
  69. },
  70. IsActive: form.Active,
  71. HookTaskType: models.ToHookTaskType(form.Type),
  72. }
  73. if w.HookTaskType == models.SLACK {
  74. channel, ok := form.Config["channel"]
  75. if !ok {
  76. ctx.JSON(422, &base.ApiJsonErr{"missing config option: channel", base.DOC_URL})
  77. return
  78. }
  79. meta, err := json.Marshal(&models.Slack{
  80. Channel: channel,
  81. })
  82. if err != nil {
  83. ctx.JSON(500, &base.ApiJsonErr{"slack: JSON marshal failed: " + err.Error(), base.DOC_URL})
  84. return
  85. }
  86. w.Meta = string(meta)
  87. }
  88. if err := w.UpdateEvent(); err != nil {
  89. ctx.JSON(500, &base.ApiJsonErr{"UpdateEvent: " + err.Error(), base.DOC_URL})
  90. return
  91. } else if err := models.CreateWebhook(w); err != nil {
  92. ctx.JSON(500, &base.ApiJsonErr{"CreateWebhook: " + err.Error(), base.DOC_URL})
  93. return
  94. }
  95. ctx.JSON(201, map[string]interface{}{
  96. "ok": true,
  97. })
  98. }
  99. type EditRepoHookForm struct {
  100. Config map[string]string `json:"config"`
  101. Active *bool `json:"active"`
  102. }
  103. // PATCH /repos/:username/:reponame/hooks/:id
  104. // https://developer.github.com/v3/repos/hooks/#edit-a-hook
  105. func EditRepoHook(ctx *middleware.Context, form EditRepoHookForm) {
  106. w, err := models.GetWebhookById(ctx.ParamsInt64(":id"))
  107. if err != nil {
  108. ctx.JSON(500, &base.ApiJsonErr{"GetWebhookById: " + err.Error(), base.DOC_URL})
  109. return
  110. }
  111. if form.Config != nil {
  112. if url, ok := form.Config["url"]; ok {
  113. w.Url = url
  114. }
  115. if ct, ok := form.Config["content_type"]; ok {
  116. if !models.IsValidHookContentType(ct) {
  117. ctx.JSON(422, &base.ApiJsonErr{"invalid content type", base.DOC_URL})
  118. return
  119. }
  120. w.ContentType = models.ToHookContentType(ct)
  121. }
  122. if w.HookTaskType == models.SLACK {
  123. if channel, ok := form.Config["channel"]; ok {
  124. meta, err := json.Marshal(&models.Slack{
  125. Channel: channel,
  126. })
  127. if err != nil {
  128. ctx.JSON(500, &base.ApiJsonErr{"slack: JSON marshal failed: " + err.Error(), base.DOC_URL})
  129. return
  130. }
  131. w.Meta = string(meta)
  132. }
  133. }
  134. }
  135. if form.Active != nil {
  136. w.IsActive = *form.Active
  137. }
  138. // FIXME: edit events
  139. if err := models.UpdateWebhook(w); err != nil {
  140. ctx.JSON(500, &base.ApiJsonErr{"UpdateWebhook: " + err.Error(), base.DOC_URL})
  141. return
  142. }
  143. ctx.JSON(200, map[string]interface{}{
  144. "ok": true,
  145. })
  146. }