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.

177 lines
4.6 KiB

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