You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

webhook.go 18 kB

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