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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. IssueComment bool `json:"issue_comment"`
  54. Push bool `json:"push"`
  55. PullRequest bool `json:"pull_request"`
  56. Repository bool `json:"repository"`
  57. Release bool `json:"release"`
  58. }
  59. // HookEvent represents events that will delivery hook.
  60. type HookEvent struct {
  61. PushOnly bool `json:"push_only"`
  62. SendEverything bool `json:"send_everything"`
  63. ChooseEvents bool `json:"choose_events"`
  64. BranchFilter string `json:"branch_filter"`
  65. HookEvents `json:"events"`
  66. }
  67. // HookStatus is the status of a web hook
  68. type HookStatus int
  69. // Possible statuses of a web hook
  70. const (
  71. HookStatusNone = iota
  72. HookStatusSucceed
  73. HookStatusFail
  74. )
  75. // Webhook represents a web hook object.
  76. type Webhook struct {
  77. ID int64 `xorm:"pk autoincr"`
  78. RepoID int64 `xorm:"INDEX"`
  79. OrgID int64 `xorm:"INDEX"`
  80. URL string `xorm:"url TEXT"`
  81. Signature string `xorm:"TEXT"`
  82. HTTPMethod string `xorm:"http_method"`
  83. ContentType HookContentType
  84. Secret string `xorm:"TEXT"`
  85. Events string `xorm:"TEXT"`
  86. *HookEvent `xorm:"-"`
  87. IsSSL bool `xorm:"is_ssl"`
  88. IsActive bool `xorm:"INDEX"`
  89. HookTaskType HookTaskType
  90. Meta string `xorm:"TEXT"` // store hook-specific attributes
  91. LastStatus HookStatus // Last delivery status
  92. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  93. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  94. }
  95. // AfterLoad updates the webhook object upon setting a column
  96. func (w *Webhook) AfterLoad() {
  97. w.HookEvent = &HookEvent{}
  98. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  99. log.Error("Unmarshal[%d]: %v", w.ID, err)
  100. }
  101. }
  102. // History returns history of webhook by given conditions.
  103. func (w *Webhook) History(page int) ([]*HookTask, error) {
  104. return HookTasks(w.ID, page)
  105. }
  106. // UpdateEvent handles conversion from HookEvent to Events.
  107. func (w *Webhook) UpdateEvent() error {
  108. data, err := json.Marshal(w.HookEvent)
  109. w.Events = string(data)
  110. return err
  111. }
  112. // HasCreateEvent returns true if hook enabled create event.
  113. func (w *Webhook) HasCreateEvent() bool {
  114. return w.SendEverything ||
  115. (w.ChooseEvents && w.HookEvents.Create)
  116. }
  117. // HasDeleteEvent returns true if hook enabled delete event.
  118. func (w *Webhook) HasDeleteEvent() bool {
  119. return w.SendEverything ||
  120. (w.ChooseEvents && w.HookEvents.Delete)
  121. }
  122. // HasForkEvent returns true if hook enabled fork event.
  123. func (w *Webhook) HasForkEvent() bool {
  124. return w.SendEverything ||
  125. (w.ChooseEvents && w.HookEvents.Fork)
  126. }
  127. // HasIssuesEvent returns true if hook enabled issues event.
  128. func (w *Webhook) HasIssuesEvent() bool {
  129. return w.SendEverything ||
  130. (w.ChooseEvents && w.HookEvents.Issues)
  131. }
  132. // HasIssueCommentEvent returns true if hook enabled issue_comment event.
  133. func (w *Webhook) HasIssueCommentEvent() bool {
  134. return w.SendEverything ||
  135. (w.ChooseEvents && w.HookEvents.IssueComment)
  136. }
  137. // HasPushEvent returns true if hook enabled push event.
  138. func (w *Webhook) HasPushEvent() bool {
  139. return w.PushOnly || w.SendEverything ||
  140. (w.ChooseEvents && w.HookEvents.Push)
  141. }
  142. // HasPullRequestEvent returns true if hook enabled pull request event.
  143. func (w *Webhook) HasPullRequestEvent() bool {
  144. return w.SendEverything ||
  145. (w.ChooseEvents && w.HookEvents.PullRequest)
  146. }
  147. // HasReleaseEvent returns if hook enabled release event.
  148. func (w *Webhook) HasReleaseEvent() bool {
  149. return w.SendEverything ||
  150. (w.ChooseEvents && w.HookEvents.Release)
  151. }
  152. // HasRepositoryEvent returns if hook enabled repository event.
  153. func (w *Webhook) HasRepositoryEvent() bool {
  154. return w.SendEverything ||
  155. (w.ChooseEvents && w.HookEvents.Repository)
  156. }
  157. // EventCheckers returns event checkers
  158. func (w *Webhook) EventCheckers() []struct {
  159. Has func() bool
  160. Type HookEventType
  161. } {
  162. return []struct {
  163. Has func() bool
  164. Type HookEventType
  165. }{
  166. {w.HasCreateEvent, HookEventCreate},
  167. {w.HasDeleteEvent, HookEventDelete},
  168. {w.HasForkEvent, HookEventFork},
  169. {w.HasPushEvent, HookEventPush},
  170. {w.HasIssuesEvent, HookEventIssues},
  171. {w.HasIssueCommentEvent, HookEventIssueComment},
  172. {w.HasPullRequestEvent, HookEventPullRequest},
  173. {w.HasRepositoryEvent, HookEventRepository},
  174. {w.HasReleaseEvent, HookEventRelease},
  175. }
  176. }
  177. // EventsArray returns an array of hook events
  178. func (w *Webhook) EventsArray() []string {
  179. events := make([]string, 0, 7)
  180. for _, c := range w.EventCheckers() {
  181. if c.Has() {
  182. events = append(events, string(c.Type))
  183. }
  184. }
  185. return events
  186. }
  187. // CreateWebhook creates a new web hook.
  188. func CreateWebhook(w *Webhook) error {
  189. return createWebhook(x, w)
  190. }
  191. func createWebhook(e Engine, w *Webhook) error {
  192. _, err := e.Insert(w)
  193. return err
  194. }
  195. // getWebhook uses argument bean as query condition,
  196. // ID must be specified and do not assign unnecessary fields.
  197. func getWebhook(bean *Webhook) (*Webhook, error) {
  198. has, err := x.Get(bean)
  199. if err != nil {
  200. return nil, err
  201. } else if !has {
  202. return nil, ErrWebhookNotExist{bean.ID}
  203. }
  204. return bean, nil
  205. }
  206. // GetWebhookByID returns webhook of repository by given ID.
  207. func GetWebhookByID(id int64) (*Webhook, error) {
  208. return getWebhook(&Webhook{
  209. ID: id,
  210. })
  211. }
  212. // GetWebhookByRepoID returns webhook of repository by given ID.
  213. func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) {
  214. return getWebhook(&Webhook{
  215. ID: id,
  216. RepoID: repoID,
  217. })
  218. }
  219. // GetWebhookByOrgID returns webhook of organization by given ID.
  220. func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
  221. return getWebhook(&Webhook{
  222. ID: id,
  223. OrgID: orgID,
  224. })
  225. }
  226. // GetActiveWebhooksByRepoID returns all active webhooks of repository.
  227. func GetActiveWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  228. return getActiveWebhooksByRepoID(x, repoID)
  229. }
  230. func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
  231. webhooks := make([]*Webhook, 0, 5)
  232. return webhooks, e.Where("is_active=?", true).
  233. Find(&webhooks, &Webhook{RepoID: repoID})
  234. }
  235. // GetWebhooksByRepoID returns all webhooks of a repository.
  236. func GetWebhooksByRepoID(repoID int64, listOptions ListOptions) ([]*Webhook, error) {
  237. if listOptions.Page == 0 {
  238. webhooks := make([]*Webhook, 0, 5)
  239. return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
  240. }
  241. sess := listOptions.getPaginatedSession()
  242. webhooks := make([]*Webhook, 0, listOptions.PageSize)
  243. return webhooks, sess.Find(&webhooks, &Webhook{RepoID: repoID})
  244. }
  245. // GetActiveWebhooksByOrgID returns all active webhooks for an organization.
  246. func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  247. return getActiveWebhooksByOrgID(x, orgID)
  248. }
  249. func getActiveWebhooksByOrgID(e Engine, orgID int64) (ws []*Webhook, err error) {
  250. err = e.
  251. Where("org_id=?", orgID).
  252. And("is_active=?", true).
  253. Find(&ws)
  254. return ws, err
  255. }
  256. // GetWebhooksByOrgID returns paginated webhooks for an organization.
  257. func GetWebhooksByOrgID(orgID int64, listOptions ListOptions) ([]*Webhook, error) {
  258. if listOptions.Page == 0 {
  259. ws := make([]*Webhook, 0, 5)
  260. return ws, x.Find(&ws, &Webhook{OrgID: orgID})
  261. }
  262. sess := listOptions.getPaginatedSession()
  263. ws := make([]*Webhook, 0, listOptions.PageSize)
  264. return ws, sess.Find(&ws, &Webhook{OrgID: orgID})
  265. }
  266. // GetDefaultWebhook returns admin-default webhook by given ID.
  267. func GetDefaultWebhook(id int64) (*Webhook, error) {
  268. webhook := &Webhook{ID: id}
  269. has, err := x.
  270. Where("repo_id=? AND org_id=?", 0, 0).
  271. Get(webhook)
  272. if err != nil {
  273. return nil, err
  274. } else if !has {
  275. return nil, ErrWebhookNotExist{id}
  276. }
  277. return webhook, nil
  278. }
  279. // GetDefaultWebhooks returns all admin-default webhooks.
  280. func GetDefaultWebhooks() ([]*Webhook, error) {
  281. return getDefaultWebhooks(x)
  282. }
  283. func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
  284. webhooks := make([]*Webhook, 0, 5)
  285. return webhooks, e.
  286. Where("repo_id=? AND org_id=?", 0, 0).
  287. Find(&webhooks)
  288. }
  289. // UpdateWebhook updates information of webhook.
  290. func UpdateWebhook(w *Webhook) error {
  291. _, err := x.ID(w.ID).AllCols().Update(w)
  292. return err
  293. }
  294. // UpdateWebhookLastStatus updates last status of webhook.
  295. func UpdateWebhookLastStatus(w *Webhook) error {
  296. _, err := x.ID(w.ID).Cols("last_status").Update(w)
  297. return err
  298. }
  299. // deleteWebhook uses argument bean as query condition,
  300. // ID must be specified and do not assign unnecessary fields.
  301. func deleteWebhook(bean *Webhook) (err error) {
  302. sess := x.NewSession()
  303. defer sess.Close()
  304. if err = sess.Begin(); err != nil {
  305. return err
  306. }
  307. if count, err := sess.Delete(bean); err != nil {
  308. return err
  309. } else if count == 0 {
  310. return ErrWebhookNotExist{ID: bean.ID}
  311. } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
  312. return err
  313. }
  314. return sess.Commit()
  315. }
  316. // DeleteWebhookByRepoID deletes webhook of repository by given ID.
  317. func DeleteWebhookByRepoID(repoID, id int64) error {
  318. return deleteWebhook(&Webhook{
  319. ID: id,
  320. RepoID: repoID,
  321. })
  322. }
  323. // DeleteWebhookByOrgID deletes webhook of organization by given ID.
  324. func DeleteWebhookByOrgID(orgID, id int64) error {
  325. return deleteWebhook(&Webhook{
  326. ID: id,
  327. OrgID: orgID,
  328. })
  329. }
  330. // DeleteDefaultWebhook deletes an admin-default webhook by given ID.
  331. func DeleteDefaultWebhook(id int64) error {
  332. sess := x.NewSession()
  333. defer sess.Close()
  334. if err := sess.Begin(); err != nil {
  335. return err
  336. }
  337. count, err := sess.
  338. Where("repo_id=? AND org_id=?", 0, 0).
  339. Delete(&Webhook{ID: id})
  340. if err != nil {
  341. return err
  342. } else if count == 0 {
  343. return ErrWebhookNotExist{ID: id}
  344. }
  345. if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
  346. return err
  347. }
  348. return sess.Commit()
  349. }
  350. // copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
  351. func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
  352. ws, err := getDefaultWebhooks(e)
  353. if err != nil {
  354. return fmt.Errorf("GetDefaultWebhooks: %v", err)
  355. }
  356. for _, w := range ws {
  357. w.ID = 0
  358. w.RepoID = repoID
  359. if err := createWebhook(e, w); err != nil {
  360. return fmt.Errorf("CreateWebhook: %v", err)
  361. }
  362. }
  363. return nil
  364. }
  365. // ___ ___ __ ___________ __
  366. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  367. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  368. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  369. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  370. // \/ \/ \/ \/ \/
  371. // HookTaskType is the type of an hook task
  372. type HookTaskType int
  373. // Types of hook tasks
  374. const (
  375. GOGS HookTaskType = iota + 1
  376. SLACK
  377. GITEA
  378. DISCORD
  379. DINGTALK
  380. TELEGRAM
  381. MSTEAMS
  382. )
  383. var hookTaskTypes = map[string]HookTaskType{
  384. "gitea": GITEA,
  385. "gogs": GOGS,
  386. "slack": SLACK,
  387. "discord": DISCORD,
  388. "dingtalk": DINGTALK,
  389. "telegram": TELEGRAM,
  390. "msteams": MSTEAMS,
  391. }
  392. // ToHookTaskType returns HookTaskType by given name.
  393. func ToHookTaskType(name string) HookTaskType {
  394. return hookTaskTypes[name]
  395. }
  396. // Name returns the name of an hook task type
  397. func (t HookTaskType) Name() string {
  398. switch t {
  399. case GITEA:
  400. return "gitea"
  401. case GOGS:
  402. return "gogs"
  403. case SLACK:
  404. return "slack"
  405. case DISCORD:
  406. return "discord"
  407. case DINGTALK:
  408. return "dingtalk"
  409. case TELEGRAM:
  410. return "telegram"
  411. case MSTEAMS:
  412. return "msteams"
  413. }
  414. return ""
  415. }
  416. // IsValidHookTaskType returns true if given name is a valid hook task type.
  417. func IsValidHookTaskType(name string) bool {
  418. _, ok := hookTaskTypes[name]
  419. return ok
  420. }
  421. // HookEventType is the type of an hook event
  422. type HookEventType string
  423. // Types of hook events
  424. const (
  425. HookEventCreate HookEventType = "create"
  426. HookEventDelete HookEventType = "delete"
  427. HookEventFork HookEventType = "fork"
  428. HookEventPush HookEventType = "push"
  429. HookEventIssues HookEventType = "issues"
  430. HookEventIssueComment HookEventType = "issue_comment"
  431. HookEventPullRequest HookEventType = "pull_request"
  432. HookEventRepository HookEventType = "repository"
  433. HookEventRelease HookEventType = "release"
  434. HookEventPullRequestApproved HookEventType = "pull_request_approved"
  435. HookEventPullRequestRejected HookEventType = "pull_request_rejected"
  436. HookEventPullRequestComment HookEventType = "pull_request_comment"
  437. )
  438. // HookRequest represents hook task request information.
  439. type HookRequest struct {
  440. Headers map[string]string `json:"headers"`
  441. }
  442. // HookResponse represents hook task response information.
  443. type HookResponse struct {
  444. Status int `json:"status"`
  445. Headers map[string]string `json:"headers"`
  446. Body string `json:"body"`
  447. }
  448. // HookTask represents a hook task.
  449. type HookTask struct {
  450. ID int64 `xorm:"pk autoincr"`
  451. RepoID int64 `xorm:"INDEX"`
  452. HookID int64
  453. UUID string
  454. Type HookTaskType
  455. URL string `xorm:"TEXT"`
  456. Signature string `xorm:"TEXT"`
  457. api.Payloader `xorm:"-"`
  458. PayloadContent string `xorm:"TEXT"`
  459. HTTPMethod string `xorm:"http_method"`
  460. ContentType HookContentType
  461. EventType HookEventType
  462. IsSSL bool
  463. IsDelivered bool
  464. Delivered int64
  465. DeliveredString string `xorm:"-"`
  466. // History info.
  467. IsSucceed bool
  468. RequestContent string `xorm:"TEXT"`
  469. RequestInfo *HookRequest `xorm:"-"`
  470. ResponseContent string `xorm:"TEXT"`
  471. ResponseInfo *HookResponse `xorm:"-"`
  472. }
  473. // BeforeUpdate will be invoked by XORM before updating a record
  474. // representing this object
  475. func (t *HookTask) BeforeUpdate() {
  476. if t.RequestInfo != nil {
  477. t.RequestContent = t.simpleMarshalJSON(t.RequestInfo)
  478. }
  479. if t.ResponseInfo != nil {
  480. t.ResponseContent = t.simpleMarshalJSON(t.ResponseInfo)
  481. }
  482. }
  483. // AfterLoad updates the webhook object upon setting a column
  484. func (t *HookTask) AfterLoad() {
  485. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  486. if len(t.RequestContent) == 0 {
  487. return
  488. }
  489. t.RequestInfo = &HookRequest{}
  490. if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  491. log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err)
  492. }
  493. if len(t.ResponseContent) > 0 {
  494. t.ResponseInfo = &HookResponse{}
  495. if err := json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  496. log.Error("Unmarshal ResponseContent[%d]: %v", t.ID, err)
  497. }
  498. }
  499. }
  500. func (t *HookTask) simpleMarshalJSON(v interface{}) string {
  501. p, err := json.Marshal(v)
  502. if err != nil {
  503. log.Error("Marshal [%d]: %v", t.ID, err)
  504. }
  505. return string(p)
  506. }
  507. // HookTasks returns a list of hook tasks by given conditions.
  508. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  509. tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
  510. return tasks, x.
  511. Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
  512. Where("hook_id=?", hookID).
  513. Desc("id").
  514. Find(&tasks)
  515. }
  516. // CreateHookTask creates a new hook task,
  517. // it handles conversion from Payload to PayloadContent.
  518. func CreateHookTask(t *HookTask) error {
  519. return createHookTask(x, t)
  520. }
  521. func createHookTask(e Engine, t *HookTask) error {
  522. data, err := t.Payloader.JSONPayload()
  523. if err != nil {
  524. return err
  525. }
  526. t.UUID = gouuid.NewV4().String()
  527. t.PayloadContent = string(data)
  528. _, err = e.Insert(t)
  529. return err
  530. }
  531. // UpdateHookTask updates information of hook task.
  532. func UpdateHookTask(t *HookTask) error {
  533. _, err := x.ID(t.ID).AllCols().Update(t)
  534. return err
  535. }
  536. // FindUndeliveredHookTasks represents find the undelivered hook tasks
  537. func FindUndeliveredHookTasks() ([]*HookTask, error) {
  538. tasks := make([]*HookTask, 0, 10)
  539. if err := x.Where("is_delivered=?", false).Find(&tasks); err != nil {
  540. return nil, err
  541. }
  542. return tasks, nil
  543. }
  544. // FindRepoUndeliveredHookTasks represents find the undelivered hook tasks of one repository
  545. func FindRepoUndeliveredHookTasks(repoID int64) ([]*HookTask, error) {
  546. tasks := make([]*HookTask, 0, 5)
  547. if err := x.Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
  548. return nil, err
  549. }
  550. return tasks, nil
  551. }