diff options
author | zeripath <art27@cantab.net> | 2021-03-01 21:08:10 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-01 22:08:10 +0100 |
commit | f0e15250b9e322cc7731ba026d12387c2b549a42 (patch) | |
tree | f13d46119077ba924d620ef172b91daa315bda0a /services/webhook | |
parent | 59fd641d1fb021e35aea7f9f4a1916cc11ef5c51 (diff) | |
download | gitea-f0e15250b9e322cc7731ba026d12387c2b549a42.tar.gz gitea-f0e15250b9e322cc7731ba026d12387c2b549a42.zip |
Migrate to use jsoniter instead of encoding/json (#14841)
* Migrate to use jsoniter
* fix tests
* update gitea.com/go-chi/binding
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
Diffstat (limited to 'services/webhook')
-rw-r--r-- | services/webhook/dingtalk.go | 3 | ||||
-rw-r--r-- | services/webhook/discord.go | 5 | ||||
-rw-r--r-- | services/webhook/feishu.go | 3 | ||||
-rw-r--r-- | services/webhook/matrix.go | 6 | ||||
-rw-r--r-- | services/webhook/msteams.go | 3 | ||||
-rw-r--r-- | services/webhook/slack.go | 5 | ||||
-rw-r--r-- | services/webhook/telegram.go | 4 |
7 files changed, 22 insertions, 7 deletions
diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index a9032db046..0401464a44 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -5,7 +5,6 @@ package webhook import ( - "encoding/json" "fmt" "strings" @@ -13,6 +12,7 @@ import ( "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" + jsoniter "github.com/json-iterator/go" dingtalk "github.com/lunny/dingtalk_webhook" ) @@ -30,6 +30,7 @@ func (d *DingtalkPayload) SetSecret(_ string) {} // 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 diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 530e7adbda..d28904715f 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -5,7 +5,6 @@ package webhook import ( - "encoding/json" "errors" "fmt" "strconv" @@ -16,6 +15,7 @@ import ( "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,6 +68,7 @@ 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) } @@ -101,6 +102,7 @@ func (d *DiscordPayload) SetSecret(_ string) {} // 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 @@ -407,6 +409,7 @@ 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()) } diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 3d789e6c1f..847a991f36 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -5,13 +5,13 @@ package webhook import ( - "encoding/json" "fmt" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" + jsoniter "github.com/json-iterator/go" ) type ( @@ -40,6 +40,7 @@ func (f *FeishuPayload) SetSecret(_ string) {} // 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 diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index 063147198a..1658dd4b44 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -6,7 +6,6 @@ package webhook import ( "crypto/sha1" - "encoding/json" "errors" "fmt" "html" @@ -19,6 +18,7 @@ import ( "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,6 +39,7 @@ 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) } @@ -80,6 +81,7 @@ func (m *MatrixPayloadUnsafe) SetSecret(_ string) {} // 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 @@ -229,6 +231,7 @@ 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,6 +265,7 @@ func getMessageBody(htmlText string) string { // The access_token is removed from t.PayloadContent func getMatrixHookRequest(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 diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index a68c97ea37..dc83a47c8d 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -5,13 +5,13 @@ package webhook import ( - "encoding/json" "fmt" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" + jsoniter "github.com/json-iterator/go" ) type ( @@ -60,6 +60,7 @@ func (m *MSTeamsPayload) SetSecret(_ string) {} // 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 diff --git a/services/webhook/slack.go b/services/webhook/slack.go index aaecad6c67..f5c857f2a9 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -5,7 +5,6 @@ package webhook import ( - "encoding/json" "errors" "fmt" "strings" @@ -15,6 +14,7 @@ import ( "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,6 +28,7 @@ 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) } @@ -60,6 +61,7 @@ func (s *SlackPayload) SetSecret(_ string) {} // 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 @@ -320,6 +322,7 @@ 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()) } diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 84fc210042..5b78b46f8e 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -5,7 +5,6 @@ package webhook import ( - "encoding/json" "fmt" "strings" @@ -14,6 +13,7 @@ import ( "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,6 +34,7 @@ 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) } @@ -52,6 +53,7 @@ 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 |