mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Add an abstract json layout to make it's easier to change json library (#16528)
* Add an abstract json layout to make it's easier to change json library * Fix import * Fix import sequence * Fix blank lines * Fix blank lines
This commit is contained in:
		@@ -10,9 +10,9 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/json"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
 | 
			
		||||
	jsoniter "github.com/json-iterator/go"
 | 
			
		||||
	dingtalk "github.com/lunny/dingtalk_webhook"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -27,7 +27,6 @@ var (
 | 
			
		||||
 | 
			
		||||
// JSONPayload Marshals the DingtalkPayload to json
 | 
			
		||||
func (d *DingtalkPayload) JSONPayload() ([]byte, error) {
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	data, err := json.MarshalIndent(d, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
 
 | 
			
		||||
@@ -12,10 +12,10 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/json"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
	jsoniter "github.com/json-iterator/go"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type (
 | 
			
		||||
@@ -68,7 +68,6 @@ type (
 | 
			
		||||
// GetDiscordHook returns discord metadata
 | 
			
		||||
func GetDiscordHook(w *models.Webhook) *DiscordMeta {
 | 
			
		||||
	s := &DiscordMeta{}
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
 | 
			
		||||
		log.Error("webhook.GetDiscordHook(%d): %v", w.ID, err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -99,7 +98,6 @@ var (
 | 
			
		||||
 | 
			
		||||
// JSONPayload Marshals the DiscordPayload to json
 | 
			
		||||
func (d *DiscordPayload) JSONPayload() ([]byte, error) {
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	data, err := json.MarshalIndent(d, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
@@ -250,7 +248,6 @@ func GetDiscordPayload(p api.Payloader, event models.HookEventType, meta string)
 | 
			
		||||
	s := new(DiscordPayload)
 | 
			
		||||
 | 
			
		||||
	discord := &DiscordMeta{}
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	if err := json.Unmarshal([]byte(meta), &discord); err != nil {
 | 
			
		||||
		return s, errors.New("GetDiscordPayload meta json:" + err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,8 +10,8 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/json"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
	jsoniter "github.com/json-iterator/go"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type (
 | 
			
		||||
@@ -37,7 +37,6 @@ func newFeishuTextPayload(text string) *FeishuPayload {
 | 
			
		||||
 | 
			
		||||
// JSONPayload Marshals the FeishuPayload to json
 | 
			
		||||
func (f *FeishuPayload) JSONPayload() ([]byte, error) {
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	data, err := json.MarshalIndent(f, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
 
 | 
			
		||||
@@ -15,10 +15,10 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/json"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
	jsoniter "github.com/json-iterator/go"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const matrixPayloadSizeLimit = 1024 * 64
 | 
			
		||||
@@ -39,7 +39,6 @@ var messageTypeText = map[int]string{
 | 
			
		||||
// GetMatrixHook returns Matrix metadata
 | 
			
		||||
func GetMatrixHook(w *models.Webhook) *MatrixMeta {
 | 
			
		||||
	s := &MatrixMeta{}
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
 | 
			
		||||
		log.Error("webhook.GetMatrixHook(%d): %v", w.ID, err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -78,7 +77,6 @@ type MatrixPayloadSafe struct {
 | 
			
		||||
 | 
			
		||||
// JSONPayload Marshals the MatrixPayloadUnsafe to json
 | 
			
		||||
func (m *MatrixPayloadUnsafe) JSONPayload() ([]byte, error) {
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	data, err := json.MarshalIndent(m, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
@@ -228,7 +226,6 @@ func GetMatrixPayload(p api.Payloader, event models.HookEventType, meta string)
 | 
			
		||||
	s := new(MatrixPayloadUnsafe)
 | 
			
		||||
 | 
			
		||||
	matrix := &MatrixMeta{}
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	if err := json.Unmarshal([]byte(meta), &matrix); err != nil {
 | 
			
		||||
		return s, errors.New("GetMatrixPayload meta json:" + err.Error())
 | 
			
		||||
	}
 | 
			
		||||
@@ -262,7 +259,6 @@ func getMessageBody(htmlText string) string {
 | 
			
		||||
// The access_token is removed from t.PayloadContent
 | 
			
		||||
func getMatrixHookRequest(w *models.Webhook, t *models.HookTask) (*http.Request, error) {
 | 
			
		||||
	payloadunsafe := MatrixPayloadUnsafe{}
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	if err := json.Unmarshal([]byte(t.PayloadContent), &payloadunsafe); err != nil {
 | 
			
		||||
		log.Error("Matrix Hook delivery failed: %v", err)
 | 
			
		||||
		return nil, err
 | 
			
		||||
 
 | 
			
		||||
@@ -10,8 +10,8 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/json"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
	jsoniter "github.com/json-iterator/go"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type (
 | 
			
		||||
@@ -57,7 +57,6 @@ type (
 | 
			
		||||
 | 
			
		||||
// JSONPayload Marshals the MSTeamsPayload to json
 | 
			
		||||
func (m *MSTeamsPayload) JSONPayload() ([]byte, error) {
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	data, err := json.MarshalIndent(m, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
 
 | 
			
		||||
@@ -11,10 +11,10 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/json"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
	jsoniter "github.com/json-iterator/go"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// SlackMeta contains the slack metadata
 | 
			
		||||
@@ -28,7 +28,6 @@ type SlackMeta struct {
 | 
			
		||||
// GetSlackHook returns slack metadata
 | 
			
		||||
func GetSlackHook(w *models.Webhook) *SlackMeta {
 | 
			
		||||
	s := &SlackMeta{}
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
 | 
			
		||||
		log.Error("webhook.GetSlackHook(%d): %v", w.ID, err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -58,7 +57,6 @@ type SlackAttachment struct {
 | 
			
		||||
 | 
			
		||||
// JSONPayload Marshals the SlackPayload to json
 | 
			
		||||
func (s *SlackPayload) JSONPayload() ([]byte, error) {
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	data, err := json.MarshalIndent(s, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
@@ -279,7 +277,6 @@ func GetSlackPayload(p api.Payloader, event models.HookEventType, meta string) (
 | 
			
		||||
	s := new(SlackPayload)
 | 
			
		||||
 | 
			
		||||
	slack := &SlackMeta{}
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	if err := json.Unmarshal([]byte(meta), &slack); err != nil {
 | 
			
		||||
		return s, errors.New("GetSlackPayload meta json:" + err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,10 +10,10 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/json"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/markup"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
	jsoniter "github.com/json-iterator/go"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type (
 | 
			
		||||
@@ -34,7 +34,6 @@ type (
 | 
			
		||||
// GetTelegramHook returns telegram metadata
 | 
			
		||||
func GetTelegramHook(w *models.Webhook) *TelegramMeta {
 | 
			
		||||
	s := &TelegramMeta{}
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
 | 
			
		||||
		log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -50,7 +49,6 @@ func (t *TelegramPayload) JSONPayload() ([]byte, error) {
 | 
			
		||||
	t.ParseMode = "HTML"
 | 
			
		||||
	t.DisableWebPreview = true
 | 
			
		||||
	t.Message = markup.Sanitize(t.Message)
 | 
			
		||||
	json := jsoniter.ConfigCompatibleWithStandardLibrary
 | 
			
		||||
	data, err := json.MarshalIndent(t, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return []byte{}, err
 | 
			
		||||
 
 | 
			
		||||
@@ -5,12 +5,12 @@
 | 
			
		||||
package webhook
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/json"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user