2
0

webhook.go 21 KB

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