Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

webhook.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "crypto/tls"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "strings"
  11. "time"
  12. "github.com/go-xorm/xorm"
  13. gouuid "github.com/satori/go.uuid"
  14. api "code.gitea.io/sdk/gitea"
  15. "code.gitea.io/gitea/modules/httplib"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/sync"
  19. )
  20. // HookQueue is a global queue of web hooks
  21. var HookQueue = sync.NewUniqueQueue(setting.Webhook.QueueLength)
  22. // HookContentType is the content type of a web hook
  23. type HookContentType int
  24. const (
  25. // ContentTypeJSON is a JSON payload for web hooks
  26. ContentTypeJSON HookContentType = iota + 1
  27. // ContentTypeForm is an url-encoded form payload for web hook
  28. ContentTypeForm
  29. )
  30. var hookContentTypes = map[string]HookContentType{
  31. "json": ContentTypeJSON,
  32. "form": ContentTypeForm,
  33. }
  34. // ToHookContentType returns HookContentType by given name.
  35. func ToHookContentType(name string) HookContentType {
  36. return hookContentTypes[name]
  37. }
  38. // Name returns the name of a given web hook's content type
  39. func (t HookContentType) Name() string {
  40. switch t {
  41. case ContentTypeJSON:
  42. return "json"
  43. case ContentTypeForm:
  44. return "form"
  45. }
  46. return ""
  47. }
  48. // IsValidHookContentType returns true if given name is a valid hook content type.
  49. func IsValidHookContentType(name string) bool {
  50. _, ok := hookContentTypes[name]
  51. return ok
  52. }
  53. // HookEvents is a set of web hook events
  54. type HookEvents struct {
  55. Create bool `json:"create"`
  56. Push bool `json:"push"`
  57. PullRequest bool `json:"pull_request"`
  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. HookEvents `json:"events"`
  65. }
  66. // HookStatus is the status of a web hook
  67. type HookStatus int
  68. // Possible statuses of a web hook
  69. const (
  70. HookStatusNone = iota
  71. HookStatusSucceed
  72. HookStatusFail
  73. )
  74. // Webhook represents a web hook object.
  75. type Webhook struct {
  76. ID int64 `xorm:"pk autoincr"`
  77. RepoID int64 `xorm:"INDEX"`
  78. OrgID int64 `xorm:"INDEX"`
  79. URL string `xorm:"url TEXT"`
  80. ContentType HookContentType
  81. Secret string `xorm:"TEXT"`
  82. Events string `xorm:"TEXT"`
  83. *HookEvent `xorm:"-"`
  84. IsSSL bool `xorm:"is_ssl"`
  85. IsActive bool `xorm:"INDEX"`
  86. HookTaskType HookTaskType
  87. Meta string `xorm:"TEXT"` // store hook-specific attributes
  88. LastStatus HookStatus // Last delivery status
  89. Created time.Time `xorm:"-"`
  90. CreatedUnix int64 `xorm:"INDEX"`
  91. Updated time.Time `xorm:"-"`
  92. UpdatedUnix int64 `xorm:"INDEX"`
  93. }
  94. // BeforeInsert will be invoked by XORM before inserting a record
  95. // representing this object
  96. func (w *Webhook) BeforeInsert() {
  97. w.CreatedUnix = time.Now().Unix()
  98. w.UpdatedUnix = w.CreatedUnix
  99. }
  100. // BeforeUpdate will be invoked by XORM before updating a record
  101. // representing this object
  102. func (w *Webhook) BeforeUpdate() {
  103. w.UpdatedUnix = time.Now().Unix()
  104. }
  105. // AfterSet updates the webhook object upon setting a column
  106. func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
  107. var err error
  108. switch colName {
  109. case "events":
  110. w.HookEvent = &HookEvent{}
  111. if err = json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  112. log.Error(3, "Unmarshal[%d]: %v", w.ID, err)
  113. }
  114. case "created_unix":
  115. w.Created = time.Unix(w.CreatedUnix, 0).Local()
  116. case "updated_unix":
  117. w.Updated = time.Unix(w.UpdatedUnix, 0).Local()
  118. }
  119. }
  120. // GetSlackHook returns slack metadata
  121. func (w *Webhook) GetSlackHook() *SlackMeta {
  122. s := &SlackMeta{}
  123. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  124. log.Error(4, "webhook.GetSlackHook(%d): %v", w.ID, err)
  125. }
  126. return s
  127. }
  128. // History returns history of webhook by given conditions.
  129. func (w *Webhook) History(page int) ([]*HookTask, error) {
  130. return HookTasks(w.ID, page)
  131. }
  132. // UpdateEvent handles conversion from HookEvent to Events.
  133. func (w *Webhook) UpdateEvent() error {
  134. data, err := json.Marshal(w.HookEvent)
  135. w.Events = string(data)
  136. return err
  137. }
  138. // HasCreateEvent returns true if hook enabled create event.
  139. func (w *Webhook) HasCreateEvent() bool {
  140. return w.SendEverything ||
  141. (w.ChooseEvents && w.HookEvents.Create)
  142. }
  143. // HasPushEvent returns true if hook enabled push event.
  144. func (w *Webhook) HasPushEvent() bool {
  145. return w.PushOnly || w.SendEverything ||
  146. (w.ChooseEvents && w.HookEvents.Push)
  147. }
  148. // HasPullRequestEvent returns true if hook enabled pull request event.
  149. func (w *Webhook) HasPullRequestEvent() bool {
  150. return w.SendEverything ||
  151. (w.ChooseEvents && w.HookEvents.PullRequest)
  152. }
  153. // EventsArray returns an array of hook events
  154. func (w *Webhook) EventsArray() []string {
  155. events := make([]string, 0, 3)
  156. if w.HasCreateEvent() {
  157. events = append(events, "create")
  158. }
  159. if w.HasPushEvent() {
  160. events = append(events, "push")
  161. }
  162. if w.HasPullRequestEvent() {
  163. events = append(events, "pull_request")
  164. }
  165. return events
  166. }
  167. // CreateWebhook creates a new web hook.
  168. func CreateWebhook(w *Webhook) error {
  169. _, err := x.Insert(w)
  170. return err
  171. }
  172. // getWebhook uses argument bean as query condition,
  173. // ID must be specified and do not assign unnecessary fields.
  174. func getWebhook(bean *Webhook) (*Webhook, error) {
  175. has, err := x.Get(bean)
  176. if err != nil {
  177. return nil, err
  178. } else if !has {
  179. return nil, ErrWebhookNotExist{bean.ID}
  180. }
  181. return bean, nil
  182. }
  183. // GetWebhookByRepoID returns webhook of repository by given ID.
  184. func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) {
  185. return getWebhook(&Webhook{
  186. ID: id,
  187. RepoID: repoID,
  188. })
  189. }
  190. // GetWebhookByOrgID returns webhook of organization by given ID.
  191. func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
  192. return getWebhook(&Webhook{
  193. ID: id,
  194. OrgID: orgID,
  195. })
  196. }
  197. // GetActiveWebhooksByRepoID returns all active webhooks of repository.
  198. func GetActiveWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  199. webhooks := make([]*Webhook, 0, 5)
  200. return webhooks, x.Where("is_active=?", true).
  201. Find(&webhooks, &Webhook{RepoID: repoID})
  202. }
  203. // GetWebhooksByRepoID returns all webhooks of a repository.
  204. func GetWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  205. webhooks := make([]*Webhook, 0, 5)
  206. return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
  207. }
  208. // GetActiveWebhooksByOrgID returns all active webhooks for an organization.
  209. func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  210. err = x.
  211. Where("org_id=?", orgID).
  212. And("is_active=?", true).
  213. Find(&ws)
  214. return ws, err
  215. }
  216. // GetWebhooksByOrgID returns all webhooks for an organization.
  217. func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  218. err = x.Find(&ws, &Webhook{OrgID: orgID})
  219. return ws, err
  220. }
  221. // UpdateWebhook updates information of webhook.
  222. func UpdateWebhook(w *Webhook) error {
  223. _, err := x.Id(w.ID).AllCols().Update(w)
  224. return err
  225. }
  226. // deleteWebhook uses argument bean as query condition,
  227. // ID must be specified and do not assign unnecessary fields.
  228. func deleteWebhook(bean *Webhook) (err error) {
  229. sess := x.NewSession()
  230. defer sessionRelease(sess)
  231. if err = sess.Begin(); err != nil {
  232. return err
  233. }
  234. if count, err := sess.Delete(bean); err != nil {
  235. return err
  236. } else if count == 0 {
  237. return ErrWebhookNotExist{ID: bean.ID}
  238. } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
  239. return err
  240. }
  241. return sess.Commit()
  242. }
  243. // DeleteWebhookByRepoID deletes webhook of repository by given ID.
  244. func DeleteWebhookByRepoID(repoID, id int64) error {
  245. return deleteWebhook(&Webhook{
  246. ID: id,
  247. RepoID: repoID,
  248. })
  249. }
  250. // DeleteWebhookByOrgID deletes webhook of organization by given ID.
  251. func DeleteWebhookByOrgID(orgID, id int64) error {
  252. return deleteWebhook(&Webhook{
  253. ID: id,
  254. OrgID: orgID,
  255. })
  256. }
  257. // ___ ___ __ ___________ __
  258. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  259. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  260. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  261. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  262. // \/ \/ \/ \/ \/
  263. // HookTaskType is the type of an hook task
  264. type HookTaskType int
  265. // Types of hook tasks
  266. const (
  267. GOGS HookTaskType = iota + 1
  268. SLACK
  269. )
  270. var hookTaskTypes = map[string]HookTaskType{
  271. "gogs": GOGS,
  272. "slack": SLACK,
  273. }
  274. // ToHookTaskType returns HookTaskType by given name.
  275. func ToHookTaskType(name string) HookTaskType {
  276. return hookTaskTypes[name]
  277. }
  278. // Name returns the name of an hook task type
  279. func (t HookTaskType) Name() string {
  280. switch t {
  281. case GOGS:
  282. return "gogs"
  283. case SLACK:
  284. return "slack"
  285. }
  286. return ""
  287. }
  288. // IsValidHookTaskType returns true if given name is a valid hook task type.
  289. func IsValidHookTaskType(name string) bool {
  290. _, ok := hookTaskTypes[name]
  291. return ok
  292. }
  293. // HookEventType is the type of an hook event
  294. type HookEventType string
  295. // Types of hook events
  296. const (
  297. HookEventCreate HookEventType = "create"
  298. HookEventPush HookEventType = "push"
  299. HookEventPullRequest HookEventType = "pull_request"
  300. )
  301. // HookRequest represents hook task request information.
  302. type HookRequest struct {
  303. Headers map[string]string `json:"headers"`
  304. }
  305. // HookResponse represents hook task response information.
  306. type HookResponse struct {
  307. Status int `json:"status"`
  308. Headers map[string]string `json:"headers"`
  309. Body string `json:"body"`
  310. }
  311. // HookTask represents a hook task.
  312. type HookTask struct {
  313. ID int64 `xorm:"pk autoincr"`
  314. RepoID int64 `xorm:"INDEX"`
  315. HookID int64
  316. UUID string
  317. Type HookTaskType
  318. URL string `xorm:"TEXT"`
  319. api.Payloader `xorm:"-"`
  320. PayloadContent string `xorm:"TEXT"`
  321. ContentType HookContentType
  322. EventType HookEventType
  323. IsSSL bool
  324. IsDelivered bool
  325. Delivered int64
  326. DeliveredString string `xorm:"-"`
  327. // History info.
  328. IsSucceed bool
  329. RequestContent string `xorm:"TEXT"`
  330. RequestInfo *HookRequest `xorm:"-"`
  331. ResponseContent string `xorm:"TEXT"`
  332. ResponseInfo *HookResponse `xorm:"-"`
  333. }
  334. // BeforeUpdate will be invoked by XORM before updating a record
  335. // representing this object
  336. func (t *HookTask) BeforeUpdate() {
  337. if t.RequestInfo != nil {
  338. t.RequestContent = t.simpleMarshalJSON(t.RequestInfo)
  339. }
  340. if t.ResponseInfo != nil {
  341. t.ResponseContent = t.simpleMarshalJSON(t.ResponseInfo)
  342. }
  343. }
  344. // AfterSet updates the webhook object upon setting a column
  345. func (t *HookTask) AfterSet(colName string, _ xorm.Cell) {
  346. var err error
  347. switch colName {
  348. case "delivered":
  349. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  350. case "request_content":
  351. if len(t.RequestContent) == 0 {
  352. return
  353. }
  354. t.RequestInfo = &HookRequest{}
  355. if err = json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  356. log.Error(3, "Unmarshal[%d]: %v", t.ID, err)
  357. }
  358. case "response_content":
  359. if len(t.ResponseContent) == 0 {
  360. return
  361. }
  362. t.ResponseInfo = &HookResponse{}
  363. if err = json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  364. log.Error(3, "Unmarshal [%d]: %v", t.ID, err)
  365. }
  366. }
  367. }
  368. func (t *HookTask) simpleMarshalJSON(v interface{}) string {
  369. p, err := json.Marshal(v)
  370. if err != nil {
  371. log.Error(3, "Marshal [%d]: %v", t.ID, err)
  372. }
  373. return string(p)
  374. }
  375. // HookTasks returns a list of hook tasks by given conditions.
  376. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  377. tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
  378. return tasks, x.
  379. Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
  380. Where("hook_id=?", hookID).
  381. Desc("id").
  382. Find(&tasks)
  383. }
  384. // CreateHookTask creates a new hook task,
  385. // it handles conversion from Payload to PayloadContent.
  386. func CreateHookTask(t *HookTask) error {
  387. data, err := t.Payloader.JSONPayload()
  388. if err != nil {
  389. return err
  390. }
  391. t.UUID = gouuid.NewV4().String()
  392. t.PayloadContent = string(data)
  393. _, err = x.Insert(t)
  394. return err
  395. }
  396. // UpdateHookTask updates information of hook task.
  397. func UpdateHookTask(t *HookTask) error {
  398. _, err := x.Id(t.ID).AllCols().Update(t)
  399. return err
  400. }
  401. // PrepareWebhooks adds new webhooks to task queue for given payload.
  402. func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
  403. ws, err := GetActiveWebhooksByRepoID(repo.ID)
  404. if err != nil {
  405. return fmt.Errorf("GetActiveWebhooksByRepoID: %v", err)
  406. }
  407. // check if repo belongs to org and append additional webhooks
  408. if repo.MustOwner().IsOrganization() {
  409. // get hooks for org
  410. orgHooks, err := GetActiveWebhooksByOrgID(repo.OwnerID)
  411. if err != nil {
  412. return fmt.Errorf("GetActiveWebhooksByOrgID: %v", err)
  413. }
  414. ws = append(ws, orgHooks...)
  415. }
  416. if len(ws) == 0 {
  417. return nil
  418. }
  419. var payloader api.Payloader
  420. for _, w := range ws {
  421. switch event {
  422. case HookEventCreate:
  423. if !w.HasCreateEvent() {
  424. continue
  425. }
  426. case HookEventPush:
  427. if !w.HasPushEvent() {
  428. continue
  429. }
  430. case HookEventPullRequest:
  431. if !w.HasPullRequestEvent() {
  432. continue
  433. }
  434. }
  435. // Use separate objects so modifications won't be made on payload on non-Gogs type hooks.
  436. switch w.HookTaskType {
  437. case SLACK:
  438. payloader, err = GetSlackPayload(p, event, w.Meta)
  439. if err != nil {
  440. return fmt.Errorf("GetSlackPayload: %v", err)
  441. }
  442. default:
  443. p.SetSecret(w.Secret)
  444. payloader = p
  445. }
  446. if err = CreateHookTask(&HookTask{
  447. RepoID: repo.ID,
  448. HookID: w.ID,
  449. Type: w.HookTaskType,
  450. URL: w.URL,
  451. Payloader: payloader,
  452. ContentType: w.ContentType,
  453. EventType: event,
  454. IsSSL: w.IsSSL,
  455. }); err != nil {
  456. return fmt.Errorf("CreateHookTask: %v", err)
  457. }
  458. }
  459. return nil
  460. }
  461. func (t *HookTask) deliver() {
  462. t.IsDelivered = true
  463. timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
  464. req := httplib.Post(t.URL).SetTimeout(timeout, timeout).
  465. Header("X-Gogs-Delivery", t.UUID).
  466. Header("X-Gogs-Event", string(t.EventType)).
  467. Header("X-GitHub-Delivery", t.UUID).
  468. Header("X-GitHub-Event", string(t.EventType)).
  469. SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify})
  470. switch t.ContentType {
  471. case ContentTypeJSON:
  472. req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
  473. case ContentTypeForm:
  474. req.Param("payload", t.PayloadContent)
  475. }
  476. // Record delivery information.
  477. t.RequestInfo = &HookRequest{
  478. Headers: map[string]string{},
  479. }
  480. for k, vals := range req.Headers() {
  481. t.RequestInfo.Headers[k] = strings.Join(vals, ",")
  482. }
  483. t.ResponseInfo = &HookResponse{
  484. Headers: map[string]string{},
  485. }
  486. defer func() {
  487. t.Delivered = time.Now().UnixNano()
  488. if t.IsSucceed {
  489. log.Trace("Hook delivered: %s", t.UUID)
  490. } else {
  491. log.Trace("Hook delivery failed: %s", t.UUID)
  492. }
  493. // Update webhook last delivery status.
  494. w, err := GetWebhookByRepoID(t.RepoID, t.HookID)
  495. if err != nil {
  496. log.Error(5, "GetWebhookByID: %v", err)
  497. return
  498. }
  499. if t.IsSucceed {
  500. w.LastStatus = HookStatusSucceed
  501. } else {
  502. w.LastStatus = HookStatusFail
  503. }
  504. if err = UpdateWebhook(w); err != nil {
  505. log.Error(5, "UpdateWebhook: %v", err)
  506. return
  507. }
  508. }()
  509. resp, err := req.Response()
  510. if err != nil {
  511. t.ResponseInfo.Body = fmt.Sprintf("Delivery: %v", err)
  512. return
  513. }
  514. defer resp.Body.Close()
  515. // Status code is 20x can be seen as succeed.
  516. t.IsSucceed = resp.StatusCode/100 == 2
  517. t.ResponseInfo.Status = resp.StatusCode
  518. for k, vals := range resp.Header {
  519. t.ResponseInfo.Headers[k] = strings.Join(vals, ",")
  520. }
  521. p, err := ioutil.ReadAll(resp.Body)
  522. if err != nil {
  523. t.ResponseInfo.Body = fmt.Sprintf("read body: %s", err)
  524. return
  525. }
  526. t.ResponseInfo.Body = string(p)
  527. }
  528. // DeliverHooks checks and delivers undelivered hooks.
  529. // TODO: shoot more hooks at same time.
  530. func DeliverHooks() {
  531. tasks := make([]*HookTask, 0, 10)
  532. x.
  533. Where("is_delivered=?", false).
  534. Iterate(new(HookTask),
  535. func(idx int, bean interface{}) error {
  536. t := bean.(*HookTask)
  537. t.deliver()
  538. tasks = append(tasks, t)
  539. return nil
  540. })
  541. // Update hook task status.
  542. for _, t := range tasks {
  543. if err := UpdateHookTask(t); err != nil {
  544. log.Error(4, "UpdateHookTask [%d]: %v", t.ID, err)
  545. }
  546. }
  547. // Start listening on new hook requests.
  548. for repoID := range HookQueue.Queue() {
  549. log.Trace("DeliverHooks [repo_id: %v]", repoID)
  550. HookQueue.Remove(repoID)
  551. tasks = make([]*HookTask, 0, 5)
  552. if err := x.Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
  553. log.Error(4, "Get repository [%s] hook tasks: %v", repoID, err)
  554. continue
  555. }
  556. for _, t := range tasks {
  557. t.deliver()
  558. if err := UpdateHookTask(t); err != nil {
  559. log.Error(4, "UpdateHookTask [%d]: %v", t.ID, err)
  560. continue
  561. }
  562. }
  563. }
  564. }
  565. // InitDeliverHooks starts the hooks delivery thread
  566. func InitDeliverHooks() {
  567. go DeliverHooks()
  568. }