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.

webhook.go 23KB

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