@kevisual/cnb 0.0.74 → 0.0.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/cnb",
3
- "version": "0.0.74",
3
+ "version": "0.0.76",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "basename": "/root/cnb",
@@ -77,7 +77,44 @@ export class RepoLabel extends CNBCore {
77
77
  }
78
78
  });
79
79
  }
80
-
80
+ async getAll(repo: string): Promise<Result<Label[]>> {
81
+ let labels: Label[] = [];
82
+ const pageSize = 99;
83
+ for (let page = 1; ; page++) {
84
+ const res = await this.list(repo, { page, page_size: pageSize });
85
+ if (res.code !== 200) {
86
+ return { code: res.code, message: res.message, data: labels };
87
+ }
88
+ labels = labels.concat(res.data);
89
+ if (res.data.length < pageSize) {
90
+ break;
91
+ }
92
+ }
93
+ return { code: 200, data: labels, message: '获取标签列表成功' };
94
+ }
95
+ async importLabels(opts: { repo: string, labels: PostLabelForm[] }): Promise<Result> {
96
+ const labels = opts.labels;
97
+ const repo = opts.repo;
98
+ const addLabels: Label[] = [];
99
+ for (const label of labels) {
100
+ try {
101
+ const res = await this.create(repo, label);
102
+ if (res.code === 200) {
103
+ addLabels.push(res.data);
104
+ } else {
105
+ if (res.code === 409) {
106
+ console.warn(`标签已存在 ${label.name},跳过创建`);
107
+ } else {
108
+ console.error(`创建标签失败 ${label.name}:`, res.message);
109
+ }
110
+ }
111
+ } catch (e) {
112
+ console.error(`创建标签失败 ${label.name}:`, e);
113
+ break;
114
+ }
115
+ }
116
+ return { code: 200, data: { addLabels }, message: '导入标签成功' };
117
+ }
81
118
  /**
82
119
  * 创建一个标签
83
120
  * @param repo 仓库路径
@@ -112,10 +112,10 @@ export class Mission extends CNBCore {
112
112
  /**
113
113
  * 改变任务集可见性
114
114
  * @param mission 任务集路径
115
- * @param visibility 可见性 (PrivatePublic)
115
+ * @param visibility 可见性 (privatepublic)
116
116
  * @returns 操作结果
117
117
  */
118
- setVisibility(mission: string, visibility: 'Private' | 'Public'): Promise<Result<any>> {
118
+ setVisibility(mission: string, visibility: 'private' | 'public'): Promise<Result<any>> {
119
119
  const url = `/${mission}/-/settings/set_visibility`;
120
120
  return this.post({
121
121
  url,
@@ -146,9 +146,16 @@ type GetMissionsParams = {
146
146
  };
147
147
 
148
148
  type CreateMissionData = {
149
+ /**
150
+ * 任务集名称,必填
151
+ */
149
152
  name: string;
150
153
  description?: string;
151
- visibility?: 'Private' | 'Public';
154
+ visibility?: 'private' | 'public';
155
+ /**
156
+ * 关联的仓库列表,格式为 `${group}/${repo}`,如 `my-group/my-repo`
157
+ */
158
+ repos: string[];
152
159
  };
153
160
 
154
161
  type Missions4User = {
@@ -156,7 +163,7 @@ type Missions4User = {
156
163
  name: string;
157
164
  slug_path: string;
158
165
  description: string;
159
- visibility: 'Private' | 'Public';
166
+ visibility: 'private' | 'public';
160
167
  created_at: string;
161
168
  updated_at: string;
162
169
  web_url: string;
@@ -8,22 +8,27 @@ type DownloadFn = (url: string, opts?: { headers?: any, hash?: string, token?: s
8
8
  */
9
9
  export const downloadByCNBApi: DownloadFn = async (url, opts) => {
10
10
  const hash = opts?.hash;
11
- const headers = opts?.token ? { Authorization: `${opts.token}` } : {};
11
+ const headers = {
12
+ Accept: "application/vnd.cnb.api+json",
13
+ // Authorization: `${CNB_API_KEY}`
14
+ ...(opts?.token ? { Authorization: `${opts.token}` } : {}),
15
+ ...opts?.headers
16
+ }
12
17
  const res = await fetch(url, {
13
- headers: {
14
- Accept: "application/vnd.cnb.api+json",
15
- ...headers,
16
- ...opts?.headers
17
- }
18
+ headers: headers
18
19
  });
19
20
  type CNBResponse = {
20
- type?: 'blob' | 'file' | 'dir';
21
+ type?: 'blob' | 'file' | 'dir' | 'lfs';
21
22
  size?: number;
22
23
  path: string;
23
24
  name: string;
24
25
  sha?: string;
25
26
  encoding?: string;
26
27
  content?: string;
28
+
29
+ lfs_oid?: string;
30
+ lfs_size?: number;
31
+ lfs_download_url?: string;
27
32
  }
28
33
  let resJson: CNBResponse;
29
34
  try {
@@ -32,13 +37,40 @@ export const downloadByCNBApi: DownloadFn = async (url, opts) => {
32
37
  console.error(`下载失败 ${url}: ${res.statusText}`, e);
33
38
  return { code: res.status, buffer: null };
34
39
  }
35
- if (res.ok && resJson.type === 'blob' && resJson.encoding === 'base64' && resJson.content) {
36
- if (hash && resJson.sha === hash) {
37
- // 文件未修改,跳过下载
38
- return { code: 304, buffer: null };
40
+ if (res.ok) {
41
+ if (resJson.size === 0) {
42
+ return { code: 200, buffer: Buffer.from('') };
39
43
  }
40
- const buffer = Buffer.from(resJson.content, 'base64');
41
- return { code: 200, buffer };
44
+ if (resJson.type === 'blob' && resJson.encoding === 'base64') {
45
+ if (resJson.content) {
46
+ if (hash && resJson.sha === hash) {
47
+ // 文件未修改,跳过下载
48
+ return { code: 304, buffer: null };
49
+ }
50
+ const buffer = Buffer.from(resJson.content, 'base64');
51
+ return { code: 200, buffer };
52
+ }
53
+ }
54
+ if (resJson.type === 'lfs') {
55
+ const lfs_download_url = resJson.lfs_download_url;
56
+ if (lfs_download_url) {
57
+ const lfsRes = await fetch(lfs_download_url, {
58
+ headers: headers
59
+ });
60
+ if (lfsRes.ok) {
61
+ const etag = lfsRes.headers.get('ETag');
62
+ if (hash && etag === hash) {
63
+ // 文件未修改,跳过下载
64
+ return { code: 304, buffer: null };
65
+ }
66
+ const arrayBuffer = await lfsRes.arrayBuffer();
67
+ return { code: 200, buffer: Buffer.from(arrayBuffer) };
68
+ }
69
+ }
70
+ }
71
+
72
+ console.error(`下载失败 ${url}: 内容为空`, resJson);
73
+ return { code: 200, buffer: null };
42
74
  } else {
43
75
  console.error(`下载失败 ${url}: ${res.statusText}`, resJson);
44
76
  return { code: res.status, buffer: null };