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' 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 const entries = listener.cacheEntries
core.summary.addRaw( return `
`\n<details><summary><h4>Caching for gradle-build-action was ${listener.cacheStatus} - expand for details</h4></summary>\n` <details>
) <summary><h4>Caching for gradle-build-action was ${listener.cacheStatus} - expand for details</h4></summary>
${renderEntryTable(entries)}
core.summary.addTable([ <h5>Cache Entry Details</h5>
[ <pre>
{data: '', header: true}, ${renderEntryDetails(listener)}
{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}
</pre> </pre>
</details> </details>
`) `
} }
export function logCachingReport(listener: CacheListener): void { function renderEntryTable(entries: CacheEntryListener[]): string {
const entries = listener.cacheEntries return `
<table>
core.startGroup(`Caching for gradle-build-action was ${listener.cacheStatus} - expand for details`) <tr><td></td><th>Count</th><th>Total Size (Mb)</th></tr>
<tr><td>Entries Restored</td>
core.info( <td>${getCount(entries, e => e.restoredSize)}</td>
`Entries Restored: ${getCount(entries, e => e.restoredSize)} (${getSize(entries, e => e.restoredSize)} Mb)` <td>${getSize(entries, e => e.restoredSize)}</td>
) </tr>
core.info(`Entries Saved : ${getCount(entries, e => e.savedSize)} (${getSize(entries, e => e.savedSize)} Mb)`) <tr><td>Entries Saved</td>
<td>${getCount(entries, e => e.savedSize)}</td>
core.info(`Cache Entry Details`) <td>${getSize(entries, e => e.savedSize)}</td>
core.info(renderEntryDetails(listener)) </tr>
</table>
core.endGroup() `
} }
function renderEntryDetails(listener: CacheListener): string { function renderEntryDetails(listener: CacheListener): string {

View file

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

View file

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