import { API_URL } from "https://esm.town/v/std/API_URL?v=5";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=42";
import { Octokit } from "npm:@octokit/rest";
import { DateTime } from "npm:luxon";
async function getLatestCommit(ghUser: string, ghRepo: string, branch: string, client: Octokit)
{
const { data: refData } = await client.rest.git.getRef({
owner: ghUser,
repo: ghRepo,
ref: `heads/${branch}`,
});
return refData.object.sha;
}
async function createNewTree(
ghUser: string,
ghRepo: string,
tree: {
path?: string;
mode?: "100644" | "100755" | "040000" | "160000" | "120000";
type?: "tree" | "commit" | "blob";
sha?: string;
content?: string;
}[],
commitSHA: string,
client: Octokit,
) {
const {
data: { sha: currentTreeSHA },
} = await client.rest.git.createTree({
owner: ghUser,
repo: ghRepo,
tree,
base_tree: commitSHA,
parents: [commitSHA],
});
return currentTreeSHA;
}
async function createNewCommit(
ghUser: string,
ghRepo: string,
commitSHA: string,
currentTreeSHA: string,
message: string,
client: Octokit,
) {
const {
data: { sha: newCommitSHA },
} = await client.rest.git.createCommit({
owner: ghUser,
repo: ghRepo,
tree: currentTreeSHA,
message: message,
parents: [commitSHA],
});
return newCommitSHA;
}
async function updateBranchRef(
owner: string,
repo: string,
newCommitSHA: string,
branch: string = "main",
client: Octokit,
) {
const result = await client.rest.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommitSHA,
});
return result;
}
type Options = { branch?: string; prefix?: string; message?: string; ts?: boolean };
export async function valToGH(
repo: string,
val: string | string[],
ghToken: string,
options?: Options,
) {
const client = new Octokit({ auth: ghToken });
const [ghUser, ghRepo] = repo.split("/");
if (repo.split("/").length !== 2) {
throw new Error("Repo name must be of the form {user}/{repo}");
}
let valsToCommit = typeof val === "string" ? [val] : val;
if (valsToCommit.every(val => val.split("/").length !== 2)) {
throw new Error("All val names must be of the form {user}/{val}");
}