1
0
Fork 0
forked from forgejo/forgejo

[REFACTOR] webhook.Handler interface

This commit is contained in:
oliverpool 2024-03-20 15:44:01 +01:00
parent 142459bbe0
commit 702152bfde
35 changed files with 378 additions and 210 deletions

View file

@ -314,33 +314,41 @@ func getPackagePayloadInfo(p *api.PackagePayload, linkFormatter linkFormatter, w
// ToHook convert models.Webhook to api.Hook
// This function is not part of the convert package to prevent an import cycle
func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {
// config is deprecated, but kept for compatibility
config := map[string]string{
"url": w.URL,
"content_type": w.ContentType.Name(),
}
if w.Type == webhook_module.SLACK {
s := GetSlackHook(w)
config["channel"] = s.Channel
config["username"] = s.Username
config["icon_url"] = s.IconURL
config["color"] = s.Color
if s, ok := (slackHandler{}.Metadata(w)).(*SlackMeta); ok {
config["channel"] = s.Channel
config["username"] = s.Username
config["icon_url"] = s.IconURL
config["color"] = s.Color
}
}
authorizationHeader, err := w.HeaderAuthorization()
if err != nil {
return nil, err
}
var metadata any
if handler := GetWebhookHandler(w.Type); handler != nil {
metadata = handler.Metadata(w)
}
return &api.Hook{
ID: w.ID,
Type: w.Type,
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
Active: w.IsActive,
BranchFilter: w.BranchFilter,
URL: w.URL,
Config: config,
Events: w.EventsArray(),
AuthorizationHeader: authorizationHeader,
ContentType: w.ContentType.Name(),
Metadata: metadata,
Active: w.IsActive,
Updated: w.UpdatedUnix.AsTime(),
Created: w.CreatedUnix.AsTime(),
BranchFilter: w.BranchFilter,
}, nil
}