2022-06-02 12:57:35 -06:00
|
|
|
import * as core from '@actions/core'
|
2024-01-01 11:58:02 -07:00
|
|
|
import * as github from '@actions/github'
|
2023-12-31 14:42:48 -07:00
|
|
|
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'
|
|
|
|
|
|
|
|
import * as params from './input-params'
|
2022-06-22 16:35:55 -06:00
|
|
|
import {BuildResult} from './build-results'
|
2023-12-31 14:42:48 -07:00
|
|
|
import {CacheListener, generateCachingReport} from './cache-reporting'
|
2022-06-02 12:57:35 -06:00
|
|
|
|
2023-12-31 14:42:48 -07:00
|
|
|
export async function generateJobSummary(buildResults: BuildResult[], cacheListener: CacheListener): Promise<void> {
|
|
|
|
const summaryTable = renderSummaryTable(buildResults)
|
|
|
|
const cachingReport = generateCachingReport(cacheListener)
|
2022-06-02 22:39:09 -06:00
|
|
|
|
2024-01-01 12:31:36 -07:00
|
|
|
if (shouldGenerateJobSummary(buildResults)) {
|
2024-01-01 11:58:02 -07:00
|
|
|
core.info('Generating Job Summary')
|
|
|
|
|
2023-12-31 14:42:48 -07:00
|
|
|
core.summary.addRaw(summaryTable)
|
|
|
|
core.summary.addRaw(cachingReport)
|
|
|
|
await core.summary.write()
|
2022-06-02 12:57:35 -06:00
|
|
|
} else {
|
2023-12-31 14:42:48 -07:00
|
|
|
core.info('============================')
|
|
|
|
core.info(summaryTable)
|
|
|
|
core.info('============================')
|
|
|
|
core.info(cachingReport)
|
|
|
|
core.info('============================')
|
2022-06-02 12:57:35 -06:00
|
|
|
}
|
2024-01-01 11:58:02 -07:00
|
|
|
|
2024-01-01 12:31:36 -07:00
|
|
|
if (shouldAddPRComment(buildResults)) {
|
2024-01-01 11:58:02 -07:00
|
|
|
await addPRComment(summaryTable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addPRComment(jobSummary: string): Promise<void> {
|
|
|
|
try {
|
|
|
|
const github_token = params.getGithubToken()
|
|
|
|
|
|
|
|
const context = github.context
|
|
|
|
if (context.payload.pull_request == null) {
|
|
|
|
core.info('No pull_request trigger: 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 = `<h3>Job Summary for gradle-build-action</h3>
|
|
|
|
<h5>${github.context.workflow} :: <em>${github.context.job}</em></h5>
|
|
|
|
|
|
|
|
${jobSummary}`
|
|
|
|
|
|
|
|
const octokit = github.getOctokit(github_token)
|
|
|
|
await octokit.rest.issues.createComment({
|
|
|
|
...context.repo,
|
|
|
|
issue_number: pull_request_number,
|
|
|
|
body: prComment
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
core.warning(`Failed to generate PR comment: ${String(error)}`)
|
|
|
|
}
|
2022-06-02 12:57:35 -06:00
|
|
|
}
|
|
|
|
|
2023-12-31 14:42:48 -07:00
|
|
|
function renderSummaryTable(results: BuildResult[]): string {
|
|
|
|
if (results.length === 0) {
|
|
|
|
return 'No Gradle build results detected.'
|
2022-06-20 17:53:30 -06:00
|
|
|
}
|
|
|
|
|
2023-12-31 14:42:48 -07:00
|
|
|
return `
|
2022-06-19 09:56:40 -06:00
|
|
|
<table>
|
|
|
|
<tr>
|
2024-01-01 11:58:02 -07:00
|
|
|
<th>Gradle Root Project</th>
|
2022-06-19 09:56:40 -06:00
|
|
|
<th>Requested Tasks</th>
|
|
|
|
<th>Gradle Version</th>
|
|
|
|
<th>Build Outcome</th>
|
2023-07-22 08:49:39 -06:00
|
|
|
<th>Build Scan®</th>
|
2022-06-19 09:56:40 -06:00
|
|
|
</tr>${results.map(result => renderBuildResultRow(result)).join('')}
|
|
|
|
</table>
|
2023-12-31 14:42:48 -07:00
|
|
|
`
|
2022-06-19 09:56:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderBuildResultRow(result: BuildResult): string {
|
|
|
|
return `
|
|
|
|
<tr>
|
|
|
|
<td>${result.rootProjectName}</td>
|
|
|
|
<td>${result.requestedTasks}</td>
|
|
|
|
<td align='center'>${result.gradleVersion}</td>
|
|
|
|
<td align='center'>${renderOutcome(result)}</td>
|
|
|
|
<td>${renderBuildScan(result)}</td>
|
|
|
|
</tr>`
|
2022-06-02 12:57:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderOutcome(result: BuildResult): string {
|
2022-06-19 09:40:27 -06:00
|
|
|
return result.buildFailed ? ':x:' : ':white_check_mark:'
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderBuildScan(result: BuildResult): string {
|
|
|
|
if (result.buildScanFailed) {
|
|
|
|
return renderBuildScanBadge(
|
2022-06-19 09:56:40 -06:00
|
|
|
'PUBLISH_FAILED',
|
2022-06-19 09:40:27 -06:00
|
|
|
'orange',
|
|
|
|
'https://docs.gradle.com/enterprise/gradle-plugin/#troubleshooting'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (result.buildScanUri) {
|
|
|
|
return renderBuildScanBadge('PUBLISHED', '06A0CE', result.buildScanUri)
|
|
|
|
}
|
2022-06-19 09:56:40 -06:00
|
|
|
return renderBuildScanBadge('NOT_PUBLISHED', 'lightgrey', 'https://scans.gradle.com')
|
2022-06-19 09:40:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderBuildScanBadge(outcomeText: string, outcomeColor: string, targetUrl: string): string {
|
2023-07-22 08:49:39 -06:00
|
|
|
const badgeUrl = `https://img.shields.io/badge/Build%20Scan%C2%AE-${outcomeText}-${outcomeColor}?logo=Gradle`
|
2022-06-19 09:40:27 -06:00
|
|
|
const badgeHtml = `<img src="${badgeUrl}" alt="Build Scan ${outcomeText}" />`
|
2022-06-04 23:10:32 -06:00
|
|
|
return `<a href="${targetUrl}" rel="nofollow">${badgeHtml}</a>`
|
2022-06-02 12:57:35 -06:00
|
|
|
}
|
2022-06-20 17:53:30 -06:00
|
|
|
|
2024-01-01 12:31:36 -07:00
|
|
|
function shouldGenerateJobSummary(buildResults: BuildResult[]): boolean {
|
2023-12-31 14:42:48 -07:00
|
|
|
// Check if Job Summary is supported on this platform
|
|
|
|
if (!process.env[SUMMARY_ENV_VAR]) {
|
|
|
|
return false
|
2022-06-20 17:53:30 -06:00
|
|
|
}
|
2023-12-31 14:42:48 -07:00
|
|
|
|
2024-01-01 12:31:36 -07:00
|
|
|
// Check if Job Summary is disabled using the deprecated input
|
|
|
|
if (!params.isJobSummaryEnabled()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return shouldAddJobSummary(params.getJobSummaryOption(), buildResults)
|
2022-06-20 17:53:30 -06:00
|
|
|
}
|
2024-01-01 11:58:02 -07:00
|
|
|
|
2024-01-01 12:31:36 -07:00
|
|
|
function shouldAddPRComment(buildResults: BuildResult[]): boolean {
|
|
|
|
return shouldAddJobSummary(params.getPRCommentOption(), buildResults)
|
|
|
|
}
|
|
|
|
|
|
|
|
function shouldAddJobSummary(option: params.JobSummaryOption, buildResults: BuildResult[]): boolean {
|
|
|
|
switch (option) {
|
|
|
|
case params.JobSummaryOption.Always:
|
|
|
|
return true
|
|
|
|
case params.JobSummaryOption.Never:
|
|
|
|
return false
|
|
|
|
case params.JobSummaryOption.OnFailure:
|
|
|
|
core.info(`Got these build results: ${JSON.stringify(buildResults)}`)
|
|
|
|
return buildResults.some(result => result.buildFailed)
|
|
|
|
}
|
2024-01-01 11:58:02 -07:00
|
|
|
}
|