168 lines
4.8 KiB
TypeScript
168 lines
4.8 KiB
TypeScript
import { CNBCore, CNBCoreOptions, RequestOptions, Result } from "../cnb-core.ts";
|
|
|
|
export class Repo extends CNBCore {
|
|
constructor(options: CNBCoreOptions) {
|
|
super({ token: options.token, cookie: options.cookie });
|
|
}
|
|
/**
|
|
* 创建代码仓库
|
|
* @param data
|
|
* @returns
|
|
*/
|
|
createRepo(data: CreateRepoData): Promise<any> {
|
|
const name = data.name;
|
|
const [group, repo] = name.includes('/') ? name.split('/') : ['', name];
|
|
const url = `/${group}/-/repos`;
|
|
let postData: CreateRepoData = {
|
|
...data,
|
|
description: data.description || '',
|
|
name: repo,
|
|
license: data.license || 'Unlicense',
|
|
visibility: data.visibility || 'private',
|
|
};
|
|
return this.post({ url, data: postData });
|
|
}
|
|
deleteRepo(name: string): Promise<any> {
|
|
const url = `https://cnb.cool/${name}`;
|
|
return this.delete({ url, useCookie: true });
|
|
}
|
|
async createCommit(repo: string, data: CreateCommitData): Promise<any> {
|
|
const commitList = await this.getCommitList(repo, {
|
|
page: 1,
|
|
page_size: 1,
|
|
}, { useOrigin: true }).catch((err) => {
|
|
// console.error("Error fetching commit list:", err);
|
|
return []
|
|
});
|
|
const preCommitSha = commitList.length > 0 ? commitList[0].sha : undefined;
|
|
if (!data.parent_commit_sha && preCommitSha) {
|
|
data.parent_commit_sha = preCommitSha;
|
|
}
|
|
const url = `https://cnb.cool/${repo}/-/git/commits`;
|
|
const postData: CreateCommitData = {
|
|
...data,
|
|
base_branch: data.base_branch || 'refs/heads/main',
|
|
message: data.message || 'commit from cnb sdk',
|
|
parent_commit_sha: data.parent_commit_sha,
|
|
files: data.files || [],
|
|
new_branch: data.new_branch || 'refs/heads/main',
|
|
};
|
|
if (!postData.parent_commit_sha) {
|
|
delete postData.parent_commit_sha;
|
|
delete postData.base_branch;
|
|
}
|
|
return this.post({
|
|
url,
|
|
data: postData,
|
|
useCookie: true,
|
|
headers: {
|
|
'Accept': 'application/vnd.cnb.web+json',
|
|
}
|
|
});
|
|
}
|
|
createBlobs(repo: string, data: { content: string, encoding?: 'utf-8' | 'base64' }): Promise<any> {
|
|
const url = `/${repo}/-/git/blobs`;
|
|
const postData = {
|
|
content: data.content,
|
|
encoding: data.encoding || 'utf-8',
|
|
};
|
|
return this.post({ url, data: postData });
|
|
}
|
|
uploadFiles(repo: string, data: { ext?: any, name?: string, path?: string, size?: number }): Promise<any> {
|
|
const url = `/${repo}/-/upload/files`
|
|
return this.post({ url, data });
|
|
}
|
|
getCommitList(repo: string, params: { author?: string, commiter?: string, page?: number, page_size?: number, sha?: string, since?: string, until?: string }, opts?: RequestOptions): Promise<any> {
|
|
const url = `/${repo}/-/git/commits`;
|
|
return this.get({ url, params, ...opts });
|
|
}
|
|
getRepoList(params: {
|
|
desc?: boolean;
|
|
filter_type?: 'private' | 'public' | 'secret';
|
|
flags?: 'KnowledgeBase';
|
|
order_by?: 'created_at' | 'last_updated_at' | 'stars' | 'slug_path' | 'forks';
|
|
page?: number;
|
|
page_size?: number;
|
|
role?: 'owner' | 'maintainer' | 'developer' | 'reporter' | 'guest';
|
|
search?: string;
|
|
status?: 'active' | 'archived';
|
|
}): Promise<Result<RepoItem[]>> {
|
|
const url = '/user/repos';
|
|
let _params = {
|
|
role: 'developer',
|
|
status: 'active',
|
|
...params,
|
|
page: params.page || 1,
|
|
page_size: params.page_size || 999,
|
|
}
|
|
if(!_params.search) {
|
|
delete _params.search;
|
|
}
|
|
return this.get({ url, params: _params });
|
|
}
|
|
}
|
|
|
|
type CreateRepoData = {
|
|
description: string;
|
|
license?: 'MIT' | 'Apache-2.0' | 'GPL-3.0' | 'Unlicense';
|
|
name: string;
|
|
visibility: 'private' | 'public' | 'secret';
|
|
}
|
|
|
|
type CreateCommitData = {
|
|
base_branch?: string; // "refs/heads/main"
|
|
new_branch?: string; // "refs/heads/main"
|
|
message?: string;
|
|
parent_commit_sha?: string;
|
|
files?: Array<{
|
|
content: string;
|
|
path: string;
|
|
encoding?: 'raw' | 'utf-8' | 'base64';
|
|
is_delete?: boolean;
|
|
is_executable?: boolean;
|
|
}>;
|
|
}
|
|
|
|
type RepoItem = {
|
|
id: string;
|
|
name: string;
|
|
freeze: boolean;
|
|
status: number;
|
|
visibility_level: 'Public' | 'Private' | 'Secret';
|
|
flags: string;
|
|
created_at: string;
|
|
updated_at: string;
|
|
description: string;
|
|
site: string;
|
|
topics: string;
|
|
license: string;
|
|
display_module: {
|
|
activity: boolean;
|
|
contributors: boolean;
|
|
release: boolean;
|
|
};
|
|
star_count: number;
|
|
fork_count: number;
|
|
mark_count: number;
|
|
last_updated_at: string | null;
|
|
web_url: string;
|
|
path: string;
|
|
tags: string[] | null;
|
|
open_issue_count: number;
|
|
open_pull_request_count: number;
|
|
languages: {
|
|
language: string;
|
|
color: string;
|
|
};
|
|
second_languages: {
|
|
language: string;
|
|
color: string;
|
|
};
|
|
last_update_username: string;
|
|
last_update_nickname: string;
|
|
access: string;
|
|
stared: boolean;
|
|
star_time: string;
|
|
pinned: boolean;
|
|
pinned_time: string;
|
|
} |