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

169 lines
4.5 KiB

10 years ago
  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. }