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
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 51 additions & 6 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.

4 changes: 2 additions & 2 deletions src/__mocks__/ctrf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vi } from 'vitest'
import type { Report } from '../ctrf/core/types/ctrf.js'
import type { CTRFReport } from 'ctrf'

const mockMergeReports = vi.fn((reports: Report[]) => {
const mockMergeReports = vi.fn((reports: CTRFReport[]) => {
if (reports.length === 0) return null
if (reports.length === 1) return reports[0]

Expand Down
6 changes: 3 additions & 3 deletions src/__mocks__/junit-to-ctrf.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { vi } from 'vitest'
import type { Report } from '../ctrf/core/types/ctrf.js'
import type { CTRFReport } from 'ctrf'

export const convertJUnitToCTRFReport = vi
.fn()
.mockImplementation(async (): Promise<Report | null> => {
.mockImplementation(async (): Promise<CTRFReport | null> => {
// Mock implementation that returns a basic CTRF report
await Promise.resolve() // Satisfy eslint require-await rule
return {
Expand All @@ -26,5 +26,5 @@ export const convertJUnitToCTRFReport = vi
},
tests: []
}
} as Report
} as CTRFReport
})
14 changes: 7 additions & 7 deletions src/client/github/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { context } from '@actions/github'
import AdmZip from 'adm-zip'
import { components } from '@octokit/openapi-types'
import { createGitHubClient } from './index.js'
import { Report } from '../../ctrf/core/types/ctrf.js'
import type { CTRFReport } from 'ctrf'
import { DefaultArtifactClient } from '@actions/artifact'
import fs from 'fs'
import path from 'path'
Expand All @@ -16,7 +16,7 @@ type Artifact = components['schemas']['artifact']
*/
export async function uploadArtifact(
artifactName: string,
report: Report,
report: CTRFReport,
tempDir = './temp'
): Promise<void> {
const filePath = path.join(tempDir, `ctrf-report.json`)
Expand Down Expand Up @@ -101,8 +101,8 @@ export async function downloadArtifact(downloadUrl: string): Promise<Buffer> {
export async function processArtifactsFromRun(
workflowRun: import('@octokit/openapi-types').components['schemas']['workflow-run'],
artifactName: string
): Promise<Report[]> {
const reports: Report[] = []
): Promise<CTRFReport[]> {
const reports: CTRFReport[] = []
const artifacts = await fetchArtifacts(
context.repo.owner,
context.repo.repo,
Expand All @@ -127,15 +127,15 @@ export async function processArtifactsFromRun(
* @param artifactBuffer - The buffer containing the zipped artifact.
* @returns A CTRF report object or null if not found.
*/
export function unzipArtifact(artifactBuffer: Buffer): Report | null {
export function unzipArtifact(artifactBuffer: Buffer): CTRFReport | null {
const zip = new AdmZip(artifactBuffer)
const zipEntries = zip.getEntries()
let report: Report | null = null
let report: CTRFReport | null = null

for (const zipEntry of zipEntries) {
if (zipEntry.entryName.endsWith('.json')) {
const jsonData = zipEntry.getData().toString('utf8')
report = JSON.parse(jsonData) as Report
report = JSON.parse(jsonData) as CTRFReport
break
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/ctrf/adapter/legacy-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { CTRFReport, Test, Environment } from 'ctrf'

/**
* Test shape as emitted by pre-v1 reporters.
*
* Differences from canonical Test:
* - suite: may be a plain string instead of string[]
*/
export type LegacyTest = Omit<Test, 'suite'> & {
suite?: string | string[]
}

/**
* Environment shape as emitted by pre-v1 reporters.
*
* Differences from canonical Environment:
* - buildNumber: may be a string (CI systems often inject it as a string)
*/
export type LegacyEnvironment = Omit<Environment, 'buildNumber'> & {
buildNumber?: string | number
}

/**
* Root report shape at parse boundaries (disk, network, CI artifact).
*
* Use this type when reading untrusted/unversioned CTRF JSON.
* Pass through normalizeLegacyReport() to obtain a canonical CTRFReport.
*/
export type LegacyCTRFReport = Omit<CTRFReport, 'results'> & {
results: Omit<CTRFReport['results'], 'tests' | 'environment'> & {
tests: LegacyTest[]
environment?: LegacyEnvironment
}
}
158 changes: 158 additions & 0 deletions src/ctrf/adapter/normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import type { CTRFReport, Test, Environment } from 'ctrf'
import type {
LegacyCTRFReport,
LegacyEnvironment,
LegacyTest
} from './legacy-types.js'

// ---------------------------------------------------------------------------
// Version utilities
// ---------------------------------------------------------------------------

/**
* Parse a semver string ("major.minor.patch") into a numeric tuple.
* Returns [0, 0, 0] for any string that cannot be parsed.
*/
export function parseSemVer(version: string): [number, number, number] {
const parts = version.split('.').map(Number)
const [major = 0, minor = 0, patch = 0] = parts
if (parts.length !== 3 || parts.some(isNaN)) return [0, 0, 0]
return [major, minor, patch]
}

/**
* Return true if specVersion is absent or parses as a version below 1.0.0.
* All current CTRF reporters emit "0.0.0"; once the spec reaches 1.0.0 the
* normalizations in this file will no longer be needed.
*/
export function isPreV1(specVersion: string | undefined): boolean {
if (!specVersion) return true
const [major] = parseSemVer(specVersion)
return major < 1
}

// ---------------------------------------------------------------------------
// Individual field normalizers (exported for fine-grained reuse)
// ---------------------------------------------------------------------------

/**
* Normalize Test.suite from a legacy plain string to the canonical string[].
*/
export function normalizeTestSuite(test: LegacyTest): Test {
return {
...test,
suite: typeof test.suite === 'string' ? [test.suite] : test.suite
}
}

/**
* Normalize Environment.buildNumber from a legacy string to the canonical number.
* Returns undefined if the string cannot be coerced to a finite integer.
*/
export function normalizeEnvironmentBuildNumber(
env: LegacyEnvironment | undefined
): Environment | undefined {
if (!env) return undefined
const { buildNumber, ...rest } = env
const normalized: number | undefined =
typeof buildNumber === 'string'
? parseInt(buildNumber, 10) || undefined
: buildNumber
return { ...rest, buildNumber: normalized }
}

// ---------------------------------------------------------------------------
// Plugin interface — extensible normalization pipeline
// ---------------------------------------------------------------------------

/**
* A NormalizerPlugin declares which spec versions it targets and how to
* transform a LegacyCTRFReport before it is handed to the rest of the app.
*
* Add a new plugin to handle any field-level changes introduced in a future
* CTRF spec version without modifying existing plugins.
*
* @example
* // Handle a hypothetical v1.x field change:
* const v1FieldPlugin: NormalizerPlugin = {
* appliesTo: (v) => { const [major] = parseSemVer(v ?? '0'); return major === 1 },
* normalize: (raw) => ({ ...raw, results: { ...raw.results, ... } })
* }
* export const customNormalize = createReportNormalizer([preV1Plugin, v1FieldPlugin])
*/
export interface NormalizerPlugin {
/**
* Called with the report's specVersion (may be undefined for very old reports).
* Return true if this plugin should be applied.
*/
appliesTo: (specVersion: string | undefined) => boolean
/**
* Transform the raw report. Receives and returns LegacyCTRFReport so plugins
* can be composed in sequence before the final cast to CTRFReport.
*/
normalize: (raw: LegacyCTRFReport) => LegacyCTRFReport
}

/**
* Built-in plugin for pre-v1 CTRF reports.
*
* Handles:
* - Test.suite: string → string[]
* - Environment.buildNumber: string → number
*/
export const preV1Plugin: NormalizerPlugin = {
appliesTo: specVersion => isPreV1(specVersion),
normalize: raw => ({
...raw,
results: {
...raw.results,
tests: raw.results.tests.map(normalizeTestSuite),
environment: normalizeEnvironmentBuildNumber(raw.results.environment)
}
})
}

// ---------------------------------------------------------------------------
// Normalizer factory
// ---------------------------------------------------------------------------

/**
* Build a report normalizer from an ordered list of plugins.
*
* Plugins are applied in order; only those whose appliesTo() predicate returns
* true for the given specVersion are executed.
*
* @example
* const normalize = createReportNormalizer([preV1Plugin, myCustomPlugin])
* const report: CTRFReport = normalize(rawParsed)
*/
export function createReportNormalizer(
plugins: NormalizerPlugin[]
): (raw: LegacyCTRFReport) => CTRFReport {
return (raw: LegacyCTRFReport): CTRFReport => {
const specVersion = raw.specVersion
const result = plugins
.filter(p => p.appliesTo(specVersion))
.reduce<LegacyCTRFReport>((acc, p) => p.normalize(acc), raw)
return result as unknown as CTRFReport
}
}

// ---------------------------------------------------------------------------
// Default normalizer — apply all built-in plugins
// ---------------------------------------------------------------------------

/**
* Normalize a CTRF report read from disk or received over the network.
*
* Applies all built-in legacy normalizations based on the report's specVersion:
* - Pre-v1: normalizes Test.suite and Environment.buildNumber
*
* Reports at specVersion >= 1.0.0 are returned as-is.
*
* @example
* const raw: LegacyCTRFReport = JSON.parse(content)
* const report: CTRFReport = normalizeLegacyReport(raw)
*/
export const normalizeLegacyReport: (raw: LegacyCTRFReport) => CTRFReport =
createReportNormalizer([preV1Plugin])
8 changes: 4 additions & 4 deletions src/ctrf/average-test-duration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Report } from '../ctrf/core/types/ctrf.js'
import type { CTRFReport } from 'ctrf'

/**
* Calculates the average number of tests per run across all reports.
Expand All @@ -8,9 +8,9 @@ import { Report } from '../ctrf/core/types/ctrf.js'
* @returns The average number of tests per run, rounded to the nearest integer
*/
export function calculateAverageTestsPerRun(
report: Report,
previousReports: Report[]
): Report {
report: CTRFReport,
previousReports: CTRFReport[]
): CTRFReport {
const totalTests =
report.results.tests.length +
previousReports.reduce((sum, r) => sum + r.results.summary.tests, 0)
Expand Down
Loading
Loading