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.

706 lines
18 KiB

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