feat: 添加 Repo 和 User 模块,增强 CNBCore 功能

This commit is contained in:
2025-12-15 17:08:39 +08:00
parent 5e43eb2db7
commit 52ccf115fb
9 changed files with 390 additions and 32 deletions

View File

@@ -1,8 +1,14 @@
import { CNB } from "../src";
import dotenv from "dotenv";
import util from 'node:util';
dotenv.config();
export const cnb = new CNB(process.env.CNB_TOKEN || "");
export const token = process.env.CNB_TOKEN || "";
export const cookie = process.env.CNB_COOKIE || "";
console.log("Using CNB_TOKEN:", token.slice(0, 4) + "****", cookie);
export const cnb = new CNB({ token, cookie });
export const showMore = (obj: any) => {
return util.inspect(obj, { showHidden: false, depth: null, colors: true });
}
// const worksaceList = await cnb.workspace.list({ status: 'running' });
// console.log("worksaceList", worksaceList);

69
test/create-repo.ts Normal file
View File

@@ -0,0 +1,69 @@
import { Repo } from "../src/repo";
import { token, showMore, cookie } from "./common.ts";
const repo = new Repo({ group: "kevisual/demo", token: token, cookie: cookie });
// const res = await repo.createRepo({
// name: "test-cnb-2",
// description: "This is my new repository",
// visibility: "private",
// license: 'MIT',
// });
// console.log("res", showMore(res));
const repoName = "test-cnb";
// const commitList = await repo.getCommitList(repoName, {
// page: 1,
// page_size: 1,
// }).catch((err) => {
// console.error("Error fetching commit list:", err);
// return []
// });
// console.log("commitList", showMore(commitList));
// const preCommitSha = commitList.length > 0 ? commitList[0].sha : undefined;
const commitRes = await repo.createCommit(repoName, {
message: "Initial commit3",
files: [
{ path: "README.md", content: "# Hello World\nThis is my first commit!", encoding: 'raw' },
{ path: "a.md", content: "# Hello World\nThis is my first commit2!", encoding: 'raw' },
],
});
console.log("commitRes", showMore(commitRes));
// const blobRes = await repo.createBlobs(repoName, {
// content: Buffer.from("Hello, CNB!").toString("base64"),
// encoding: "base64",
// });
// console.log("blobRes", showMore(blobRes));
// sha: 4b909f74428c24221a9f795c591f5eb560817f2d
// const bufferText = Buffer.from("This is a sample file content. 232332");
// const bufferSize = Buffer.byteLength(bufferText);
// console.log("bufferSize", bufferSize);
// const uploadRes = await repo.uploadFiles(repoName, {
// name: "sample.txt",
// ext: { "a": "b" },
// size: Buffer.byteLength(bufferText),
// });
// console.log("uploadRes", showMore(uploadRes));
// const upload_url = uploadRes.upload_url
// const upload_token = uploadRes.token;
// const uploadResponse = await repo.putFile({
// url: upload_url,
// token: upload_token,
// content: bufferText,
// });
// console.log("uploadResponse", showMore(uploadResponse));

10
test/repo.ts Normal file
View File

@@ -0,0 +1,10 @@
import { Repo } from "../src/repo";
import { token, showMore, cookie } from "./common.ts";
const repo = new Repo({ group: "kevisual/demo", token: token, cookie: cookie });
const listRes = await repo.getRepoList({ page: 1, page_size: 10 });
console.log("listRes", showMore(listRes));

9
test/user.ts Normal file
View File

@@ -0,0 +1,9 @@
import { User } from "../src/user/index";
import { token, showMore, cookie } from "./common.ts";
const user = new User({ token: token, cookie: cookie });
const currentUser = await user.getCurrentUser();
console.log("currentUser", showMore(currentUser));