Browse Source
Granular webhook events (#9626)
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>mj
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 602 additions and 151 deletions
-
2models/migrations/migrations.go
-
101models/migrations/v130.go
-
168models/webhook.go
-
6models/webhook_test.go
-
33modules/auth/repo_form.go
-
143modules/notification/webhook/webhook.go
-
2modules/structs/hook.go
-
6modules/webhook/deliver.go
-
11modules/webhook/dingtalk.go
-
19modules/webhook/discord.go
-
2modules/webhook/feishu.go
-
15modules/webhook/msteams.go
-
11modules/webhook/slack.go
-
11modules/webhook/telegram.go
-
41options/locale/locale_en-US.ini
-
35routers/api/v1/utils/hook.go
-
27routers/repo/webhook.go
-
120templates/repo/settings/webhook/settings.tmpl
@ -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 |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue