diff --git a/README.md b/README.md index 7efc30c..81356c1 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ This way even entityIds like environmentIds or testCaseIds will be autocompleted # octomind -Octomind cli tool. Version: 1.3.2. Additional documentation see https://octomind.dev/docs/api-reference/ +Octomind cli tool. Version: 1.3.1. Additional documentation see https://octomind.dev/docs/api-reference/ **Usage:** `octomind [options] [command]` @@ -351,6 +351,22 @@ Delete a test case | `-c, --test-case-id ` | Test case ID | Yes | | | `-t, --test-target-id [id]` | Test target ID, if not provided will use the test target id from the config | No | | +## code + +Get code of a specific test case + +**Usage:** `code [options]` + +### Options + +| Option | Description | Required | Default | +|:-------|:----------|:---------|:--------| +| `-j, --json` | Output raw JSON response | No | | +| `-c, --test-case-id ` | Test case ID | Yes | | +| `-u, --url ` | URL to execute the test case against | Yes | | +| `-e, --environment-id [id]` | Environment ID | No | default | +| `-t, --test-target-id [id]` | Test target ID, if not provided will use the test target id from the config | No | | + ## test-case Get details of a specific test case diff --git a/package.json b/package.json index 4bc6072..ffe3e31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@octomind/octomind", - "version": "1.3.2", + "version": "1.3.3", "description": "a command line client for octomind apis", "main": "./dist/index.js", "packageManager": "pnpm@10.13.1", diff --git a/src/cli.ts b/src/cli.ts index 3ca9f63..ee43b93 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -28,6 +28,7 @@ import { GetTestCaseParams, GetTestReportParams, getEnvironment, + getTestCaseCode, listEnvironments, listNotifications, listPrivateLocations, @@ -299,6 +300,17 @@ export const buildCmd = (): CompletableCommand => { .helpGroup("test-cases") .action(addTestTargetWrapper(deleteTestCase)); + createCommandWithCommonOptions(program, "code") + .completer(testCaseIdCompleter) + .completer(testTargetIdCompleter) + .description("Get code of a specific test case") + .helpGroup("test-cases") + .requiredOption("-c, --test-case-id ", "Test case ID") + .requiredOption("-u, --url ", "URL to execute the test case against") + .option("-e, --environment-id [id]", "Environment ID", "default") + .addOption(testTargetIdOption) + .action(addTestTargetWrapper(getTestCaseCode)); + createCommandWithCommonOptions(program, "test-case") .completer(testCaseIdCompleter) .completer(testTargetIdCompleter) diff --git a/src/tools/client.ts b/src/tools/client.ts index 18cbc09..d5c93ac 100644 --- a/src/tools/client.ts +++ b/src/tools/client.ts @@ -2,6 +2,7 @@ import createClient, { Middleware } from "openapi-fetch"; import type { components, paths } from "../api"; // generated by openapi-typescript import { loadConfig } from "../config"; +import { version } from "../version"; export const BASE_URL = process.env.OCTOMIND_API_URL || "https://app.octomind.dev/api"; @@ -23,6 +24,7 @@ const authMiddleware: Middleware = { ); } request.headers.set("x-api-key", apiKey); + request.headers.set("user-agent", `octomind-cli/${version}`); return request; }, async onResponse({ response }) { diff --git a/src/tools/test-cases.ts b/src/tools/test-cases.ts index 9f4242e..666e756 100644 --- a/src/tools/test-cases.ts +++ b/src/tools/test-cases.ts @@ -1,6 +1,7 @@ import type { components, paths } from "../api"; // generated by openapi-typescript import { getUrl } from "../url"; import { client, handleError, ListOptions, logJson } from "./client"; +import { getEnvironments } from "./environments"; export type TestCaseResponse = components["schemas"]["TestCaseResponse"]; export type TestCasesResponse = components["schemas"]["TestCasesResponse"]; @@ -122,6 +123,52 @@ export const getTestCases = async ( return data; }; + +export const getTestCaseCode = async ( + options: GetTestCaseParams & + ListOptions & { + url: string; + environmentId?: string; + }, +): Promise => { + if (options.environmentId === "default") { + const environments = await getEnvironments({ + testTargetId: options.testTargetId, + }); + options.environmentId = environments.find((e) => e.type === "DEFAULT")?.id; + if (!options.environmentId) { + throw new Error("no default environment found"); + } + } + const { data, error } = await client.GET( + "/apiKey/v2/test-targets/{testTargetId}/test-cases/{testCaseId}/code", + { + params: { + path: { + testTargetId: options.testTargetId, + testCaseId: options.testCaseId, + }, + query: { + executionUrl: options.url, + environmentId: options.environmentId, + }, + }, + }, + ); + + handleError(error); + + if (options.json) { + logJson(data); + return; + } + + if (!data) { + throw new Error("no test code found"); + } + console.log(data.testCode); +}; + export const listTestCases = async ( options: GetTestCasesOptions & ListOptions, ): Promise => {