[REFACTOR] add Icon to webhook.Interface

This commit is contained in:
oliverpool 2024-03-22 16:02:48 +01:00
parent bc04183e47
commit 120fa61a0a
15 changed files with 54 additions and 2 deletions

View file

@ -132,11 +132,13 @@ func WebhookNew(ctx *context.Context) {
}
hookType := ctx.Params(":type")
if webhook_service.GetWebhookHandler(hookType) == nil {
handler := webhook_service.GetWebhookHandler(hookType)
if handler == nil {
ctx.NotFound("GetWebhookHandler", nil)
return
}
ctx.Data["HookType"] = hookType
ctx.Data["WebhookHandler"] = handler
ctx.Data["BaseLink"] = orCtx.LinkNew
ctx.Data["BaseLinkNew"] = orCtx.LinkNew
@ -194,6 +196,7 @@ func WebhookCreate(ctx *context.Context) {
ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
ctx.Data["HookType"] = hookType
ctx.Data["WebhookHandler"] = handler
orCtx, err := getOwnerRepoCtx(ctx)
if err != nil {
@ -358,6 +361,7 @@ func checkWebhook(ctx *context.Context) (*ownerRepoCtx, *webhook.Webhook) {
if handler := webhook_service.GetWebhookHandler(w.Type); handler != nil {
ctx.Data["HookMetadata"] = handler.Metadata(w)
ctx.Data["WebhookHandler"] = handler
}
ctx.Data["History"], err = w.History(ctx, 1)

View file

@ -10,6 +10,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"html/template"
"io"
"net/http"
"net/url"
@ -17,6 +18,7 @@ import (
webhook_model "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/svg"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/forms"
)
@ -34,6 +36,14 @@ func (dh defaultHandler) Type() webhook_module.HookType {
return webhook_module.GITEA
}
func (dh defaultHandler) Icon(size int) template.HTML {
if dh.forgejo {
// forgejo.svg is not in web_src/svg/, so svg.RenderHTML does not work
return imgIcon("forgejo.svg", size)
}
return svg.RenderHTML("gitea-gitea", size, "img")
}
func (defaultHandler) Metadata(*webhook_model.Webhook) any { return nil }
func (defaultHandler) FormFields(bind func(any)) FormFields {

View file

@ -6,6 +6,7 @@ package webhook
import (
"context"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"
@ -22,6 +23,8 @@ type dingtalkHandler struct{}
func (dingtalkHandler) Type() webhook_module.HookType { return webhook_module.DINGTALK }
func (dingtalkHandler) Metadata(*webhook_model.Webhook) any { return nil }
func (dingtalkHandler) Icon(size int) template.HTML { return imgIcon("dingtalk.ico", size) }
func (dingtalkHandler) FormFields(bind func(any)) FormFields {
var form struct {
forms.WebhookForm

View file

@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"html/template"
"net/http"
"net/url"
"strconv"
@ -26,6 +27,7 @@ import (
type discordHandler struct{}
func (discordHandler) Type() webhook_module.HookType { return webhook_module.DISCORD }
func (discordHandler) Icon(size int) template.HTML { return imgIcon("discord.png", size) }
func (discordHandler) FormFields(bind func(any)) FormFields {
var form struct {

View file

@ -6,6 +6,7 @@ package webhook
import (
"context"
"fmt"
"html/template"
"net/http"
"strings"
@ -19,6 +20,7 @@ import (
type feishuHandler struct{}
func (feishuHandler) Type() webhook_module.HookType { return webhook_module.FEISHU }
func (feishuHandler) Icon(size int) template.HTML { return imgIcon("feishu.png", size) }
func (feishuHandler) FormFields(bind func(any)) FormFields {
var form struct {

View file

@ -6,7 +6,9 @@ package webhook
import (
"fmt"
"html"
"html/template"
"net/url"
"strconv"
"strings"
webhook_model "code.gitea.io/gitea/models/webhook"
@ -352,3 +354,9 @@ func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {
Created: w.CreatedUnix.AsTime(),
}, nil
}
func imgIcon(name string, size int) template.HTML {
s := strconv.Itoa(size)
src := html.EscapeString(setting.StaticURLPrefix + "/assets/img/" + name)
return template.HTML(`<img width="` + s + `" height="` + s + `" src="` + src + `">`)
}

View file

@ -4,6 +4,7 @@
package webhook
import (
"html/template"
"net/http"
webhook_model "code.gitea.io/gitea/models/webhook"
@ -14,6 +15,7 @@ import (
type gogsHandler struct{ defaultHandler }
func (gogsHandler) Type() webhook_module.HookType { return webhook_module.GOGS }
func (gogsHandler) Icon(size int) template.HTML { return imgIcon("gogs.ico", size) }
func (gogsHandler) FormFields(bind func(any)) FormFields {
var form struct {

View file

@ -9,6 +9,7 @@ import (
"crypto/sha1"
"encoding/hex"
"fmt"
"html/template"
"net/http"
"net/url"
"regexp"
@ -20,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/svg"
"code.gitea.io/gitea/modules/util"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/forms"
@ -29,6 +31,10 @@ type matrixHandler struct{}
func (matrixHandler) Type() webhook_module.HookType { return webhook_module.MATRIX }
func (matrixHandler) Icon(size int) template.HTML {
return svg.RenderHTML("gitea-matrix", size, "img")
}
func (matrixHandler) FormFields(bind func(any)) FormFields {
var form struct {
forms.WebhookForm

View file

@ -6,6 +6,7 @@ package webhook
import (
"context"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"
@ -22,6 +23,7 @@ type msteamsHandler struct{}
func (msteamsHandler) Type() webhook_module.HookType { return webhook_module.MSTEAMS }
func (msteamsHandler) Metadata(*webhook_model.Webhook) any { return nil }
func (msteamsHandler) Icon(size int) template.HTML { return imgIcon("msteams.png", size) }
func (msteamsHandler) FormFields(bind func(any)) FormFields {
var form struct {

View file

@ -6,6 +6,7 @@ package webhook
import (
"context"
"fmt"
"html/template"
"net/http"
"net/url"
@ -19,6 +20,7 @@ import (
type packagistHandler struct{}
func (packagistHandler) Type() webhook_module.HookType { return webhook_module.PACKAGIST }
func (packagistHandler) Icon(size int) template.HTML { return imgIcon("packagist.png", size) }
func (packagistHandler) FormFields(bind func(any)) FormFields {
var form struct {

View file

@ -6,6 +6,7 @@ package webhook
import (
"context"
"fmt"
"html/template"
"net/http"
"regexp"
"strings"
@ -26,6 +27,7 @@ import (
type slackHandler struct{}
func (slackHandler) Type() webhook_module.HookType { return webhook_module.SLACK }
func (slackHandler) Icon(size int) template.HTML { return imgIcon("slack.png", size) }
type slackForm struct {
forms.WebhookForm

View file

@ -6,6 +6,7 @@ package webhook
import (
"context"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"
@ -22,6 +23,7 @@ import (
type telegramHandler struct{}
func (telegramHandler) Type() webhook_module.HookType { return webhook_module.TELEGRAM }
func (telegramHandler) Icon(size int) template.HTML { return imgIcon("telegram.png", size) }
func (telegramHandler) FormFields(bind func(any)) FormFields {
var form struct {

View file

@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"html/template"
"net/http"
"strings"
@ -35,6 +36,7 @@ type Handler interface {
// 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)
Icon(size int) template.HTML
}
type FormFields struct {

View file

@ -6,6 +6,7 @@ package webhook
import (
"context"
"fmt"
"html/template"
"net/http"
"strings"
@ -21,6 +22,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) Icon(size int) template.HTML {
return imgIcon("wechatwork.png", size)
}
func (wechatworkHandler) FormFields(bind func(any)) FormFields {
var form struct {
forms.WebhookForm

View file

@ -2,7 +2,7 @@
{{.CustomHeaderTitle}}
<div class="ui right type dropdown">
<div class="text tw-flex tw-items-center">
{{template "shared/webhook/icon" (dict "Size" 20 "HookType" .ctxData.HookType)}}
{{.ctxData.WebhookHandler.Icon 20}}
{{ctx.Locale.Tr (print "repo.settings.web_hook_name_" .ctxData.HookType)}}
</div>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}