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.

232 lines
5.7 KiB

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
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
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
10 years ago
10 years ago
10 years ago
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 models
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "time"
  9. "github.com/gogits/gogs/modules/httplib"
  10. "github.com/gogits/gogs/modules/log"
  11. "github.com/gogits/gogs/modules/setting"
  12. "github.com/gogits/gogs/modules/uuid"
  13. )
  14. var (
  15. ErrWebhookNotExist = errors.New("Webhook does not exist")
  16. )
  17. type HookContentType int
  18. const (
  19. JSON HookContentType = iota + 1
  20. FORM
  21. )
  22. // HookEvent represents events that will delivery hook.
  23. type HookEvent struct {
  24. PushOnly bool `json:"push_only"`
  25. }
  26. // Webhook represents a web hook object.
  27. type Webhook struct {
  28. Id int64
  29. RepoId int64
  30. Url string `xorm:"TEXT"`
  31. ContentType HookContentType
  32. Secret string `xorm:"TEXT"`
  33. Events string `xorm:"TEXT"`
  34. *HookEvent `xorm:"-"`
  35. IsSsl bool
  36. IsActive bool
  37. }
  38. // GetEvent handles conversion from Events to HookEvent.
  39. func (w *Webhook) GetEvent() {
  40. w.HookEvent = &HookEvent{}
  41. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  42. log.Error(4, "webhook.GetEvent(%d): %v", w.Id, err)
  43. }
  44. }
  45. // UpdateEvent handles conversion from HookEvent to Events.
  46. func (w *Webhook) UpdateEvent() error {
  47. data, err := json.Marshal(w.HookEvent)
  48. w.Events = string(data)
  49. return err
  50. }
  51. // HasPushEvent returns true if hook enbaled push event.
  52. func (w *Webhook) HasPushEvent() bool {
  53. if w.PushOnly {
  54. return true
  55. }
  56. return false
  57. }
  58. // CreateWebhook creates a new web hook.
  59. func CreateWebhook(w *Webhook) error {
  60. _, err := x.Insert(w)
  61. return err
  62. }
  63. // GetWebhookById returns webhook by given ID.
  64. func GetWebhookById(hookId int64) (*Webhook, error) {
  65. w := &Webhook{Id: hookId}
  66. has, err := x.Get(w)
  67. if err != nil {
  68. return nil, err
  69. } else if !has {
  70. return nil, ErrWebhookNotExist
  71. }
  72. return w, nil
  73. }
  74. // GetActiveWebhooksByRepoId returns all active webhooks of repository.
  75. func GetActiveWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
  76. err = x.Find(&ws, &Webhook{RepoId: repoId, IsActive: true})
  77. return ws, err
  78. }
  79. // GetWebhooksByRepoId returns all webhooks of repository.
  80. func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
  81. err = x.Find(&ws, &Webhook{RepoId: repoId})
  82. return ws, err
  83. }
  84. // UpdateWebhook updates information of webhook.
  85. func UpdateWebhook(w *Webhook) error {
  86. _, err := x.AllCols().Update(w)
  87. return err
  88. }
  89. // DeleteWebhook deletes webhook of repository.
  90. func DeleteWebhook(hookId int64) error {
  91. _, err := x.Delete(&Webhook{Id: hookId})
  92. return err
  93. }
  94. // ___ ___ __ ___________ __
  95. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  96. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  97. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  98. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  99. // \/ \/ \/ \/ \/
  100. type HookTaskType int
  101. const (
  102. WEBHOOK HookTaskType = iota + 1
  103. SERVICE
  104. )
  105. type HookEventType string
  106. const (
  107. PUSH HookEventType = "push"
  108. )
  109. type PayloadAuthor struct {
  110. Name string `json:"name"`
  111. Email string `json:"email"`
  112. }
  113. type PayloadCommit struct {
  114. Id string `json:"id"`
  115. Message string `json:"message"`
  116. Url string `json:"url"`
  117. Author *PayloadAuthor `json:"author"`
  118. }
  119. type PayloadRepo struct {
  120. Id int64 `json:"id"`
  121. Name string `json:"name"`
  122. Url string `json:"url"`
  123. Description string `json:"description"`
  124. Website string `json:"website"`
  125. Watchers int `json:"watchers"`
  126. Owner *PayloadAuthor `json:"author"`
  127. Private bool `json:"private"`
  128. }
  129. // Payload represents a payload information of hook.
  130. type Payload struct {
  131. Secret string `json:"secret"`
  132. Ref string `json:"ref"`
  133. Commits []*PayloadCommit `json:"commits"`
  134. Repo *PayloadRepo `json:"repository"`
  135. Pusher *PayloadAuthor `json:"pusher"`
  136. }
  137. // HookTask represents a hook task.
  138. type HookTask struct {
  139. Id int64
  140. Uuid string
  141. Type HookTaskType
  142. Url string
  143. *Payload `xorm:"-"`
  144. PayloadContent string `xorm:"TEXT"`
  145. ContentType HookContentType
  146. EventType HookEventType
  147. IsSsl bool
  148. IsDeliveried bool
  149. IsSucceed bool
  150. }
  151. // CreateHookTask creates a new hook task,
  152. // it handles conversion from Payload to PayloadContent.
  153. func CreateHookTask(t *HookTask) error {
  154. data, err := json.Marshal(t.Payload)
  155. if err != nil {
  156. return err
  157. }
  158. t.Uuid = uuid.NewV4().String()
  159. t.PayloadContent = string(data)
  160. _, err = x.Insert(t)
  161. return err
  162. }
  163. // UpdateHookTask updates information of hook task.
  164. func UpdateHookTask(t *HookTask) error {
  165. _, err := x.AllCols().Update(t)
  166. return err
  167. }
  168. // DeliverHooks checks and delivers undelivered hooks.
  169. func DeliverHooks() {
  170. timeout := time.Duration(setting.WebhookDeliverTimeout) * time.Second
  171. x.Where("is_deliveried=?", false).Iterate(new(HookTask),
  172. func(idx int, bean interface{}) error {
  173. t := bean.(*HookTask)
  174. req := httplib.Post(t.Url).SetTimeout(timeout, timeout).
  175. Header("X-Gogs-Delivery", t.Uuid).
  176. Header("X-Gogs-Event", string(t.EventType))
  177. switch t.ContentType {
  178. case JSON:
  179. req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
  180. case FORM:
  181. req.Param("payload", t.PayloadContent)
  182. }
  183. t.IsDeliveried = true
  184. // TODO: record response.
  185. if _, err := req.Response(); err != nil {
  186. log.Error(4, "Delivery: %v", err)
  187. } else {
  188. t.IsSucceed = true
  189. }
  190. if err := UpdateHookTask(t); err != nil {
  191. log.Error(4, "UpdateHookTask: %v", err)
  192. return nil
  193. }
  194. log.Trace("Hook delivered(%s): %s", t.Uuid, t.PayloadContent)
  195. return nil
  196. })
  197. }