You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.8 KiB

  1. // Copyright 2020 The Gitea 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 migrations
  5. import (
  6. "encoding/json"
  7. "code.gitea.io/gitea/modules/setting"
  8. "xorm.io/xorm"
  9. )
  10. func expandWebhooks(x *xorm.Engine) error {
  11. type HookEvents struct {
  12. Create bool `json:"create"`
  13. Delete bool `json:"delete"`
  14. Fork bool `json:"fork"`
  15. Issues bool `json:"issues"`
  16. IssueAssign bool `json:"issue_assign"`
  17. IssueLabel bool `json:"issue_label"`
  18. IssueMilestone bool `json:"issue_milestone"`
  19. IssueComment bool `json:"issue_comment"`
  20. Push bool `json:"push"`
  21. PullRequest bool `json:"pull_request"`
  22. PullRequestAssign bool `json:"pull_request_assign"`
  23. PullRequestLabel bool `json:"pull_request_label"`
  24. PullRequestMilestone bool `json:"pull_request_milestone"`
  25. PullRequestComment bool `json:"pull_request_comment"`
  26. PullRequestReview bool `json:"pull_request_review"`
  27. PullRequestSync bool `json:"pull_request_sync"`
  28. Repository bool `json:"repository"`
  29. Release bool `json:"release"`
  30. }
  31. type HookEvent struct {
  32. PushOnly bool `json:"push_only"`
  33. SendEverything bool `json:"send_everything"`
  34. ChooseEvents bool `json:"choose_events"`
  35. BranchFilter string `json:"branch_filter"`
  36. HookEvents `json:"events"`
  37. }
  38. type Webhook struct {
  39. ID int64
  40. Events string
  41. }
  42. var bytes []byte
  43. var last int
  44. batchSize := setting.Database.IterateBufferSize
  45. sess := x.NewSession()
  46. defer sess.Close()
  47. for {
  48. if err := sess.Begin(); err != nil {
  49. return err
  50. }
  51. var results = make([]Webhook, 0, batchSize)
  52. err := x.OrderBy("id").
  53. Limit(batchSize, last).
  54. Find(&results)
  55. if err != nil {
  56. return err
  57. }
  58. if len(results) == 0 {
  59. break
  60. }
  61. last += len(results)
  62. for _, res := range results {
  63. var events HookEvent
  64. if err = json.Unmarshal([]byte(res.Events), &events); err != nil {
  65. return err
  66. }
  67. if !events.ChooseEvents {
  68. continue
  69. }
  70. if events.Issues {
  71. events.IssueAssign = true
  72. events.IssueLabel = true
  73. events.IssueMilestone = true
  74. events.IssueComment = true
  75. }
  76. if events.PullRequest {
  77. events.PullRequestAssign = true
  78. events.PullRequestLabel = true
  79. events.PullRequestMilestone = true
  80. events.PullRequestComment = true
  81. events.PullRequestReview = true
  82. events.PullRequestSync = true
  83. }
  84. if bytes, err = json.Marshal(&events); err != nil {
  85. return err
  86. }
  87. _, err = sess.Exec("UPDATE webhook SET events = ? WHERE id = ?", string(bytes), res.ID)
  88. if err != nil {
  89. return err
  90. }
  91. }
  92. if err := sess.Commit(); err != nil {
  93. return err
  94. }
  95. }
  96. return nil
  97. }