webhook.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 db
  5. import (
  6. "crypto/hmac"
  7. "crypto/sha256"
  8. "crypto/tls"
  9. "encoding/hex"
  10. "fmt"
  11. "io/ioutil"
  12. "strings"
  13. "time"
  14. jsoniter "github.com/json-iterator/go"
  15. gouuid "github.com/satori/go.uuid"
  16. log "unknwon.dev/clog/v2"
  17. "xorm.io/xorm"
  18. api "github.com/gogs/go-gogs-client"
  19. "gogs.io/gogs/internal/conf"
  20. "gogs.io/gogs/internal/errutil"
  21. "gogs.io/gogs/internal/httplib"
  22. "gogs.io/gogs/internal/netutil"
  23. "gogs.io/gogs/internal/sync"
  24. )
  25. var HookQueue = sync.NewUniqueQueue(1000)
  26. type HookContentType int
  27. const (
  28. JSON HookContentType = iota + 1
  29. FORM
  30. )
  31. var hookContentTypes = map[string]HookContentType{
  32. "json": JSON,
  33. "form": FORM,
  34. }
  35. // ToHookContentType returns HookContentType by given name.
  36. func ToHookContentType(name string) HookContentType {
  37. return hookContentTypes[name]
  38. }
  39. func (t HookContentType) Name() string {
  40. switch t {
  41. case JSON:
  42. return "json"
  43. case FORM:
  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. type HookEvents struct {
  54. Create bool `json:"create"`
  55. Delete bool `json:"delete"`
  56. Fork bool `json:"fork"`
  57. Push bool `json:"push"`
  58. Issues bool `json:"issues"`
  59. PullRequest bool `json:"pull_request"`
  60. IssueComment bool `json:"issue_comment"`
  61. Release bool `json:"release"`
  62. }
  63. // HookEvent represents events that will delivery hook.
  64. type HookEvent struct {
  65. PushOnly bool `json:"push_only"`
  66. SendEverything bool `json:"send_everything"`
  67. ChooseEvents bool `json:"choose_events"`
  68. HookEvents `json:"events"`
  69. }
  70. type HookStatus int
  71. const (
  72. HOOK_STATUS_NONE = iota
  73. HOOK_STATUS_SUCCEED
  74. HOOK_STATUS_FAILED
  75. )
  76. // Webhook represents a web hook object.
  77. type Webhook struct {
  78. ID int64
  79. RepoID int64
  80. OrgID int64
  81. URL string `xorm:"url TEXT"`
  82. ContentType HookContentType
  83. Secret string `xorm:"TEXT"`
  84. Events string `xorm:"TEXT"`
  85. *HookEvent `xorm:"-"` // LEGACY [1.0]: Cannot ignore JSON (i.e. json:"-") here, it breaks old backup archive
  86. IsSSL bool `xorm:"is_ssl"`
  87. IsActive bool
  88. HookTaskType HookTaskType
  89. Meta string `xorm:"TEXT"` // store hook-specific attributes
  90. LastStatus HookStatus // Last delivery status
  91. Created time.Time `xorm:"-" json:"-"`
  92. CreatedUnix int64
  93. Updated time.Time `xorm:"-" json:"-"`
  94. UpdatedUnix int64
  95. }
  96. func (w *Webhook) BeforeInsert() {
  97. w.CreatedUnix = time.Now().Unix()
  98. w.UpdatedUnix = w.CreatedUnix
  99. }
  100. func (w *Webhook) BeforeUpdate() {
  101. w.UpdatedUnix = time.Now().Unix()
  102. }
  103. func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
  104. var err error
  105. switch colName {
  106. case "events":
  107. w.HookEvent = &HookEvent{}
  108. if err = jsoniter.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  109. log.Error("Unmarshal [%d]: %v", w.ID, err)
  110. }
  111. case "created_unix":
  112. w.Created = time.Unix(w.CreatedUnix, 0).Local()
  113. case "updated_unix":
  114. w.Updated = time.Unix(w.UpdatedUnix, 0).Local()
  115. }
  116. }
  117. func (w *Webhook) SlackMeta() *SlackMeta {
  118. s := &SlackMeta{}
  119. if err := jsoniter.Unmarshal([]byte(w.Meta), s); err != nil {
  120. log.Error("Failed to get Slack meta [webhook_id: %d]: %v", w.ID, err)
  121. }
  122. return s
  123. }
  124. // History returns history of webhook by given conditions.
  125. func (w *Webhook) History(page int) ([]*HookTask, error) {
  126. return HookTasks(w.ID, page)
  127. }
  128. // UpdateEvent handles conversion from HookEvent to Events.
  129. func (w *Webhook) UpdateEvent() error {
  130. data, err := jsoniter.Marshal(w.HookEvent)
  131. w.Events = string(data)
  132. return err
  133. }
  134. // HasCreateEvent returns true if hook enabled create event.
  135. func (w *Webhook) HasCreateEvent() bool {
  136. return w.SendEverything ||
  137. (w.ChooseEvents && w.HookEvents.Create)
  138. }
  139. // HasDeleteEvent returns true if hook enabled delete event.
  140. func (w *Webhook) HasDeleteEvent() bool {
  141. return w.SendEverything ||
  142. (w.ChooseEvents && w.HookEvents.Delete)
  143. }
  144. // HasForkEvent returns true if hook enabled fork event.
  145. func (w *Webhook) HasForkEvent() bool {
  146. return w.SendEverything ||
  147. (w.ChooseEvents && w.HookEvents.Fork)
  148. }
  149. // HasPushEvent returns true if hook enabled push event.
  150. func (w *Webhook) HasPushEvent() bool {
  151. return w.PushOnly || w.SendEverything ||
  152. (w.ChooseEvents && w.HookEvents.Push)
  153. }
  154. // HasIssuesEvent returns true if hook enabled issues event.
  155. func (w *Webhook) HasIssuesEvent() bool {
  156. return w.SendEverything ||
  157. (w.ChooseEvents && w.HookEvents.Issues)
  158. }
  159. // HasPullRequestEvent returns true if hook enabled pull request event.
  160. func (w *Webhook) HasPullRequestEvent() bool {
  161. return w.SendEverything ||
  162. (w.ChooseEvents && w.HookEvents.PullRequest)
  163. }
  164. // HasIssueCommentEvent returns true if hook enabled issue comment event.
  165. func (w *Webhook) HasIssueCommentEvent() bool {
  166. return w.SendEverything ||
  167. (w.ChooseEvents && w.HookEvents.IssueComment)
  168. }
  169. // HasReleaseEvent returns true if hook enabled release event.
  170. func (w *Webhook) HasReleaseEvent() bool {
  171. return w.SendEverything ||
  172. (w.ChooseEvents && w.HookEvents.Release)
  173. }
  174. type eventChecker struct {
  175. checker func() bool
  176. typ HookEventType
  177. }
  178. func (w *Webhook) EventsArray() []string {
  179. events := make([]string, 0, 8)
  180. eventCheckers := []eventChecker{
  181. {w.HasCreateEvent, HOOK_EVENT_CREATE},
  182. {w.HasDeleteEvent, HOOK_EVENT_DELETE},
  183. {w.HasForkEvent, HOOK_EVENT_FORK},
  184. {w.HasPushEvent, HOOK_EVENT_PUSH},
  185. {w.HasIssuesEvent, HOOK_EVENT_ISSUES},
  186. {w.HasPullRequestEvent, HOOK_EVENT_PULL_REQUEST},
  187. {w.HasIssueCommentEvent, HOOK_EVENT_ISSUE_COMMENT},
  188. {w.HasReleaseEvent, HOOK_EVENT_RELEASE},
  189. }
  190. for _, c := range eventCheckers {
  191. if c.checker() {
  192. events = append(events, string(c.typ))
  193. }
  194. }
  195. return events
  196. }
  197. // CreateWebhook creates a new web hook.
  198. func CreateWebhook(w *Webhook) error {
  199. _, err := x.Insert(w)
  200. return err
  201. }
  202. var _ errutil.NotFound = (*ErrWebhookNotExist)(nil)
  203. type ErrWebhookNotExist struct {
  204. args map[string]interface{}
  205. }
  206. func IsErrWebhookNotExist(err error) bool {
  207. _, ok := err.(ErrWebhookNotExist)
  208. return ok
  209. }
  210. func (err ErrWebhookNotExist) Error() string {
  211. return fmt.Sprintf("webhook does not exist: %v", err.args)
  212. }
  213. func (ErrWebhookNotExist) NotFound() bool {
  214. return true
  215. }
  216. // getWebhook uses argument bean as query condition,
  217. // ID must be specified and do not assign unnecessary fields.
  218. func getWebhook(bean *Webhook) (*Webhook, error) {
  219. has, err := x.Get(bean)
  220. if err != nil {
  221. return nil, err
  222. } else if !has {
  223. return nil, ErrWebhookNotExist{args: map[string]interface{}{"webhookID": bean.ID}}
  224. }
  225. return bean, nil
  226. }
  227. // GetWebhookByID returns webhook by given ID.
  228. // Use this function with caution of accessing unauthorized webhook,
  229. // which means should only be used in non-user interactive functions.
  230. func GetWebhookByID(id int64) (*Webhook, error) {
  231. return getWebhook(&Webhook{
  232. ID: id,
  233. })
  234. }
  235. // GetWebhookOfRepoByID returns webhook of repository by given ID.
  236. func GetWebhookOfRepoByID(repoID, id int64) (*Webhook, error) {
  237. return getWebhook(&Webhook{
  238. ID: id,
  239. RepoID: repoID,
  240. })
  241. }
  242. // GetWebhookByOrgID returns webhook of organization by given ID.
  243. func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
  244. return getWebhook(&Webhook{
  245. ID: id,
  246. OrgID: orgID,
  247. })
  248. }
  249. // getActiveWebhooksByRepoID returns all active webhooks of repository.
  250. func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
  251. webhooks := make([]*Webhook, 0, 5)
  252. return webhooks, e.Where("repo_id = ?", repoID).And("is_active = ?", true).Find(&webhooks)
  253. }
  254. // GetWebhooksByRepoID returns all webhooks of a repository.
  255. func GetWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  256. webhooks := make([]*Webhook, 0, 5)
  257. return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
  258. }
  259. // UpdateWebhook updates information of webhook.
  260. func UpdateWebhook(w *Webhook) error {
  261. _, err := x.Id(w.ID).AllCols().Update(w)
  262. return err
  263. }
  264. // deleteWebhook uses argument bean as query condition,
  265. // ID must be specified and do not assign unnecessary fields.
  266. func deleteWebhook(bean *Webhook) (err error) {
  267. sess := x.NewSession()
  268. defer sess.Close()
  269. if err = sess.Begin(); err != nil {
  270. return err
  271. }
  272. if _, err = sess.Delete(bean); err != nil {
  273. return err
  274. } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
  275. return err
  276. }
  277. return sess.Commit()
  278. }
  279. // DeleteWebhookOfRepoByID deletes webhook of repository by given ID.
  280. func DeleteWebhookOfRepoByID(repoID, id int64) error {
  281. return deleteWebhook(&Webhook{
  282. ID: id,
  283. RepoID: repoID,
  284. })
  285. }
  286. // DeleteWebhookOfOrgByID deletes webhook of organization by given ID.
  287. func DeleteWebhookOfOrgByID(orgID, id int64) error {
  288. return deleteWebhook(&Webhook{
  289. ID: id,
  290. OrgID: orgID,
  291. })
  292. }
  293. // GetWebhooksByOrgID returns all webhooks for an organization.
  294. func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  295. err = x.Find(&ws, &Webhook{OrgID: orgID})
  296. return ws, err
  297. }
  298. // getActiveWebhooksByOrgID returns all active webhooks for an organization.
  299. func getActiveWebhooksByOrgID(e Engine, orgID int64) ([]*Webhook, error) {
  300. ws := make([]*Webhook, 0, 3)
  301. return ws, e.Where("org_id=?", orgID).And("is_active=?", true).Find(&ws)
  302. }
  303. // ___ ___ __ ___________ __
  304. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  305. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  306. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  307. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  308. // \/ \/ \/ \/ \/
  309. type HookTaskType int
  310. const (
  311. GOGS HookTaskType = iota + 1
  312. SLACK
  313. DISCORD
  314. DINGTALK
  315. )
  316. var hookTaskTypes = map[string]HookTaskType{
  317. "gogs": GOGS,
  318. "slack": SLACK,
  319. "discord": DISCORD,
  320. "dingtalk": DINGTALK,
  321. }
  322. // ToHookTaskType returns HookTaskType by given name.
  323. func ToHookTaskType(name string) HookTaskType {
  324. return hookTaskTypes[name]
  325. }
  326. func (t HookTaskType) Name() string {
  327. switch t {
  328. case GOGS:
  329. return "gogs"
  330. case SLACK:
  331. return "slack"
  332. case DISCORD:
  333. return "discord"
  334. case DINGTALK:
  335. return "dingtalk"
  336. }
  337. return ""
  338. }
  339. // IsValidHookTaskType returns true if given name is a valid hook task type.
  340. func IsValidHookTaskType(name string) bool {
  341. _, ok := hookTaskTypes[name]
  342. return ok
  343. }
  344. type HookEventType string
  345. const (
  346. HOOK_EVENT_CREATE HookEventType = "create"
  347. HOOK_EVENT_DELETE HookEventType = "delete"
  348. HOOK_EVENT_FORK HookEventType = "fork"
  349. HOOK_EVENT_PUSH HookEventType = "push"
  350. HOOK_EVENT_ISSUES HookEventType = "issues"
  351. HOOK_EVENT_PULL_REQUEST HookEventType = "pull_request"
  352. HOOK_EVENT_ISSUE_COMMENT HookEventType = "issue_comment"
  353. HOOK_EVENT_RELEASE HookEventType = "release"
  354. )
  355. // HookRequest represents hook task request information.
  356. type HookRequest struct {
  357. Headers map[string]string `json:"headers"`
  358. }
  359. // HookResponse represents hook task response information.
  360. type HookResponse struct {
  361. Status int `json:"status"`
  362. Headers map[string]string `json:"headers"`
  363. Body string `json:"body"`
  364. }
  365. // HookTask represents a hook task.
  366. type HookTask struct {
  367. ID int64
  368. RepoID int64 `xorm:"INDEX"`
  369. HookID int64
  370. UUID string
  371. Type HookTaskType
  372. URL string `xorm:"TEXT"`
  373. Signature string `xorm:"TEXT"`
  374. api.Payloader `xorm:"-" json:"-"`
  375. PayloadContent string `xorm:"TEXT"`
  376. ContentType HookContentType
  377. EventType HookEventType
  378. IsSSL bool
  379. IsDelivered bool
  380. Delivered int64
  381. DeliveredString string `xorm:"-" json:"-"`
  382. // History info.
  383. IsSucceed bool
  384. RequestContent string `xorm:"TEXT"`
  385. RequestInfo *HookRequest `xorm:"-" json:"-"`
  386. ResponseContent string `xorm:"TEXT"`
  387. ResponseInfo *HookResponse `xorm:"-" json:"-"`
  388. }
  389. func (t *HookTask) BeforeUpdate() {
  390. if t.RequestInfo != nil {
  391. t.RequestContent = t.ToJSON(t.RequestInfo)
  392. }
  393. if t.ResponseInfo != nil {
  394. t.ResponseContent = t.ToJSON(t.ResponseInfo)
  395. }
  396. }
  397. func (t *HookTask) AfterSet(colName string, _ xorm.Cell) {
  398. var err error
  399. switch colName {
  400. case "delivered":
  401. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  402. case "request_content":
  403. if t.RequestContent == "" {
  404. return
  405. }
  406. t.RequestInfo = &HookRequest{}
  407. if err = jsoniter.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  408. log.Error("Unmarshal[%d]: %v", t.ID, err)
  409. }
  410. case "response_content":
  411. if t.ResponseContent == "" {
  412. return
  413. }
  414. t.ResponseInfo = &HookResponse{}
  415. if err = jsoniter.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  416. log.Error("Unmarshal [%d]: %v", t.ID, err)
  417. }
  418. }
  419. }
  420. func (t *HookTask) ToJSON(v interface{}) string {
  421. p, err := jsoniter.Marshal(v)
  422. if err != nil {
  423. log.Error("Marshal [%d]: %v", t.ID, err)
  424. }
  425. return string(p)
  426. }
  427. // HookTasks returns a list of hook tasks by given conditions.
  428. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  429. tasks := make([]*HookTask, 0, conf.Webhook.PagingNum)
  430. return tasks, x.Limit(conf.Webhook.PagingNum, (page-1)*conf.Webhook.PagingNum).Where("hook_id=?", hookID).Desc("id").Find(&tasks)
  431. }
  432. // createHookTask creates a new hook task,
  433. // it handles conversion from Payload to PayloadContent.
  434. func createHookTask(e Engine, t *HookTask) error {
  435. data, err := t.Payloader.JSONPayload()
  436. if err != nil {
  437. return err
  438. }
  439. t.UUID = gouuid.NewV4().String()
  440. t.PayloadContent = string(data)
  441. _, err = e.Insert(t)
  442. return err
  443. }
  444. var _ errutil.NotFound = (*ErrHookTaskNotExist)(nil)
  445. type ErrHookTaskNotExist struct {
  446. args map[string]interface{}
  447. }
  448. func IsHookTaskNotExist(err error) bool {
  449. _, ok := err.(ErrHookTaskNotExist)
  450. return ok
  451. }
  452. func (err ErrHookTaskNotExist) Error() string {
  453. return fmt.Sprintf("hook task does not exist: %v", err.args)
  454. }
  455. func (ErrHookTaskNotExist) NotFound() bool {
  456. return true
  457. }
  458. // GetHookTaskOfWebhookByUUID returns hook task of given webhook by UUID.
  459. func GetHookTaskOfWebhookByUUID(webhookID int64, uuid string) (*HookTask, error) {
  460. hookTask := &HookTask{
  461. HookID: webhookID,
  462. UUID: uuid,
  463. }
  464. has, err := x.Get(hookTask)
  465. if err != nil {
  466. return nil, err
  467. } else if !has {
  468. return nil, ErrHookTaskNotExist{args: map[string]interface{}{"webhookID": webhookID, "uuid": uuid}}
  469. }
  470. return hookTask, nil
  471. }
  472. // UpdateHookTask updates information of hook task.
  473. func UpdateHookTask(t *HookTask) error {
  474. _, err := x.Id(t.ID).AllCols().Update(t)
  475. return err
  476. }
  477. // prepareHookTasks adds list of webhooks to task queue.
  478. func prepareHookTasks(e Engine, repo *Repository, event HookEventType, p api.Payloader, webhooks []*Webhook) (err error) {
  479. if len(webhooks) == 0 {
  480. return nil
  481. }
  482. var payloader api.Payloader
  483. for _, w := range webhooks {
  484. switch event {
  485. case HOOK_EVENT_CREATE:
  486. if !w.HasCreateEvent() {
  487. continue
  488. }
  489. case HOOK_EVENT_DELETE:
  490. if !w.HasDeleteEvent() {
  491. continue
  492. }
  493. case HOOK_EVENT_FORK:
  494. if !w.HasForkEvent() {
  495. continue
  496. }
  497. case HOOK_EVENT_PUSH:
  498. if !w.HasPushEvent() {
  499. continue
  500. }
  501. case HOOK_EVENT_ISSUES:
  502. if !w.HasIssuesEvent() {
  503. continue
  504. }
  505. case HOOK_EVENT_PULL_REQUEST:
  506. if !w.HasPullRequestEvent() {
  507. continue
  508. }
  509. case HOOK_EVENT_ISSUE_COMMENT:
  510. if !w.HasIssueCommentEvent() {
  511. continue
  512. }
  513. case HOOK_EVENT_RELEASE:
  514. if !w.HasReleaseEvent() {
  515. continue
  516. }
  517. }
  518. // Use separate objects so modifications won't be made on payload on non-Gogs type hooks.
  519. switch w.HookTaskType {
  520. case SLACK:
  521. payloader, err = GetSlackPayload(p, event, w.Meta)
  522. if err != nil {
  523. return fmt.Errorf("GetSlackPayload: %v", err)
  524. }
  525. case DISCORD:
  526. payloader, err = GetDiscordPayload(p, event, w.Meta)
  527. if err != nil {
  528. return fmt.Errorf("GetDiscordPayload: %v", err)
  529. }
  530. case DINGTALK:
  531. payloader, err = GetDingtalkPayload(p, event)
  532. if err != nil {
  533. return fmt.Errorf("GetDingtalkPayload: %v", err)
  534. }
  535. default:
  536. payloader = p
  537. }
  538. var signature string
  539. if len(w.Secret) > 0 {
  540. data, err := payloader.JSONPayload()
  541. if err != nil {
  542. log.Error("prepareWebhooks.JSONPayload: %v", err)
  543. }
  544. sig := hmac.New(sha256.New, []byte(w.Secret))
  545. _, _ = sig.Write(data)
  546. signature = hex.EncodeToString(sig.Sum(nil))
  547. }
  548. if err = createHookTask(e, &HookTask{
  549. RepoID: repo.ID,
  550. HookID: w.ID,
  551. Type: w.HookTaskType,
  552. URL: w.URL,
  553. Signature: signature,
  554. Payloader: payloader,
  555. ContentType: w.ContentType,
  556. EventType: event,
  557. IsSSL: w.IsSSL,
  558. }); err != nil {
  559. return fmt.Errorf("createHookTask: %v", err)
  560. }
  561. }
  562. // It's safe to fail when the whole function is called during hook execution
  563. // because resource released after exit. Also, there is no process started to
  564. // consume this input during hook execution.
  565. go HookQueue.Add(repo.ID)
  566. return nil
  567. }
  568. func prepareWebhooks(e Engine, repo *Repository, event HookEventType, p api.Payloader) error {
  569. webhooks, err := getActiveWebhooksByRepoID(e, repo.ID)
  570. if err != nil {
  571. return fmt.Errorf("getActiveWebhooksByRepoID [%d]: %v", repo.ID, err)
  572. }
  573. // check if repo belongs to org and append additional webhooks
  574. if repo.mustOwner(e).IsOrganization() {
  575. // get hooks for org
  576. orgws, err := getActiveWebhooksByOrgID(e, repo.OwnerID)
  577. if err != nil {
  578. return fmt.Errorf("getActiveWebhooksByOrgID [%d]: %v", repo.OwnerID, err)
  579. }
  580. webhooks = append(webhooks, orgws...)
  581. }
  582. return prepareHookTasks(e, repo, event, p, webhooks)
  583. }
  584. // PrepareWebhooks adds all active webhooks to task queue.
  585. func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
  586. return prepareWebhooks(x, repo, event, p)
  587. }
  588. // TestWebhook adds the test webhook matches the ID to task queue.
  589. func TestWebhook(repo *Repository, event HookEventType, p api.Payloader, webhookID int64) error {
  590. webhook, err := GetWebhookOfRepoByID(repo.ID, webhookID)
  591. if err != nil {
  592. return fmt.Errorf("GetWebhookOfRepoByID [repo_id: %d, id: %d]: %v", repo.ID, webhookID, err)
  593. }
  594. return prepareHookTasks(x, repo, event, p, []*Webhook{webhook})
  595. }
  596. func (t *HookTask) deliver() {
  597. if netutil.IsBlockedLocalHostname(t.URL, conf.Security.LocalNetworkAllowlist) {
  598. t.ResponseContent = "Payload URL resolved to a local network address that is implicitly blocked."
  599. return
  600. }
  601. t.IsDelivered = true
  602. timeout := time.Duration(conf.Webhook.DeliverTimeout) * time.Second
  603. req := httplib.Post(t.URL).SetTimeout(timeout, timeout).
  604. Header("X-Github-Delivery", t.UUID).
  605. Header("X-Github-Event", string(t.EventType)).
  606. Header("X-Gogs-Delivery", t.UUID).
  607. Header("X-Gogs-Signature", t.Signature).
  608. Header("X-Gogs-Event", string(t.EventType)).
  609. SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Webhook.SkipTLSVerify})
  610. switch t.ContentType {
  611. case JSON:
  612. req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
  613. case FORM:
  614. req.Param("payload", t.PayloadContent)
  615. }
  616. // Record delivery information.
  617. t.RequestInfo = &HookRequest{
  618. Headers: map[string]string{},
  619. }
  620. for k, vals := range req.Headers() {
  621. t.RequestInfo.Headers[k] = strings.Join(vals, ",")
  622. }
  623. t.ResponseInfo = &HookResponse{
  624. Headers: map[string]string{},
  625. }
  626. defer func() {
  627. t.Delivered = time.Now().UnixNano()
  628. if t.IsSucceed {
  629. log.Trace("Hook delivered: %s", t.UUID)
  630. } else {
  631. log.Trace("Hook delivery failed: %s", t.UUID)
  632. }
  633. // Update webhook last delivery status.
  634. w, err := GetWebhookByID(t.HookID)
  635. if err != nil {
  636. log.Error("GetWebhookByID: %v", err)
  637. return
  638. }
  639. if t.IsSucceed {
  640. w.LastStatus = HOOK_STATUS_SUCCEED
  641. } else {
  642. w.LastStatus = HOOK_STATUS_FAILED
  643. }
  644. if err = UpdateWebhook(w); err != nil {
  645. log.Error("UpdateWebhook: %v", err)
  646. return
  647. }
  648. }()
  649. resp, err := req.Response()
  650. if err != nil {
  651. t.ResponseInfo.Body = fmt.Sprintf("Delivery: %v", err)
  652. return
  653. }
  654. defer resp.Body.Close()
  655. // Status code is 20x can be seen as succeed.
  656. t.IsSucceed = resp.StatusCode/100 == 2
  657. t.ResponseInfo.Status = resp.StatusCode
  658. for k, vals := range resp.Header {
  659. t.ResponseInfo.Headers[k] = strings.Join(vals, ",")
  660. }
  661. p, err := ioutil.ReadAll(resp.Body)
  662. if err != nil {
  663. t.ResponseInfo.Body = fmt.Sprintf("read body: %s", err)
  664. return
  665. }
  666. t.ResponseInfo.Body = string(p)
  667. }
  668. // DeliverHooks checks and delivers undelivered hooks.
  669. // TODO: shoot more hooks at same time.
  670. func DeliverHooks() {
  671. tasks := make([]*HookTask, 0, 10)
  672. _ = x.Where("is_delivered = ?", false).Iterate(new(HookTask),
  673. func(idx int, bean interface{}) error {
  674. t := bean.(*HookTask)
  675. t.deliver()
  676. tasks = append(tasks, t)
  677. return nil
  678. })
  679. // Update hook task status.
  680. for _, t := range tasks {
  681. if err := UpdateHookTask(t); err != nil {
  682. log.Error("UpdateHookTask [%d]: %v", t.ID, err)
  683. }
  684. }
  685. // Start listening on new hook requests.
  686. for repoID := range HookQueue.Queue() {
  687. log.Trace("DeliverHooks [repo_id: %v]", repoID)
  688. HookQueue.Remove(repoID)
  689. tasks = make([]*HookTask, 0, 5)
  690. if err := x.Where("repo_id = ?", repoID).And("is_delivered = ?", false).Find(&tasks); err != nil {
  691. log.Error("Get repository [%s] hook tasks: %v", repoID, err)
  692. continue
  693. }
  694. for _, t := range tasks {
  695. t.deliver()
  696. if err := UpdateHookTask(t); err != nil {
  697. log.Error("UpdateHookTask [%d]: %v", t.ID, err)
  698. continue
  699. }
  700. }
  701. }
  702. }
  703. func InitDeliverHooks() {
  704. go DeliverHooks()
  705. }