mirror of
https://github.com/go-gitea/gitea
synced 2025-08-07 01:58:19 +00:00
This PR created a mock webhook server in the tests and added integration tests for generic webhooks. It also fixes bugs in package webhooks and pull request comment webhooks. This also corrected an error on the package webhook. The previous implementation uses a `User` struct as an organization, now it has been corrected but it will not be consistent with the previous implementation, some fields which not belong to the organization have been removed. Backport #33396 Backport part of #33337
This commit is contained in:
@@ -190,3 +190,7 @@ func newDingtalkRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_
|
||||
var pc payloadConvertor[DingtalkPayload] = dingtalkConvertor{}
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.DINGTALK, newDingtalkRequest)
|
||||
}
|
||||
|
@@ -277,6 +277,10 @@ func newDiscordRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_m
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.DISCORD, newDiscordRequest)
|
||||
}
|
||||
|
||||
func parseHookPullRequestEventType(event webhook_module.HookEventType) (string, error) {
|
||||
switch event {
|
||||
case webhook_module.HookEventPullRequestReviewApproved:
|
||||
|
@@ -170,3 +170,7 @@ func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_mo
|
||||
var pc payloadConvertor[FeishuPayload] = feishuConvertor{}
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.FEISHU, newFeishuRequest)
|
||||
}
|
||||
|
@@ -319,8 +319,8 @@ func packageTestPayload() *api.PackagePayload {
|
||||
AvatarURL: "http://localhost:3000/user1/avatar",
|
||||
},
|
||||
Repository: nil,
|
||||
Organization: &api.User{
|
||||
UserName: "org1",
|
||||
Organization: &api.Organization{
|
||||
Name: "org1",
|
||||
AvatarURL: "http://localhost:3000/org1/avatar",
|
||||
},
|
||||
Package: &api.Package{
|
||||
|
@@ -24,6 +24,10 @@ import (
|
||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.MATRIX, newMatrixRequest)
|
||||
}
|
||||
|
||||
func newMatrixRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
||||
meta := &MatrixMeta{}
|
||||
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
|
||||
|
@@ -349,3 +349,7 @@ func newMSTeamsRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_m
|
||||
var pc payloadConvertor[MSTeamsPayload] = msteamsConvertor{}
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.MSTEAMS, newMSTeamsRequest)
|
||||
}
|
||||
|
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
issues_model "code.gitea.io/gitea/models/issues"
|
||||
"code.gitea.io/gitea/models/organization"
|
||||
packages_model "code.gitea.io/gitea/models/packages"
|
||||
"code.gitea.io/gitea/models/perm"
|
||||
access_model "code.gitea.io/gitea/models/perm/access"
|
||||
@@ -924,10 +925,16 @@ func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_mo
|
||||
return
|
||||
}
|
||||
|
||||
var org *api.Organization
|
||||
if pd.Owner.IsOrganization() {
|
||||
org = convert.ToOrganization(ctx, organization.OrgFromUser(pd.Owner))
|
||||
}
|
||||
|
||||
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventPackage, &api.PackagePayload{
|
||||
Action: action,
|
||||
Package: apiPackage,
|
||||
Sender: convert.ToUser(ctx, sender, nil),
|
||||
Action: action,
|
||||
Package: apiPackage,
|
||||
Organization: org,
|
||||
Sender: convert.ToUser(ctx, sender, nil),
|
||||
}); err != nil {
|
||||
log.Error("PrepareWebhooks: %v", err)
|
||||
}
|
||||
|
@@ -120,3 +120,7 @@ func newPackagistRequest(_ context.Context, w *webhook_model.Webhook, t *webhook
|
||||
}
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.PACKAGIST, newPackagistRequest)
|
||||
}
|
||||
|
@@ -295,6 +295,10 @@ func newSlackRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_mod
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.SLACK, newSlackRequest)
|
||||
}
|
||||
|
||||
var slackChannel = regexp.MustCompile(`^#?[a-z0-9_-]{1,80}$`)
|
||||
|
||||
// IsValidSlackChannel validates a channel name conforms to what slack expects:
|
||||
|
@@ -187,3 +187,7 @@ func newTelegramRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_
|
||||
var pc payloadConvertor[TelegramPayload] = telegramConvertor{}
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.TELEGRAM, newTelegramRequest)
|
||||
}
|
||||
|
@@ -27,16 +27,12 @@ import (
|
||||
"github.com/gobwas/glob"
|
||||
)
|
||||
|
||||
var webhookRequesters = map[webhook_module.HookType]func(context.Context, *webhook_model.Webhook, *webhook_model.HookTask) (req *http.Request, body []byte, err error){
|
||||
webhook_module.SLACK: newSlackRequest,
|
||||
webhook_module.DISCORD: newDiscordRequest,
|
||||
webhook_module.DINGTALK: newDingtalkRequest,
|
||||
webhook_module.TELEGRAM: newTelegramRequest,
|
||||
webhook_module.MSTEAMS: newMSTeamsRequest,
|
||||
webhook_module.FEISHU: newFeishuRequest,
|
||||
webhook_module.MATRIX: newMatrixRequest,
|
||||
webhook_module.WECHATWORK: newWechatworkRequest,
|
||||
webhook_module.PACKAGIST: newPackagistRequest,
|
||||
type Requester func(context.Context, *webhook_model.Webhook, *webhook_model.HookTask) (req *http.Request, body []byte, err error)
|
||||
|
||||
var webhookRequesters = map[webhook_module.HookType]Requester{}
|
||||
|
||||
func RegisterWebhookRequester(hookType webhook_module.HookType, requester Requester) {
|
||||
webhookRequesters[hookType] = requester
|
||||
}
|
||||
|
||||
// IsValidHookTaskType returns true if a webhook registered
|
||||
|
@@ -179,3 +179,7 @@ func newWechatworkRequest(_ context.Context, w *webhook_model.Webhook, t *webhoo
|
||||
var pc payloadConvertor[WechatworkPayload] = wechatworkConvertor{}
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterWebhookRequester(webhook_module.WECHATWORK, newWechatworkRequest)
|
||||
}
|
||||
|
Reference in New Issue
Block a user