1
0
Fork 0
forked from forgejo/forgejo

[REFACTOR] webhook matrix endpoints

This commit is contained in:
oliverpool 2024-03-21 13:23:27 +01:00
parent e41e18f87e
commit 8dfbbfef07
16 changed files with 134 additions and 49 deletions

View file

@ -35,6 +35,10 @@ func (dh defaultHandler) Type() webhook_module.HookType {
func (defaultHandler) Metadata(*webhook_model.Webhook) any { return nil }
func (defaultHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
func (defaultHandler) NewRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (req *http.Request, body []byte, err error) {
switch w.HTTPMethod {
case "":

View file

@ -23,6 +23,9 @@ type dingtalkHandler struct{}
func (dingtalkHandler) Type() webhook_module.HookType { return webhook_module.DINGTALK }
func (dingtalkHandler) Metadata(*webhook_model.Webhook) any { return nil }
func (dingtalkHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
type (
// DingtalkPayload represents

View file

@ -26,6 +26,10 @@ type discordHandler struct{}
func (discordHandler) Type() webhook_module.HookType { return webhook_module.DISCORD }
func (discordHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
type (
// DiscordEmbedFooter for Embed Footer Structure.
DiscordEmbedFooter struct {

View file

@ -17,7 +17,12 @@ import (
type feishuHandler struct{}
func (feishuHandler) Type() webhook_module.HookType { return webhook_module.FEISHU }
func (feishuHandler) Type() webhook_module.HookType { return webhook_module.FEISHU }
func (feishuHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
func (feishuHandler) Metadata(*webhook_model.Webhook) any { return nil }
type (

View file

@ -22,12 +22,40 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/forms"
)
type matrixHandler struct{}
func (matrixHandler) Type() webhook_module.HookType { return webhook_module.MATRIX }
func (matrixHandler) FormFields(bind func(any)) FormFields {
var form struct {
forms.WebhookForm
HomeserverURL string `binding:"Required;ValidUrl"`
RoomID string `binding:"Required"`
MessageType int
// enforce requirement of authorization_header
// (value will still be set in the embedded WebhookForm)
AuthorizationHeader string `binding:"Required"`
}
bind(&form)
return FormFields{
WebhookForm: form.WebhookForm,
URL: fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, url.PathEscape(form.RoomID)),
ContentType: webhook_model.ContentTypeJSON,
Secret: "",
HTTPMethod: http.MethodPut,
Metadata: &MatrixMeta{
HomeserverURL: form.HomeserverURL,
Room: form.RoomID,
MessageType: form.MessageType,
},
}
}
func (matrixHandler) NewRequest(ctx 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 {

View file

@ -22,6 +22,10 @@ type msteamsHandler struct{}
func (msteamsHandler) Type() webhook_module.HookType { return webhook_module.MSTEAMS }
func (msteamsHandler) Metadata(*webhook_model.Webhook) any { return nil }
func (msteamsHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
type (
// MSTeamsFact for Fact Structure
MSTeamsFact struct {

View file

@ -18,6 +18,10 @@ type packagistHandler struct{}
func (packagistHandler) Type() webhook_module.HookType { return webhook_module.PACKAGIST }
func (packagistHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
type (
// PackagistPayload represents a packagist payload
// as expected by https://packagist.org/about

View file

@ -23,6 +23,10 @@ type slackHandler struct{}
func (slackHandler) Type() webhook_module.HookType { return webhook_module.SLACK }
func (slackHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
// SlackMeta contains the slack metadata
type SlackMeta struct {
Channel string `json:"channel"`

View file

@ -21,6 +21,10 @@ type telegramHandler struct{}
func (telegramHandler) Type() webhook_module.HookType { return webhook_module.TELEGRAM }
func (telegramHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
type (
// TelegramPayload represents
TelegramPayload struct {

View file

@ -23,14 +23,27 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/forms"
"github.com/gobwas/glob"
)
type Handler interface {
Type() webhook_module.HookType
NewRequest(context.Context, *webhook_model.Webhook, *webhook_model.HookTask) (req *http.Request, body []byte, err error)
Metadata(*webhook_model.Webhook) any
// FormFields provides a function to bind the request to the form.
// If form implements the [binding.Validator] interface, the Validate method will be called
FormFields(bind func(form any)) FormFields
NewRequest(context.Context, *webhook_model.Webhook, *webhook_model.HookTask) (req *http.Request, body []byte, err error)
}
type FormFields struct {
forms.WebhookForm
URL string
ContentType webhook_model.HookContentType
Secret string
HTTPMethod string
Metadata any
}
var webhookHandlers = []Handler{

View file

@ -20,6 +20,10 @@ type wechatworkHandler struct{}
func (wechatworkHandler) Type() webhook_module.HookType { return webhook_module.WECHATWORK }
func (wechatworkHandler) Metadata(*webhook_model.Webhook) any { return nil }
func (wechatworkHandler) FormFields(bind func(any)) FormFields {
panic("TODO")
}
type (
// WechatworkPayload represents
WechatworkPayload struct {