Skip to content

Commit 445e847

Browse files
rmartine-iasgr2m
andauthored
fix: avoid creating blops for UTF-8 text files (#133)
Co-authored-by: Gregor Martynus <[email protected]>
1 parent 0295fd0 commit 445e847

File tree

2 files changed

+15
-66
lines changed

2 files changed

+15
-66
lines changed

src/value-to-tree-object.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,29 @@ export async function valueToTreeObject(
88
path: string,
99
value: string | File
1010
) {
11-
let mode = "100644";
12-
if (value !== null && typeof value !== "string") {
13-
mode = value.mode || mode;
14-
}
11+
const defaultMode = "100644";
1512

1613
// Text files can be changed through the .content key
1714
if (typeof value === "string") {
1815
return {
1916
path,
20-
mode: mode,
17+
mode: defaultMode,
2118
content: value,
2219
};
2320
}
2421

22+
const mode = value.mode ?? defaultMode;
23+
24+
// UTF-8 files can be treated as text files
25+
// https://github.com/gr2m/octokit-plugin-create-pull-request/pull/133
26+
if (value.encoding === "utf-8") {
27+
return {
28+
path,
29+
mode: mode,
30+
content: value.content,
31+
};
32+
}
33+
2534
// Binary files need to be created first using the git blob API,
2635
// then changed by referencing in the .sha key
2736
const { data } = await octokit.request(

test/fixtures/happy-path-with-mode.json

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -306,66 +306,6 @@
306306
]
307307
}
308308
},
309-
{
310-
"request": {
311-
"method": "POST",
312-
"baseUrl": "https://api.github.com",
313-
"headers": {
314-
"accept": "application/vnd.github.v3+json",
315-
"user-agent": "octokit-core.js/3.2.5 Node.js/18.3.0 (darwin; arm64)"
316-
},
317-
"mediaType": {
318-
"format": "",
319-
"previews": []
320-
},
321-
"request": {},
322-
"url": "/repos/{owner}/{repo}/git/blobs",
323-
"owner": "gr2m",
324-
"repo": "pull-request-test",
325-
"content": "echo Hello World",
326-
"encoding": "utf-8",
327-
"mode": "100755"
328-
},
329-
"response": {
330-
"status": 201,
331-
"url": "https://api.github.com/repos/gr2m/pull-request-test/git/blobs",
332-
"headers": {
333-
"access-control-allow-origin": "*",
334-
"access-control-expose-headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
335-
"cache-control": "private, max-age=60, s-maxage=60",
336-
"connection": "close",
337-
"content-length": "161",
338-
"content-security-policy": "default-src 'none'",
339-
"content-type": "application/json; charset=utf-8",
340-
"date": "Tue, 28 Jun 2022 00:21:09 GMT",
341-
"etag": "\"8766a6d276b4ae70c5318400840f7c1f1951b5243b0d6d29a0104281c29f9077\"",
342-
"github-authentication-token-expiration": "2022-08-24 23:46:54 UTC",
343-
"location": "https://api.github.com/repos/gr2m/pull-request-test/git/blobs/c346b86cea27ec508d99b869b35d45b5edbd53f0",
344-
"referrer-policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
345-
"server": "GitHub.com",
346-
"strict-transport-security": "max-age=31536000; includeSubdomains; preload",
347-
"vary": "Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With",
348-
"x-accepted-oauth-scopes": "",
349-
"x-content-type-options": "nosniff",
350-
"x-frame-options": "deny",
351-
"x-github-api-version-selected": "",
352-
"x-github-api-version-selected-reason": "unavailable",
353-
"x-github-media-type": "github.v3; format=json",
354-
"x-github-request-id": "F8BE:2BA5:2E5128:398B6D:62BA4975",
355-
"x-oauth-scopes": "notifications, repo, workflow, write:org",
356-
"x-ratelimit-limit": "5000",
357-
"x-ratelimit-remaining": "4948",
358-
"x-ratelimit-reset": "1656378677",
359-
"x-ratelimit-resource": "core",
360-
"x-ratelimit-used": "52",
361-
"x-xss-protection": "0"
362-
},
363-
"data": {
364-
"sha": "c346b86cea27ec508d99b869b35d45b5edbd53f0",
365-
"url": "https://api.github.com/repos/gr2m/pull-request-test/git/blobs/c346b86cea27ec508d99b869b35d45b5edbd53f0"
366-
}
367-
}
368-
},
369309
{
370310
"request": {
371311
"method": "POST",
@@ -392,7 +332,7 @@
392332
{
393333
"path": "path/to/file2.sh",
394334
"mode": "100755",
395-
"sha": "c346b86cea27ec508d99b869b35d45b5edbd53f0"
335+
"content": "echo Hello World"
396336
}
397337
]
398338
},

0 commit comments

Comments
 (0)