Files
blitzkit/scripts/commitMultipleFiles.ts
T

76 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-07-15 01:21:59 +00:00
import { Octokit } from '@octokit/rest';
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
octokit: Octokit,
owner: string,
repo: string,
branch: string,
message: string,
2023-11-27 16:52:38 -06:00
changes: FileChange[],
) {
// Get the latest commit SHA on the specified branch
const { data: refData } = await octokit.git.getRef({
owner,
repo,
ref: `heads/${branch}`,
});
const latestCommitSha = refData.object.sha;
// Get the tree SHA for the latest commit
const { data: commitData } = await octokit.git.getCommit({
owner,
repo,
commit_sha: latestCommitSha,
});
const treeSha = commitData.tree.sha;
// Create blobs for each file
const blobs = await Promise.all(
changes.map(async (change) => {
const { data: blobData } = await octokit.git.createBlob({
owner,
repo,
content: change.content,
2023-11-27 16:52:38 -06:00
encoding: change.encoding,
});
2023-07-15 01:21:59 +00:00
return {
sha: blobData.sha,
path: change.path,
mode: '100644',
type: 'blob',
2023-07-15 01:21:59 +00:00
} as const;
}),
);
// Create a new tree with the new blobs
const { data: treeData } = await octokit.git.createTree({
owner,
repo,
base_tree: treeSha,
tree: blobs,
});
// Create a new commit with the new tree
const { data: newCommitData } = await octokit.git.createCommit({
owner,
repo,
message,
tree: treeData.sha,
parents: [latestCommitSha],
});
// Update the branch reference to point to the new commit
await octokit.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommitData.sha,
});
}