Skip to content

Commit 5a44e84

Browse files
committed
Add support for committing submodules
1 parent b0871af commit 5a44e84

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

create-or-update-files.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,32 @@ module.exports = function(octokit, opts) {
7171

7272
let contents = properties.contents || properties;
7373
let mode = properties.mode || "100644";
74+
let type = properties.type || "blob";
7475

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

79-
let file = (
80-
await octokit.git.createBlob({
81-
owner,
82-
repo,
83-
content: Buffer.from(contents).toString("base64"),
84-
encoding: "base64"
85-
})
86-
).data;
80+
let fileSha;
81+
if (type == "commit") {
82+
fileSha = contents;
83+
} else {
84+
let file = (
85+
await octokit.git.createBlob({
86+
owner,
87+
repo,
88+
content: Buffer.from(contents).toString("base64"),
89+
encoding: "base64"
90+
})
91+
).data;
92+
fileSha = file.sha;
93+
}
8794

8895
treeItems.push({
8996
path: fileName,
90-
sha: file.sha,
97+
sha: fileSha,
9198
mode: mode,
92-
type: "blob"
99+
type: type
93100
});
94101
}
95102

create-or-update-files.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,31 @@ test(`no file contents provided`, async () => {
141141
);
142142
});
143143

144+
test(`success (submodule, branch exists)`, async () => {
145+
const body = {
146+
...validRequest,
147+
changes: [
148+
{
149+
message: "Your submodule commit message",
150+
files: {
151+
my_submodule: {
152+
contents: "new-submodule-sha",
153+
mode: "160000",
154+
type: "commit"
155+
}
156+
}
157+
}
158+
]
159+
};
160+
161+
mockGetRef(branch, `sha-${branch}`, true);
162+
mockCreateTreeSubmodule(`sha-${branch}`);
163+
mockCommitSubmodule(`sha-${branch}`);
164+
mockUpdateRef(branch);
165+
166+
await expect(run(body)).resolves.toEqual(branch);
167+
});
168+
144169
test(`success (branch exists)`, async () => {
145170
const body = {
146171
...validRequest
@@ -277,6 +302,31 @@ function mockCreateBlobFileThree() {
277302
);
278303
}
279304

305+
function mockCreateTreeSubmodule(baseTree) {
306+
const expectedBody = {
307+
tree: [
308+
{
309+
path: "my_submodule",
310+
sha: "new-submodule-sha",
311+
mode: "160000",
312+
type: "commit"
313+
}
314+
],
315+
base_tree: baseTree
316+
};
317+
318+
const m = nock("https://api.github.com").post(
319+
`/repos/${owner}/${repo}/git/trees`,
320+
expectedBody
321+
);
322+
323+
const body = {
324+
sha: "4112258c05f8ce2b0570f1bbb1a330c0f9595ff9"
325+
};
326+
327+
m.reply(200, body);
328+
}
329+
280330
function mockCreateTree(baseTree) {
281331
const expectedBody = {
282332
tree: [
@@ -333,6 +383,25 @@ function mockCreateTreeSecond(baseTree) {
333383
m.reply(200, body);
334384
}
335385

386+
function mockCommitSubmodule(baseTree) {
387+
const expectedBody = {
388+
message: "Your submodule commit message",
389+
tree: "4112258c05f8ce2b0570f1bbb1a330c0f9595ff9",
390+
parents: [baseTree]
391+
};
392+
393+
const m = nock("https://api.github.com").post(
394+
`/repos/${owner}/${repo}/git/commits`,
395+
expectedBody
396+
);
397+
398+
const body = {
399+
sha: "ef105a72c03ce2743d90944c2977b1b5563b43c0"
400+
};
401+
402+
m.reply(200, body);
403+
}
404+
336405
function mockCommit(baseTree) {
337406
const expectedBody = {
338407
message: "Your commit message",

0 commit comments

Comments
 (0)