feat(scenarios): export used snippets with a scenario
- list the snippets a scenario uses in the export dialog, checked by default, with the versions the scenario uses - export each selected snippet once per used version (latest and pinned) via the new snippetIds query parameter - resolve used snippets from the exported version's steps - show the scenario version in the export dialog - document the export query parameters - bump version to 1.22.0
This commit is contained in:
1 parent
f43d902297
commit
23fa302b25
13 files changed
+163
-18
No files matched your search
@@ -13,6 +13,7 @@ import {
|
||||
ValidateNested,
|
||||
} from "class-validator";
|
||||
import { EnvironmentData } from "../../environment/environment.entity";
|
||||
import { SnippetExportDto } from "../../snippet/dto/snippet-export.dto";
|
||||
|
||||
export class ScenarioStepExportDto {
|
||||
@ApiPropertyOptional()
|
||||
@@ -154,4 +155,5 @@ export class ScenarioExportDto {
|
||||
export type ExportEntity =
|
||||
| EnvironmentExportInlineDto
|
||||
| CredentialExportDto
|
||||
| SnippetExportDto
|
||||
| ScenarioExportDto;
|
||||
@@ -135,18 +135,17 @@ export class ScenarioController {
|
||||
@Param("id", ParseUUIDPipe) id: string,
|
||||
@Query("includeEnvironment") includeEnvironment: string,
|
||||
@Query("credentialIds") credentialIds: string | string[] | undefined,
|
||||
@Query("snippetIds") snippetIds: string | string[] | undefined,
|
||||
@Query("version", new ParseIntPipe({ optional: true }))
|
||||
version: number | undefined,
|
||||
@Res({ passthrough: true }) res: Response,
|
||||
) {
|
||||
const credIds: string[] = credentialIds
|
||||
? Array.isArray(credentialIds)
|
||||
? credentialIds
|
||||
: credentialIds.split(",").filter(Boolean)
|
||||
: [];
|
||||
const toIds = (ids: string | string[] | undefined): string[] =>
|
||||
ids ? (Array.isArray(ids) ? ids : ids.split(",").filter(Boolean)) : [];
|
||||
const entities = await this.scenarioService.exportScenario(id, {
|
||||
includeEnvironment: includeEnvironment === "true",
|
||||
credentialIds: credIds,
|
||||
credentialIds: toIds(credentialIds),
|
||||
snippetIds: toIds(snippetIds),
|
||||
version,
|
||||
});
|
||||
res.setHeader("Content-Type", "application/yaml; charset=utf-8");
|
||||
@@ -155,12 +154,17 @@ export class ScenarioController {
|
||||
const kindLabels: Record<string, string> = {
|
||||
environment: "Environment",
|
||||
credential: "Credential",
|
||||
snippet: "Snippet",
|
||||
scenario: "Scenario",
|
||||
};
|
||||
return (entities as ExportEntity[])
|
||||
.map((entity) => {
|
||||
const label = kindLabels[entity.kind ?? ""] ?? entity.kind ?? "Entity";
|
||||
const comment = `# ${label}: ${entity.name ?? entity.id ?? ""}`;
|
||||
const title =
|
||||
entity.kind === "snippet"
|
||||
? `${entity.alias ?? entity.name} v${entity.version}`
|
||||
: entity.name;
|
||||
const comment = `# ${label}: ${title ?? entity.id ?? ""}`;
|
||||
return `---\n${comment}\n${yamlStringify(entity)}`;
|
||||
})
|
||||
.join("");
|
||||
|
||||
@@ -486,11 +486,18 @@ export class ScenarioService implements OnModuleInit {
|
||||
*/
|
||||
async getUsedSnippets(scenarioId: string): Promise<UsedSnippet[]> {
|
||||
const scenario = await this.findOne(scenarioId);
|
||||
const snippetMap = await this.snippetService.buildSnippetMap();
|
||||
const usedRefs = resolveUsedSnippetAliases(
|
||||
return this.resolveUsedSnippets(
|
||||
scenario.steps.map((s) => s.execCode ?? ""),
|
||||
snippetMap,
|
||||
).map(parseSnippetRef);
|
||||
);
|
||||
}
|
||||
|
||||
private async resolveUsedSnippets(
|
||||
execCodes: string[],
|
||||
): Promise<UsedSnippet[]> {
|
||||
const snippetMap = await this.snippetService.buildSnippetMap();
|
||||
const usedRefs = resolveUsedSnippetAliases(execCodes, snippetMap).map(
|
||||
parseSnippetRef,
|
||||
);
|
||||
if (usedRefs.length === 0) return [];
|
||||
const snippets = await this.snippetService.findByAliases([
|
||||
...new Set(usedRefs.map((r) => r.alias)),
|
||||
@@ -699,6 +706,8 @@ export class ScenarioService implements OnModuleInit {
|
||||
options: {
|
||||
includeEnvironment: boolean;
|
||||
credentialIds: string[];
|
||||
/** Used snippets to include, at every version the scenario uses. */
|
||||
snippetIds?: string[];
|
||||
/** Version to export; defaults to the latest. */
|
||||
version?: number;
|
||||
},
|
||||
@@ -741,6 +750,25 @@ export class ScenarioService implements OnModuleInit {
|
||||
}
|
||||
}
|
||||
|
||||
// Include selected snippets, once per used version (latest or pinned)
|
||||
const snippetIds = options.snippetIds ?? [];
|
||||
if (snippetIds.length > 0) {
|
||||
const used = await this.resolveUsedSnippets(
|
||||
content.steps.map((s) => s.execCode ?? ""),
|
||||
);
|
||||
const seen = new Set<string>();
|
||||
for (const snippet of used) {
|
||||
if (!snippetIds.includes(snippet.id)) continue;
|
||||
const version = snippet.pinnedVersion ?? snippet.version;
|
||||
const key = `${snippet.id}@${version}`;
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
entities.push(
|
||||
await this.snippetService.exportSnippet(snippet, version),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Scenario is always last
|
||||
const allCreds = scenario.scenarioCredentials ?? [];
|
||||
const credentialMappings = allCreds
|
||||
|
||||
@@ -277,6 +277,47 @@ describe("Versioning", () => {
|
||||
expect(first[first.length - 1]).toMatchObject({ version: 1, steps: [] });
|
||||
});
|
||||
|
||||
it("includes selected used snippets at the versions the scenario uses", async () => {
|
||||
const alias = `exp-snip-${uniq()}`;
|
||||
const sn = await snippetService.create({
|
||||
alias,
|
||||
title: "t",
|
||||
code: "return 'one';",
|
||||
});
|
||||
await snippetService.update(sn.id, { code: "return 'two';" });
|
||||
const other = await snippetService.create({
|
||||
alias: `exp-other-${uniq()}`,
|
||||
title: "t",
|
||||
code: "return 3;",
|
||||
});
|
||||
|
||||
const sc = await scenarioService.create({ name: `exp-sn-${uniq()}` });
|
||||
await scenarioService.createStep(sc.id, {
|
||||
execCode: `await context.runSnippet("${alias}"); await context.runSnippet("${alias}@1"); await context.runSnippet("${alias}"); return context.runSnippet("${other.alias}");`,
|
||||
});
|
||||
|
||||
const withSnippet = await scenarioService.exportScenario(sc.id, {
|
||||
includeEnvironment: false,
|
||||
credentialIds: [],
|
||||
snippetIds: [sn.id],
|
||||
});
|
||||
expect(withSnippet.map((e) => e.kind)).toEqual([
|
||||
"snippet",
|
||||
"snippet",
|
||||
"scenario",
|
||||
]);
|
||||
expect(withSnippet.slice(0, 2)).toMatchObject([
|
||||
{ id: sn.id, version: 2, code: "return 'two';" },
|
||||
{ id: sn.id, version: 1, code: "return 'one';" },
|
||||
]);
|
||||
|
||||
const without = await scenarioService.exportScenario(sc.id, {
|
||||
includeEnvironment: false,
|
||||
credentialIds: [],
|
||||
});
|
||||
expect(without.map((e) => e.kind)).toEqual(["scenario"]);
|
||||
});
|
||||
|
||||
it("restores exact versions by id + version", async () => {
|
||||
const id = crypto.randomUUID();
|
||||
const base = { kind: "scenario" as const, id, name: "imported" };
|
||||
|
||||
Reference in new issue
Block a user