Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

- Draw each run before a retry in the timeline on the thread it records (`threadId` on `retryAttempts` entries, from CTRF 0.3.0), falling back to its test's thread.
- Pass the `junit-to-ctrf` integration's `options` (`output`, `toolname`, `useSuiteName`, `env`) to the converter, and skip the conversion when the integration has `"enabled": false`.

## 1.4.0 - 2026-11-21

Expand Down
154 changes: 154 additions & 0 deletions __tests__/ctrf/junit-integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { vi, describe, it, expect, beforeEach } from "vitest";
import { convertJUnitToCTRFReport } from "junit-to-ctrf";
import type { CTRFReport } from "ctrf";
import { prepareReport } from "../../src/ctrf/report-preparation.js";
import { readCtrfReports } from "../../src/utils/index.js";
import type { GitHubContext, Inputs } from "../../src/types/index.js";
import type {
IntegrationsConfig,
JUnitConfig,
} from "../../src/types/integrations.js";

vi.mock("@actions/core");
vi.mock("../../src/utils/index.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../../src/utils/index.js")>()),
readCtrfReports: vi.fn(),
}));

const githubContext = {
job: "unit-tests",
run_number: 7,
run_id: 123,
build_url: "https://github.com/owner/repo/actions/runs/123#summary",
ref: "refs/heads/main",
sha: "abc123",
repoName: "repo",
repository: { html_url: "https://github.com/owner/repo" },
} as GitHubContext;

function createInputs(integrationsConfig: IntegrationsConfig): Inputs {
return {
ctrfPath: "reports/*.xml",
integrationsConfig,
} as Inputs;
}

function createReport(environment?: CTRFReport["results"]["environment"]) {
return {
reportFormat: "CTRF",
specVersion: "0.0.0",
results: {
tool: { name: "junit-to-ctrf" },
summary: {
tests: 0,
passed: 0,
failed: 0,
skipped: 0,
pending: 0,
other: 0,
start: 0,
stop: 0,
},
tests: [],
environment,
},
} as CTRFReport;
}

describe("prepareReport with the junit-to-ctrf integration", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("should pass the integration options to the converter", async () => {
await prepareReport(
createInputs({
"junit-to-ctrf": {
enabled: true,
action: "convert",
options: {
output: "./ctrf-reports/ctrf-report.json",
toolname: "surefire",
useSuiteName: true,
env: {
appName: "my-app",
buildUrl: "https://ci.example.com/job?id=1",
},
},
},
}),
githubContext,
);

expect(convertJUnitToCTRFReport).toHaveBeenCalledWith("reports/*.xml", {
outputPath: "./ctrf-reports/ctrf-report.json",
toolName: "surefire",
useSuiteName: true,
envProps: ["appName=my-app", "buildUrl=https://ci.example.com/job?id=1"],
});
});

it("should convert with default options when none are set", async () => {
await prepareReport(
createInputs({ "junit-to-ctrf": { enabled: true, action: "convert" } }),
githubContext,
);

expect(convertJUnitToCTRFReport).toHaveBeenCalledWith("reports/*.xml", {});
});

it("should keep env values from the config when adding run details", async () => {
vi.mocked(convertJUnitToCTRFReport).mockResolvedValueOnce(
createReport({
appName: "my-app",
buildUrl: "https://ci.example.com/job?id=1",
}),
);

const report = await prepareReport(
createInputs({
"junit-to-ctrf": {
enabled: true,
action: "convert",
options: {
env: {
appName: "my-app",
buildUrl: "https://ci.example.com/job?id=1",
},
},
},
}),
githubContext,
);

expect(report.results.environment).toMatchObject({
appName: "my-app",
buildUrl: "https://ci.example.com/job?id=1",
buildName: "unit-tests",
buildNumber: 7,
});
});

it("should read CTRF reports instead when the integration is disabled", async () => {
vi.mocked(readCtrfReports).mockReturnValueOnce(createReport());

await prepareReport(
createInputs({ "junit-to-ctrf": { enabled: false, action: "convert" } }),
githubContext,
);

expect(convertJUnitToCTRFReport).not.toHaveBeenCalled();
expect(readCtrfReports).toHaveBeenCalledWith("reports/*.xml", undefined);
});

it("should still convert when enabled is not set", async () => {
await prepareReport(
createInputs({
"junit-to-ctrf": { action: "convert" } as JUnitConfig,
}),
githubContext,
);

expect(convertJUnitToCTRFReport).toHaveBeenCalledTimes(1);
});
});
17 changes: 15 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

33 changes: 29 additions & 4 deletions src/ctrf/report-preparation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto";
import * as core from "@actions/core";
import { uploadArtifact } from "../client/github/index.js";
import type { GitHubContext, Inputs } from "../types/index.js";
import type { JUnitConfig } from "../types/integrations.js";
import type { CTRFReport } from "ctrf";
import { readCtrfReports, writeReportToFile } from "../utils/index.js";
import {
Expand All @@ -10,7 +11,7 @@ import {
} from "./enrichers.js";
import { stripAnsiFromErrors } from "./helpers.js";
import { processPreviousResultsAndMetrics } from "./metrics.js";
import { convertJUnitToCTRFReport } from "junit-to-ctrf";
import { convertJUnitToCTRFReport, type ConvertOptions } from "junit-to-ctrf";
import { addFooterDisplayFlags } from "./report-conditionals.js";
import {
prefixTestNames,
Expand Down Expand Up @@ -41,7 +42,10 @@ export async function prepareReport(
core.startGroup(`📜 Preparing CTRF report`);
if (hasJunitIntegration(inputs)) {
core.info("JUnit integration detected");
const junitResult = await convertJUnitToCTRFReport(inputs.ctrfPath);
const junitResult = await convertJUnitToCTRFReport(
inputs.ctrfPath,
getJunitConvertOptions(inputs.integrationsConfig["junit-to-ctrf"]),
);
if (junitResult === null) {
throw new Error(`JUnit report not found at: ${inputs.ctrfPath}`);
}
Expand Down Expand Up @@ -95,11 +99,32 @@ export async function prepareReport(

/**
* Determines if junit-to-ctrf integration is enabled and configured in the inputs.
* This checks if there is a valid junit configuration in the integrationsConfig.
* This checks if there is a junit configuration in the integrationsConfig that
* is not disabled with `"enabled": false`.
*
* @param inputs - The user-provided inputs.
* @returns `true` if junit integration is configured, otherwise `false`.
*/
function hasJunitIntegration(inputs: Inputs): boolean {
return Boolean(inputs.integrationsConfig?.["junit-to-ctrf"]);
const junitConfig = inputs.integrationsConfig?.["junit-to-ctrf"];
return Boolean(junitConfig && junitConfig.enabled !== false);
}

/**
* Maps the junit-to-ctrf integration options to the converter's options.
* `env` is passed as "key=value" strings, the format junit-to-ctrf expects.
*
* @param config - The junit-to-ctrf integration config.
* @returns The options for `convertJUnitToCTRFReport`.
*/
function getJunitConvertOptions(config?: JUnitConfig): ConvertOptions {
const options = config?.options ?? {};
return {
outputPath: options.output,
toolName: options.toolname,
useSuiteName: options.useSuiteName,
envProps: options.env
? Object.entries(options.env).map(([key, value]) => `${key}=${value}`)
: undefined,
};
}
1 change: 1 addition & 0 deletions src/types/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface IntegrationsConfig {
slack?: SlackConfig;
teams?: TeamsConfig;
ai?: AIConfig;
"junit-to-ctrf"?: JUnitConfig;
[key: string]: IntegrationConfig | undefined;
}

Expand Down
Loading