Files
qinglong/cli/src/remote/commands/subscription.ts
T
whyour 801a71d740 feat(cli): cover OpenAPI with a remote npm CLI and internal panel tools (#3074)
* feat(cli): add unified Commander CLI for QingLong 2.x

* fix(cli): publish via npm and address security review feedback

* ci(cli): package npm artifacts and remove evaluation collateral

* test(cli): use a fixed shell fixture for log retention

* refactor(cli): separate remote npm client from panel tools

* feat(cli): cover active panel OpenAPI resources

* docs(cli): unify authentication and skill guidance

* refactor(cli): isolate internal commands and generate Commander help

* refactor(cli): organize remote and internal modules by responsibility

* ci(cli): publish verified npm archives from master

* fix(cli): publish under the whyour npm scope

* ci: use npm trusted publishing for both packages

* docs: introduce the published CLI on the project homepage

* fix(cli): preserve server log truncation and correct login hints

* fix(cli): accept dashboard record request bodies

* fix(cli): preserve stdin for local task execution

* fix(cli): resolve task executables after changing directory

* fix(cli): preserve shell function tasks and sanitize test failures

* fix(cli): preserve shell hook state and resolve workdir after hooks

* fix(cli): preserve cleanup across shared shell task timeouts

* fix(cli): isolate shell control descriptors and reap timed-out descendants
2026-09-25 23:24:41 +08:00

92 lines
2.9 KiB
TypeScript

import { translate } from '../../shared/i18n/index';
import type { Invocation } from '../../shared/cli/arguments';
import { isRecord, request } from '../api/client';
import { session } from './auth';
import { fail } from '../../shared/errors';
import type { ApiResponse, LogResponse } from '../../shared/types';
// Subscription API records can contain private repository credentials and hooks.
// The management view deliberately exposes only the fields needed for selection.
const visibleFields = [
'id',
'name',
'alias',
'type',
'status',
'is_disabled',
'schedule',
'schedule_type',
'interval_schedule',
'branch',
'whitelist',
'blacklist',
'dependences',
'extensions',
'autoAddCron',
'autoDelCron',
'createdAt',
'updatedAt',
];
function projectSubscription(value: unknown): Record<string, unknown> {
if (
!isRecord(value) ||
typeof value.id !== 'number' ||
!Number.isSafeInteger(value.id) ||
value.id <= 0
)
fail(translate(process.env, '订阅响应无效。'));
return Object.fromEntries(
visibleFields
.filter((field) => value[field] !== undefined)
.map((field) => [field, value[field]]),
);
}
export async function subscription(
command: Invocation,
): Promise<ApiResponse<unknown> | LogResponse> {
const config = await session();
const action = command.name.split(' ')[1]!;
if (action === 'list') {
const search =
typeof command.values.search === 'string'
? `?${new URLSearchParams({ searchValue: command.values.search })}`
: '';
const result = await request(config, `subscriptions${search}`);
if (!Array.isArray(result.data))
fail(translate(process.env, '订阅列表响应无效。'));
return { code: 200, data: result.data.map(projectSubscription) };
}
const id = Number(command.positionals[0]);
if (action === 'get') {
const result = projectSubscription(
(await request(config, `subscriptions/${id}`)).data,
);
if (result.id !== id)
fail(translate(process.env, '返回的订阅 ID 与请求不一致。'));
return { code: 200, data: result };
}
if (action === 'logs') {
const result = await request(config, `subscriptions/${id}/log`);
if (typeof result.data !== 'string')
fail(translate(process.env, '订阅日志响应无效。'));
const lines = result.data.replace(/\r\n/g, '\n').split('\n');
if (lines.at(-1) === '') lines.pop();
const count = Number(command.values.tail);
return {
code: 200,
data: lines.slice(-count).join('\n'),
truncated: result.truncated === true || lines.length > count,
};
}
if (!['run', 'stop', 'enable', 'disable'].includes(action))
fail(translate(process.env, '不支持的订阅操作。'), 2);
await request(config, `subscriptions/${action}`, {
method: 'PUT',
body: [id],
});
return { code: 200, data: { subscriptionId: id, action, accepted: true } };
}