Skip to content

Commit 9cad8cb

Browse files
committed
fix
1 parent 07afc37 commit 9cad8cb

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

modules/assetfs/embed.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,11 @@ func GenerateEmbedBindata(fsRootPath, outputFile string) error {
365365
if err = embedFiles(meta.Root, fsRootPath, ""); err != nil {
366366
return err
367367
}
368-
jsonBuf, err := json.Marshal(meta) // can't use json.NewEncoder here because it writes extra EOL
368+
jsonBuf, err := json.Marshal(meta)
369369
if err != nil {
370370
return err
371371
}
372372
_, _ = output.Write([]byte{'\n'})
373-
_, err = output.Write(jsonBuf)
373+
_, err = output.Write(bytes.TrimSpace(jsonBuf))
374374
return err
375375
}

modules/json/jsonv2.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package json
77

88
import (
99
"bytes"
10+
jsonv1 "encoding/json" //nolint:depguard // this package wraps it
1011
jsonv2 "encoding/json/v2" //nolint:depguard // this package wraps it
1112
"io"
1213
)
@@ -41,11 +42,6 @@ func (JSONv2) Marshal(v any) ([]byte, error) {
4142
}
4243

4344
func (JSONv2) Unmarshal(data []byte, v any) error {
44-
// legacy behavior: treat empty or whitespace-only input as no input, it should be safe
45-
data = bytes.TrimSpace(data)
46-
if len(data) == 0 {
47-
return nil
48-
}
4945
return jsonv2.Unmarshal(data, v, jsonv2DefaultUnmarshalOptions())
5046
}
5147

@@ -59,7 +55,7 @@ func (JSONv2) NewDecoder(reader io.Reader) Decoder {
5955

6056
// Indent implements Interface using standard library (JSON v2 doesn't have Indent yet)
6157
func (JSONv2) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
62-
return json.Indent(dst, src, prefix, indent)
58+
return jsonv1.Indent(dst, src, prefix, indent)
6359
}
6460

6561
type encoderV2 struct {

modules/lfs/http_client_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestHTTPClientDownload(t *testing.T) {
193193
},
194194
{
195195
endpoint: "https://invalid-json-response.io",
196-
expectedError: "invalid json",
196+
expectedError: "/(invalid json|jsontext: invalid character)/",
197197
},
198198
{
199199
endpoint: "https://valid-batch-request-download.io",
@@ -258,7 +258,11 @@ func TestHTTPClientDownload(t *testing.T) {
258258
return nil
259259
})
260260
if c.expectedError != "" {
261-
assert.ErrorContains(t, err, c.expectedError)
261+
if strings.HasPrefix(c.expectedError, "/") && strings.HasSuffix(c.expectedError, "/") {
262+
assert.Regexp(t, strings.Trim(c.expectedError, "/"), err.Error())
263+
} else {
264+
assert.ErrorContains(t, err, c.expectedError)
265+
}
262266
} else {
263267
assert.NoError(t, err)
264268
}
@@ -297,7 +301,7 @@ func TestHTTPClientUpload(t *testing.T) {
297301
},
298302
{
299303
endpoint: "https://invalid-json-response.io",
300-
expectedError: "invalid json",
304+
expectedError: "/(invalid json|jsontext: invalid character)/",
301305
},
302306
{
303307
endpoint: "https://valid-batch-request-upload.io",
@@ -352,7 +356,11 @@ func TestHTTPClientUpload(t *testing.T) {
352356
return io.NopCloser(new(bytes.Buffer)), objectError
353357
})
354358
if c.expectedError != "" {
355-
assert.ErrorContains(t, err, c.expectedError)
359+
if strings.HasPrefix(c.expectedError, "/") && strings.HasSuffix(c.expectedError, "/") {
360+
assert.Regexp(t, strings.Trim(c.expectedError, "/"), err.Error())
361+
} else {
362+
assert.ErrorContains(t, err, c.expectedError)
363+
}
356364
} else {
357365
assert.NoError(t, err)
358366
}

services/webhook/deliver_test.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,54 @@ func TestWebhookDeliverHookTask(t *testing.T) {
131131
assert.NoError(t, unittest.PrepareTestDatabase())
132132

133133
done := make(chan struct{}, 1)
134+
version2Body := `{
135+
"body": "[[test/repo](http://localhost:3000/test/repo)] user1 pushed 2 commits to [test](http://localhost:3000/test/repo/src/branch/test):\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1",
136+
"msgtype": "",
137+
"format": "org.matrix.custom.html",
138+
"formatted_body": "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] user1 pushed 2 commits to <a href=\"http://localhost:3000/test/repo/src/branch/test\">test</a>:<br><a href=\"http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778\">2020558</a>: commit message - user1<br><a href=\"http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778\">2020558</a>: commit message - user1",
139+
"io.gitea.commits": [
140+
{
141+
"id": "2020558fe2e34debb818a514715839cabd25e778",
142+
"message": "commit message",
143+
"url": "http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778",
144+
"author": {
145+
"name": "user1",
146+
"email": "user1@localhost",
147+
"username": "user1"
148+
},
149+
"committer": {
150+
"name": "user1",
151+
"email": "user1@localhost",
152+
"username": "user1"
153+
},
154+
"verification": null,
155+
"timestamp": "0001-01-01T00:00:00Z",
156+
"added": null,
157+
"removed": null,
158+
"modified": null
159+
},
160+
{
161+
"id": "2020558fe2e34debb818a514715839cabd25e778",
162+
"message": "commit message",
163+
"url": "http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778",
164+
"author": {
165+
"name": "user1",
166+
"email": "user1@localhost",
167+
"username": "user1"
168+
},
169+
"committer": {
170+
"name": "user1",
171+
"email": "user1@localhost",
172+
"username": "user1"
173+
},
174+
"verification": null,
175+
"timestamp": "0001-01-01T00:00:00Z",
176+
"added": null,
177+
"removed": null,
178+
"modified": null
179+
}
180+
]
181+
}`
134182
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
135183
assert.Equal(t, "PUT", r.Method)
136184
switch r.URL.Path {
@@ -142,13 +190,13 @@ func TestWebhookDeliverHookTask(t *testing.T) {
142190
assert.NoError(t, err)
143191
assert.Equal(t, `{"data": 42}`, string(body))
144192

145-
case "/webhook/6db5dc1e282529a8c162c7fe93dd2667494eeb51":
193+
case "/webhook/4ddf3b1533e54f082ae6eadfc1b5530be36c8893":
146194
// Version 2
147195
assert.Equal(t, "push", r.Header.Get("X-GitHub-Event"))
148196
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
149197
body, err := io.ReadAll(r.Body)
150198
assert.NoError(t, err)
151-
assert.Len(t, body, 2147)
199+
assert.JSONEq(t, version2Body, string(body))
152200

153201
default:
154202
w.WriteHeader(http.StatusNotFound)

services/webhook/matrix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ func getMessageBody(htmlText string) string {
274274

275275
// getMatrixTxnID computes the transaction ID to ensure idempotency
276276
func getMatrixTxnID(payload []byte) (string, error) {
277+
payload = bytes.TrimSpace(payload)
277278
if len(payload) >= matrixPayloadSizeLimit {
278279
return "", fmt.Errorf("getMatrixTxnID: payload size %d > %d", len(payload), matrixPayloadSizeLimit)
279280
}

0 commit comments

Comments
 (0)