fix: add toNormalizedJson to match github output

This commit is contained in:
Derrick Hammer 2024-02-11 05:35:15 -05:00
parent 300b074f86
commit 865c639557
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 27 additions and 1 deletions

View File

@ -10,6 +10,8 @@ import (
"gorm.io/gorm"
"io"
"net/http"
"regexp"
"strings"
)
type WebhookManager struct {
@ -63,6 +65,8 @@ func (whm *WebhookManager) sendWebhooks(request interface{}, r *http.Request) {
}
payload := getWebhookData(r, whm.logger)
payload = toNormalizedJson(payload)
signature := generatePayloadSignature(payload, app.WebhookSecret)
req.Header.Add("X-Hub-Signature-256", signature)
@ -86,5 +90,27 @@ func (whm *WebhookManager) sendWebhooks(request interface{}, r *http.Request) {
}
}(app)
}
}
/*
* Ported from https://github.com/octokit/webhooks.js/blob/984f3f51e077dbc7637aa55406c3bb114dbb7f82/src/to-normalized-json-string.ts
*/
func toNormalizedJson(payload []byte) []byte {
jsonString := string(payload)
// Regex to find Unicode escape sequences
re := regexp.MustCompile(`\\u([\da-fA-F]{4})`)
// Function to convert found sequences to uppercase
replaceFunc := func(match string) string {
parts := strings.Split(match, "\\u")
// Convert the Unicode sequence part to uppercase
return parts[0] + "\\u" + strings.ToUpper(parts[1])
}
// Replace the matches in the jsonString
normalizedString := re.ReplaceAllStringFunc(jsonString, replaceFunc)
return []byte(normalizedString)
}