webhook.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. "fmt"
  9. "io/ioutil"
  10. "strings"
  11. "sync"
  12. "time"
  13. "github.com/Unknwon/com"
  14. "github.com/go-xorm/xorm"
  15. gouuid "github.com/satori/go.uuid"
  16. api "github.com/gogits/go-gogs-client"
  17. "github.com/gogits/gogs/modules/httplib"
  18. "github.com/gogits/gogs/modules/log"
  19. "github.com/gogits/gogs/modules/setting"
  20. )
  21. type HookContentType int
  22. const (
  23. JSON HookContentType = iota + 1
  24. FORM
  25. )
  26. var hookContentTypes = map[string]HookContentType{
  27. "json": JSON,
  28. "form": FORM,
  29. }
  30. // ToHookContentType returns HookContentType by given name.
  31. func ToHookContentType(name string) HookContentType {
  32. return hookContentTypes[name]
  33. }
  34. func (t HookContentType) Name() string {
  35. switch t {
  36. case JSON:
  37. return "json"
  38. case FORM:
  39. return "form"
  40. }
  41. return ""
  42. }
  43. // IsValidHookContentType returns true if given name is a valid hook content type.
  44. func IsValidHookContentType(name string) bool {
  45. _, ok := hookContentTypes[name]
  46. return ok
  47. }
  48. type HookEvents struct {
  49. Create bool `json:"create"`
  50. Push bool `json:"push"`
  51. }
  52. // HookEvent represents events that will delivery hook.
  53. type HookEvent struct {
  54. PushOnly bool `json:"push_only"`
  55. SendEverything bool `json:"send_everything"`
  56. ChooseEvents bool `json:"choose_events"`
  57. HookEvents `json:"events"`
  58. }
  59. type HookStatus int
  60. const (
  61. HOOK_STATUS_NONE = iota
  62. HOOK_STATUS_SUCCEED
  63. HOOK_STATUS_FAILED
  64. )
  65. // Webhook represents a web hook object.
  66. type Webhook struct {
  67. ID int64 `xorm:"pk autoincr"`
  68. RepoID int64
  69. OrgID int64
  70. URL string `xorm:"url TEXT"`
  71. ContentType HookContentType
  72. Secret string `xorm:"TEXT"`
  73. Events string `xorm:"TEXT"`
  74. *HookEvent `xorm:"-"`
  75. IsSSL bool `xorm:"is_ssl"`
  76. IsActive bool
  77. HookTaskType HookTaskType
  78. Meta string `xorm:"TEXT"` // store hook-specific attributes
  79. LastStatus HookStatus // Last delivery status
  80. Created time.Time `xorm:"-"`
  81. CreatedUnix int64
  82. Updated time.Time `xorm:"-"`
  83. UpdatedUnix int64
  84. }
  85. func (w *Webhook) BeforeInsert() {
  86. w.CreatedUnix = time.Now().UTC().Unix()
  87. w.UpdatedUnix = w.CreatedUnix
  88. }
  89. func (w *Webhook) BeforeUpdate() {
  90. w.UpdatedUnix = time.Now().UTC().Unix()
  91. }
  92. func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
  93. var err error
  94. switch colName {
  95. case "events":
  96. w.HookEvent = &HookEvent{}
  97. if err = json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  98. log.Error(3, "Unmarshal[%d]: %v", w.ID, err)
  99. }
  100. case "created_unix":
  101. w.Created = time.Unix(w.CreatedUnix, 0).Local()
  102. case "updated_unix":
  103. w.Updated = time.Unix(w.UpdatedUnix, 0).Local()
  104. }
  105. }
  106. func (w *Webhook) GetSlackHook() *SlackMeta {
  107. s := &SlackMeta{}
  108. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  109. log.Error(4, "webhook.GetSlackHook(%d): %v", w.ID, err)
  110. }
  111. return s
  112. }
  113. // History returns history of webhook by given conditions.
  114. func (w *Webhook) History(page int) ([]*HookTask, error) {
  115. return HookTasks(w.ID, page)
  116. }
  117. // UpdateEvent handles conversion from HookEvent to Events.
  118. func (w *Webhook) UpdateEvent() error {
  119. data, err := json.Marshal(w.HookEvent)
  120. w.Events = string(data)
  121. return err
  122. }
  123. // HasCreateEvent returns true if hook enabled create event.
  124. func (w *Webhook) HasCreateEvent() bool {
  125. return w.SendEverything ||
  126. (w.ChooseEvents && w.HookEvents.Create)
  127. }
  128. // HasPushEvent returns true if hook enabled push event.
  129. func (w *Webhook) HasPushEvent() bool {
  130. return w.PushOnly || w.SendEverything ||
  131. (w.ChooseEvents && w.HookEvents.Push)
  132. }
  133. func (w *Webhook) EventsArray() []string {
  134. events := make([]string, 0, 2)
  135. if w.HasCreateEvent() {
  136. events = append(events, "create")
  137. }
  138. if w.HasPushEvent() {
  139. events = append(events, "push")
  140. }
  141. return events
  142. }
  143. // CreateWebhook creates a new web hook.
  144. func CreateWebhook(w *Webhook) error {
  145. _, err := x.Insert(w)
  146. return err
  147. }
  148. // GetWebhookByID returns webhook of repository by given ID.
  149. func GetWebhookByID(repoID, id int64) (*Webhook, error) {
  150. w := new(Webhook)
  151. has, err := x.Id(id).And("repo_id=?", repoID).Get(w)
  152. if err != nil {
  153. return nil, err
  154. } else if !has {
  155. return nil, ErrWebhookNotExist{id}
  156. }
  157. return w, nil
  158. }
  159. // GetActiveWebhooksByRepoID returns all active webhooks of repository.
  160. func GetActiveWebhooksByRepoID(repoID int64) (ws []*Webhook, err error) {
  161. err = x.Where("repo_id=?", repoID).And("is_active=?", true).Find(&ws)
  162. return ws, err
  163. }
  164. // GetWebhooksByRepoID returns all webhooks of repository.
  165. func GetWebhooksByRepoID(repoID int64) (ws []*Webhook, err error) {
  166. err = x.Find(&ws, &Webhook{RepoID: repoID})
  167. return ws, err
  168. }
  169. // UpdateWebhook updates information of webhook.
  170. func UpdateWebhook(w *Webhook) error {
  171. _, err := x.Id(w.ID).AllCols().Update(w)
  172. return err
  173. }
  174. // DeleteWebhook deletes webhook of repository.
  175. func DeleteWebhook(id int64) (err error) {
  176. sess := x.NewSession()
  177. defer sessionRelease(sess)
  178. if err = sess.Begin(); err != nil {
  179. return err
  180. }
  181. if _, err = sess.Delete(&Webhook{ID: id}); err != nil {
  182. return err
  183. } else if _, err = sess.Delete(&HookTask{HookID: id}); err != nil {
  184. return err
  185. }
  186. return sess.Commit()
  187. }
  188. // GetWebhooksByOrgId returns all webhooks for an organization.
  189. func GetWebhooksByOrgId(orgID int64) (ws []*Webhook, err error) {
  190. err = x.Find(&ws, &Webhook{OrgID: orgID})
  191. return ws, err
  192. }
  193. // GetActiveWebhooksByOrgID returns all active webhooks for an organization.
  194. func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  195. err = x.Where("org_id=?", orgID).And("is_active=?", true).Find(&ws)
  196. return ws, err
  197. }
  198. // ___ ___ __ ___________ __
  199. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  200. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  201. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  202. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  203. // \/ \/ \/ \/ \/
  204. type HookTaskType int
  205. const (
  206. GOGS HookTaskType = iota + 1
  207. SLACK
  208. )
  209. var hookTaskTypes = map[string]HookTaskType{
  210. "gogs": GOGS,
  211. "slack": SLACK,
  212. }
  213. // ToHookTaskType returns HookTaskType by given name.
  214. func ToHookTaskType(name string) HookTaskType {
  215. return hookTaskTypes[name]
  216. }
  217. func (t HookTaskType) Name() string {
  218. switch t {
  219. case GOGS:
  220. return "gogs"
  221. case SLACK:
  222. return "slack"
  223. }
  224. return ""
  225. }
  226. // IsValidHookTaskType returns true if given name is a valid hook task type.
  227. func IsValidHookTaskType(name string) bool {
  228. _, ok := hookTaskTypes[name]
  229. return ok
  230. }
  231. type HookEventType string
  232. const (
  233. HOOK_EVENT_CREATE HookEventType = "create"
  234. HOOK_EVENT_PUSH HookEventType = "push"
  235. )
  236. // HookRequest represents hook task request information.
  237. type HookRequest struct {
  238. Headers map[string]string `json:"headers"`
  239. }
  240. // HookResponse represents hook task response information.
  241. type HookResponse struct {
  242. Status int `json:"status"`
  243. Headers map[string]string `json:"headers"`
  244. Body string `json:"body"`
  245. }
  246. // HookTask represents a hook task.
  247. type HookTask struct {
  248. ID int64 `xorm:"pk autoincr"`
  249. RepoID int64 `xorm:"INDEX"`
  250. HookID int64
  251. UUID string
  252. Type HookTaskType
  253. URL string `xorm:"TEXT"`
  254. api.Payloader `xorm:"-"`
  255. PayloadContent string `xorm:"TEXT"`
  256. ContentType HookContentType
  257. EventType HookEventType
  258. IsSSL bool
  259. IsDelivered bool
  260. Delivered int64
  261. DeliveredString string `xorm:"-"`
  262. // History info.
  263. IsSucceed bool
  264. RequestContent string `xorm:"TEXT"`
  265. RequestInfo *HookRequest `xorm:"-"`
  266. ResponseContent string `xorm:"TEXT"`
  267. ResponseInfo *HookResponse `xorm:"-"`
  268. }
  269. func (t *HookTask) BeforeUpdate() {
  270. if t.RequestInfo != nil {
  271. t.RequestContent = t.MarshalJSON(t.RequestInfo)
  272. }
  273. if t.ResponseInfo != nil {
  274. t.ResponseContent = t.MarshalJSON(t.ResponseInfo)
  275. }
  276. }
  277. func (t *HookTask) AfterSet(colName string, _ xorm.Cell) {
  278. var err error
  279. switch colName {
  280. case "delivered":
  281. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  282. case "request_content":
  283. if len(t.RequestContent) == 0 {
  284. return
  285. }
  286. t.RequestInfo = &HookRequest{}
  287. if err = json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  288. log.Error(3, "Unmarshal[%d]: %v", t.ID, err)
  289. }
  290. case "response_content":
  291. if len(t.ResponseContent) == 0 {
  292. return
  293. }
  294. t.ResponseInfo = &HookResponse{}
  295. if err = json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  296. log.Error(3, "Unmarshal [%d]: %v", t.ID, err)
  297. }
  298. }
  299. }
  300. func (t *HookTask) MarshalJSON(v interface{}) string {
  301. p, err := json.Marshal(v)
  302. if err != nil {
  303. log.Error(3, "Marshal [%d]: %v", t.ID, err)
  304. }
  305. return string(p)
  306. }
  307. // HookTasks returns a list of hook tasks by given conditions.
  308. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  309. tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
  310. return tasks, x.Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).Where("hook_id=?", hookID).Desc("id").Find(&tasks)
  311. }
  312. // CreateHookTask creates a new hook task,
  313. // it handles conversion from Payload to PayloadContent.
  314. func CreateHookTask(t *HookTask) error {
  315. data, err := t.Payloader.JSONPayload()
  316. if err != nil {
  317. return err
  318. }
  319. t.UUID = gouuid.NewV4().String()
  320. t.PayloadContent = string(data)
  321. _, err = x.Insert(t)
  322. return err
  323. }
  324. // UpdateHookTask updates information of hook task.
  325. func UpdateHookTask(t *HookTask) error {
  326. _, err := x.Id(t.ID).AllCols().Update(t)
  327. return err
  328. }
  329. // PrepareWebhooks adds new webhooks to task queue for given payload.
  330. func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
  331. if err := repo.GetOwner(); err != nil {
  332. return fmt.Errorf("GetOwner: %v", err)
  333. }
  334. ws, err := GetActiveWebhooksByRepoID(repo.ID)
  335. if err != nil {
  336. return fmt.Errorf("GetActiveWebhooksByRepoID: %v", err)
  337. }
  338. // check if repo belongs to org and append additional webhooks
  339. if repo.Owner.IsOrganization() {
  340. // get hooks for org
  341. orgws, err := GetActiveWebhooksByOrgID(repo.OwnerID)
  342. if err != nil {
  343. return fmt.Errorf("GetActiveWebhooksByOrgID: %v", err)
  344. }
  345. ws = append(ws, orgws...)
  346. }
  347. if len(ws) == 0 {
  348. return nil
  349. }
  350. var payloader api.Payloader
  351. for _, w := range ws {
  352. switch event {
  353. case HOOK_EVENT_CREATE:
  354. if !w.HasCreateEvent() {
  355. continue
  356. }
  357. case HOOK_EVENT_PUSH:
  358. if !w.HasPushEvent() {
  359. continue
  360. }
  361. }
  362. // Use separate objects so modifcations won't be made on payload on non-Gogs type hooks.
  363. switch w.HookTaskType {
  364. case SLACK:
  365. payloader, err = GetSlackPayload(p, event, w.Meta)
  366. if err != nil {
  367. return fmt.Errorf("GetSlackPayload: %v", err)
  368. }
  369. default:
  370. p.SetSecret(w.Secret)
  371. payloader = p
  372. }
  373. if err = CreateHookTask(&HookTask{
  374. RepoID: repo.ID,
  375. HookID: w.ID,
  376. Type: w.HookTaskType,
  377. URL: w.URL,
  378. Payloader: payloader,
  379. ContentType: w.ContentType,
  380. EventType: HOOK_EVENT_PUSH,
  381. IsSSL: w.IsSSL,
  382. }); err != nil {
  383. return fmt.Errorf("CreateHookTask: %v", err)
  384. }
  385. }
  386. return nil
  387. }
  388. // UniqueQueue represents a queue that guarantees only one instance of same ID is in the line.
  389. type UniqueQueue struct {
  390. lock sync.Mutex
  391. ids map[string]bool
  392. queue chan string
  393. }
  394. func (q *UniqueQueue) Queue() <-chan string {
  395. return q.queue
  396. }
  397. func NewUniqueQueue(queueLength int) *UniqueQueue {
  398. if queueLength <= 0 {
  399. queueLength = 100
  400. }
  401. return &UniqueQueue{
  402. ids: make(map[string]bool),
  403. queue: make(chan string, queueLength),
  404. }
  405. }
  406. func (q *UniqueQueue) Remove(id interface{}) {
  407. q.lock.Lock()
  408. defer q.lock.Unlock()
  409. delete(q.ids, com.ToStr(id))
  410. }
  411. func (q *UniqueQueue) AddFunc(id interface{}, fn func()) {
  412. newid := com.ToStr(id)
  413. if q.Exist(id) {
  414. return
  415. }
  416. q.lock.Lock()
  417. q.ids[newid] = true
  418. if fn != nil {
  419. fn()
  420. }
  421. q.lock.Unlock()
  422. q.queue <- newid
  423. }
  424. func (q *UniqueQueue) Add(id interface{}) {
  425. q.AddFunc(id, nil)
  426. }
  427. func (q *UniqueQueue) Exist(id interface{}) bool {
  428. q.lock.Lock()
  429. defer q.lock.Unlock()
  430. return q.ids[com.ToStr(id)]
  431. }
  432. var HookQueue = NewUniqueQueue(setting.Webhook.QueueLength)
  433. func (t *HookTask) deliver() {
  434. t.IsDelivered = true
  435. timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
  436. req := httplib.Post(t.URL).SetTimeout(timeout, timeout).
  437. Header("X-Gogs-Delivery", t.UUID).
  438. Header("X-Gogs-Event", string(t.EventType)).
  439. SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify})
  440. switch t.ContentType {
  441. case JSON:
  442. req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
  443. case FORM:
  444. req.Param("payload", t.PayloadContent)
  445. }
  446. // Record delivery information.
  447. t.RequestInfo = &HookRequest{
  448. Headers: map[string]string{},
  449. }
  450. for k, vals := range req.Headers() {
  451. t.RequestInfo.Headers[k] = strings.Join(vals, ",")
  452. }
  453. t.ResponseInfo = &HookResponse{
  454. Headers: map[string]string{},
  455. }
  456. defer func() {
  457. t.Delivered = time.Now().UTC().UnixNano()
  458. if t.IsSucceed {
  459. log.Trace("Hook delivered: %s", t.UUID)
  460. } else {
  461. log.Trace("Hook delivery failed: %s", t.UUID)
  462. }
  463. // Update webhook last delivery status.
  464. w, err := GetWebhookByID(t.RepoID, t.HookID)
  465. if err != nil {
  466. log.Error(5, "GetWebhookByID: %v", err)
  467. return
  468. }
  469. if t.IsSucceed {
  470. w.LastStatus = HOOK_STATUS_SUCCEED
  471. } else {
  472. w.LastStatus = HOOK_STATUS_FAILED
  473. }
  474. if err = UpdateWebhook(w); err != nil {
  475. log.Error(5, "UpdateWebhook: %v", err)
  476. return
  477. }
  478. }()
  479. resp, err := req.Response()
  480. if err != nil {
  481. t.ResponseInfo.Body = fmt.Sprintf("Delivery: %v", err)
  482. return
  483. }
  484. defer resp.Body.Close()
  485. // Status code is 20x can be seen as succeed.
  486. t.IsSucceed = resp.StatusCode/100 == 2
  487. t.ResponseInfo.Status = resp.StatusCode
  488. for k, vals := range resp.Header {
  489. t.ResponseInfo.Headers[k] = strings.Join(vals, ",")
  490. }
  491. p, err := ioutil.ReadAll(resp.Body)
  492. if err != nil {
  493. t.ResponseInfo.Body = fmt.Sprintf("read body: %s", err)
  494. return
  495. }
  496. t.ResponseInfo.Body = string(p)
  497. }
  498. // DeliverHooks checks and delivers undelivered hooks.
  499. // TODO: shoot more hooks at same time.
  500. func DeliverHooks() {
  501. tasks := make([]*HookTask, 0, 10)
  502. x.Where("is_delivered=?", false).Iterate(new(HookTask),
  503. func(idx int, bean interface{}) error {
  504. t := bean.(*HookTask)
  505. t.deliver()
  506. tasks = append(tasks, t)
  507. return nil
  508. })
  509. // Update hook task status.
  510. for _, t := range tasks {
  511. if err := UpdateHookTask(t); err != nil {
  512. log.Error(4, "UpdateHookTask [%d]: %v", t.ID, err)
  513. }
  514. }
  515. // Start listening on new hook requests.
  516. for repoID := range HookQueue.Queue() {
  517. log.Trace("DeliverHooks [%v]: processing delivery hooks", repoID)
  518. HookQueue.Remove(repoID)
  519. tasks = make([]*HookTask, 0, 5)
  520. if err := x.Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
  521. log.Error(4, "Get repository [%d] hook tasks: %v", repoID, err)
  522. continue
  523. }
  524. for _, t := range tasks {
  525. t.deliver()
  526. if err := UpdateHookTask(t); err != nil {
  527. log.Error(4, "UpdateHookTask [%d]: %v", t.ID, err)
  528. continue
  529. }
  530. }
  531. }
  532. }
  533. func InitDeliverHooks() {
  534. go DeliverHooks()
  535. }