import type { EnvironmentsConfig } from "@allurereport/core-api"; import { UsageError } from "clipanion"; import { normalizeCommandEnvironmentOptions } from "./environment.js"; const SAFE_TOKEN = /^[A-Za-z0-9._/:@=-]+$/; // control, format (zero-width, bidi, tag characters, soft hyphen), line and paragraph separators: // anything that can make the printed workflow look different from what is actually executed const INVISIBLE_CHARS = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u; // Versions are the ones used by this repository's own workflow; they are examples, pin the ones you use const CHECKOUT_ACTION = "actions/checkout@v6"; const UPLOAD_ARTIFACT_ACTION = "actions/upload-artifact@v7"; const DOWNLOAD_ARTIFACT_ACTION = "actions/download-artifact@v8"; const DEFAULT_TEST_COMMAND: readonly string[] = ["npm", "test"]; const ENVIRONMENT = "${{ matrix.environment }}"; const NOT_CANCELLED = "${{ !cancelled() }}"; export type GhWorkflowOptions = { environmentIds: readonly string[]; testCommand?: readonly string[]; config?: string; }; export const shellQuote = (value: string): string => // a word starting with "=" would be expanded by zsh (EQUALS option), so it is quoted as well SAFE_TOKEN.test(value) && !value.startsWith("=") ? value : `'${value.replace(/'/g, `'\\''`)}'`; const assertPrintable = (label: string, value: string): string => { if (INVISIBLE_CHARS.test(value)) { throw new UsageError(`${label} must not contain control or invisible characters`); } return value; }; /** Validates ids given with `--env` with the same rules as `allure run --environment`; they need not be declared. */ export const normalizeEnvironmentIds = (values: readonly string[]): string[] => [ ...new Set(values.map((value) => normalizeCommandEnvironmentOptions({ environment: value }).environment as string)), ]; /** Ids declared in the config (integer-like ids first, as JavaScript orders object keys). */ export const configuredEnvironmentIds = (environments: EnvironmentsConfig | undefined): string[] => { const ids = Object.keys(environments ?? {}); if (ids.length === 0) { throw new UsageError( "The Allure config has no environments (or no allurerc file was found). " + "Declare `environments` in the config or pass environment ids with --env", ); } return ids; }; /** * Renders two jobs, `test` (one matrix entry per environment: run with a dump and upload it) and `report` * (download all dumps and generate one report), as children of the workflow `jobs` mapping. The environment, * dump file, artifact and download names have to agree, so they are derived from one place. */ export const buildGhWorkflowYaml = ({ environmentIds, testCommand = DEFAULT_TEST_COMMAND, config, }: GhWorkflowOptions): string => { if (environmentIds.length === 0) { throw new UsageError("At least one environment is required"); } const testCommandLine = testCommand.map((token) => shellQuote(assertPrintable("The test command", token))).join(" "); const configOption = config === undefined ? "" : ` --config=${shellQuote(assertPrintable("--config", config))}`; // JSON strings are valid YAML double-quoted scalars, and keep ids like `on` or `123` from becoming booleans or numbers const matrix = environmentIds.map((id) => JSON.stringify(id)).join(", "); const results = `allure-results-${ENVIRONMENT}`; return `# Generated by "allure gh-workflow". Paste it under "jobs:" and adjust the TODO steps. test: name: Test (${ENVIRONMENT}) runs-on: ubuntu-latest strategy: fail-fast: false matrix: environment: [${matrix}] steps: - uses: ${CHECKOUT_ACTION} # TODO: set up your runtime and install dependencies - name: Test run: | npx allure run${configOption} --environment=${ENVIRONMENT} --dump=${results} -- ${testCommandLine} - name: Upload Allure results if: ${NOT_CANCELLED} uses: ${UPLOAD_ARTIFACT_ACTION} with: name: ${results} path: ./${results}.zip report: name: Report needs: [test] if: ${NOT_CANCELLED} runs-on: ubuntu-latest steps: - uses: ${CHECKOUT_ACTION} # TODO: set up your runtime and install dependencies - name: Download Allure results uses: ${DOWNLOAD_ARTIFACT_ACTION} with: pattern: allure-results-* path: ./ - name: Generate report run: | npx allure generate${configOption} --dump="allure-results-*/*.zip" --output=./allure-report `; };