/** * File / content operations */ import type { GiteaClient } from "./client.js"; export interface FileContent { name: string; path: string; sha: string; content: string; encoding: string; size: number; html_url: string; } export interface FileCommitResponse { content: FileContent; commit: { sha: string; html_url: string }; } export interface Branch { name: string; commit: { id: string; message: string }; } /** Get file content from a repo */ export async function getFileContent( client: GiteaClient, owner: string, repo: string, filepath: string, opts: { ref?: string } = {}, ): Promise { const ref = opts.ref ? `?ref=${encodeURIComponent(opts.ref)}` : ""; return client.get(`/repos/${owner}/${repo}/contents/${filepath}${ref}`); } /** Create or update a file in a repo */ export async function updateFile( client: GiteaClient, owner: string, repo: string, filepath: string, opts: { content: string; message: string; branch?: string; sha?: string }, ): Promise { return client.put(`/repos/${owner}/${repo}/contents/${filepath}`, { content: Buffer.from(opts.content).toString("base64"), message: opts.message, branch: opts.branch, sha: opts.sha, }); } /** Delete a file in a repo */ export async function deleteFile( client: GiteaClient, owner: string, repo: string, filepath: string, opts: { sha: string; message: string; branch?: string }, ): Promise { // Gitea's delete file endpoint uses DELETE with a body await client.request("DELETE", `/repos/${owner}/${repo}/contents/${filepath}`, { sha: opts.sha, message: opts.message, branch: opts.branch, }); } /** Create a branch */ export async function createBranch( client: GiteaClient, owner: string, repo: string, opts: { name: string; oldRef?: string }, ): Promise { return client.post(`/repos/${owner}/${repo}/branches`, { new_branch_name: opts.name, old_branch_name: opts.oldRef ?? "main", }); } /** List branches */ export async function listBranches( client: GiteaClient, owner: string, repo: string, ): Promise { return client.get(`/repos/${owner}/${repo}/branches`); }