Files
blitzkit/src/core/blitzrinth/commitMultipleFiles.ts
T

69 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-01-20 22:45:25 -06:00
import { GithubChangeBlob, createBlob } from './createBlob';
2023-12-06 10:17:42 -06:00
import { octokit } from './octokit';
2023-07-15 01:21:59 +00:00
2023-11-27 16:52:38 -06:00
export interface FileChange {
path: string;
content: string;
encoding: 'utf-8' | 'base64';
}
export default async function commitMultipleFiles(
2023-07-15 01:21:59 +00:00
owner: string,
repo: string,
branch: string,
message: string,
2023-11-27 16:52:38 -06:00
changes: FileChange[],
2023-12-05 19:14:53 -06:00
verbose = false,
) {
2023-11-27 17:50:02 -06:00
const latestCommitSha = (
await octokit.git.getRef({
owner,
repo,
ref: `heads/${branch}`,
})
).data.object.sha;
const treeSha = (
await octokit.git.getCommit({
owner,
repo,
commit_sha: latestCommitSha,
})
).data.tree.sha;
2024-01-20 22:45:25 -06:00
const blobs: GithubChangeBlob[] = [];
2023-07-15 01:21:59 +00:00
2023-11-27 17:50:02 -06:00
for (const changeIndexString in changes) {
const changeIndex = parseInt(changeIndexString);
const change = changes[changeIndex];
2024-01-20 22:45:25 -06:00
const createdBlob = await createBlob(owner, repo, change);
blobs.push(createdBlob!);
2023-11-27 17:50:02 -06:00
2023-12-05 19:14:53 -06:00
if (verbose) {
2023-11-27 17:50:02 -06:00
console.log(
`Created blob ${changeIndex + 1} / ${changes.length} (${(
(100 * (changeIndex + 1)) /
changes.length
).toFixed(2)}%)`,
);
}
}
const { data: treeData } = await octokit.git.createTree({
owner,
repo,
base_tree: treeSha,
tree: blobs,
});
const { data: newCommitData } = await octokit.git.createCommit({
owner,
repo,
message,
tree: treeData.sha,
parents: [latestCommitSha],
});
await octokit.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommitData.sha,
});
}