feat(export-import): add yaml export/import with id upsert for credentials, snippets, and scenarios

- all entities export a kind field (credential/snippet/scenario) for safe type checking on import
- import upserts by id: overwrites if id exists, creates with explicit id otherwise
- scenario export now includes id and step ids; import deletes old steps before recreating
- add GET /:id/export and POST /import endpoints to credential and snippet controllers
- add UuidBadge ui component: shortens uuid to first+last 4 hex chars, tooltip, clipboard copy with animated ClipboardCheck icon pop
- apply UuidBadge across all entity id display sites (detail pages, card footers, table columns)
- add export/import buttons to CredentialsPage, SnippetsPage, CredentialDetailPage, SnippetDetailPage
This commit is contained in:
ars9 committed 2026-04-10 13:09:09 +03:00
1 parent 1164289173
commit 32be7c0a59
54 files changed
+728 -272

No files matched your search

@@ -1,12 +1,10 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsInt, IsNotEmpty, IsPositive, IsString } from "class-validator";
import { IsNotEmpty, IsString, IsUUID } from "class-validator";
export class AddScenarioCredentialDto {
@ApiProperty({ example: 1 })
@IsInt()
@IsPositive()
credentialId: number;
@ApiProperty({ example: "uuid-here" })
@IsUUID()
credentialId: string
@ApiProperty({ example: "api_key" })
@IsString()
@IsNotEmpty()
+20 -2
View File
@@ -7,6 +7,7 @@ import {
IsNotEmpty,
IsOptional,
IsString,
IsUUID,
Min,
ValidateNested,
} from "class-validator";
@@ -22,10 +23,11 @@ export class ScenarioStepExportDto {
@IsIn(["login", "exec", "sign"])
type: StepType;
@ApiProperty()
@ApiPropertyOptional()
@IsOptional()
@IsString()
@IsNotEmpty()
sessionName: string | null;
title: string | null;
@ApiPropertyOptional()
@IsOptional()
@@ -41,6 +43,22 @@ export class ScenarioStepExportDto {
}
export class ScenarioExportDto {
@ApiPropertyOptional()
@IsOptional()
@IsIn(["scenario"])
kind?: "scenario";
@ApiPropertyOptional()
@IsOptional()
@IsUUID()
id?: string;
@ApiPropertyOptional()
@IsOptional()
@IsString()
@IsNotEmpty()
title?: string;
@ApiProperty()
@IsString()
@IsNotEmpty()
@@ -13,14 +13,14 @@ import { CredentialEntity } from "../credential/credential.entity";
@Entity("scenario_credentials")
@Unique(["scenarioId", "alias"])
export class ScenarioCredentialEntity {
@PrimaryGeneratedColumn()
id: number;
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
scenarioId: number;
@Column("text")
scenarioId: string;
@Column()
credentialId: number;
@Column("text")
credentialId: string;
@Column()
alias: string;
@@ -13,18 +13,18 @@ export type LogLevel = "log" | "warn" | "error";
@Entity("scenario_run_logs")
export class ScenarioRunLogEntity {
@PrimaryGeneratedColumn()
id: number;
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
runId: number;
@Column("text")
runId: string;
@ManyToOne(() => ScenarioRunEntity, { onDelete: "CASCADE" })
@JoinColumn({ name: "runId" })
run: ScenarioRunEntity;
@Column({ nullable: true })
stepRunId: number | null;
@Column({ type: "text", nullable: true })
stepRunId: string | null;
@ManyToOne(() => ScenarioRunStepEntity, {
onDelete: "SET NULL",
@@ -20,11 +20,11 @@ export type RunStepStatus =
@Entity("scenario_run_steps")
export class ScenarioRunStepEntity {
@PrimaryGeneratedColumn()
id: number;
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
runId: number;
@Column("text")
runId: string;
@ManyToOne(() => ScenarioRunEntity, (run) => run.stepRuns, {
onDelete: "CASCADE",
@@ -32,8 +32,8 @@ export class ScenarioRunStepEntity {
@JoinColumn({ name: "runId" })
run: ScenarioRunEntity;
@Column()
scenarioStepId: number;
@Column("text")
scenarioStepId: string;
@ManyToOne(() => ScenarioStepEntity, { onDelete: "CASCADE" })
@JoinColumn({ name: "scenarioStepId" })
+4 -4
View File
@@ -15,11 +15,11 @@ export type RunStatus = "pending" | "in_progress" | "pass" | "fail";
@Entity("scenario_runs")
export class ScenarioRunEntity {
@PrimaryGeneratedColumn()
id: number;
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
scenarioId: number;
@Column("text")
scenarioId: string;
@ManyToOne(() => ScenarioEntity, { onDelete: "CASCADE" })
@JoinColumn({ name: "scenarioId" })
@@ -33,14 +33,14 @@ interface BrowserHandle {
@Injectable()
export class ScenarioSchedulerService {
private readonly logger = new TraceLogger(ScenarioSchedulerService.name);
private readonly activeRuns = new Set<number>();
private readonly runBrowsers = new Map<number, BrowserHandle>();
private readonly activeRuns = new Set<string>();
private readonly runBrowsers = new Map<string, BrowserHandle>();
// Cache credential maps per run (built once when a run starts)
private readonly runCredentials = new Map<number, Record<string, unknown>>();
private readonly runCredentials = new Map<string, Record<string, unknown>>();
// Cache environment URLs per run (resolved from the first login step)
private readonly runEnvironments = new Map<number, EnvironmentUrls>();
private readonly runEnvironments = new Map<string, EnvironmentUrls>();
// Cache snippet code map per run (built once when a run starts)
private readonly runSnippets = new Map<number, Record<string, string>>();
private readonly runSnippets = new Map<string, Record<string, string>>();
constructor(
@InjectRepository(ScenarioRunEntity)
@@ -57,8 +57,8 @@ export class ScenarioSchedulerService {
) {}
private persistLog(
runId: number,
stepRunId: number | null,
runId: string,
stepRunId: string | null,
level: "log" | "warn" | "error",
message: string,
): void {
@@ -67,7 +67,7 @@ export class ScenarioSchedulerService {
);
}
private stepLogger(stepRunId: number, runId: number): ScriptLogger {
private stepLogger(stepRunId: string, runId: string): ScriptLogger {
return (level, msg) => {
this.logger[level](`StepRun #${stepRunId} script: ${msg}`);
this.persistLog(runId, stepRunId, level, msg);
@@ -98,7 +98,7 @@ export class ScenarioSchedulerService {
* no login step or the environment cannot be found.
*/
private async resolveRunEnvironment(
scenarioId: number,
scenarioId: string,
): Promise<EnvironmentUrls | null> {
try {
const scenario = await this.scenarioService.findOne(scenarioId);
@@ -147,7 +147,7 @@ export class ScenarioSchedulerService {
}
}
private async processRunToCompletion(runId: number): Promise<void> {
private async processRunToCompletion(runId: string): Promise<void> {
try {
let stepRun = await this.runStepRepo.findOne({
where: { runId, status: "pending" },
@@ -207,7 +207,7 @@ export class ScenarioSchedulerService {
// ── Shared browser per run ─────────────────────────────────────────────────
private async getOrCreateBrowserHandle(runId: number): Promise<BrowserHandle> {
private async getOrCreateBrowserHandle(runId: string): Promise<BrowserHandle> {
const existing = this.runBrowsers.get(runId);
if (existing) return existing;
@@ -224,7 +224,7 @@ export class ScenarioSchedulerService {
return handle;
}
private async closeBrowserHandle(runId: number): Promise<void> {
private async closeBrowserHandle(runId: string): Promise<void> {
const handle = this.runBrowsers.get(runId);
if (!handle) return;
this.runBrowsers.delete(runId);
+4 -4
View File
@@ -13,11 +13,11 @@ export type StepType = "login" | "exec" | "sign";
@Entity("scenario_steps")
export class ScenarioStepEntity {
@PrimaryGeneratedColumn()
id: number;
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
scenarioId: number;
@Column("text")
scenarioId: string;
@ManyToOne(() => ScenarioEntity, (scenario) => scenario.steps, {
onDelete: "CASCADE",
+22 -22
View File
@@ -5,7 +5,7 @@ import {
Get,
HttpCode,
Param,
ParseIntPipe,
ParseUUIDPipe,
Patch,
Post,
Query,
@@ -61,7 +61,7 @@ export class ScenarioController {
@ApiOperation({ summary: "Get a scenario with its steps" })
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario not found" })
findOne(@Param("id", ParseIntPipe) id: number) {
findOne(@Param("id", ParseUUIDPipe) id: string) {
return this.scenarioService.findOne(id);
}
@@ -70,7 +70,7 @@ export class ScenarioController {
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario not found" })
update(
@Param("id", ParseIntPipe) id: number,
@Param("id", ParseUUIDPipe) id: string,
@Body() dto: UpdateScenarioDto,
) {
return this.scenarioService.update(id, dto);
@@ -81,7 +81,7 @@ export class ScenarioController {
@ApiOperation({ summary: "Delete a scenario and all its steps" })
@ApiResponse({ status: 204 })
@ApiResponse({ status: 404, description: "Scenario not found" })
remove(@Param("id", ParseIntPipe) id: number) {
remove(@Param("id", ParseUUIDPipe) id: string) {
return this.scenarioService.remove(id);
}
@@ -89,7 +89,7 @@ export class ScenarioController {
@ApiOperation({ summary: "Export a scenario as a portable JSON payload" })
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario not found" })
exportScenario(@Param("id", ParseIntPipe) id: number) {
exportScenario(@Param("id", ParseUUIDPipe) id: string) {
return this.scenarioService.exportScenario(id);
}
@@ -100,7 +100,7 @@ export class ScenarioController {
@ApiResponse({ status: 201, description: "Step created" })
@ApiResponse({ status: 404, description: "Scenario not found" })
createStep(
@Param("id", ParseIntPipe) id: number,
@Param("id", ParseUUIDPipe) id: string,
@Body() dto: CreateScenarioStepDto,
) {
return this.scenarioService.createStep(id, dto);
@@ -111,8 +111,8 @@ export class ScenarioController {
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario or step not found" })
findStep(
@Param("id", ParseIntPipe) id: number,
@Param("stepId", ParseIntPipe) stepId: number,
@Param("id", ParseUUIDPipe) id: string,
@Param("stepId", ParseUUIDPipe) stepId: string,
) {
return this.scenarioService.findStep(id, stepId);
}
@@ -122,8 +122,8 @@ export class ScenarioController {
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario or step not found" })
updateStep(
@Param("id", ParseIntPipe) id: number,
@Param("stepId", ParseIntPipe) stepId: number,
@Param("id", ParseUUIDPipe) id: string,
@Param("stepId", ParseUUIDPipe) stepId: string,
@Body() dto: UpdateScenarioStepDto,
) {
return this.scenarioService.updateStep(id, stepId, dto);
@@ -135,8 +135,8 @@ export class ScenarioController {
@ApiResponse({ status: 204 })
@ApiResponse({ status: 404, description: "Scenario or step not found" })
removeStep(
@Param("id", ParseIntPipe) id: number,
@Param("stepId", ParseIntPipe) stepId: number,
@Param("id", ParseUUIDPipe) id: string,
@Param("stepId", ParseUUIDPipe) stepId: string,
) {
return this.scenarioService.removeStep(id, stepId);
}
@@ -147,7 +147,7 @@ export class ScenarioController {
@ApiOperation({ summary: "List credentials assigned to a scenario" })
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario not found" })
findScenarioCredentials(@Param("id", ParseIntPipe) id: number) {
findScenarioCredentials(@Param("id", ParseUUIDPipe) id: string) {
return this.scenarioService.findScenarioCredentials(id);
}
@@ -157,7 +157,7 @@ export class ScenarioController {
@ApiResponse({ status: 404, description: "Scenario or credential not found" })
@ApiResponse({ status: 409, description: "Alias already used in this scenario" })
addScenarioCredential(
@Param("id", ParseIntPipe) id: number,
@Param("id", ParseUUIDPipe) id: string,
@Body() dto: AddScenarioCredentialDto,
) {
return this.scenarioService.addScenarioCredential(id, dto);
@@ -169,8 +169,8 @@ export class ScenarioController {
@ApiResponse({ status: 204 })
@ApiResponse({ status: 404, description: "Scenario or credential assignment not found" })
removeScenarioCredential(
@Param("id", ParseIntPipe) id: number,
@Param("scCredId", ParseIntPipe) scCredId: number,
@Param("id", ParseUUIDPipe) id: string,
@Param("scCredId", ParseUUIDPipe) scCredId: string,
) {
return this.scenarioService.removeScenarioCredential(id, scCredId);
}
@@ -184,7 +184,7 @@ export class ScenarioController {
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario not found" })
findRuns(
@Param("id", ParseIntPipe) id: number,
@Param("id", ParseUUIDPipe) id: string,
@Query() query: RunsQueryDto,
) {
return this.scenarioService.findRuns(id, query);
@@ -194,7 +194,7 @@ export class ScenarioController {
@ApiOperation({ summary: "Create a new run for a scenario" })
@ApiResponse({ status: 201, description: "Run created with step runs" })
@ApiResponse({ status: 404, description: "Scenario not found" })
createRun(@Param("id", ParseIntPipe) id: number) {
createRun(@Param("id", ParseUUIDPipe) id: string) {
return this.scenarioService.createRun(id);
}
@@ -203,8 +203,8 @@ export class ScenarioController {
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario or run not found" })
findRun(
@Param("id", ParseIntPipe) id: number,
@Param("runId", ParseIntPipe) runId: number,
@Param("id", ParseUUIDPipe) id: string,
@Param("runId", ParseUUIDPipe) runId: string,
) {
return this.scenarioService.findRun(id, runId);
}
@@ -218,8 +218,8 @@ export class ScenarioController {
@ApiResponse({ status: 200 })
@ApiResponse({ status: 404, description: "Scenario or run not found" })
waitForRun(
@Param("id", ParseIntPipe) id: number,
@Param("runId", ParseIntPipe) runId: number,
@Param("id", ParseUUIDPipe) id: string,
@Param("runId", ParseUUIDPipe) runId: string,
) {
return this.scenarioService.waitForRun(id, runId);
}
+2 -2
View File
@@ -11,8 +11,8 @@ import { ScenarioCredentialEntity } from "./scenario-credential.entity";
@Entity("scenarios")
export class ScenarioEntity {
@PrimaryGeneratedColumn()
id: number;
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
name: string;
+44 -26
View File
@@ -63,7 +63,7 @@ export class ScenarioService {
return { data, total, page, limit };
}
async findOne(id: number): Promise<ScenarioEntity> {
async findOne(id: string): Promise<ScenarioEntity> {
const scenario = await this.scenarioRepo.findOne({
where: { id },
relations: ["steps", "scenarioCredentials", "scenarioCredentials.credential"],
@@ -73,13 +73,13 @@ export class ScenarioService {
return scenario;
}
async update(id: number, dto: UpdateScenarioDto): Promise<ScenarioEntity> {
async update(id: string, dto: UpdateScenarioDto): Promise<ScenarioEntity> {
const scenario = await this.findOne(id);
Object.assign(scenario, dto);
return this.scenarioRepo.save(scenario);
}
async remove(id: number): Promise<void> {
async remove(id: string): Promise<void> {
await this.findOne(id);
await this.scenarioRepo.delete(id);
}
@@ -87,7 +87,7 @@ export class ScenarioService {
// ── Steps ─────────────────────────────────────────────────────────────────
async createStep(
scenarioId: number,
scenarioId: string,
dto: CreateScenarioStepDto,
): Promise<ScenarioStepEntity> {
await this.findOne(scenarioId);
@@ -102,8 +102,8 @@ export class ScenarioService {
}
async findStep(
scenarioId: number,
stepId: number,
scenarioId: string,
stepId: string,
): Promise<ScenarioStepEntity> {
const step = await this.stepRepo.findOneBy({ id: stepId, scenarioId });
if (!step)
@@ -114,8 +114,8 @@ export class ScenarioService {
}
async updateStep(
scenarioId: number,
stepId: number,
scenarioId: string,
stepId: string,
dto: UpdateScenarioStepDto,
): Promise<ScenarioStepEntity> {
const step = await this.findStep(scenarioId, stepId);
@@ -123,7 +123,7 @@ export class ScenarioService {
return this.stepRepo.save(step);
}
async removeStep(scenarioId: number, stepId: number): Promise<void> {
async removeStep(scenarioId: string, stepId: string): Promise<void> {
await this.findStep(scenarioId, stepId);
await this.stepRepo.delete(stepId);
}
@@ -131,7 +131,7 @@ export class ScenarioService {
// ── Scenario Credentials ──────────────────────────────────────────────────
async findScenarioCredentials(
scenarioId: number,
scenarioId: string,
): Promise<ScenarioCredentialEntity[]> {
await this.findOne(scenarioId); // 404 guard
return this.scenarioCredRepo.find({
@@ -142,7 +142,7 @@ export class ScenarioService {
}
async addScenarioCredential(
scenarioId: number,
scenarioId: string,
dto: AddScenarioCredentialDto,
): Promise<ScenarioCredentialEntity> {
await this.findOne(scenarioId); // 404 guard
@@ -168,8 +168,8 @@ export class ScenarioService {
}
async removeScenarioCredential(
scenarioId: number,
scCredId: number,
scenarioId: string,
scCredId: string,
): Promise<void> {
await this.findOne(scenarioId); // 404 guard
const sc = await this.scenarioCredRepo.findOneBy({
@@ -187,7 +187,7 @@ export class ScenarioService {
* Builds a map of alias → parsed credential data for use in code execution.
* Returns null values for credentials with no data.
*/
async buildCredentialMap(scenarioId: number): Promise<Record<string, unknown>> {
async buildCredentialMap(scenarioId: string): Promise<Record<string, unknown>> {
const scs = await this.findScenarioCredentials(scenarioId);
const map: Record<string, unknown> = {};
for (const sc of scs) {
@@ -207,7 +207,7 @@ export class ScenarioService {
// ── Runs ──────────────────────────────────────────────────────────────────
async findRuns(
scenarioId: number,
scenarioId: string,
query: RunsQueryDto,
): Promise<PaginatedResult<ScenarioRunEntity>> {
await this.findOne(scenarioId); // 404 guard
@@ -245,8 +245,8 @@ export class ScenarioService {
}
async findRun(
scenarioId: number,
runId: number,
scenarioId: string,
runId: string,
): Promise<ScenarioRunEntity & { logs: ScenarioRunLogEntity[] }> {
await this.findOne(scenarioId); // 404 guard
const run = await this.runRepo.findOne({
@@ -266,8 +266,8 @@ export class ScenarioService {
}
async waitForRun(
scenarioId: number,
runId: number,
scenarioId: string,
runId: string,
timeoutMs = 300_000,
): Promise<ScenarioRunEntity & { logs: ScenarioRunLogEntity[] }> {
const deadline = Date.now() + timeoutMs;
@@ -285,7 +285,7 @@ export class ScenarioService {
return this.findRun(scenarioId, runId);
}
async createRun(scenarioId: number): Promise<ScenarioRunEntity> {
async createRun(scenarioId: string): Promise<ScenarioRunEntity> {
const scenario = await this.findOne(scenarioId);
const run = await this.runRepo.save(
@@ -313,14 +313,16 @@ export class ScenarioService {
// ── Export / Import ───────────────────────────────────────────────────────
async exportScenario(id: number): Promise<ScenarioExportDto> {
async exportScenario(id: string): Promise<ScenarioExportDto> {
const scenario = await this.findOne(id);
return {
kind: "scenario",
id: scenario.id,
name: scenario.name,
steps: scenario.steps.map((s) => ({
order: s.order,
type: s.type,
sessionName: s.sessionName,
title: s.title,
execCode: s.execCode,
validateCode: s.validateCode,
})),
@@ -328,16 +330,32 @@ export class ScenarioService {
}
async importScenario(dto: ScenarioExportDto): Promise<ScenarioEntity> {
const scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({ name: dto.name }),
);
// Upsert: if id provided and entity exists, replace steps; else create new
let scenario: ScenarioEntity;
if (dto.id) {
const existing = await this.scenarioRepo.findOneBy({ id: dto.id });
if (existing) {
existing.name = dto.name;
scenario = await this.scenarioRepo.save(existing);
// Delete old steps and recreate
await this.stepRepo.delete({ scenarioId: scenario.id });
} else {
scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({ id: dto.id, name: dto.name }),
);
}
} else {
scenario = await this.scenarioRepo.save(
this.scenarioRepo.create({ name: dto.name }),
);
}
if (dto.steps.length > 0) {
const steps = dto.steps.map((s) =>
this.stepRepo.create({
scenarioId: scenario.id,
order: s.order,
type: s.type,
sessionName: s.sessionName,
title: s.title ?? null,
execCode: s.execCode ?? null,
validateCode: s.validateCode ?? null,
}),