Skip to content

Commit

Permalink
feat: update files (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jun 16, 2020
1 parent 46c805a commit cc4b9af
Show file tree
Hide file tree
Showing 7 changed files with 1,089 additions and 29 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ https://github.com/settings/tokens/new?scopes=repo

## Todos

- **Editing files** based on current content
Add support to pass a function as file content, the function will be called with the current file content, if present.
- **Multiple commits**
Split up changes among multiple edits

Expand Down
80 changes: 56 additions & 24 deletions src/create-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Options = {

type Changes = {
files: {
[path: string]: string | File;
[path: string]: string | File | UpdateFunction;
};
commit: string;
};
Expand All @@ -26,6 +26,14 @@ type File = {
encoding: "utf-8" | "base64";
};

type UpdateFunctionFile = {
size: number;
encoding: "base64";
content: string;
};

type UpdateFunction = (file: UpdateFunctionFile) => string | File;

export async function octokitCreatePullRequest(
octokit: Octokit,
{ owner, repo, title, body, base, head, changes }: Options
Expand Down Expand Up @@ -113,31 +121,24 @@ export async function octokitCreatePullRequest(
}
}

// Text files can be changed through the .content key
if (typeof value === "string") {
return {
path,
mode: "100644",
content: value,
};
// When passed a function, retrieve the content of the file, pass it
// to the function, then return the result
if (typeof value === "function") {
const { data: file } = await octokit.request(
"GET /repos/:owner/:repo/contents/:path",
{
owner: fork,
repo,
ref: firstCommit.sha,
path,
}
);

const result = await value(file as UpdateFunctionFile);
return valueToTreeObject(octokit, owner, repo, path, result);
}

// Binary files need to be created first using the git blob API,
// then changed by referencing in the .sha key
const { data } = await octokit.request(
"POST /repos/:owner/:repo/git/blobs",
{
owner,
repo,
...value,
}
);
const blobSha = data.sha;
return {
path,
mode: "100644",
sha: blobSha,
};
return valueToTreeObject(octokit, owner, repo, path, value);
})
)
).filter(Boolean) as TreeParameter;
Expand Down Expand Up @@ -181,3 +182,34 @@ export async function octokitCreatePullRequest(
body,
});
}

async function valueToTreeObject(
octokit: Octokit,
owner: string,
repo: string,
path: string,
value: string | File
) {
// Text files can be changed through the .content key
if (typeof value === "string") {
return {
path,
mode: "100644",
content: value,
};
}

// Binary files need to be created first using the git blob API,
// then changed by referencing in the .sha key
const { data } = await octokit.request("POST /repos/:owner/:repo/git/blobs", {
owner,
repo,
...value,
});
const blobSha = data.sha;
return {
path,
mode: "100644",
sha: blobSha,
};
}
2 changes: 1 addition & 1 deletion test/create-binary-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RequestError } from "@octokit/request-error";
import { createPullRequest } from "../src";
const Octokit = Core.plugin(createPullRequest);

test("happy path", async () => {
test("create binary file", async () => {
const fixtures = require("./fixtures/create-binary-file");
const fixturePr = fixtures[fixtures.length - 1].response;
const octokit = new Octokit();
Expand Down
2 changes: 1 addition & 1 deletion test/delete-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RequestError } from "@octokit/request-error";
import { createPullRequest } from "../src";
const Octokit = Core.plugin(createPullRequest);

test("happy path", async () => {
test("delete files", async () => {
const fixtures = require("./fixtures/delete-files");
const fixturePr = fixtures[fixtures.length - 1].response;
const octokit = new Octokit();
Expand Down

0 comments on commit cc4b9af

Please sign in to comment.