aboutsummaryrefslogtreecommitdiffstats
path: root/models/migrations/v130.go
diff options
context:
space:
mode:
authorJohn Olheiser <john.olheiser@gmail.com>2020-03-05 23:10:48 -0600
committerGitHub <noreply@github.com>2020-03-06 07:10:48 +0200
commit3f1c0841cb9fc61136b85c6b39613679d2851707 (patch)
treed0b15f3b809891d83f41fed5ab7378765330525a /models/migrations/v130.go
parent80db44267ccb688c596e8375523af5cd92864d87 (diff)
downloadgitea-3f1c0841cb9fc61136b85c6b39613679d2851707.tar.gz
gitea-3f1c0841cb9fc61136b85c6b39613679d2851707.zip
Granular webhook events (#9626)
* Initial work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add PR reviews and API coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Split up events Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add migration and locale Signed-off-by: jolheiser <john.olheiser@gmail.com> * Format Signed-off-by: jolheiser <john.olheiser@gmail.com> * Revert IsPull Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix comments Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix PR reviews Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix issue_comment Signed-off-by: jolheiser <john.olheiser@gmail.com> * Make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Migrations Signed-off-by: jolheiser <john.olheiser@gmail.com> * Backwards compatible API Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix feishu Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move session commit Signed-off-by: jolheiser <john.olheiser@gmail.com> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
Diffstat (limited to 'models/migrations/v130.go')
-rw-r--r--models/migrations/v130.go101
1 files changed, 101 insertions, 0 deletions
diff --git a/models/migrations/v130.go b/models/migrations/v130.go
new file mode 100644
index 0000000000..3a7efedf86
--- /dev/null
+++ b/models/migrations/v130.go
@@ -0,0 +1,101 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+ "encoding/json"
+
+ "xorm.io/xorm"
+)
+
+func expandWebhooks(x *xorm.Engine) error {
+
+ type ChooseEvents struct {
+ Issues bool `json:"issues"`
+ IssueAssign bool `json:"issue_assign"`
+ IssueLabel bool `json:"issue_label"`
+ IssueMilestone bool `json:"issue_milestone"`
+ IssueComment bool `json:"issue_comment"`
+ PullRequest bool `json:"pull_request"`
+ PullRequestAssign bool `json:"pull_request_assign"`
+ PullRequestLabel bool `json:"pull_request_label"`
+ PullRequestMilestone bool `json:"pull_request_milestone"`
+ PullRequestComment bool `json:"pull_request_comment"`
+ PullRequestReview bool `json:"pull_request_review"`
+ PullRequestSync bool `json:"pull_request_sync"`
+ }
+
+ type Events struct {
+ PushOnly bool `json:"push_only"`
+ SendEverything bool `json:"send_everything"`
+ ChooseEvents bool `json:"choose_events"`
+ BranchFilter string `json:"branch_filter"`
+ Events ChooseEvents `json:"events"`
+ }
+
+ type Webhook struct {
+ ID int64
+ Events string
+ }
+
+ var events Events
+ var bytes []byte
+ var last int
+ const batchSize = 50
+ sess := x.NewSession()
+ defer sess.Close()
+ for {
+ if err := sess.Begin(); err != nil {
+ return err
+ }
+ var results = make([]Webhook, 0, batchSize)
+ err := x.OrderBy("id").
+ Limit(batchSize, last).
+ Find(&results)
+ if err != nil {
+ return err
+ }
+ if len(results) == 0 {
+ break
+ }
+ last += len(results)
+
+ for _, res := range results {
+ if err = json.Unmarshal([]byte(res.Events), &events); err != nil {
+ return err
+ }
+
+ if events.Events.Issues {
+ events.Events.IssueAssign = true
+ events.Events.IssueLabel = true
+ events.Events.IssueMilestone = true
+ events.Events.IssueComment = true
+ }
+
+ if events.Events.PullRequest {
+ events.Events.PullRequestAssign = true
+ events.Events.PullRequestLabel = true
+ events.Events.PullRequestMilestone = true
+ events.Events.PullRequestComment = true
+ events.Events.PullRequestReview = true
+ events.Events.PullRequestSync = true
+ }
+
+ if bytes, err = json.Marshal(&events); err != nil {
+ return err
+ }
+
+ _, err = sess.Exec("UPDATE webhook SET events = ? WHERE id = ?", string(bytes), res.ID)
+ if err != nil {
+ return err
+ }
+ }
+
+ if err := sess.Commit(); err != nil {
+ return err
+ }
+ }
+ return nil
+}