Skip to content

Commit 9c0cfef

Browse files
committed
Add support for base64 encoded files
1 parent b897eb4 commit 9c0cfef

5 files changed

+76
-2
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ I hope it works`,
4949
});
5050
```
5151

52+
If you want to upload non-text data, you can `base64`` encode the content and provide that as the value. Here's an example that would upload a small GitHub icon to a repository:
53+
54+
```javascript
55+
const commits = await octokit.rest.repos.createOrUpdateFiles({
56+
owner,
57+
repo,
58+
branch,
59+
createBranch,
60+
changes: [
61+
{
62+
message: "Add Icon",
63+
files: {
64+
"icon.png": `iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADFUlEQVR42u2WXyjzYRTHvz/N3zZ/5sJyoWEUEleKrYbcaEXKXCBWVsrFkii3LrVwo6Sslj/hhqS4cecOtcWFtUbI34RlNn83786p1fu+F+9+a/Ou982pp56e3+l3Ps855/k+j6BWqwMABMTBPoMmBAE+4xE8ZN8A3wCiAYqKinB8fAy/3/9Hv6ysLCQnJ+P6+jp2AEqlEvPz87i/v8fc3By2t7dRUFCAzMxM/k7rNDo7O1FbWwu73Q6TyRQ7gOrqapjNZtFpvby8RFtbW+wAVCoVrFaraICTkxPORswAhoaG0NzcLBqAbGxsDKurq9EDyGQyrK+vQyKRRARwdnaG9vb26AHKy8sxNTXF8/39fSwsLODx8RGpqalIS0sjPYfP58Pz8zOys7NhMBj4xNB6XV0dPj4+ogMoKyvD9PQ0bm5u0NHRgZaWFg5ssVh+8aOaJyUlYWVlBUtLS5BKpXwiogaQy+VYW1vDxsYGxsfHUV9fj6urK9ze3uLi4oJ9UlJSoNVqcXd3x6kfHBzkzDU2NkZeAhKShIQETiH9mHY7MjKC8/NzDA8Pw9DdDc/TEzY3Nznt5CcIArq6urgU1C8TExO8To1IPjTIh9ZIL8ICkDMNr9eLl5cX3g39rK+vj1NKYA6HgwXq7e2Nz31+fj6X4PX1lcvT39+Pg4MDVkUqBwWn8fDwEFkJEhMT8f7+jtLSUhiNRjidTuzu7rLUymTSIJAfHo+HS1VRUYGamhrMzs5ib28vbPpFAVDNqRFnZmY4lQqFAg0NDejt7eUskdHOSKi2trZwenoqKrBoALKenh4EAgEsLi7yZTQ6OoqqqiqeU3DSiJ2dHQwMDEQUXDRAeno6JicnuSGpLwoLC2Gz2ZCTk8NZcbvdyM3NhV6v/xqAEASda2oq6niqcUlJCQMcHR2hsrISra2tXwfwsy0vL+Pw8BDFxcXc9X8VgBqPhIlqTgB0QlwuFzQaDZqamsI+WKIG0Ol0fOYzMjJY+UgH8vLyWKpJDwjuSwFCRi8ieqL9Po/U/p1H6TfA/wsQvL0CQuhWiYP9AJQGkyweNFh0AAAAAElFTkSuQmCC`
65+
}
66+
},
67+
],
68+
});
69+
```
70+
5271
In addition, you can set the `mode` of a file change. For example, if you wanted to update a submodule pointer:
5372

5473
```javascript

create-or-update-files.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const isBase64 = require("is-base64");
12
module.exports = function(octokit, opts) {
23
return new Promise(async (resolve, reject) => {
34
// Up front validation
@@ -247,11 +248,17 @@ async function createBlob(octokit, owner, repo, contents, type) {
247248
if (type === "commit") {
248249
return contents;
249250
} else {
251+
let content = contents;
252+
253+
if (!isBase64(content)) {
254+
content = Buffer.from(contents).toString("base64");
255+
}
256+
250257
const file = (
251258
await octokit.rest.git.createBlob({
252259
owner,
253260
repo,
254-
content: Buffer.from(contents).toString("base64"),
261+
content,
255262
encoding: "base64"
256263
})
257264
).data;

create-or-update-files.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,31 @@ test(`success (branch exists)`, async () => {
189189
await expect(run(body)).resolves.toEqual(mockCommitList);
190190
});
191191

192+
test(`success (base64 encoded body)`, async () => {
193+
const body = {
194+
...validRequest,
195+
changes: [
196+
{
197+
message: "Your commit message",
198+
files: {
199+
"test.md": "SGVsbG8gV29ybGQ=",
200+
"test2.md": {
201+
contents: `Something else`
202+
}
203+
}
204+
}
205+
]
206+
};
207+
mockGetRef(branch, `sha-${branch}`, true);
208+
mockCreateBlobBase64PreEncoded();
209+
mockCreateBlobFileTwo();
210+
mockCreateTree(`sha-${branch}`);
211+
mockCommit(`sha-${branch}`);
212+
mockUpdateRef(branch);
213+
214+
await expect(run(body)).resolves.toEqual(mockCommitList);
215+
});
216+
192217
test(`success (committer details)`, async () => {
193218
const committer = {
194219
name: "Ashley Person",
@@ -469,6 +494,13 @@ function mockCreateBlobFileFour() {
469494
return mockCreateBlob("aGk=", "f65b65200aea4fecbe0db6ddac1c0848cdda1d9b");
470495
}
471496

497+
function mockCreateBlobBase64PreEncoded() {
498+
return mockCreateBlob(
499+
"SGVsbG8gV29ybGQ=",
500+
"afb296bb7f3e327767bdda481c4877ba4a09e02e"
501+
);
502+
}
503+
472504
function mockCreateTreeSubmodule(baseTree) {
473505
const expectedBody = {
474506
tree: [

package-lock.json

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"prettier": "^1.19.1"
2525
},
2626
"dependencies": {
27+
"is-base64": "^1.1.0",
2728
"nock": "^11.7.0"
2829
},
2930
"peerDependencies": {

0 commit comments

Comments
 (0)