judge4c-old/src/actions/index.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

"use server";
import api from "@/lib/gitea";
import { GitEntry, APIError } from "gitea-js";
export async function retrieveTreeStructure(
owner: string,
repo: string,
sha: string
): Promise<GitEntry[]> {
try {
const response = await api.repos.getTree(owner, repo, sha, {
recursive: true,
});
return response.data.tree || [];
} catch (error) {
if ((error as APIError).message) {
console.error("Gitea API [getTree] Error", error);
} else {
console.error("Unexpected Error", error);
}
throw new Error("Failed to retrieve tree structure");
}
}
export async function retrieveFileContent(
owner: string,
repo: string,
path: string
): Promise<{ encoding: string; content: string }> {
try {
const response = await api.repos.repoGetContents(owner, repo, path);
const { encoding, content } = response.data;
return { encoding: encoding || "", content: content || "" };
} catch (error) {
if ((error as APIError).message) {
console.error("Gitea API [repoGetContents] Error", error);
} else {
console.error("Unexpected Error", error);
}
throw new Error("Failed to retrieve file contents");
}
}