Skip to content
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
60 changes: 31 additions & 29 deletions create-or-update-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,35 +165,37 @@ module.exports = function (octokit, opts) {
}
}

for (const batch of chunk(Object.keys(change.files), batchSize)) {
await Promise.all(
batch.map(async (fileName) => {
const properties = change.files[fileName] || "";

const contents = properties.contents || properties;
const mode = properties.mode || "100644";
const type = properties.type || "blob";

if (!contents) {
return reject(`No file contents provided for ${fileName}`);
}

const fileSha = await createBlob(
octokit,
owner,
repo,
contents,
type,
);

treeItems.push({
path: fileName,
sha: fileSha,
mode: mode,
type: type,
});
}),
);
if (hasFiles) {
for (const batch of chunk(Object.keys(change.files), batchSize)) {
await Promise.all(
batch.map(async (fileName) => {
const properties = change.files[fileName] || "";

const contents = properties.contents || properties;
const mode = properties.mode || "100644";
const type = properties.type || "blob";

if (!contents) {
return reject(`No file contents provided for ${fileName}`);
}

const fileSha = await createBlob(
octokit,
owner,
repo,
contents,
type,
);

treeItems.push({
path: fileName,
sha: fileSha,
mode: mode,
type: type,
});
}),
);
}
}

// no need to issue further requests if there are no updates, creations and deletions
Expand Down
49 changes: 49 additions & 0 deletions create-or-update-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,29 @@ test("success (fileToDelete exists)", async () => {
await expect(run(body)).resolves.toEqual(mockSecondCommitList);
});

test("success (fileToDelete exists, no content provided)", async () => {
mockGetRef(branch, `sha-${branch}`, false);
mockGetRef(base, `sha-${base}`, true);
mockGetContents("wow-this-file-disappeared", `sha-${base}`, true);
mockCreateTreeWithDeleteOnly(`sha-${base}`);
mockCommitSecond(`sha-${base}`);
mockCreateRefSecond(branch);

const changes = [
{
message: "This is the second commit",
filesToDelete: ["wow-this-file-disappeared"],
},
];

const body = {
...validRequest,
changes,
};

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

test("failure (fileToDelete is missing)", async () => {
mockGetRef(branch, `sha-${branch}`, false);
mockGetRef(base, `sha-${base}`, true);
Expand Down Expand Up @@ -658,6 +681,32 @@ function mockCreateTreeWithIgnoredDelete(baseTree) {
m.reply(200, body);
}

function mockCreateTreeWithDeleteOnly(baseTree) {
// The order here is important. Removals must be applied before creations
const expectedBody = {
tree: [
{
path: "wow-this-file-disappeared",
sha: null,
mode: "100644",
type: "commit",
},
],
base_tree: baseTree,
};

const m = nock("https://api.github.com").post(
`/repos/${owner}/${repo}/git/trees`,
expectedBody,
);

const body = {
sha: "fffff6bbf5ab983d31b1cca28e204b71ab722764",
};

m.reply(200, body);
}

function mockCreateTreeWithDelete(baseTree) {
// The order here is important. Removals must be applied before creations
const expectedBody = {
Expand Down