Add tests for webhook and fix some webhook bugs (#33396)
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 commit is contained in:
parent
256b94e9e9
commit
75940a0191
22 changed files with 630 additions and 89 deletions
|
@ -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"
|
||||
|
@ -920,10 +921,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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue