import * as core from '@actions/core' import * as github from '@actions/github' import {RequestError} from '@octokit/request-error' import {BuildResults, BuildResult} from './build-results' import {SummaryConfig, getActionId, getGithubToken} from './configuration' import {Deprecation, getDeprecations, getErrors} from './deprecation-collector' export async function generateJobSummary( buildResults: BuildResults, cachingReport: string, config: SummaryConfig ): Promise { const errors = renderErrors() if (errors) { core.summary.addRaw(errors) await core.summary.write() return } const summaryTable = renderSummaryTable(buildResults.results) const hasFailure = buildResults.anyFailed() if (config.shouldGenerateJobSummary(hasFailure)) { core.info('Generating Job Summary') core.summary.addRaw(summaryTable) core.summary.addRaw(cachingReport) await core.summary.write() } else { core.info('============================') core.info(summaryTable) core.info('============================') core.info(cachingReport) core.info('============================') } if (config.shouldAddPRComment(hasFailure)) { await addPRComment(summaryTable) } } async function addPRComment(jobSummary: string): Promise { const context = github.context if (context.payload.pull_request == null) { core.info('No pull_request trigger detected: not adding PR comment') return } const pull_request_number = context.payload.pull_request.number core.info(`Adding Job Summary as comment to PR #${pull_request_number}.`) const prComment = `

Job Summary for Gradle

${context.workflow} :: ${context.job}
${jobSummary}` const github_token = getGithubToken() const octokit = github.getOctokit(github_token) try { await octokit.rest.issues.createComment({ ...context.repo, issue_number: pull_request_number, body: prComment }) } catch (error) { if (error instanceof RequestError) { core.warning(buildWarningMessage(error)) } else { throw error } } } function buildWarningMessage(error: RequestError): string { const mainWarning = `Failed to generate PR comment.\n${String(error)}` if (error.message === 'Resource not accessible by integration') { return `${mainWarning} Please ensure that the 'pull-requests: write' permission is available for the workflow job. Note that this permission is never available for a workflow triggered from a repository fork. ` } return mainWarning } export function renderSummaryTable(results: BuildResult[]): string { return `${renderDeprecations()}\n${renderBuildResults(results)}` } function renderErrors(): string | undefined { const errors = getErrors() if (errors.length === 0) { return undefined } return errors.map(error => `:x: ${error}`).join('\n') } function renderDeprecations(): string { const deprecations = getDeprecations() if (deprecations.length === 0) { return '' } return `

Deprecation warnings

This job uses deprecated functionality from the ${getActionId()} action. Follow the links for upgrade details.

Gradle Build Results

` } function getDeprecationHtml(deprecation: Deprecation): string { return `${deprecation.message}` } function renderBuildResults(results: BuildResult[]): string { if (results.length === 0) { return 'No Gradle build results detected.' } return ` ${results.map(result => renderBuildResultRow(result)).join('')}
Gradle Root Project Requested Tasks Gradle Version Build Outcome Build Scan®
` } function renderBuildResultRow(result: BuildResult): string { return ` ${truncateString(result.rootProjectName, 30)} ${truncateString(result.requestedTasks, 60)} ${result.gradleVersion} ${renderOutcome(result)} ${renderBuildScan(result)} ` } function renderOutcome(result: BuildResult): string { return result.buildFailed ? ':x:' : ':white_check_mark:' } interface BadgeSpec { text: string alt: string color: string logo: boolean targetUrl: string } function renderBuildScan(result: BuildResult): string { if (result.buildScanFailed) { return renderBuildScanBadge({ text: 'Publish failed', alt: 'Build Scan publish failed', color: 'orange', logo: false, targetUrl: 'https://docs.gradle.com/develocity/gradle-plugin/#troubleshooting' }) } if (result.buildScanUri) { return renderBuildScanBadge({ text: 'Build Scan®', alt: 'Build Scan published', color: '06A0CE', logo: true, targetUrl: result.buildScanUri }) } return renderBuildScanBadge({ text: 'Not published', alt: 'Build Scan not published', color: 'lightgrey', logo: false, targetUrl: 'https://scans.gradle.com' }) } function renderBuildScanBadge({text, alt, color, logo, targetUrl}: BadgeSpec): string { const encodedText = encodeURIComponent(text) const badgeUrl = `https://img.shields.io/badge/${encodedText}-${color}${logo ? '?logo=Gradle' : ''}` const badgeHtml = `${alt}` return `${badgeHtml}` } function truncateString(str: string, maxLength: number): string { if (str.length > maxLength) { return `
${str.slice(0, maxLength - 1)}…
` } else { return str } }