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.

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