Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Estimate time from several workflow runs #38

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ inputs:
test-report-branch:
description: Branch to find the test report artifacts
required: true
test-report-workflow-count:
description: Number of workflows to find the test report artifacts
required: true
default: '1'
shard-count:
description: Number of shards
required: true
Expand Down
23 changes: 12 additions & 11 deletions src/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Octokit } from './github'

type Inputs = {
testReportWorkflowFilename: string
testReportWorkflowCount: number
testReportArtifactNamePrefix: string
testReportBranch: string
testReportDirectory: string
Expand All @@ -22,18 +23,18 @@ export type TestWorkflowRun = {
export const downloadTestReportsFromLastWorkflowRuns = async (
octokit: Octokit,
inputs: Inputs,
): Promise<TestWorkflowRun | undefined> => {
const lastWorkflowRuns = await findLastWorkflowRuns(octokit, inputs)
if (lastWorkflowRuns.length === 0) {
return
}
const lastWorkflowRun = lastWorkflowRuns[0]
): Promise<TestWorkflowRun[]> => {
const artifactClient = new DefaultArtifactClient()
const testReportFiles = await downloadTestReportArtifacts(octokit, artifactClient, inputs, lastWorkflowRun.id)
return {
url: lastWorkflowRun.html_url,
testReportFiles,
const workflowRuns = await findLastWorkflowRuns(octokit, inputs)
const testWorkflowRuns: TestWorkflowRun[] = []
for (const lastWorkflowRun of workflowRuns) {
const testReportFiles = await downloadTestReportArtifacts(octokit, artifactClient, inputs, lastWorkflowRun.id)
testWorkflowRuns.push({
url: lastWorkflowRun.url,
testReportFiles,
})
}
return testWorkflowRuns
}

const findLastWorkflowRuns = async (octokit: Octokit, inputs: Inputs) => {
Expand All @@ -44,7 +45,7 @@ const findLastWorkflowRuns = async (octokit: Octokit, inputs: Inputs) => {
workflow_id: inputs.testReportWorkflowFilename,
branch: inputs.testReportBranch,
status: 'success',
per_page: 1,
per_page: inputs.testReportWorkflowCount,
})
core.info(`Found ${listWorkflowRuns.workflow_runs.length} workflow run:`)
for (const lastWorkflowRun of listWorkflowRuns.workflow_runs) {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const main = async (): Promise<void> => {
testFiles: core.getInput('test-files', { required: true }),
testReportArtifactNamePrefix: core.getInput('test-report-artifact-name-prefix', { required: true }),
testReportBranch: core.getInput('test-report-branch', { required: true }),
testReportWorkflowCount: parseInt(core.getInput('test-report-workflow-count', { required: true })),
shardCount: parseInt(core.getInput('shard-count', { required: true })),
shardsArtifactName: core.getInput('shards-artifact-name', { required: true }),
owner: github.context.repo.owner,
Expand Down
10 changes: 6 additions & 4 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as os from 'os'
import * as path from 'path'
import { getOctokit } from './github'
import { downloadTestReportsFromLastWorkflowRuns } from './artifact'
import { parseTestReportFiles } from './junitxml'
import { parseTestReportsOfWorkflowRuns } from './testreport'
import {
tryDownloadShardsIfAlreadyExists,
distributeTestFilesToShards,
Expand All @@ -19,6 +19,7 @@ type Inputs = {
testFiles: string
testReportArtifactNamePrefix: string
testReportBranch: string
testReportWorkflowCount: number
shardCount: number
shardsArtifactName: string
owner: string
Expand Down Expand Up @@ -47,23 +48,24 @@ export const run = async (inputs: Inputs): Promise<Outputs> => {
}

const testReportDirectory = path.join(tempDirectory, 'test-reports')
const testWorkflowRun = await downloadTestReportsFromLastWorkflowRuns(octokit, {
const testWorkflowRuns = await downloadTestReportsFromLastWorkflowRuns(octokit, {
testReportArtifactNamePrefix: inputs.testReportArtifactNamePrefix,
testReportWorkflowCount: inputs.testReportWorkflowCount,
testReportBranch: inputs.testReportBranch,
testReportWorkflowFilename: inputs.workflowFilename,
testReportDirectory,
owner: inputs.owner,
repo: inputs.repo,
token: inputs.token,
})
const testFiles = await parseTestReportFiles(testWorkflowRun?.testReportFiles ?? [])
const testFiles = await parseTestReportsOfWorkflowRuns(testWorkflowRuns)

const shardSet = distributeTestFilesToShards(workingTestFilenames, testFiles, inputs.shardCount)
core.info(`Generated ${shardSet.shards.length} shards`)

const shardsLock = await writeShardsWithLock(shardSet.shards, shardsDirectory, inputs.shardsArtifactName)
if (shardsLock.currentJobAcquiredLock) {
writeSummary(shardSet, testWorkflowRun)
writeSummary(shardSet, testWorkflowRuns)
}

await ensureTestFilesConsistency(shardsDirectory, workingTestFilenames)
Expand Down
15 changes: 9 additions & 6 deletions src/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core'
import { ShardSet } from './shard'
import { TestWorkflowRun } from './artifact'

export const writeSummary = (shardSet: ShardSet, testWorkflowRun: TestWorkflowRun | undefined) => {
export const writeSummary = (shardSet: ShardSet, testWorkflowRuns: TestWorkflowRun[]) => {
core.summary.addHeading('Summary of parallel-test-action')
core.summary.addRaw(
'This action distributes the test files to the shards based on the estimated time from the test reports.',
Expand Down Expand Up @@ -51,11 +51,14 @@ export const writeSummary = (shardSet: ShardSet, testWorkflowRun: TestWorkflowRu
)

core.summary.addHeading('Test reports', 2)
if (testWorkflowRun) {
core.summary.addRaw('This action downloaded the test reports from ')
core.summary.addLink('the last success workflow run', testWorkflowRun.url)
core.summary.addHeading('Files', 3)
core.summary.addList(testWorkflowRun.testReportFiles)
if (testWorkflowRuns.length > 0) {
core.summary.addRaw('This action downloaded the test reports from:')
for (const [index, testWorkflowRun] of testWorkflowRuns.entries()) {
core.summary.addHeading(`Workflow Run #${index + 1}`, 3)
core.summary.addLink(testWorkflowRun.url, testWorkflowRun.url)
core.summary.addHeading(`Files`, 4)
core.summary.addList(testWorkflowRun.testReportFiles)
}
} else {
core.summary.addRaw('No test reports found')
}
Expand Down
50 changes: 50 additions & 0 deletions src/testreport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { parseTestReportFiles } from './junitxml'

type TestReportsOfWorkflowRun = {
testReportFiles: string[]
}

type TestFile = {
filename: string
totalTime: number
totalTestCases: number
}

export const parseTestReportsOfWorkflowRuns = async (
testReportsOfWorkflowRuns: TestReportsOfWorkflowRun[],
): Promise<TestFile[]> => {
const testFilesOfWorkflowRuns: TestFile[][] = []
for (const testReportsOfWorkflowRun of testReportsOfWorkflowRuns) {
const testFiles = await parseTestReportFiles(testReportsOfWorkflowRun.testReportFiles)
testFilesOfWorkflowRuns.push(testFiles)
}
return calculateAveregedTestFiles(testFilesOfWorkflowRuns)
}

const calculateAveregedTestFiles = (testFilesOfWorkflowRuns: TestFile[][]): TestFile[] => {
const testFileMap = new Map<
string,
TestFile & {
effectiveWorkflowRuns: number
}
>()
for (const testFilesOfWorkflowRun of testFilesOfWorkflowRuns) {
for (const testFile of testFilesOfWorkflowRun) {
const currentTestFile = testFileMap.get(testFile.filename) ?? {
filename: testFile.filename,
totalTime: 0,
totalTestCases: 0,
effectiveWorkflowRuns: 0,
}
currentTestFile.totalTime += testFile.totalTime
currentTestFile.totalTestCases += testFile.totalTestCases
currentTestFile.effectiveWorkflowRuns++
testFileMap.set(testFile.filename, currentTestFile)
}
}
for (const [, testFile] of testFileMap) {
testFile.totalTime /= testFile.effectiveWorkflowRuns
testFile.totalTestCases /= testFile.effectiveWorkflowRuns
}
return [...testFileMap.values()]
}