初始化 Gitea 项目结构,添加核心 API 封装和相关配置文件

This commit is contained in:
2026-02-19 20:38:03 +08:00
commit 94c666a993
14 changed files with 943 additions and 0 deletions

132
src/repo/index.ts Normal file
View File

@@ -0,0 +1,132 @@
import { GiteaCore } from "../core/core";
export interface CreateRepoOptions {
name: string;
description?: string;
private?: boolean;
auto_init?: boolean;
gitignores?: string;
license?: string;
readme?: string;
}
export interface UpdateRepoOptions {
name?: string;
description?: string;
private?: boolean;
allow_rebase?: boolean;
allow_rebase_explicit?: boolean;
allow_merge_commits?: boolean;
allow_squash_merge?: boolean;
allow_rebase_update?: boolean;
default_branch?: string;
}
export class GiteaRepo extends GiteaCore {
/**
* 获取仓库信息
*/
async getRepo(owner: string, repo: string) {
const url = this.makeUrl(`/repos/${owner}/${repo}`);
return this.request({ url });
}
/**
* 创建仓库
* 格式: name="repo" -> 当前用户
* 格式: name="org/repo" -> 组织
*/
async createRepo(data: CreateRepoOptions) {
const name = data.name;
const parts = name.split('/');
if (parts.length === 2) {
const [orgOrUser, repoName] = parts;
const url = this.makeUrl(`/${orgOrUser}/repos`);
return this.request({ url, method: 'POST', data: { ...data, name: repoName } });
}
const url = this.makeUrl(`/user/repos`);
return this.request({ url, method: 'POST', data });
}
/**
* 更新仓库
*/
async updateRepo(owner: string, repo: string, data: UpdateRepoOptions) {
const url = this.makeUrl(`/repos/${owner}/${repo}`);
return this.request({ url, method: 'PATCH', data });
}
/**
* 删除仓库
*/
async deleteRepo(owner: string, repo: string) {
const url = this.makeUrl(`/repos/${owner}/${repo}`);
return this.request({ url, method: 'DELETE' });
}
/**
* 获取仓库列表
*/
async listRepos(options?: {
page?: number;
limit?: number;
type?: 'all' | 'owner' | 'public' | 'private';
}) {
const url = this.makeUrl(`/user/repos`);
return this.request({ url, method: 'GET', params: options });
}
/**
* 获取组织仓库列表
*/
async listOrgRepos(org: string, options?: {
page?: number;
limit?: number;
type?: 'all' | 'fork' | 'source' | 'mirror';
}) {
const url = this.makeUrl(`/orgs/${org}/repos`);
return this.request({ url, method: 'GET', params: options });
}
/**
* 获取仓库内容(文件或目录)
*/
async getContents(owner: string, repo: string, path: string, options?: {
ref?: string;
}) {
const url = this.makeUrl(`/repos/${owner}/${repo}/contents/${path}`);
return this.request({ url, method: 'GET', params: options });
}
/**
* 获取仓库分支列表
*/
async listBranches(owner: string, repo: string, options?: {
page?: number;
limit?: number;
}) {
const url = this.makeUrl(`/repos/${owner}/${repo}/branches`);
return this.request({ url, method: 'GET', params: options });
}
/**
* 获取仓库标签列表
*/
async listTags(owner: string, repo: string, options?: {
page?: number;
limit?: number;
}) {
const url = this.makeUrl(`/repos/${owner}/${repo}/tags`);
return this.request({ url, method: 'GET', params: options });
}
/**
* Fork 仓库
*/
async forkRepo(owner: string, repo: string, organization?: string) {
const url = this.makeUrl(`/repos/${owner}/${repo}/forks`);
return this.request({ url, method: 'POST', data: { organization } });
}
}