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

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