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 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. "context"
  8. "fmt"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. gouuid "github.com/google/uuid"
  16. jsoniter "github.com/json-iterator/go"
  17. )
  18. // HookContentType is the content type of a web hook
  19. type HookContentType int
  20. const (
  21. // ContentTypeJSON is a JSON payload for web hooks
  22. ContentTypeJSON HookContentType = iota + 1
  23. // ContentTypeForm is an url-encoded form payload for web hook
  24. ContentTypeForm
  25. )
  26. var hookContentTypes = map[string]HookContentType{
  27. "json": ContentTypeJSON,
  28. "form": ContentTypeForm,
  29. }
  30. // ToHookContentType returns HookContentType by given name.
  31. func ToHookContentType(name string) HookContentType {
  32. return hookContentTypes[name]
  33. }
  34. // HookTaskCleanupType is the type of cleanup to perform on hook_task
  35. type HookTaskCleanupType int
  36. const (
  37. // OlderThan hook_task rows will be cleaned up by the age of the row
  38. OlderThan HookTaskCleanupType = iota
  39. // PerWebhook hook_task rows will be cleaned up by leaving the most recent deliveries for each webhook
  40. PerWebhook
  41. )
  42. var hookTaskCleanupTypes = map[string]HookTaskCleanupType{
  43. "OlderThan": OlderThan,
  44. "PerWebhook": PerWebhook,
  45. }
  46. // ToHookTaskCleanupType returns HookTaskCleanupType by given name.
  47. func ToHookTaskCleanupType(name string) HookTaskCleanupType {
  48. return hookTaskCleanupTypes[name]
  49. }
  50. // Name returns the name of a given web hook's content type
  51. func (t HookContentType) Name() string {
  52. switch t {
  53. case ContentTypeJSON:
  54. return "json"
  55. case ContentTypeForm:
  56. return "form"
  57. }
  58. return ""
  59. }
  60. // IsValidHookContentType returns true if given name is a valid hook content type.
  61. func IsValidHookContentType(name string) bool {
  62. _, ok := hookContentTypes[name]
  63. return ok
  64. }
  65. // HookEvents is a set of web hook events
  66. type HookEvents struct {
  67. Create bool `json:"create"`
  68. Delete bool `json:"delete"`
  69. Fork bool `json:"fork"`
  70. Issues bool `json:"issues"`
  71. IssueAssign bool `json:"issue_assign"`
  72. IssueLabel bool `json:"issue_label"`
  73. IssueMilestone bool `json:"issue_milestone"`
  74. IssueComment bool `json:"issue_comment"`
  75. Push bool `json:"push"`
  76. PullRequest bool `json:"pull_request"`
  77. PullRequestAssign bool `json:"pull_request_assign"`
  78. PullRequestLabel bool `json:"pull_request_label"`
  79. PullRequestMilestone bool `json:"pull_request_milestone"`
  80. PullRequestComment bool `json:"pull_request_comment"`
  81. PullRequestReview bool `json:"pull_request_review"`
  82. PullRequestSync bool `json:"pull_request_sync"`
  83. Repository bool `json:"repository"`
  84. Release bool `json:"release"`
  85. }
  86. // HookEvent represents events that will delivery hook.
  87. type HookEvent struct {
  88. PushOnly bool `json:"push_only"`
  89. SendEverything bool `json:"send_everything"`
  90. ChooseEvents bool `json:"choose_events"`
  91. BranchFilter string `json:"branch_filter"`
  92. HookEvents `json:"events"`
  93. }
  94. // HookStatus is the status of a web hook
  95. type HookStatus int
  96. // Possible statuses of a web hook
  97. const (
  98. HookStatusNone = iota
  99. HookStatusSucceed
  100. HookStatusFail
  101. )
  102. // Webhook represents a web hook object.
  103. type Webhook struct {
  104. ID int64 `xorm:"pk autoincr"`
  105. RepoID int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook
  106. OrgID int64 `xorm:"INDEX"`
  107. IsSystemWebhook bool
  108. URL string `xorm:"url TEXT"`
  109. Signature string `xorm:"TEXT"`
  110. HTTPMethod string `xorm:"http_method"`
  111. ContentType HookContentType
  112. Secret string `xorm:"TEXT"`
  113. Events string `xorm:"TEXT"`
  114. *HookEvent `xorm:"-"`
  115. IsSSL bool `xorm:"is_ssl"`
  116. IsActive bool `xorm:"INDEX"`
  117. Type HookTaskType `xorm:"VARCHAR(16) 'type'"`
  118. Meta string `xorm:"TEXT"` // store hook-specific attributes
  119. LastStatus HookStatus // Last delivery status
  120. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  121. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  122. }
  123. // AfterLoad updates the webhook object upon setting a column
  124. func (w *Webhook) AfterLoad() {
  125. w.HookEvent = &HookEvent{}
  126. json := jsoniter.ConfigCompatibleWithStandardLibrary
  127. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  128. log.Error("Unmarshal[%d]: %v", w.ID, err)
  129. }
  130. }
  131. // History returns history of webhook by given conditions.
  132. func (w *Webhook) History(page int) ([]*HookTask, error) {
  133. return HookTasks(w.ID, page)
  134. }
  135. // UpdateEvent handles conversion from HookEvent to Events.
  136. func (w *Webhook) UpdateEvent() error {
  137. json := jsoniter.ConfigCompatibleWithStandardLibrary
  138. data, err := json.Marshal(w.HookEvent)
  139. w.Events = string(data)
  140. return err
  141. }
  142. // HasCreateEvent returns true if hook enabled create event.
  143. func (w *Webhook) HasCreateEvent() bool {
  144. return w.SendEverything ||
  145. (w.ChooseEvents && w.HookEvents.Create)
  146. }
  147. // HasDeleteEvent returns true if hook enabled delete event.
  148. func (w *Webhook) HasDeleteEvent() bool {
  149. return w.SendEverything ||
  150. (w.ChooseEvents && w.HookEvents.Delete)
  151. }
  152. // HasForkEvent returns true if hook enabled fork event.
  153. func (w *Webhook) HasForkEvent() bool {
  154. return w.SendEverything ||
  155. (w.ChooseEvents && w.HookEvents.Fork)
  156. }
  157. // HasIssuesEvent returns true if hook enabled issues event.
  158. func (w *Webhook) HasIssuesEvent() bool {
  159. return w.SendEverything ||
  160. (w.ChooseEvents && w.HookEvents.Issues)
  161. }
  162. // HasIssuesAssignEvent returns true if hook enabled issues assign event.
  163. func (w *Webhook) HasIssuesAssignEvent() bool {
  164. return w.SendEverything ||
  165. (w.ChooseEvents && w.HookEvents.IssueAssign)
  166. }
  167. // HasIssuesLabelEvent returns true if hook enabled issues label event.
  168. func (w *Webhook) HasIssuesLabelEvent() bool {
  169. return w.SendEverything ||
  170. (w.ChooseEvents && w.HookEvents.IssueLabel)
  171. }
  172. // HasIssuesMilestoneEvent returns true if hook enabled issues milestone event.
  173. func (w *Webhook) HasIssuesMilestoneEvent() bool {
  174. return w.SendEverything ||
  175. (w.ChooseEvents && w.HookEvents.IssueMilestone)
  176. }
  177. // HasIssueCommentEvent returns true if hook enabled issue_comment event.
  178. func (w *Webhook) HasIssueCommentEvent() bool {
  179. return w.SendEverything ||
  180. (w.ChooseEvents && w.HookEvents.IssueComment)
  181. }
  182. // HasPushEvent returns true if hook enabled push event.
  183. func (w *Webhook) HasPushEvent() bool {
  184. return w.PushOnly || w.SendEverything ||
  185. (w.ChooseEvents && w.HookEvents.Push)
  186. }
  187. // HasPullRequestEvent returns true if hook enabled pull request event.
  188. func (w *Webhook) HasPullRequestEvent() bool {
  189. return w.SendEverything ||
  190. (w.ChooseEvents && w.HookEvents.PullRequest)
  191. }
  192. // HasPullRequestAssignEvent returns true if hook enabled pull request assign event.
  193. func (w *Webhook) HasPullRequestAssignEvent() bool {
  194. return w.SendEverything ||
  195. (w.ChooseEvents && w.HookEvents.PullRequestAssign)
  196. }
  197. // HasPullRequestLabelEvent returns true if hook enabled pull request label event.
  198. func (w *Webhook) HasPullRequestLabelEvent() bool {
  199. return w.SendEverything ||
  200. (w.ChooseEvents && w.HookEvents.PullRequestLabel)
  201. }
  202. // HasPullRequestMilestoneEvent returns true if hook enabled pull request milestone event.
  203. func (w *Webhook) HasPullRequestMilestoneEvent() bool {
  204. return w.SendEverything ||
  205. (w.ChooseEvents && w.HookEvents.PullRequestMilestone)
  206. }
  207. // HasPullRequestCommentEvent returns true if hook enabled pull_request_comment event.
  208. func (w *Webhook) HasPullRequestCommentEvent() bool {
  209. return w.SendEverything ||
  210. (w.ChooseEvents && w.HookEvents.PullRequestComment)
  211. }
  212. // HasPullRequestApprovedEvent returns true if hook enabled pull request review event.
  213. func (w *Webhook) HasPullRequestApprovedEvent() bool {
  214. return w.SendEverything ||
  215. (w.ChooseEvents && w.HookEvents.PullRequestReview)
  216. }
  217. // HasPullRequestRejectedEvent returns true if hook enabled pull request review event.
  218. func (w *Webhook) HasPullRequestRejectedEvent() bool {
  219. return w.SendEverything ||
  220. (w.ChooseEvents && w.HookEvents.PullRequestReview)
  221. }
  222. // HasPullRequestReviewCommentEvent returns true if hook enabled pull request review event.
  223. func (w *Webhook) HasPullRequestReviewCommentEvent() bool {
  224. return w.SendEverything ||
  225. (w.ChooseEvents && w.HookEvents.PullRequestReview)
  226. }
  227. // HasPullRequestSyncEvent returns true if hook enabled pull request sync event.
  228. func (w *Webhook) HasPullRequestSyncEvent() bool {
  229. return w.SendEverything ||
  230. (w.ChooseEvents && w.HookEvents.PullRequestSync)
  231. }
  232. // HasReleaseEvent returns if hook enabled release event.
  233. func (w *Webhook) HasReleaseEvent() bool {
  234. return w.SendEverything ||
  235. (w.ChooseEvents && w.HookEvents.Release)
  236. }
  237. // HasRepositoryEvent returns if hook enabled repository event.
  238. func (w *Webhook) HasRepositoryEvent() bool {
  239. return w.SendEverything ||
  240. (w.ChooseEvents && w.HookEvents.Repository)
  241. }
  242. // EventCheckers returns event checkers
  243. func (w *Webhook) EventCheckers() []struct {
  244. Has func() bool
  245. Type HookEventType
  246. } {
  247. return []struct {
  248. Has func() bool
  249. Type HookEventType
  250. }{
  251. {w.HasCreateEvent, HookEventCreate},
  252. {w.HasDeleteEvent, HookEventDelete},
  253. {w.HasForkEvent, HookEventFork},
  254. {w.HasPushEvent, HookEventPush},
  255. {w.HasIssuesEvent, HookEventIssues},
  256. {w.HasIssuesAssignEvent, HookEventIssueAssign},
  257. {w.HasIssuesLabelEvent, HookEventIssueLabel},
  258. {w.HasIssuesMilestoneEvent, HookEventIssueMilestone},
  259. {w.HasIssueCommentEvent, HookEventIssueComment},
  260. {w.HasPullRequestEvent, HookEventPullRequest},
  261. {w.HasPullRequestAssignEvent, HookEventPullRequestAssign},
  262. {w.HasPullRequestLabelEvent, HookEventPullRequestLabel},
  263. {w.HasPullRequestMilestoneEvent, HookEventPullRequestMilestone},
  264. {w.HasPullRequestCommentEvent, HookEventPullRequestComment},
  265. {w.HasPullRequestApprovedEvent, HookEventPullRequestReviewApproved},
  266. {w.HasPullRequestRejectedEvent, HookEventPullRequestReviewRejected},
  267. {w.HasPullRequestCommentEvent, HookEventPullRequestReviewComment},
  268. {w.HasPullRequestSyncEvent, HookEventPullRequestSync},
  269. {w.HasRepositoryEvent, HookEventRepository},
  270. {w.HasReleaseEvent, HookEventRelease},
  271. }
  272. }
  273. // EventsArray returns an array of hook events
  274. func (w *Webhook) EventsArray() []string {
  275. events := make([]string, 0, 7)
  276. for _, c := range w.EventCheckers() {
  277. if c.Has() {
  278. events = append(events, string(c.Type))
  279. }
  280. }
  281. return events
  282. }
  283. // CreateWebhook creates a new web hook.
  284. func CreateWebhook(w *Webhook) error {
  285. return createWebhook(x, w)
  286. }
  287. func createWebhook(e Engine, w *Webhook) error {
  288. w.Type = strings.TrimSpace(w.Type)
  289. _, err := e.Insert(w)
  290. return err
  291. }
  292. // getWebhook uses argument bean as query condition,
  293. // ID must be specified and do not assign unnecessary fields.
  294. func getWebhook(bean *Webhook) (*Webhook, error) {
  295. has, err := x.Get(bean)
  296. if err != nil {
  297. return nil, err
  298. } else if !has {
  299. return nil, ErrWebhookNotExist{bean.ID}
  300. }
  301. return bean, nil
  302. }
  303. // GetWebhookByID returns webhook of repository by given ID.
  304. func GetWebhookByID(id int64) (*Webhook, error) {
  305. return getWebhook(&Webhook{
  306. ID: id,
  307. })
  308. }
  309. // GetWebhookByRepoID returns webhook of repository by given ID.
  310. func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) {
  311. return getWebhook(&Webhook{
  312. ID: id,
  313. RepoID: repoID,
  314. })
  315. }
  316. // GetWebhookByOrgID returns webhook of organization by given ID.
  317. func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
  318. return getWebhook(&Webhook{
  319. ID: id,
  320. OrgID: orgID,
  321. })
  322. }
  323. // GetActiveWebhooksByRepoID returns all active webhooks of repository.
  324. func GetActiveWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  325. return getActiveWebhooksByRepoID(x, repoID)
  326. }
  327. func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
  328. webhooks := make([]*Webhook, 0, 5)
  329. return webhooks, e.Where("is_active=?", true).
  330. Find(&webhooks, &Webhook{RepoID: repoID})
  331. }
  332. // GetWebhooksByRepoID returns all webhooks of a repository.
  333. func GetWebhooksByRepoID(repoID int64, listOptions ListOptions) ([]*Webhook, error) {
  334. if listOptions.Page == 0 {
  335. webhooks := make([]*Webhook, 0, 5)
  336. return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
  337. }
  338. sess := listOptions.getPaginatedSession()
  339. webhooks := make([]*Webhook, 0, listOptions.PageSize)
  340. return webhooks, sess.Find(&webhooks, &Webhook{RepoID: repoID})
  341. }
  342. // GetActiveWebhooksByOrgID returns all active webhooks for an organization.
  343. func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  344. return getActiveWebhooksByOrgID(x, orgID)
  345. }
  346. func getActiveWebhooksByOrgID(e Engine, orgID int64) (ws []*Webhook, err error) {
  347. err = e.
  348. Where("org_id=?", orgID).
  349. And("is_active=?", true).
  350. Find(&ws)
  351. return ws, err
  352. }
  353. // GetWebhooksByOrgID returns paginated webhooks for an organization.
  354. func GetWebhooksByOrgID(orgID int64, listOptions ListOptions) ([]*Webhook, error) {
  355. if listOptions.Page == 0 {
  356. ws := make([]*Webhook, 0, 5)
  357. return ws, x.Find(&ws, &Webhook{OrgID: orgID})
  358. }
  359. sess := listOptions.getPaginatedSession()
  360. ws := make([]*Webhook, 0, listOptions.PageSize)
  361. return ws, sess.Find(&ws, &Webhook{OrgID: orgID})
  362. }
  363. // GetDefaultWebhooks returns all admin-default webhooks.
  364. func GetDefaultWebhooks() ([]*Webhook, error) {
  365. return getDefaultWebhooks(x)
  366. }
  367. func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
  368. webhooks := make([]*Webhook, 0, 5)
  369. return webhooks, e.
  370. Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
  371. Find(&webhooks)
  372. }
  373. // GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
  374. func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
  375. webhook := &Webhook{ID: id}
  376. has, err := x.
  377. Where("repo_id=? AND org_id=?", 0, 0).
  378. Get(webhook)
  379. if err != nil {
  380. return nil, err
  381. } else if !has {
  382. return nil, ErrWebhookNotExist{id}
  383. }
  384. return webhook, nil
  385. }
  386. // GetSystemWebhooks returns all admin system webhooks.
  387. func GetSystemWebhooks() ([]*Webhook, error) {
  388. return getSystemWebhooks(x)
  389. }
  390. func getSystemWebhooks(e Engine) ([]*Webhook, error) {
  391. webhooks := make([]*Webhook, 0, 5)
  392. return webhooks, e.
  393. Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
  394. Find(&webhooks)
  395. }
  396. // UpdateWebhook updates information of webhook.
  397. func UpdateWebhook(w *Webhook) error {
  398. _, err := x.ID(w.ID).AllCols().Update(w)
  399. return err
  400. }
  401. // UpdateWebhookLastStatus updates last status of webhook.
  402. func UpdateWebhookLastStatus(w *Webhook) error {
  403. _, err := x.ID(w.ID).Cols("last_status").Update(w)
  404. return err
  405. }
  406. // deleteWebhook uses argument bean as query condition,
  407. // ID must be specified and do not assign unnecessary fields.
  408. func deleteWebhook(bean *Webhook) (err error) {
  409. sess := x.NewSession()
  410. defer sess.Close()
  411. if err = sess.Begin(); err != nil {
  412. return err
  413. }
  414. if count, err := sess.Delete(bean); err != nil {
  415. return err
  416. } else if count == 0 {
  417. return ErrWebhookNotExist{ID: bean.ID}
  418. } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
  419. return err
  420. }
  421. return sess.Commit()
  422. }
  423. // DeleteWebhookByRepoID deletes webhook of repository by given ID.
  424. func DeleteWebhookByRepoID(repoID, id int64) error {
  425. return deleteWebhook(&Webhook{
  426. ID: id,
  427. RepoID: repoID,
  428. })
  429. }
  430. // DeleteWebhookByOrgID deletes webhook of organization by given ID.
  431. func DeleteWebhookByOrgID(orgID, id int64) error {
  432. return deleteWebhook(&Webhook{
  433. ID: id,
  434. OrgID: orgID,
  435. })
  436. }
  437. // DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
  438. func DeleteDefaultSystemWebhook(id int64) error {
  439. sess := x.NewSession()
  440. defer sess.Close()
  441. if err := sess.Begin(); err != nil {
  442. return err
  443. }
  444. count, err := sess.
  445. Where("repo_id=? AND org_id=?", 0, 0).
  446. Delete(&Webhook{ID: id})
  447. if err != nil {
  448. return err
  449. } else if count == 0 {
  450. return ErrWebhookNotExist{ID: id}
  451. }
  452. if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
  453. return err
  454. }
  455. return sess.Commit()
  456. }
  457. // copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
  458. func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
  459. ws, err := getDefaultWebhooks(e)
  460. if err != nil {
  461. return fmt.Errorf("GetDefaultWebhooks: %v", err)
  462. }
  463. for _, w := range ws {
  464. w.ID = 0
  465. w.RepoID = repoID
  466. if err := createWebhook(e, w); err != nil {
  467. return fmt.Errorf("CreateWebhook: %v", err)
  468. }
  469. }
  470. return nil
  471. }
  472. // ___ ___ __ ___________ __
  473. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  474. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  475. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  476. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  477. // \/ \/ \/ \/ \/
  478. // HookTaskType is the type of an hook task
  479. type HookTaskType = string
  480. // Types of hook tasks
  481. const (
  482. GITEA HookTaskType = "gitea"
  483. GOGS HookTaskType = "gogs"
  484. SLACK HookTaskType = "slack"
  485. DISCORD HookTaskType = "discord"
  486. DINGTALK HookTaskType = "dingtalk"
  487. TELEGRAM HookTaskType = "telegram"
  488. MSTEAMS HookTaskType = "msteams"
  489. FEISHU HookTaskType = "feishu"
  490. MATRIX HookTaskType = "matrix"
  491. )
  492. // HookEventType is the type of an hook event
  493. type HookEventType string
  494. // Types of hook events
  495. const (
  496. HookEventCreate HookEventType = "create"
  497. HookEventDelete HookEventType = "delete"
  498. HookEventFork HookEventType = "fork"
  499. HookEventPush HookEventType = "push"
  500. HookEventIssues HookEventType = "issues"
  501. HookEventIssueAssign HookEventType = "issue_assign"
  502. HookEventIssueLabel HookEventType = "issue_label"
  503. HookEventIssueMilestone HookEventType = "issue_milestone"
  504. HookEventIssueComment HookEventType = "issue_comment"
  505. HookEventPullRequest HookEventType = "pull_request"
  506. HookEventPullRequestAssign HookEventType = "pull_request_assign"
  507. HookEventPullRequestLabel HookEventType = "pull_request_label"
  508. HookEventPullRequestMilestone HookEventType = "pull_request_milestone"
  509. HookEventPullRequestComment HookEventType = "pull_request_comment"
  510. HookEventPullRequestReviewApproved HookEventType = "pull_request_review_approved"
  511. HookEventPullRequestReviewRejected HookEventType = "pull_request_review_rejected"
  512. HookEventPullRequestReviewComment HookEventType = "pull_request_review_comment"
  513. HookEventPullRequestSync HookEventType = "pull_request_sync"
  514. HookEventRepository HookEventType = "repository"
  515. HookEventRelease HookEventType = "release"
  516. )
  517. // Event returns the HookEventType as an event string
  518. func (h HookEventType) Event() string {
  519. switch h {
  520. case HookEventCreate:
  521. return "create"
  522. case HookEventDelete:
  523. return "delete"
  524. case HookEventFork:
  525. return "fork"
  526. case HookEventPush:
  527. return "push"
  528. case HookEventIssues, HookEventIssueAssign, HookEventIssueLabel, HookEventIssueMilestone:
  529. return "issues"
  530. case HookEventPullRequest, HookEventPullRequestAssign, HookEventPullRequestLabel, HookEventPullRequestMilestone,
  531. HookEventPullRequestSync:
  532. return "pull_request"
  533. case HookEventIssueComment, HookEventPullRequestComment:
  534. return "issue_comment"
  535. case HookEventPullRequestReviewApproved:
  536. return "pull_request_approved"
  537. case HookEventPullRequestReviewRejected:
  538. return "pull_request_rejected"
  539. case HookEventPullRequestReviewComment:
  540. return "pull_request_comment"
  541. case HookEventRepository:
  542. return "repository"
  543. case HookEventRelease:
  544. return "release"
  545. }
  546. return ""
  547. }
  548. // HookRequest represents hook task request information.
  549. type HookRequest struct {
  550. Headers map[string]string `json:"headers"`
  551. }
  552. // HookResponse represents hook task response information.
  553. type HookResponse struct {
  554. Status int `json:"status"`
  555. Headers map[string]string `json:"headers"`
  556. Body string `json:"body"`
  557. }
  558. // HookTask represents a hook task.
  559. type HookTask struct {
  560. ID int64 `xorm:"pk autoincr"`
  561. RepoID int64 `xorm:"INDEX"`
  562. HookID int64
  563. UUID string
  564. Typ HookTaskType `xorm:"VARCHAR(16) index"`
  565. URL string `xorm:"TEXT"`
  566. Signature string `xorm:"TEXT"`
  567. api.Payloader `xorm:"-"`
  568. PayloadContent string `xorm:"TEXT"`
  569. HTTPMethod string `xorm:"http_method"`
  570. ContentType HookContentType
  571. EventType HookEventType
  572. IsSSL bool
  573. IsDelivered bool
  574. Delivered int64
  575. DeliveredString string `xorm:"-"`
  576. // History info.
  577. IsSucceed bool
  578. RequestContent string `xorm:"TEXT"`
  579. RequestInfo *HookRequest `xorm:"-"`
  580. ResponseContent string `xorm:"TEXT"`
  581. ResponseInfo *HookResponse `xorm:"-"`
  582. }
  583. // BeforeUpdate will be invoked by XORM before updating a record
  584. // representing this object
  585. func (t *HookTask) BeforeUpdate() {
  586. if t.RequestInfo != nil {
  587. t.RequestContent = t.simpleMarshalJSON(t.RequestInfo)
  588. }
  589. if t.ResponseInfo != nil {
  590. t.ResponseContent = t.simpleMarshalJSON(t.ResponseInfo)
  591. }
  592. }
  593. // AfterLoad updates the webhook object upon setting a column
  594. func (t *HookTask) AfterLoad() {
  595. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  596. if len(t.RequestContent) == 0 {
  597. return
  598. }
  599. t.RequestInfo = &HookRequest{}
  600. json := jsoniter.ConfigCompatibleWithStandardLibrary
  601. if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  602. log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err)
  603. }
  604. if len(t.ResponseContent) > 0 {
  605. t.ResponseInfo = &HookResponse{}
  606. if err := json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  607. log.Error("Unmarshal ResponseContent[%d]: %v", t.ID, err)
  608. }
  609. }
  610. }
  611. func (t *HookTask) simpleMarshalJSON(v interface{}) string {
  612. json := jsoniter.ConfigCompatibleWithStandardLibrary
  613. p, err := json.Marshal(v)
  614. if err != nil {
  615. log.Error("Marshal [%d]: %v", t.ID, err)
  616. }
  617. return string(p)
  618. }
  619. // HookTasks returns a list of hook tasks by given conditions.
  620. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  621. tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
  622. return tasks, x.
  623. Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
  624. Where("hook_id=?", hookID).
  625. Desc("id").
  626. Find(&tasks)
  627. }
  628. // CreateHookTask creates a new hook task,
  629. // it handles conversion from Payload to PayloadContent.
  630. func CreateHookTask(t *HookTask) error {
  631. return createHookTask(x, t)
  632. }
  633. func createHookTask(e Engine, t *HookTask) error {
  634. data, err := t.Payloader.JSONPayload()
  635. if err != nil {
  636. return err
  637. }
  638. t.UUID = gouuid.New().String()
  639. t.PayloadContent = string(data)
  640. _, err = e.Insert(t)
  641. return err
  642. }
  643. // UpdateHookTask updates information of hook task.
  644. func UpdateHookTask(t *HookTask) error {
  645. _, err := x.ID(t.ID).AllCols().Update(t)
  646. return err
  647. }
  648. // FindUndeliveredHookTasks represents find the undelivered hook tasks
  649. func FindUndeliveredHookTasks() ([]*HookTask, error) {
  650. tasks := make([]*HookTask, 0, 10)
  651. if err := x.Where("is_delivered=?", false).Find(&tasks); err != nil {
  652. return nil, err
  653. }
  654. return tasks, nil
  655. }
  656. // FindRepoUndeliveredHookTasks represents find the undelivered hook tasks of one repository
  657. func FindRepoUndeliveredHookTasks(repoID int64) ([]*HookTask, error) {
  658. tasks := make([]*HookTask, 0, 5)
  659. if err := x.Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
  660. return nil, err
  661. }
  662. return tasks, nil
  663. }
  664. // CleanupHookTaskTable deletes rows from hook_task as needed.
  665. func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType, olderThan time.Duration, numberToKeep int) error {
  666. log.Trace("Doing: CleanupHookTaskTable")
  667. if cleanupType == OlderThan {
  668. deleteOlderThan := time.Now().Add(-olderThan).UnixNano()
  669. deletes, err := x.
  670. Where("is_delivered = ? and delivered < ?", true, deleteOlderThan).
  671. Delete(new(HookTask))
  672. if err != nil {
  673. return err
  674. }
  675. log.Trace("Deleted %d rows from hook_task", deletes)
  676. } else if cleanupType == PerWebhook {
  677. hookIDs := make([]int64, 0, 10)
  678. err := x.Table("webhook").
  679. Where("id > 0").
  680. Cols("id").
  681. Find(&hookIDs)
  682. if err != nil {
  683. return err
  684. }
  685. for _, hookID := range hookIDs {
  686. select {
  687. case <-ctx.Done():
  688. return ErrCancelledf("Before deleting hook_task records for hook id %d", hookID)
  689. default:
  690. }
  691. if err = deleteDeliveredHookTasksByWebhook(hookID, numberToKeep); err != nil {
  692. return err
  693. }
  694. }
  695. }
  696. log.Trace("Finished: CleanupHookTaskTable")
  697. return nil
  698. }
  699. func deleteDeliveredHookTasksByWebhook(hookID int64, numberDeliveriesToKeep int) error {
  700. log.Trace("Deleting hook_task rows for webhook %d, keeping the most recent %d deliveries", hookID, numberDeliveriesToKeep)
  701. deliveryDates := make([]int64, 0, 10)
  702. err := x.Table("hook_task").
  703. Where("hook_task.hook_id = ? AND hook_task.is_delivered = ? AND hook_task.delivered is not null", hookID, true).
  704. Cols("hook_task.delivered").
  705. Join("INNER", "webhook", "hook_task.hook_id = webhook.id").
  706. OrderBy("hook_task.delivered desc").
  707. Limit(1, int(numberDeliveriesToKeep)).
  708. Find(&deliveryDates)
  709. if err != nil {
  710. return err
  711. }
  712. if len(deliveryDates) > 0 {
  713. deletes, err := x.
  714. Where("hook_id = ? and is_delivered = ? and delivered <= ?", hookID, true, deliveryDates[0]).
  715. Delete(new(HookTask))
  716. if err != nil {
  717. return err
  718. }
  719. log.Trace("Deleted %d hook_task rows for webhook %d", deletes, hookID)
  720. } else {
  721. log.Trace("No hook_task rows to delete for webhook %d", hookID)
  722. }
  723. return nil
  724. }