webhook.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. "crypto/tls"
  7. "encoding/json"
  8. "errors"
  9. "io/ioutil"
  10. "sync"
  11. "time"
  12. "github.com/gogits/gogs/modules/httplib"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/setting"
  15. "github.com/gogits/gogs/modules/uuid"
  16. )
  17. var (
  18. ErrWebhookNotExist = errors.New("Webhook does not exist")
  19. )
  20. type HookContentType int
  21. const (
  22. JSON HookContentType = iota + 1
  23. FORM
  24. )
  25. var hookContentTypes = map[string]HookContentType{
  26. "json": JSON,
  27. "form": FORM,
  28. }
  29. // ToHookContentType returns HookContentType by given name.
  30. func ToHookContentType(name string) HookContentType {
  31. return hookContentTypes[name]
  32. }
  33. func (t HookContentType) Name() string {
  34. switch t {
  35. case JSON:
  36. return "json"
  37. case FORM:
  38. return "form"
  39. }
  40. return ""
  41. }
  42. // IsValidHookContentType returns true if given name is a valid hook content type.
  43. func IsValidHookContentType(name string) bool {
  44. _, ok := hookContentTypes[name]
  45. return ok
  46. }
  47. // HookEvent represents events that will delivery hook.
  48. type HookEvent struct {
  49. PushOnly bool `json:"push_only"`
  50. }
  51. // Webhook represents a web hook object.
  52. type Webhook struct {
  53. Id int64
  54. RepoId int64
  55. Url string `xorm:"TEXT"`
  56. ContentType HookContentType
  57. Secret string `xorm:"TEXT"`
  58. Events string `xorm:"TEXT"`
  59. *HookEvent `xorm:"-"`
  60. IsSsl bool
  61. IsActive bool
  62. HookTaskType HookTaskType
  63. Meta string `xorm:"TEXT"` // store hook-specific attributes
  64. OrgId int64
  65. Created time.Time `xorm:"CREATED"`
  66. Updated time.Time `xorm:"UPDATED"`
  67. }
  68. // GetEvent handles conversion from Events to HookEvent.
  69. func (w *Webhook) GetEvent() {
  70. w.HookEvent = &HookEvent{}
  71. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  72. log.Error(4, "webhook.GetEvent(%d): %v", w.Id, err)
  73. }
  74. }
  75. func (w *Webhook) GetSlackHook() *Slack {
  76. s := &Slack{}
  77. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  78. log.Error(4, "webhook.GetSlackHook(%d): %v", w.Id, err)
  79. }
  80. return s
  81. }
  82. // UpdateEvent handles conversion from HookEvent to Events.
  83. func (w *Webhook) UpdateEvent() error {
  84. data, err := json.Marshal(w.HookEvent)
  85. w.Events = string(data)
  86. return err
  87. }
  88. // HasPushEvent returns true if hook enabled push event.
  89. func (w *Webhook) HasPushEvent() bool {
  90. if w.PushOnly {
  91. return true
  92. }
  93. return false
  94. }
  95. // CreateWebhook creates a new web hook.
  96. func CreateWebhook(w *Webhook) error {
  97. _, err := x.Insert(w)
  98. return err
  99. }
  100. // GetWebhookById returns webhook by given ID.
  101. func GetWebhookById(hookId int64) (*Webhook, error) {
  102. w := &Webhook{Id: hookId}
  103. has, err := x.Get(w)
  104. if err != nil {
  105. return nil, err
  106. } else if !has {
  107. return nil, ErrWebhookNotExist
  108. }
  109. return w, nil
  110. }
  111. // GetActiveWebhooksByRepoId returns all active webhooks of repository.
  112. func GetActiveWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
  113. err = x.Where("repo_id=?", repoId).And("is_active=?", true).Find(&ws)
  114. return ws, err
  115. }
  116. // GetWebhooksByRepoId returns all webhooks of repository.
  117. func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
  118. err = x.Find(&ws, &Webhook{RepoId: repoId})
  119. return ws, err
  120. }
  121. // UpdateWebhook updates information of webhook.
  122. func UpdateWebhook(w *Webhook) error {
  123. _, err := x.Id(w.Id).AllCols().Update(w)
  124. return err
  125. }
  126. // DeleteWebhook deletes webhook of repository.
  127. func DeleteWebhook(hookId int64) error {
  128. _, err := x.Delete(&Webhook{Id: hookId})
  129. return err
  130. }
  131. // GetWebhooksByOrgId returns all webhooks for an organization.
  132. func GetWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) {
  133. err = x.Find(&ws, &Webhook{OrgId: orgId})
  134. return ws, err
  135. }
  136. // GetActiveWebhooksByOrgId returns all active webhooks for an organization.
  137. func GetActiveWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) {
  138. err = x.Where("org_id=?", orgId).And("is_active=?", true).Find(&ws)
  139. return ws, err
  140. }
  141. // ___ ___ __ ___________ __
  142. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  143. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  144. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  145. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  146. // \/ \/ \/ \/ \/
  147. type HookTaskType int
  148. const (
  149. GOGS HookTaskType = iota + 1
  150. SLACK
  151. )
  152. var hookTaskTypes = map[string]HookTaskType{
  153. "gogs": GOGS,
  154. "slack": SLACK,
  155. }
  156. // ToHookTaskType returns HookTaskType by given name.
  157. func ToHookTaskType(name string) HookTaskType {
  158. return hookTaskTypes[name]
  159. }
  160. func (t HookTaskType) Name() string {
  161. switch t {
  162. case GOGS:
  163. return "gogs"
  164. case SLACK:
  165. return "slack"
  166. }
  167. return ""
  168. }
  169. // IsValidHookTaskType returns true if given name is a valid hook task type.
  170. func IsValidHookTaskType(name string) bool {
  171. _, ok := hookTaskTypes[name]
  172. return ok
  173. }
  174. type HookEventType string
  175. const (
  176. HOOK_EVENT_PUSH HookEventType = "push"
  177. )
  178. // FIXME: just use go-gogs-client structs maybe?
  179. type PayloadAuthor struct {
  180. Name string `json:"name"`
  181. Email string `json:"email"`
  182. UserName string `json:"username"`
  183. }
  184. type PayloadCommit struct {
  185. Id string `json:"id"`
  186. Message string `json:"message"`
  187. Url string `json:"url"`
  188. Author *PayloadAuthor `json:"author"`
  189. }
  190. type PayloadRepo struct {
  191. Id int64 `json:"id"`
  192. Name string `json:"name"`
  193. Url string `json:"url"`
  194. Description string `json:"description"`
  195. Website string `json:"website"`
  196. Watchers int `json:"watchers"`
  197. Owner *PayloadAuthor `json:"owner"`
  198. Private bool `json:"private"`
  199. }
  200. type BasePayload interface {
  201. GetJSONPayload() ([]byte, error)
  202. }
  203. // Payload represents a payload information of hook.
  204. type Payload struct {
  205. Secret string `json:"secret"`
  206. Ref string `json:"ref"`
  207. Commits []*PayloadCommit `json:"commits"`
  208. Repo *PayloadRepo `json:"repository"`
  209. Pusher *PayloadAuthor `json:"pusher"`
  210. Before string `json:"before"`
  211. After string `json:"after"`
  212. CompareUrl string `json:"compare_url"`
  213. }
  214. func (p Payload) GetJSONPayload() ([]byte, error) {
  215. data, err := json.Marshal(p)
  216. if err != nil {
  217. return []byte{}, err
  218. }
  219. return data, nil
  220. }
  221. // HookTask represents a hook task.
  222. type HookTask struct {
  223. ID int64 `xorm:"pk autoincr"`
  224. RepoID int64 `xorm:"INDEX"`
  225. HookID int64
  226. Uuid string
  227. Type HookTaskType
  228. Url string
  229. BasePayload `xorm:"-"`
  230. PayloadContent string `xorm:"TEXT"`
  231. ContentType HookContentType
  232. EventType HookEventType
  233. IsSsl bool
  234. IsDelivered bool
  235. Delivered int64
  236. IsSucceed bool
  237. }
  238. // CreateHookTask creates a new hook task,
  239. // it handles conversion from Payload to PayloadContent.
  240. func CreateHookTask(t *HookTask) error {
  241. data, err := t.BasePayload.GetJSONPayload()
  242. if err != nil {
  243. return err
  244. }
  245. t.Uuid = uuid.NewV4().String()
  246. t.PayloadContent = string(data)
  247. _, err = x.Insert(t)
  248. return err
  249. }
  250. // UpdateHookTask updates information of hook task.
  251. func UpdateHookTask(t *HookTask) error {
  252. _, err := x.Id(t.ID).AllCols().Update(t)
  253. return err
  254. }
  255. type hookQueue struct {
  256. // Make sure one repository only occur once in the queue.
  257. lock sync.Mutex
  258. repoIDs map[int64]bool
  259. queue chan int64
  260. }
  261. func (q *hookQueue) removeRepoID(id int64) {
  262. q.lock.Lock()
  263. defer q.lock.Unlock()
  264. delete(q.repoIDs, id)
  265. }
  266. func (q *hookQueue) addRepoID(id int64) {
  267. q.lock.Lock()
  268. if q.repoIDs[id] {
  269. q.lock.Unlock()
  270. return
  271. }
  272. q.repoIDs[id] = true
  273. q.lock.Unlock()
  274. q.queue <- id
  275. }
  276. // AddRepoID adds repository ID to hook delivery queue.
  277. func (q *hookQueue) AddRepoID(id int64) {
  278. go q.addRepoID(id)
  279. }
  280. var HookQueue *hookQueue
  281. func deliverHook(t *HookTask) {
  282. timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
  283. req := httplib.Post(t.Url).SetTimeout(timeout, timeout).
  284. Header("X-Gogs-Delivery", t.Uuid).
  285. Header("X-Gogs-Event", string(t.EventType)).
  286. SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify})
  287. switch t.ContentType {
  288. case JSON:
  289. req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
  290. case FORM:
  291. req.Param("payload", t.PayloadContent)
  292. }
  293. t.IsDelivered = true
  294. // FIXME: record response.
  295. switch t.Type {
  296. case GOGS:
  297. {
  298. if resp, err := req.Response(); err != nil {
  299. log.Error(5, "Delivery: %v", err)
  300. } else {
  301. resp.Body.Close()
  302. t.IsSucceed = true
  303. }
  304. }
  305. case SLACK:
  306. {
  307. if resp, err := req.Response(); err != nil {
  308. log.Error(5, "Delivery: %v", err)
  309. } else {
  310. defer resp.Body.Close()
  311. contents, err := ioutil.ReadAll(resp.Body)
  312. if err != nil {
  313. log.Error(5, "%s", err)
  314. } else {
  315. if string(contents) != "ok" {
  316. log.Error(5, "slack failed with: %s", string(contents))
  317. } else {
  318. t.IsSucceed = true
  319. }
  320. }
  321. }
  322. }
  323. }
  324. t.Delivered = time.Now().UTC().UnixNano()
  325. if t.IsSucceed {
  326. log.Trace("Hook delivered(%s): %s", t.Uuid, t.PayloadContent)
  327. }
  328. }
  329. // DeliverHooks checks and delivers undelivered hooks.
  330. func DeliverHooks() {
  331. tasks := make([]*HookTask, 0, 10)
  332. x.Where("is_delivered=?", false).Iterate(new(HookTask),
  333. func(idx int, bean interface{}) error {
  334. t := bean.(*HookTask)
  335. deliverHook(t)
  336. tasks = append(tasks, t)
  337. return nil
  338. })
  339. // Update hook task status.
  340. for _, t := range tasks {
  341. if err := UpdateHookTask(t); err != nil {
  342. log.Error(4, "UpdateHookTask(%d): %v", t.ID, err)
  343. }
  344. }
  345. HookQueue = &hookQueue{
  346. lock: sync.Mutex{},
  347. repoIDs: make(map[int64]bool),
  348. queue: make(chan int64, setting.Webhook.QueueLength),
  349. }
  350. // Start listening on new hook requests.
  351. for repoID := range HookQueue.queue {
  352. HookQueue.removeRepoID(repoID)
  353. tasks = make([]*HookTask, 0, 5)
  354. if err := x.Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
  355. log.Error(4, "Get repository(%d) hook tasks: %v", repoID, err)
  356. continue
  357. }
  358. for _, t := range tasks {
  359. deliverHook(t)
  360. if err := UpdateHookTask(t); err != nil {
  361. log.Error(4, "UpdateHookTask(%d): %v", t.ID, err)
  362. }
  363. }
  364. }
  365. }
  366. func InitDeliverHooks() {
  367. go DeliverHooks()
  368. }