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.

webhook.go 6.9KB

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