webhook.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. OrgId int64
  41. }
  42. // GetEvent handles conversion from Events to HookEvent.
  43. func (w *Webhook) GetEvent() {
  44. w.HookEvent = &HookEvent{}
  45. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  46. log.Error(4, "webhook.GetEvent(%d): %v", w.Id, err)
  47. }
  48. }
  49. func (w *Webhook) GetSlackHook() *Slack {
  50. s := &Slack{}
  51. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  52. log.Error(4, "webhook.GetSlackHook(%d): %v", w.Id, err)
  53. }
  54. return s
  55. }
  56. // UpdateEvent handles conversion from HookEvent to Events.
  57. func (w *Webhook) UpdateEvent() error {
  58. data, err := json.Marshal(w.HookEvent)
  59. w.Events = string(data)
  60. return err
  61. }
  62. // HasPushEvent returns true if hook enbaled push event.
  63. func (w *Webhook) HasPushEvent() bool {
  64. if w.PushOnly {
  65. return true
  66. }
  67. return false
  68. }
  69. // CreateWebhook creates a new web hook.
  70. func CreateWebhook(w *Webhook) error {
  71. _, err := x.Insert(w)
  72. return err
  73. }
  74. // GetWebhookById returns webhook by given ID.
  75. func GetWebhookById(hookId int64) (*Webhook, error) {
  76. w := &Webhook{Id: hookId}
  77. has, err := x.Get(w)
  78. if err != nil {
  79. return nil, err
  80. } else if !has {
  81. return nil, ErrWebhookNotExist
  82. }
  83. return w, nil
  84. }
  85. // GetActiveWebhooksByRepoId returns all active webhooks of repository.
  86. func GetActiveWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
  87. err = x.Where("repo_id=?", repoId).And("is_active=?", true).Find(&ws)
  88. return ws, err
  89. }
  90. // GetWebhooksByRepoId returns all webhooks of repository.
  91. func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
  92. err = x.Find(&ws, &Webhook{RepoId: repoId})
  93. return ws, err
  94. }
  95. // UpdateWebhook updates information of webhook.
  96. func UpdateWebhook(w *Webhook) error {
  97. _, err := x.Id(w.Id).AllCols().Update(w)
  98. return err
  99. }
  100. // DeleteWebhook deletes webhook of repository.
  101. func DeleteWebhook(hookId int64) error {
  102. _, err := x.Delete(&Webhook{Id: hookId})
  103. return err
  104. }
  105. // GetWebhooksByOrgId returns all webhooks for an organization.
  106. func GetWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) {
  107. err = x.Find(&ws, &Webhook{OrgId: orgId})
  108. return ws, err
  109. }
  110. // GetActiveWebhooksByOrgId returns all active webhooks for an organization.
  111. func GetActiveWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) {
  112. err = x.Where("org_id=?", orgId).And("is_active=?", true).Find(&ws)
  113. return ws, err
  114. }
  115. // ___ ___ __ ___________ __
  116. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  117. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  118. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  119. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  120. // \/ \/ \/ \/ \/
  121. type HookTaskType int
  122. const (
  123. GOGS HookTaskType = iota + 1
  124. SLACK
  125. )
  126. type HookEventType string
  127. const (
  128. PUSH HookEventType = "push"
  129. )
  130. type PayloadAuthor struct {
  131. Name string `json:"name"`
  132. Email string `json:"email"`
  133. UserName string `json:"username"`
  134. }
  135. type PayloadCommit struct {
  136. Id string `json:"id"`
  137. Message string `json:"message"`
  138. Url string `json:"url"`
  139. Author *PayloadAuthor `json:"author"`
  140. }
  141. type PayloadRepo struct {
  142. Id int64 `json:"id"`
  143. Name string `json:"name"`
  144. Url string `json:"url"`
  145. Description string `json:"description"`
  146. Website string `json:"website"`
  147. Watchers int `json:"watchers"`
  148. Owner *PayloadAuthor `json:"owner"`
  149. Private bool `json:"private"`
  150. }
  151. type BasePayload interface {
  152. GetJSONPayload() ([]byte, error)
  153. }
  154. // Payload represents a payload information of hook.
  155. type Payload struct {
  156. Secret string `json:"secret"`
  157. Ref string `json:"ref"`
  158. Commits []*PayloadCommit `json:"commits"`
  159. Repo *PayloadRepo `json:"repository"`
  160. Pusher *PayloadAuthor `json:"pusher"`
  161. Before string `json:"before"`
  162. After string `json:"after"`
  163. CompareUrl string `json:"compare_url"`
  164. }
  165. func (p Payload) GetJSONPayload() ([]byte, error) {
  166. data, err := json.Marshal(p)
  167. if err != nil {
  168. return []byte{}, err
  169. }
  170. return data, nil
  171. }
  172. // HookTask represents a hook task.
  173. type HookTask struct {
  174. Id int64
  175. Uuid string
  176. Type HookTaskType
  177. Url string
  178. BasePayload `xorm:"-"`
  179. PayloadContent string `xorm:"TEXT"`
  180. ContentType HookContentType
  181. EventType HookEventType
  182. IsSsl bool
  183. IsDelivered bool
  184. IsSucceed bool
  185. }
  186. // CreateHookTask creates a new hook task,
  187. // it handles conversion from Payload to PayloadContent.
  188. func CreateHookTask(t *HookTask) error {
  189. data, err := t.BasePayload.GetJSONPayload()
  190. if err != nil {
  191. return err
  192. }
  193. t.Uuid = uuid.NewV4().String()
  194. t.PayloadContent = string(data)
  195. _, err = x.Insert(t)
  196. return err
  197. }
  198. // UpdateHookTask updates information of hook task.
  199. func UpdateHookTask(t *HookTask) error {
  200. _, err := x.Id(t.Id).AllCols().Update(t)
  201. return err
  202. }
  203. var (
  204. // Prevent duplicate deliveries.
  205. // This happens with massive hook tasks cannot finish delivering
  206. // before next shooting starts.
  207. isShooting = false
  208. )
  209. // DeliverHooks checks and delivers undelivered hooks.
  210. // FIXME: maybe can use goroutine to shoot a number of them at same time?
  211. func DeliverHooks() {
  212. if isShooting {
  213. return
  214. }
  215. isShooting = true
  216. defer func() { isShooting = false }()
  217. tasks := make([]*HookTask, 0, 10)
  218. timeout := time.Duration(setting.WebhookDeliverTimeout) * time.Second
  219. x.Where("is_delivered=?", false).Iterate(new(HookTask),
  220. func(idx int, bean interface{}) error {
  221. t := bean.(*HookTask)
  222. req := httplib.Post(t.Url).SetTimeout(timeout, timeout).
  223. Header("X-Gogs-Delivery", t.Uuid).
  224. Header("X-Gogs-Event", string(t.EventType))
  225. switch t.ContentType {
  226. case JSON:
  227. req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
  228. case FORM:
  229. req.Param("payload", t.PayloadContent)
  230. }
  231. t.IsDelivered = true
  232. // FIXME: record response.
  233. switch t.Type {
  234. case GOGS:
  235. {
  236. if _, err := req.Response(); err != nil {
  237. log.Error(4, "Delivery: %v", err)
  238. } else {
  239. t.IsSucceed = true
  240. }
  241. }
  242. case SLACK:
  243. {
  244. if res, err := req.Response(); err != nil {
  245. log.Error(4, "Delivery: %v", err)
  246. } else {
  247. defer res.Body.Close()
  248. contents, err := ioutil.ReadAll(res.Body)
  249. if err != nil {
  250. log.Error(4, "%s", err)
  251. } else {
  252. if string(contents) != "ok" {
  253. log.Error(4, "slack failed with: %s", string(contents))
  254. } else {
  255. t.IsSucceed = true
  256. }
  257. }
  258. }
  259. }
  260. }
  261. tasks = append(tasks, t)
  262. if t.IsSucceed {
  263. log.Trace("Hook delivered(%s): %s", t.Uuid, t.PayloadContent)
  264. }
  265. return nil
  266. })
  267. // Update hook task status.
  268. for _, t := range tasks {
  269. if err := UpdateHookTask(t); err != nil {
  270. log.Error(4, "UpdateHookTask(%d): %v", t.Id, err)
  271. }
  272. }
  273. }