webhook.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 db
  5. import (
  6. "crypto/hmac"
  7. "crypto/sha256"
  8. "crypto/tls"
  9. "encoding/hex"
  10. "fmt"
  11. "io/ioutil"
  12. "strings"
  13. "time"
  14. jsoniter "github.com/json-iterator/go"
  15. gouuid "github.com/satori/go.uuid"
  16. log "unknwon.dev/clog/v2"
  17. "xorm.io/xorm"
  18. api "github.com/gogs/go-gogs-client"
  19. "gogs.io/gogs/internal/conf"
  20. "gogs.io/gogs/internal/errutil"
  21. "gogs.io/gogs/internal/httplib"
  22. "gogs.io/gogs/internal/sync"
  23. )
  24. var HookQueue = sync.NewUniqueQueue(1000)
  25. type HookContentType int
  26. const (
  27. JSON HookContentType = iota + 1
  28. FORM
  29. )
  30. var hookContentTypes = map[string]HookContentType{
  31. "json": JSON,
  32. "form": FORM,
  33. }
  34. // ToHookContentType returns HookContentType by given name.
  35. func ToHookContentType(name string) HookContentType {
  36. return hookContentTypes[name]
  37. }
  38. func (t HookContentType) Name() string {
  39. switch t {
  40. case JSON:
  41. return "json"
  42. case FORM:
  43. return "form"
  44. }
  45. return ""
  46. }
  47. // IsValidHookContentType returns true if given name is a valid hook content type.
  48. func IsValidHookContentType(name string) bool {
  49. _, ok := hookContentTypes[name]
  50. return ok
  51. }
  52. type HookEvents struct {
  53. Create bool `json:"create"`
  54. Delete bool `json:"delete"`
  55. Fork bool `json:"fork"`
  56. Push bool `json:"push"`
  57. Issues bool `json:"issues"`
  58. PullRequest bool `json:"pull_request"`
  59. IssueComment bool `json:"issue_comment"`
  60. Release bool `json:"release"`
  61. }
  62. // HookEvent represents events that will delivery hook.
  63. type HookEvent struct {
  64. PushOnly bool `json:"push_only"`
  65. SendEverything bool `json:"send_everything"`
  66. ChooseEvents bool `json:"choose_events"`
  67. HookEvents `json:"events"`
  68. }
  69. type HookStatus int
  70. const (
  71. HOOK_STATUS_NONE = iota
  72. HOOK_STATUS_SUCCEED
  73. HOOK_STATUS_FAILED
  74. )
  75. // Webhook represents a web hook object.
  76. type Webhook struct {
  77. ID int64
  78. RepoID int64
  79. OrgID int64
  80. URL string `xorm:"url TEXT"`
  81. ContentType HookContentType
  82. Secret string `xorm:"TEXT"`
  83. Events string `xorm:"TEXT"`
  84. *HookEvent `xorm:"-"` // LEGACY [1.0]: Cannot ignore JSON (i.e. json:"-") here, it breaks old backup archive
  85. IsSSL bool `xorm:"is_ssl"`
  86. IsActive bool
  87. HookTaskType HookTaskType
  88. Meta string `xorm:"TEXT"` // store hook-specific attributes
  89. LastStatus HookStatus // Last delivery status
  90. Created time.Time `xorm:"-" json:"-"`
  91. CreatedUnix int64
  92. Updated time.Time `xorm:"-" json:"-"`
  93. UpdatedUnix int64
  94. }
  95. func (w *Webhook) BeforeInsert() {
  96. w.CreatedUnix = time.Now().Unix()
  97. w.UpdatedUnix = w.CreatedUnix
  98. }
  99. func (w *Webhook) BeforeUpdate() {
  100. w.UpdatedUnix = time.Now().Unix()
  101. }
  102. func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
  103. var err error
  104. switch colName {
  105. case "events":
  106. w.HookEvent = &HookEvent{}
  107. if err = jsoniter.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  108. log.Error("Unmarshal [%d]: %v", w.ID, err)
  109. }
  110. case "created_unix":
  111. w.Created = time.Unix(w.CreatedUnix, 0).Local()
  112. case "updated_unix":
  113. w.Updated = time.Unix(w.UpdatedUnix, 0).Local()
  114. }
  115. }
  116. func (w *Webhook) SlackMeta() *SlackMeta {
  117. s := &SlackMeta{}
  118. if err := jsoniter.Unmarshal([]byte(w.Meta), s); err != nil {
  119. log.Error("Failed to get Slack meta [webhook_id: %d]: %v", w.ID, err)
  120. }
  121. return s
  122. }
  123. // History returns history of webhook by given conditions.
  124. func (w *Webhook) History(page int) ([]*HookTask, error) {
  125. return HookTasks(w.ID, page)
  126. }
  127. // UpdateEvent handles conversion from HookEvent to Events.
  128. func (w *Webhook) UpdateEvent() error {
  129. data, err := jsoniter.Marshal(w.HookEvent)
  130. w.Events = string(data)
  131. return err
  132. }
  133. // HasCreateEvent returns true if hook enabled create event.
  134. func (w *Webhook) HasCreateEvent() bool {
  135. return w.SendEverything ||
  136. (w.ChooseEvents && w.HookEvents.Create)
  137. }
  138. // HasDeleteEvent returns true if hook enabled delete event.
  139. func (w *Webhook) HasDeleteEvent() bool {
  140. return w.SendEverything ||
  141. (w.ChooseEvents && w.HookEvents.Delete)
  142. }
  143. // HasForkEvent returns true if hook enabled fork event.
  144. func (w *Webhook) HasForkEvent() bool {
  145. return w.SendEverything ||
  146. (w.ChooseEvents && w.HookEvents.Fork)
  147. }
  148. // HasPushEvent returns true if hook enabled push event.
  149. func (w *Webhook) HasPushEvent() bool {
  150. return w.PushOnly || w.SendEverything ||
  151. (w.ChooseEvents && w.HookEvents.Push)
  152. }
  153. // HasIssuesEvent returns true if hook enabled issues event.
  154. func (w *Webhook) HasIssuesEvent() bool {
  155. return w.SendEverything ||
  156. (w.ChooseEvents && w.HookEvents.Issues)
  157. }
  158. // HasPullRequestEvent returns true if hook enabled pull request event.
  159. func (w *Webhook) HasPullRequestEvent() bool {
  160. return w.SendEverything ||
  161. (w.ChooseEvents && w.HookEvents.PullRequest)
  162. }
  163. // HasIssueCommentEvent returns true if hook enabled issue comment event.
  164. func (w *Webhook) HasIssueCommentEvent() bool {
  165. return w.SendEverything ||
  166. (w.ChooseEvents && w.HookEvents.IssueComment)
  167. }
  168. // HasReleaseEvent returns true if hook enabled release event.
  169. func (w *Webhook) HasReleaseEvent() bool {
  170. return w.SendEverything ||
  171. (w.ChooseEvents && w.HookEvents.Release)
  172. }
  173. type eventChecker struct {
  174. checker func() bool
  175. typ HookEventType
  176. }
  177. func (w *Webhook) EventsArray() []string {
  178. events := make([]string, 0, 8)
  179. eventCheckers := []eventChecker{
  180. {w.HasCreateEvent, HOOK_EVENT_CREATE},
  181. {w.HasDeleteEvent, HOOK_EVENT_DELETE},
  182. {w.HasForkEvent, HOOK_EVENT_FORK},
  183. {w.HasPushEvent, HOOK_EVENT_PUSH},
  184. {w.HasIssuesEvent, HOOK_EVENT_ISSUES},
  185. {w.HasPullRequestEvent, HOOK_EVENT_PULL_REQUEST},
  186. {w.HasIssueCommentEvent, HOOK_EVENT_ISSUE_COMMENT},
  187. {w.HasReleaseEvent, HOOK_EVENT_RELEASE},
  188. }
  189. for _, c := range eventCheckers {
  190. if c.checker() {
  191. events = append(events, string(c.typ))
  192. }
  193. }
  194. return events
  195. }
  196. // CreateWebhook creates a new web hook.
  197. func CreateWebhook(w *Webhook) error {
  198. _, err := x.Insert(w)
  199. return err
  200. }
  201. var _ errutil.NotFound = (*ErrWebhookNotExist)(nil)
  202. type ErrWebhookNotExist struct {
  203. args map[string]interface{}
  204. }
  205. func IsErrWebhookNotExist(err error) bool {
  206. _, ok := err.(ErrWebhookNotExist)
  207. return ok
  208. }
  209. func (err ErrWebhookNotExist) Error() string {
  210. return fmt.Sprintf("webhook does not exist: %v", err.args)
  211. }
  212. func (ErrWebhookNotExist) NotFound() bool {
  213. return true
  214. }
  215. // getWebhook uses argument bean as query condition,
  216. // ID must be specified and do not assign unnecessary fields.
  217. func getWebhook(bean *Webhook) (*Webhook, error) {
  218. has, err := x.Get(bean)
  219. if err != nil {
  220. return nil, err
  221. } else if !has {
  222. return nil, ErrWebhookNotExist{args: map[string]interface{}{"webhookID": bean.ID}}
  223. }
  224. return bean, nil
  225. }
  226. // GetWebhookByID returns webhook by given ID.
  227. // Use this function with caution of accessing unauthorized webhook,
  228. // which means should only be used in non-user interactive functions.
  229. func GetWebhookByID(id int64) (*Webhook, error) {
  230. return getWebhook(&Webhook{
  231. ID: id,
  232. })
  233. }
  234. // GetWebhookOfRepoByID returns webhook of repository by given ID.
  235. func GetWebhookOfRepoByID(repoID, id int64) (*Webhook, error) {
  236. return getWebhook(&Webhook{
  237. ID: id,
  238. RepoID: repoID,
  239. })
  240. }
  241. // GetWebhookByOrgID returns webhook of organization by given ID.
  242. func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
  243. return getWebhook(&Webhook{
  244. ID: id,
  245. OrgID: orgID,
  246. })
  247. }
  248. // getActiveWebhooksByRepoID returns all active webhooks of repository.
  249. func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
  250. webhooks := make([]*Webhook, 0, 5)
  251. return webhooks, e.Where("repo_id = ?", repoID).And("is_active = ?", true).Find(&webhooks)
  252. }
  253. // GetWebhooksByRepoID returns all webhooks of a repository.
  254. func GetWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  255. webhooks := make([]*Webhook, 0, 5)
  256. return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
  257. }
  258. // UpdateWebhook updates information of webhook.
  259. func UpdateWebhook(w *Webhook) error {
  260. _, err := x.Id(w.ID).AllCols().Update(w)
  261. return err
  262. }
  263. // deleteWebhook uses argument bean as query condition,
  264. // ID must be specified and do not assign unnecessary fields.
  265. func deleteWebhook(bean *Webhook) (err error) {
  266. sess := x.NewSession()
  267. defer sess.Close()
  268. if err = sess.Begin(); err != nil {
  269. return err
  270. }
  271. if _, err = sess.Delete(bean); err != nil {
  272. return err
  273. } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
  274. return err
  275. }
  276. return sess.Commit()
  277. }
  278. // DeleteWebhookOfRepoByID deletes webhook of repository by given ID.
  279. func DeleteWebhookOfRepoByID(repoID, id int64) error {
  280. return deleteWebhook(&Webhook{
  281. ID: id,
  282. RepoID: repoID,
  283. })
  284. }
  285. // DeleteWebhookOfOrgByID deletes webhook of organization by given ID.
  286. func DeleteWebhookOfOrgByID(orgID, id int64) error {
  287. return deleteWebhook(&Webhook{
  288. ID: id,
  289. OrgID: orgID,
  290. })
  291. }
  292. // GetWebhooksByOrgID returns all webhooks for an organization.
  293. func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  294. err = x.Find(&ws, &Webhook{OrgID: orgID})
  295. return ws, err
  296. }
  297. // getActiveWebhooksByOrgID returns all active webhooks for an organization.
  298. func getActiveWebhooksByOrgID(e Engine, orgID int64) ([]*Webhook, error) {
  299. ws := make([]*Webhook, 0, 3)
  300. return ws, e.Where("org_id=?", orgID).And("is_active=?", true).Find(&ws)
  301. }
  302. // ___ ___ __ ___________ __
  303. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  304. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  305. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  306. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  307. // \/ \/ \/ \/ \/
  308. type HookTaskType int
  309. const (
  310. GOGS HookTaskType = iota + 1
  311. SLACK
  312. DISCORD
  313. DINGTALK
  314. )
  315. var hookTaskTypes = map[string]HookTaskType{
  316. "gogs": GOGS,
  317. "slack": SLACK,
  318. "discord": DISCORD,
  319. "dingtalk": DINGTALK,
  320. }
  321. // ToHookTaskType returns HookTaskType by given name.
  322. func ToHookTaskType(name string) HookTaskType {
  323. return hookTaskTypes[name]
  324. }
  325. func (t HookTaskType) Name() string {
  326. switch t {
  327. case GOGS:
  328. return "gogs"
  329. case SLACK:
  330. return "slack"
  331. case DISCORD:
  332. return "discord"
  333. case DINGTALK:
  334. return "dingtalk"
  335. }
  336. return ""
  337. }
  338. // IsValidHookTaskType returns true if given name is a valid hook task type.
  339. func IsValidHookTaskType(name string) bool {
  340. _, ok := hookTaskTypes[name]
  341. return ok
  342. }
  343. type HookEventType string
  344. const (
  345. HOOK_EVENT_CREATE HookEventType = "create"
  346. HOOK_EVENT_DELETE HookEventType = "delete"
  347. HOOK_EVENT_FORK HookEventType = "fork"
  348. HOOK_EVENT_PUSH HookEventType = "push"
  349. HOOK_EVENT_ISSUES HookEventType = "issues"
  350. HOOK_EVENT_PULL_REQUEST HookEventType = "pull_request"
  351. HOOK_EVENT_ISSUE_COMMENT HookEventType = "issue_comment"
  352. HOOK_EVENT_RELEASE HookEventType = "release"
  353. )
  354. // HookRequest represents hook task request information.
  355. type HookRequest struct {
  356. Headers map[string]string `json:"headers"`
  357. }
  358. // HookResponse represents hook task response information.
  359. type HookResponse struct {
  360. Status int `json:"status"`
  361. Headers map[string]string `json:"headers"`
  362. Body string `json:"body"`
  363. }
  364. // HookTask represents a hook task.
  365. type HookTask struct {
  366. ID int64
  367. RepoID int64 `xorm:"INDEX"`
  368. HookID int64
  369. UUID string
  370. Type HookTaskType
  371. URL string `xorm:"TEXT"`
  372. Signature string `xorm:"TEXT"`
  373. api.Payloader `xorm:"-" json:"-"`
  374. PayloadContent string `xorm:"TEXT"`
  375. ContentType HookContentType
  376. EventType HookEventType
  377. IsSSL bool
  378. IsDelivered bool
  379. Delivered int64
  380. DeliveredString string `xorm:"-" json:"-"`
  381. // History info.
  382. IsSucceed bool
  383. RequestContent string `xorm:"TEXT"`
  384. RequestInfo *HookRequest `xorm:"-" json:"-"`
  385. ResponseContent string `xorm:"TEXT"`
  386. ResponseInfo *HookResponse `xorm:"-" json:"-"`
  387. }
  388. func (t *HookTask) BeforeUpdate() {
  389. if t.RequestInfo != nil {
  390. t.RequestContent = t.ToJSON(t.RequestInfo)
  391. }
  392. if t.ResponseInfo != nil {
  393. t.ResponseContent = t.ToJSON(t.ResponseInfo)
  394. }
  395. }
  396. func (t *HookTask) AfterSet(colName string, _ xorm.Cell) {
  397. var err error
  398. switch colName {
  399. case "delivered":
  400. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  401. case "request_content":
  402. if len(t.RequestContent) == 0 {
  403. return
  404. }
  405. t.RequestInfo = &HookRequest{}
  406. if err = jsoniter.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  407. log.Error("Unmarshal[%d]: %v", t.ID, err)
  408. }
  409. case "response_content":
  410. if len(t.ResponseContent) == 0 {
  411. return
  412. }
  413. t.ResponseInfo = &HookResponse{}
  414. if err = jsoniter.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  415. log.Error("Unmarshal [%d]: %v", t.ID, err)
  416. }
  417. }
  418. }
  419. func (t *HookTask) ToJSON(v interface{}) string {
  420. p, err := jsoniter.Marshal(v)
  421. if err != nil {
  422. log.Error("Marshal [%d]: %v", t.ID, err)
  423. }
  424. return string(p)
  425. }
  426. // HookTasks returns a list of hook tasks by given conditions.
  427. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  428. tasks := make([]*HookTask, 0, conf.Webhook.PagingNum)
  429. return tasks, x.Limit(conf.Webhook.PagingNum, (page-1)*conf.Webhook.PagingNum).Where("hook_id=?", hookID).Desc("id").Find(&tasks)
  430. }
  431. // createHookTask creates a new hook task,
  432. // it handles conversion from Payload to PayloadContent.
  433. func createHookTask(e Engine, t *HookTask) error {
  434. data, err := t.Payloader.JSONPayload()
  435. if err != nil {
  436. return err
  437. }
  438. t.UUID = gouuid.NewV4().String()
  439. t.PayloadContent = string(data)
  440. _, err = e.Insert(t)
  441. return err
  442. }
  443. var _ errutil.NotFound = (*ErrHookTaskNotExist)(nil)
  444. type ErrHookTaskNotExist struct {
  445. args map[string]interface{}
  446. }
  447. func IsHookTaskNotExist(err error) bool {
  448. _, ok := err.(ErrHookTaskNotExist)
  449. return ok
  450. }
  451. func (err ErrHookTaskNotExist) Error() string {
  452. return fmt.Sprintf("hook task does not exist: %v", err.args)
  453. }
  454. func (ErrHookTaskNotExist) NotFound() bool {
  455. return true
  456. }
  457. // GetHookTaskOfWebhookByUUID returns hook task of given webhook by UUID.
  458. func GetHookTaskOfWebhookByUUID(webhookID int64, uuid string) (*HookTask, error) {
  459. hookTask := &HookTask{
  460. HookID: webhookID,
  461. UUID: uuid,
  462. }
  463. has, err := x.Get(hookTask)
  464. if err != nil {
  465. return nil, err
  466. } else if !has {
  467. return nil, ErrHookTaskNotExist{args: map[string]interface{}{"webhookID": webhookID, "uuid": uuid}}
  468. }
  469. return hookTask, nil
  470. }
  471. // UpdateHookTask updates information of hook task.
  472. func UpdateHookTask(t *HookTask) error {
  473. _, err := x.Id(t.ID).AllCols().Update(t)
  474. return err
  475. }
  476. // prepareHookTasks adds list of webhooks to task queue.
  477. func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Payloader, webhooks []*Webhook) (err error) {
  478. if len(webhooks) == 0 {
  479. return nil
  480. }
  481. var payloader api.Payloader
  482. for _, w := range webhooks {
  483. switch event {
  484. case HOOK_EVENT_CREATE:
  485. if !w.HasCreateEvent() {
  486. continue
  487. }
  488. case HOOK_EVENT_DELETE:
  489. if !w.HasDeleteEvent() {
  490. continue
  491. }
  492. case HOOK_EVENT_FORK:
  493. if !w.HasForkEvent() {
  494. continue
  495. }
  496. case HOOK_EVENT_PUSH:
  497. if !w.HasPushEvent() {
  498. continue
  499. }
  500. case HOOK_EVENT_ISSUES:
  501. if !w.HasIssuesEvent() {
  502. continue
  503. }
  504. case HOOK_EVENT_PULL_REQUEST:
  505. if !w.HasPullRequestEvent() {
  506. continue
  507. }
  508. case HOOK_EVENT_ISSUE_COMMENT:
  509. if !w.HasIssueCommentEvent() {
  510. continue
  511. }
  512. case HOOK_EVENT_RELEASE:
  513. if !w.HasReleaseEvent() {
  514. continue
  515. }
  516. }
  517. // Use separate objects so modifications won't be made on payload on non-Gogs type hooks.
  518. switch w.HookTaskType {
  519. case SLACK:
  520. payloader, err = GetSlackPayload(p, event, w.Meta)
  521. if err != nil {
  522. return fmt.Errorf("GetSlackPayload: %v", err)
  523. }
  524. case DISCORD:
  525. payloader, err = GetDiscordPayload(p, event, w.Meta)
  526. if err != nil {
  527. return fmt.Errorf("GetDiscordPayload: %v", err)
  528. }
  529. case DINGTALK:
  530. payloader, err = GetDingtalkPayload(p, event)
  531. if err != nil {
  532. return fmt.Errorf("GetDingtalkPayload: %v", err)
  533. }
  534. default:
  535. payloader = p
  536. }
  537. var signature string
  538. if len(w.Secret) > 0 {
  539. data, err := payloader.JSONPayload()
  540. if err != nil {
  541. log.Error("prepareWebhooks.JSONPayload: %v", err)
  542. }
  543. sig := hmac.New(sha256.New, []byte(w.Secret))
  544. _, _ = sig.Write(data)
  545. signature = hex.EncodeToString(sig.Sum(nil))
  546. }
  547. if err = createHookTask(e, &HookTask{
  548. RepoID: repo.ID,
  549. HookID: w.ID,
  550. Type: w.HookTaskType,
  551. URL: w.URL,
  552. Signature: signature,
  553. Payloader: payloader,
  554. ContentType: w.ContentType,
  555. EventType: event,
  556. IsSSL: w.IsSSL,
  557. }); err != nil {
  558. return fmt.Errorf("createHookTask: %v", err)
  559. }
  560. }
  561. // It's safe to fail when the whole function is called during hook execution
  562. // because resource released after exit. Also, there is no process started to
  563. // consume this input during hook execution.
  564. go HookQueue.Add(repo.ID)
  565. return nil
  566. }
  567. func prepareWebhooks(e Engine, repo *Repository, event HookEventType, p api.Payloader) error {
  568. webhooks, err := getActiveWebhooksByRepoID(e, repo.ID)
  569. if err != nil {
  570. return fmt.Errorf("getActiveWebhooksByRepoID [%d]: %v", repo.ID, err)
  571. }
  572. // check if repo belongs to org and append additional webhooks
  573. if repo.mustOwner(e).IsOrganization() {
  574. // get hooks for org
  575. orgws, err := getActiveWebhooksByOrgID(e, repo.OwnerID)
  576. if err != nil {
  577. return fmt.Errorf("getActiveWebhooksByOrgID [%d]: %v", repo.OwnerID, err)
  578. }
  579. webhooks = append(webhooks, orgws...)
  580. }
  581. return prepareHookTasks(e, repo, event, p, webhooks)
  582. }
  583. // PrepareWebhooks adds all active webhooks to task queue.
  584. func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
  585. return prepareWebhooks(x, repo, event, p)
  586. }
  587. // TestWebhook adds the test webhook matches the ID to task queue.
  588. func TestWebhook(repo *Repository, event HookEventType, p api.Payloader, webhookID int64) error {
  589. webhook, err := GetWebhookOfRepoByID(repo.ID, webhookID)
  590. if err != nil {
  591. return fmt.Errorf("GetWebhookOfRepoByID [repo_id: %d, id: %d]: %v", repo.ID, webhookID, err)
  592. }
  593. return prepareHookTasks(x, repo, event, p, []*Webhook{webhook})
  594. }
  595. func (t *HookTask) deliver() {
  596. t.IsDelivered = true
  597. timeout := time.Duration(conf.Webhook.DeliverTimeout) * time.Second
  598. req := httplib.Post(t.URL).SetTimeout(timeout, timeout).
  599. Header("X-Github-Delivery", t.UUID).
  600. Header("X-Github-Event", string(t.EventType)).
  601. Header("X-Gogs-Delivery", t.UUID).
  602. Header("X-Gogs-Signature", t.Signature).
  603. Header("X-Gogs-Event", string(t.EventType)).
  604. SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Webhook.SkipTLSVerify})
  605. switch t.ContentType {
  606. case JSON:
  607. req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
  608. case FORM:
  609. req.Param("payload", t.PayloadContent)
  610. }
  611. // Record delivery information.
  612. t.RequestInfo = &HookRequest{
  613. Headers: map[string]string{},
  614. }
  615. for k, vals := range req.Headers() {
  616. t.RequestInfo.Headers[k] = strings.Join(vals, ",")
  617. }
  618. t.ResponseInfo = &HookResponse{
  619. Headers: map[string]string{},
  620. }
  621. defer func() {
  622. t.Delivered = time.Now().UnixNano()
  623. if t.IsSucceed {
  624. log.Trace("Hook delivered: %s", t.UUID)
  625. } else {
  626. log.Trace("Hook delivery failed: %s", t.UUID)
  627. }
  628. // Update webhook last delivery status.
  629. w, err := GetWebhookByID(t.HookID)
  630. if err != nil {
  631. log.Error("GetWebhookByID: %v", err)
  632. return
  633. }
  634. if t.IsSucceed {
  635. w.LastStatus = HOOK_STATUS_SUCCEED
  636. } else {
  637. w.LastStatus = HOOK_STATUS_FAILED
  638. }
  639. if err = UpdateWebhook(w); err != nil {
  640. log.Error("UpdateWebhook: %v", err)
  641. return
  642. }
  643. }()
  644. resp, err := req.Response()
  645. if err != nil {
  646. t.ResponseInfo.Body = fmt.Sprintf("Delivery: %v", err)
  647. return
  648. }
  649. defer resp.Body.Close()
  650. // Status code is 20x can be seen as succeed.
  651. t.IsSucceed = resp.StatusCode/100 == 2
  652. t.ResponseInfo.Status = resp.StatusCode
  653. for k, vals := range resp.Header {
  654. t.ResponseInfo.Headers[k] = strings.Join(vals, ",")
  655. }
  656. p, err := ioutil.ReadAll(resp.Body)
  657. if err != nil {
  658. t.ResponseInfo.Body = fmt.Sprintf("read body: %s", err)
  659. return
  660. }
  661. t.ResponseInfo.Body = string(p)
  662. }
  663. // DeliverHooks checks and delivers undelivered hooks.
  664. // TODO: shoot more hooks at same time.
  665. func DeliverHooks() {
  666. tasks := make([]*HookTask, 0, 10)
  667. _ = x.Where("is_delivered = ?", false).Iterate(new(HookTask),
  668. func(idx int, bean interface{}) error {
  669. t := bean.(*HookTask)
  670. t.deliver()
  671. tasks = append(tasks, t)
  672. return nil
  673. })
  674. // Update hook task status.
  675. for _, t := range tasks {
  676. if err := UpdateHookTask(t); err != nil {
  677. log.Error("UpdateHookTask [%d]: %v", t.ID, err)
  678. }
  679. }
  680. // Start listening on new hook requests.
  681. for repoID := range HookQueue.Queue() {
  682. log.Trace("DeliverHooks [repo_id: %v]", repoID)
  683. HookQueue.Remove(repoID)
  684. tasks = make([]*HookTask, 0, 5)
  685. if err := x.Where("repo_id = ?", repoID).And("is_delivered = ?", false).Find(&tasks); err != nil {
  686. log.Error("Get repository [%s] hook tasks: %v", repoID, err)
  687. continue
  688. }
  689. for _, t := range tasks {
  690. t.deliver()
  691. if err := UpdateHookTask(t); err != nil {
  692. log.Error("UpdateHookTask [%d]: %v", t.ID, err)
  693. continue
  694. }
  695. }
  696. }
  697. }
  698. func InitDeliverHooks() {
  699. go DeliverHooks()
  700. }