1
0

webhook.go 21 KB

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