Skip to content

Commit

Permalink
feat: delete files by setting content to null
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Oct 6, 2019
1 parent fc5c420 commit 577652c
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions lib/create-pull-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,49 @@ async function createPullRequest(
});
let latestCommitSha = response.data[0].sha;
const treeSha = response.data[0].commit.tree.sha;
const tree = (await Promise.all(
Object.keys(changes.files).map(async path => {
if (changes.files[path] === null) {
// Deleting a non-existent file from a tree leads to an "GitRPC::BadObjectState" error
try {
if (path === "path/to/file-does-not-exist.txt") {
debugger;
}
const response = await octokit.request(
"HEAD /repos/:owner/:repo/contents/:path",
{
owner: fork,
repo,
ref: latestCommitSha,
path
}
);

return {
path,
mode: "100644",
sha: null
};
} catch (error) {
return;
}
}

response = await octokit.git.createTree({
owner: fork,
repo,
base_tree: treeSha,
tree: Object.keys(changes.files).map(path => {
return {
path,
mode: "100644",
content: changes.files[path]
};
})
)).filter(Boolean);

response = await octokit.request("POST /repos/:owner/:repo/git/trees", {
owner: fork,
repo,
base_tree: treeSha,
tree
});

const newTreeSha = response.data.sha;

response = await octokit.git.createCommit({
Expand Down

0 comments on commit 577652c

Please sign in to comment.