feat: consolidated Gitea API client and pi extension
- 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.
This commit is contained in:
91
src/files.ts
Normal file
91
src/files.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* 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`);
|
||||
}
|
||||
Reference in New Issue
Block a user