@cnbcool/cnb-api-generate 2.4.1 → 2.4.2
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/client/index.ts +4 -0
- package/client/lib/login.ts +2 -1
- package/client/lib/logout.ts +35 -0
- package/client/lib/status.ts +59 -0
- package/package.json +1 -1
- package/skills-template/SKILL.md +6 -8
package/client/index.ts
CHANGED
|
@@ -5,6 +5,8 @@ import { getExtraHelpText } from './lib/extra-help';
|
|
|
5
5
|
import { registerModuleCommands } from './lib/register-modules';
|
|
6
6
|
import { registerFallbackAction } from './lib/register-fallback';
|
|
7
7
|
import { registerLoginCommand } from './lib/login';
|
|
8
|
+
import { registerLogoutCommand } from './lib/logout';
|
|
9
|
+
import { registerStatusCommand } from './lib/status';
|
|
8
10
|
|
|
9
11
|
// ============================================================
|
|
10
12
|
// Commander 程序定义
|
|
@@ -26,6 +28,8 @@ program
|
|
|
26
28
|
// ============================================================
|
|
27
29
|
|
|
28
30
|
registerLoginCommand(program);
|
|
31
|
+
registerLogoutCommand(program);
|
|
32
|
+
registerStatusCommand(program);
|
|
29
33
|
registerModuleCommands(program);
|
|
30
34
|
registerFallbackAction(program);
|
|
31
35
|
|
package/client/lib/login.ts
CHANGED
|
@@ -16,12 +16,13 @@ export function registerLoginCommand(program: Command): void {
|
|
|
16
16
|
.description('通过 OAuth2 设备授权流登录 CNB,获取并保存 access_token')
|
|
17
17
|
.option('--client-id <string>', 'OAuth2 client_id', process.env.OAUTH2_CLIENT_ID || 'cnb_cli')
|
|
18
18
|
.option('--woa', '使用内网环境 (https://cnb.woa.com)', false)
|
|
19
|
+
.option('--host <string>', '指定自定义域名 (优先级高于 --woa)')
|
|
19
20
|
.option('--debug', '打印调试信息', false)
|
|
20
21
|
.helpOption('-h, --help', '显示帮助文档')
|
|
21
22
|
.action(async (opts) => {
|
|
22
23
|
const cfg: LoginConfig = {
|
|
23
24
|
clientID: opts.clientId,
|
|
24
|
-
platformURL: opts.woa ? 'https://cnb.woa.com' : 'https://cnb.cool',
|
|
25
|
+
platformURL: opts.host ? opts.host : opts.woa ? 'https://cnb.woa.com' : 'https://cnb.cool',
|
|
25
26
|
debug: opts.debug,
|
|
26
27
|
};
|
|
27
28
|
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { getTokenPath } from '../utils/token-path';
|
|
4
|
+
import { loadToken } from '../utils/load-token';
|
|
5
|
+
|
|
6
|
+
export function registerLogoutCommand(program: Command): void {
|
|
7
|
+
program
|
|
8
|
+
.command('logout')
|
|
9
|
+
.description('退出登录,删除本地保存的 access_token')
|
|
10
|
+
.option('--debug', '打印调试信息', false)
|
|
11
|
+
.helpOption('-h, --help', '显示帮助文档')
|
|
12
|
+
.action(async (opts) => {
|
|
13
|
+
const tokenPath = getTokenPath();
|
|
14
|
+
const token = loadToken();
|
|
15
|
+
|
|
16
|
+
if (!token) {
|
|
17
|
+
console.log('当前未登录,无需退出。');
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (opts.debug) {
|
|
22
|
+
console.log(`Token 文件路径: ${tokenPath}`);
|
|
23
|
+
console.log(`Platform URL : ${token.platform_url}`);
|
|
24
|
+
console.log(`Client ID : ${token.client_id}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
fs.unlinkSync(tokenPath);
|
|
29
|
+
console.log('已成功退出登录。');
|
|
30
|
+
} catch (err: any) {
|
|
31
|
+
console.error(`退出登录失败: ${err.message}`);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import { getTokenPath } from '../utils/token-path';
|
|
3
|
+
import { loadToken } from '../utils/load-token';
|
|
4
|
+
import { refreshAccessToken } from '../utils/refresh-token';
|
|
5
|
+
|
|
6
|
+
export function registerStatusCommand(program: Command): void {
|
|
7
|
+
program
|
|
8
|
+
.command('status')
|
|
9
|
+
.description('检查当前登录状态(token 是否存在、是否过期)')
|
|
10
|
+
.option('--debug', '打印调试信息', false)
|
|
11
|
+
.helpOption('-h, --help', '显示帮助文档')
|
|
12
|
+
.action(async (opts) => {
|
|
13
|
+
const tokenPath = getTokenPath();
|
|
14
|
+
const token = loadToken();
|
|
15
|
+
|
|
16
|
+
if (!token) {
|
|
17
|
+
console.log('⚠️ 未登录,请执行 `cnb login` 进行授权。');
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (opts.debug) {
|
|
22
|
+
console.log(`Token 文件路径 : ${tokenPath}`);
|
|
23
|
+
console.log(`Platform URL : ${token.platform_url || '未知'}`);
|
|
24
|
+
console.log(`Client ID : ${token.client_id || '未知'}`);
|
|
25
|
+
console.log(`Expires At : ${new Date(token.expires_at * 1000).toLocaleString()}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const nowSec = Math.floor(Date.now() / 1000);
|
|
29
|
+
const isExpired = nowSec >= token.expires_at;
|
|
30
|
+
|
|
31
|
+
if (isExpired) {
|
|
32
|
+
// 尝试使用 refresh_token 刷新
|
|
33
|
+
if (token.refresh_token) {
|
|
34
|
+
if (opts.debug) {
|
|
35
|
+
console.log('Token 已过期,尝试使用 refresh_token 刷新...');
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
await refreshAccessToken(token);
|
|
39
|
+
const refreshedToken = loadToken();
|
|
40
|
+
if (refreshedToken) {
|
|
41
|
+
console.log('✅ 已登录。');
|
|
42
|
+
if (opts.debug) {
|
|
43
|
+
console.log(' (Token 已自动刷新)');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
} catch (err: any) {
|
|
47
|
+
if (opts.debug) {
|
|
48
|
+
console.log(`刷新失败: ${err.message}`);
|
|
49
|
+
}
|
|
50
|
+
console.log('⚠️ 未登录,请执行 `cnb login` 重新授权。');
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
console.log('⚠️ 未登录,请执行 `cnb login` 重新授权。');
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
console.log('✅ 已登录。');
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
package/package.json
CHANGED
package/skills-template/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: cnb-api
|
|
3
|
-
description: CNB
|
|
3
|
+
description: CNB 平台交互命令,支持仓库、Issue、PR、流水线、制品库等操作。
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# cnb-api
|
|
@@ -38,16 +38,14 @@ pulls:
|
|
|
38
38
|
- `<$CNB_CLI_CMD$> pulls upload-image --file 图片路径` — 上传图片到当前 PR
|
|
39
39
|
|
|
40
40
|
注意事项:
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
-
|
|
44
|
-
-
|
|
41
|
+
- **参数自动识别**:快捷命令中的 Issue/PR 编号会自动从环境变量识别,无需额外传递。
|
|
42
|
+
- **默认仅需摘要**:默认会精简响应输出结果,只返回核心字段。添加 `--verbose` 输出完整数据。
|
|
43
|
+
- **单引号传参**:传递多行文本参数时,使用单引号可防止命令注入攻击,并减少不必要的转义。
|
|
44
|
+
- **快捷命令适用范围**: 快捷命令只能操作当前仓库的当前 Issue/PR,跨仓库或跨编号操作请参考 `更多 API`。
|
|
45
|
+
- **npc提及和召唤的区别**: 评论中直接 @npc 会召唤 npc 干活,如果只提及不召唤,应该去掉 `@` 符号,或使用反引号包裹 `@npc`。
|
|
45
46
|
|
|
46
47
|
## 更多 API
|
|
47
48
|
|
|
48
|
-
优先使用快捷命令,不满足时才使用以下这些命令
|
|
49
|
-
|
|
50
49
|
1. `<$CNB_CLI_CMD$> --help` 查看所有模块
|
|
51
50
|
2. `<$CNB_CLI_CMD$> <module> --help` 查看模块下的工具列表
|
|
52
51
|
3. `<$CNB_CLI_CMD$> <module> <tool> --help` 查看工具参数
|
|
53
|
-
4. 按帮助文档操作,禁止猜测
|