- Pure fetch-based API client (src/) with zero external dependencies - Pi extension adapter (pi-extension/) registering 17 tools - Standalone CLI (cli.ts) replacing gitea-scripts/gitea.js - Token auth everywhere (no HMAC secrets) - SKILL.md for agent auto-discovery - TOOL.md with full parameter reference Consolidates pi-bot/extensions/pi-gitea and clawbot/gitea-scripts into a single shared package.
92 lines
2.2 KiB
TypeScript
92 lines
2.2 KiB
TypeScript
/**
|
|
* 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<FileContent> {
|
|
const ref = opts.ref ? `?ref=${encodeURIComponent(opts.ref)}` : "";
|
|
return client.get<FileContent>(`/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<FileCommitResponse> {
|
|
return client.put<FileCommitResponse>(`/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<void> {
|
|
// 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<Branch> {
|
|
return client.post<Branch>(`/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<Branch[]> {
|
|
return client.get<Branch[]>(`/repos/${owner}/${repo}/branches`);
|
|
}
|