Skip to content

Add support for base64 encoded files #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ I hope it works`,
});
```

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:

```javascript
const commits = await octokit.rest.repos.createOrUpdateFiles({
owner,
repo,
branch,
createBranch,
changes: [
{
message: "Add Icon",
files: {
"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`
}
},
],
});
```

In addition, you can set the `mode` of a file change. For example, if you wanted to update a submodule pointer:

```javascript
Expand Down
9 changes: 8 additions & 1 deletion create-or-update-files.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const isBase64 = require("is-base64");
module.exports = function(octokit, opts) {
return new Promise(async (resolve, reject) => {
// Up front validation
Expand Down Expand Up @@ -247,11 +248,17 @@ async function createBlob(octokit, owner, repo, contents, type) {
if (type === "commit") {
return contents;
} else {
let content = contents;

if (!isBase64(content)) {
content = Buffer.from(contents).toString("base64");
}

const file = (
await octokit.rest.git.createBlob({
owner,
repo,
content: Buffer.from(contents).toString("base64"),
content,
encoding: "base64"
})
).data;
Expand Down
32 changes: 32 additions & 0 deletions create-or-update-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,31 @@ test(`success (branch exists)`, async () => {
await expect(run(body)).resolves.toEqual(mockCommitList);
});

test(`success (base64 encoded body)`, async () => {
const body = {
...validRequest,
changes: [
{
message: "Your commit message",
files: {
"test.md": "SGVsbG8gV29ybGQ=",
"test2.md": {
contents: `Something else`
}
}
}
]
};
mockGetRef(branch, `sha-${branch}`, true);
mockCreateBlobBase64PreEncoded();
mockCreateBlobFileTwo();
mockCreateTree(`sha-${branch}`);
mockCommit(`sha-${branch}`);
mockUpdateRef(branch);

await expect(run(body)).resolves.toEqual(mockCommitList);
});

test(`success (committer details)`, async () => {
const committer = {
name: "Ashley Person",
Expand Down Expand Up @@ -469,6 +494,13 @@ function mockCreateBlobFileFour() {
return mockCreateBlob("aGk=", "f65b65200aea4fecbe0db6ddac1c0848cdda1d9b");
}

function mockCreateBlobBase64PreEncoded() {
return mockCreateBlob(
"SGVsbG8gV29ybGQ=",
"afb296bb7f3e327767bdda481c4877ba4a09e02e"
);
}

function mockCreateTreeSubmodule(baseTree) {
const expectedBody = {
tree: [
Expand Down
17 changes: 16 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"prettier": "^1.19.1"
},
"dependencies": {
"is-base64": "^1.1.0",
"nock": "^11.7.0"
},
"peerDependencies": {
Expand Down