Skip to content

Commit 74a5c2b

Browse files
committed
Fix commits with only filesToDelete
1 parent da63aea commit 74a5c2b

File tree

2 files changed

+80
-29
lines changed

2 files changed

+80
-29
lines changed

create-or-update-files.js

+31-29
Original file line numberDiff line numberDiff line change
@@ -165,35 +165,37 @@ module.exports = function (octokit, opts) {
165165
}
166166
}
167167

168-
for (const batch of chunk(Object.keys(change.files), batchSize)) {
169-
await Promise.all(
170-
batch.map(async (fileName) => {
171-
const properties = change.files[fileName] || "";
172-
173-
const contents = properties.contents || properties;
174-
const mode = properties.mode || "100644";
175-
const type = properties.type || "blob";
176-
177-
if (!contents) {
178-
return reject(`No file contents provided for ${fileName}`);
179-
}
180-
181-
const fileSha = await createBlob(
182-
octokit,
183-
owner,
184-
repo,
185-
contents,
186-
type,
187-
);
188-
189-
treeItems.push({
190-
path: fileName,
191-
sha: fileSha,
192-
mode: mode,
193-
type: type,
194-
});
195-
}),
196-
);
168+
if (hasFiles) {
169+
for (const batch of chunk(Object.keys(change.files), batchSize)) {
170+
await Promise.all(
171+
batch.map(async (fileName) => {
172+
const properties = change.files[fileName] || "";
173+
174+
const contents = properties.contents || properties;
175+
const mode = properties.mode || "100644";
176+
const type = properties.type || "blob";
177+
178+
if (!contents) {
179+
return reject(`No file contents provided for ${fileName}`);
180+
}
181+
182+
const fileSha = await createBlob(
183+
octokit,
184+
owner,
185+
repo,
186+
contents,
187+
type,
188+
);
189+
190+
treeItems.push({
191+
path: fileName,
192+
sha: fileSha,
193+
mode: mode,
194+
type: type,
195+
});
196+
}),
197+
);
198+
}
197199
}
198200

199201
// no need to issue further requests if there are no updates, creations and deletions

create-or-update-files.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,29 @@ test("success (fileToDelete exists)", async () => {
442442
await expect(run(body)).resolves.toEqual(mockSecondCommitList);
443443
});
444444

445+
test("success (fileToDelete exists, no content provided)", async () => {
446+
mockGetRef(branch, `sha-${branch}`, false);
447+
mockGetRef(base, `sha-${base}`, true);
448+
mockGetContents("wow-this-file-disappeared", `sha-${base}`, true);
449+
mockCreateTreeWithDeleteOnly(`sha-${base}`);
450+
mockCommitSecond(`sha-${base}`);
451+
mockCreateRefSecond(branch);
452+
453+
const changes = [
454+
{
455+
message: "This is the second commit",
456+
filesToDelete: ["wow-this-file-disappeared"],
457+
},
458+
];
459+
460+
const body = {
461+
...validRequest,
462+
changes,
463+
};
464+
465+
await expect(run(body)).resolves.toEqual(mockSecondCommitList);
466+
});
467+
445468
test("failure (fileToDelete is missing)", async () => {
446469
mockGetRef(branch, `sha-${branch}`, false);
447470
mockGetRef(base, `sha-${base}`, true);
@@ -658,6 +681,32 @@ function mockCreateTreeWithIgnoredDelete(baseTree) {
658681
m.reply(200, body);
659682
}
660683

684+
function mockCreateTreeWithDeleteOnly(baseTree) {
685+
// The order here is important. Removals must be applied before creations
686+
const expectedBody = {
687+
tree: [
688+
{
689+
path: "wow-this-file-disappeared",
690+
sha: null,
691+
mode: "100644",
692+
type: "commit",
693+
},
694+
],
695+
base_tree: baseTree,
696+
};
697+
698+
const m = nock("https://api.github.com").post(
699+
`/repos/${owner}/${repo}/git/trees`,
700+
expectedBody,
701+
);
702+
703+
const body = {
704+
sha: "fffff6bbf5ab983d31b1cca28e204b71ab722764",
705+
};
706+
707+
m.reply(200, body);
708+
}
709+
661710
function mockCreateTreeWithDelete(baseTree) {
662711
// The order here is important. Removals must be applied before creations
663712
const expectedBody = {

0 commit comments

Comments
 (0)