Skip to content

Commit e8a269d

Browse files
committed
🐛 Fix newline replacement not replacing CRLFs, fix req body empty when error
1 parent e2991f3 commit e8a269d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func upsertGitHubRepo(repo map[string]interface{}, mirrorConfigUsed MirrorDefini
250250
path := githubNameFromGitlabPath(repo["fullPath"].(string), mirrorConfigUsed)
251251

252252
description := fmt.Sprintf("%.250s (mirror)", repo["description"])
253+
description = strings.ReplaceAll(description, "\r\n", " ")
253254
description = strings.ReplaceAll(description, "\n", " ")
254255

255256
checkUrl := fmt.Sprintf("https://github.com/%s/%s", githubOrg, path)
@@ -261,6 +262,8 @@ func upsertGitHubRepo(repo map[string]interface{}, mirrorConfigUsed MirrorDefini
261262
client := &http.Client{}
262263
var req *http.Request
263264

265+
// For debugging when an error occurs
266+
var jsonBodyString string
264267
if resp.StatusCode == 200 {
265268
ll.Log("Updating", "cyan", "%s/%s", githubOrg, path)
266269
url := fmt.Sprintf("https://api.github.com/repos/%s/%s", githubOrg, path)
@@ -270,6 +273,7 @@ func upsertGitHubRepo(repo map[string]interface{}, mirrorConfigUsed MirrorDefini
270273
"homepage": repo["webUrl"],
271274
}
272275
jsonBody, _ := json.Marshal(reqBody)
276+
jsonBodyString = string(jsonBody)
273277

274278
req, err = http.NewRequest("PATCH", url, bytes.NewBuffer(jsonBody))
275279
if err != nil {
@@ -285,6 +289,7 @@ func upsertGitHubRepo(repo map[string]interface{}, mirrorConfigUsed MirrorDefini
285289
"homepage": repo["webUrl"],
286290
}
287291
jsonBody, _ := json.Marshal(reqBody)
292+
jsonBodyString = string(jsonBody)
288293

289294
req, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
290295
if err != nil {
@@ -305,8 +310,7 @@ func upsertGitHubRepo(repo map[string]interface{}, mirrorConfigUsed MirrorDefini
305310

306311
if resp.StatusCode >= 400 {
307312
respBody, _ := io.ReadAll(resp.Body)
308-
reqBody, _ := io.ReadAll(req.Body)
309-
return "", fmt.Errorf("GitHub API error: %s: %s: for request: %s", resp.Status, respBody, reqBody)
313+
return "", fmt.Errorf("GitHub API error: %s: %s: for request: %s", resp.Status, respBody, jsonBodyString)
310314
}
311315

312316
return path, nil

0 commit comments

Comments
 (0)