Refactor and simplify summary generation

This commit is contained in:
daz 2023-12-31 14:42:48 -07:00
parent bc72ac9e9d
commit 24e9e9dc6b
No known key found for this signature in database
3 changed files with 53 additions and 87 deletions

View File

@ -1,4 +1,3 @@
import * as core from '@actions/core'
import * as cache from '@actions/cache'
/**
@ -112,47 +111,36 @@ export class CacheEntryListener {
}
}
export function writeCachingReport(listener: CacheListener): void {
export function generateCachingReport(listener: CacheListener): string {
const entries = listener.cacheEntries
core.summary.addRaw(
`\n<details><summary><h4>Caching for gradle-build-action was ${listener.cacheStatus} - expand for details</h4></summary>\n`
)
return `
<details>
<summary><h4>Caching for gradle-build-action was ${listener.cacheStatus} - expand for details</h4></summary>
${renderEntryTable(entries)}
core.summary.addTable([
[
{data: '', header: true},
{data: 'Count', header: true},
{data: 'Total Size (Mb)', header: true}
],
['Entries Restored', `${getCount(entries, e => e.restoredSize)}`, `${getSize(entries, e => e.restoredSize)}`],
['Entries Saved', `${getCount(entries, e => e.savedSize)}`, `${getSize(entries, e => e.savedSize)}`]
])
core.summary.addHeading('Cache Entry Details', 5)
const entryDetails = renderEntryDetails(listener)
core.summary.addRaw(`<pre>
${entryDetails}
<h5>Cache Entry Details</h5>
<pre>
${renderEntryDetails(listener)}
</pre>
</details>
`)
`
}
export function logCachingReport(listener: CacheListener): void {
const entries = listener.cacheEntries
core.startGroup(`Caching for gradle-build-action was ${listener.cacheStatus} - expand for details`)
core.info(
`Entries Restored: ${getCount(entries, e => e.restoredSize)} (${getSize(entries, e => e.restoredSize)} Mb)`
)
core.info(`Entries Saved : ${getCount(entries, e => e.savedSize)} (${getSize(entries, e => e.savedSize)} Mb)`)
core.info(`Cache Entry Details`)
core.info(renderEntryDetails(listener))
core.endGroup()
function renderEntryTable(entries: CacheEntryListener[]): string {
return `
<table>
<tr><td></td><th>Count</th><th>Total Size (Mb)</th></tr>
<tr><td>Entries Restored</td>
<td>${getCount(entries, e => e.restoredSize)}</td>
<td>${getSize(entries, e => e.restoredSize)}</td>
</tr>
<tr><td>Entries Saved</td>
<td>${getCount(entries, e => e.savedSize)}</td>
<td>${getSize(entries, e => e.savedSize)}</td>
</tr>
</table>
`
}
function renderEntryDetails(listener: CacheListener): string {

View File

@ -1,35 +1,34 @@
import * as core from '@actions/core'
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'
import * as params from './input-params'
import {BuildResult} from './build-results'
import {writeCachingReport, CacheListener, logCachingReport} from './cache-reporting'
import {CacheListener, generateCachingReport} from './cache-reporting'
export async function writeJobSummary(buildResults: BuildResult[], cacheListener: CacheListener): Promise<void> {
core.info('Writing job summary')
export async function generateJobSummary(buildResults: BuildResult[], cacheListener: CacheListener): Promise<void> {
const summaryTable = renderSummaryTable(buildResults)
const cachingReport = generateCachingReport(cacheListener)
if (buildResults.length === 0) {
core.debug('No Gradle build results found. Summary table will not be generated.')
if (shouldGenerateJobSummary()) {
core.summary.addRaw(summaryTable)
core.summary.addRaw(cachingReport)
await core.summary.write()
} else {
writeSummaryTable(buildResults)
core.info('============================')
core.info(summaryTable)
core.info('============================')
core.info(cachingReport)
core.info('============================')
}
writeCachingReport(cacheListener)
await core.summary.write()
}
export async function logJobSummary(buildResults: BuildResult[], cacheListener: CacheListener): Promise<void> {
if (buildResults.length === 0) {
core.debug('No Gradle build results found. Summary table will not be logged.')
} else {
logSummaryTable(buildResults)
function renderSummaryTable(results: BuildResult[]): string {
if (results.length === 0) {
return 'No Gradle build results detected.'
}
logCachingReport(cacheListener)
}
function writeSummaryTable(results: BuildResult[]): void {
core.summary.addHeading('Gradle Builds', 3)
core.summary.addRaw(`
return `
<h3>Gradle Builds</h3>
<table>
<tr>
<th>Root Project</th>
@ -39,7 +38,7 @@ function writeSummaryTable(results: BuildResult[]): void {
<th>Build Scan®</th>
</tr>${results.map(result => renderBuildResultRow(result)).join('')}
</table>
`)
`
}
function renderBuildResultRow(result: BuildResult): string {
@ -77,18 +76,11 @@ function renderBuildScanBadge(outcomeText: string, outcomeColor: string, targetU
return `<a href="${targetUrl}" rel="nofollow">${badgeHtml}</a>`
}
function logSummaryTable(results: BuildResult[]): void {
core.info('============================')
core.info('Gradle Builds')
core.info('----------------------------')
core.info('Root Project | Requested Tasks | Gradle Version | Build Outcome | Build Scan®')
core.info('----------------------------')
for (const result of results) {
core.info(
`${result.rootProjectName} | ${result.requestedTasks} | ${result.gradleVersion} | ${
result.buildFailed ? 'FAILED' : 'SUCCESS'
} | ${result.buildScanFailed ? 'Publish failed' : result.buildScanUri}`
)
function shouldGenerateJobSummary(): boolean {
// Check if Job Summary is supported on this platform
if (!process.env[SUMMARY_ENV_VAR]) {
return false
}
core.info('============================')
return params.isJobSummaryEnabled()
}

View File

@ -1,14 +1,13 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'
import * as path from 'path'
import * as os from 'os'
import * as caches from './caches'
import * as layout from './repository-layout'
import * as params from './input-params'
import * as dependencyGraph from './dependency-graph'
import * as jobSummary from './job-summary'
import {logJobSummary, writeJobSummary} from './job-summary'
import {loadBuildResults} from './build-results'
import {CacheListener} from './cache-reporting'
import {DaemonController} from './daemon-controller'
@ -56,11 +55,7 @@ export async function complete(): Promise<void> {
await caches.save(gradleUserHome, cacheListener, daemonController)
if (shouldGenerateJobSummary()) {
await writeJobSummary(buildResults, cacheListener)
} else {
logJobSummary(buildResults, cacheListener)
}
await jobSummary.generateJobSummary(buildResults, cacheListener)
await dependencyGraph.complete(params.getDependencyGraphOption())
@ -93,12 +88,3 @@ async function determineUserHome(): Promise<string> {
core.debug(`Determined user.home from java -version output: '${userHome}'`)
return userHome
}
function shouldGenerateJobSummary(): boolean {
// Check if Job Summary is supported on this platform
if (!process.env[SUMMARY_ENV_VAR]) {
return false
}
return params.isJobSummaryEnabled()
}