import * as core from '@actions/core' import fs from 'fs' import path from 'path' import {logCachingReport, CacheListener} from './cache-reporting' export interface BuildResult { get rootProjectName(): string get rootProjectDir(): string get requestedTasks(): string get gradleVersion(): string get gradleHomeDir(): string get buildFailed(): boolean get buildScanUri(): string get buildScanFailed(): boolean } export async function writeJobSummary(buildResults: BuildResult[], cacheListener: CacheListener): Promise { core.info('Writing job summary') if (buildResults.length === 0) { core.debug('No Gradle build results found. Summary table will not be generated.') } else { writeSummaryTable(buildResults) } logCachingReport(cacheListener) await core.summary.write() } export function loadBuildResults(): BuildResult[] { const buildResultsDir = path.resolve(process.env['RUNNER_TEMP']!, '.build-results') if (!fs.existsSync(buildResultsDir)) { return [] } return fs.readdirSync(buildResultsDir).map(file => { // Every file in the .build-results dir should be a BuildResults JSON const filePath = path.join(buildResultsDir, file) const content = fs.readFileSync(filePath, 'utf8') return JSON.parse(content) as BuildResult }) } function writeSummaryTable(results: BuildResult[]): void { core.summary.addHeading('Gradle Builds', 3) core.summary.addRaw(` ${results.map(result => renderBuildResultRow(result)).join('')}
Root Project Requested Tasks Gradle Version Build Outcome Build Scan™
`) } function renderBuildResultRow(result: BuildResult): string { return ` ${result.rootProjectName} ${result.requestedTasks} ${result.gradleVersion} ${renderOutcome(result)} ${renderBuildScan(result)} ` } function renderOutcome(result: BuildResult): string { return result.buildFailed ? ':x:' : ':white_check_mark:' } function renderBuildScan(result: BuildResult): string { if (result.buildScanFailed) { return renderBuildScanBadge( 'PUBLISH_FAILED', 'orange', 'https://docs.gradle.com/enterprise/gradle-plugin/#troubleshooting' ) } if (result.buildScanUri) { return renderBuildScanBadge('PUBLISHED', '06A0CE', result.buildScanUri) } return renderBuildScanBadge('NOT_PUBLISHED', 'lightgrey', 'https://scans.gradle.com') } function renderBuildScanBadge(outcomeText: string, outcomeColor: string, targetUrl: string): string { const badgeUrl = `https://img.shields.io/badge/Build%20Scan%E2%84%A2-${outcomeText}-${outcomeColor}?logo=Gradle` const badgeHtml = `Build Scan ${outcomeText}` return `${badgeHtml}` }