mirror of
https://github.com/gradle/gradle-build-action.git
synced 2024-12-26 02:06:20 +08:00
Automatically add Job Summary as PR comment
Rather than requiring a separate step to add a PR comment, the `gradle-build-action` can now automatically add the Job Summary as a PR comment Fixes #1020
This commit is contained in:
parent
24e9e9dc6b
commit
34a07dced0
13
.github/workflows/demo-pr-build-scan-comment.yml
vendored
13
.github/workflows/demo-pr-build-scan-comment.yml
vendored
@ -10,18 +10,9 @@ jobs:
|
|||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Setup Gradle
|
- name: Setup Gradle
|
||||||
uses: ./
|
uses: ./
|
||||||
|
with:
|
||||||
|
add-pr-comment: true
|
||||||
- name: Run build with Gradle wrapper
|
- name: Run build with Gradle wrapper
|
||||||
id: gradle
|
id: gradle
|
||||||
working-directory: .github/workflow-samples/kotlin-dsl
|
working-directory: .github/workflow-samples/kotlin-dsl
|
||||||
run: ./gradlew build --scan
|
run: ./gradlew build --scan
|
||||||
- name: "Add Build Scan URL as PR comment"
|
|
||||||
uses: actions/github-script@v7
|
|
||||||
with:
|
|
||||||
github-token: ${{secrets.GITHUB_TOKEN}}
|
|
||||||
script: |
|
|
||||||
github.rest.issues.createComment({
|
|
||||||
issue_number: context.issue.number,
|
|
||||||
owner: context.repo.owner,
|
|
||||||
repo: context.repo.repo,
|
|
||||||
body: 'PR ready for review: ${{ steps.gradle.outputs.build-scan-url }}'
|
|
||||||
})
|
|
||||||
|
@ -63,6 +63,11 @@ inputs:
|
|||||||
required: false
|
required: false
|
||||||
default: true
|
default: true
|
||||||
|
|
||||||
|
add-pr-comment:
|
||||||
|
description: When 'true', a summary of the Gradle builds will be added as a PR comment. No action will be taken if the workflow was not triggered from a pull request.
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
|
||||||
dependency-graph:
|
dependency-graph:
|
||||||
description: Specifies if a GitHub dependency snapshot should be generated for each Gradle build, and if so, how. Valid values are 'disabled' (default), 'generate', 'generate-and-submit', 'generate-and-upload' and 'download-and-submit'.
|
description: Specifies if a GitHub dependency snapshot should be generated for each Gradle build, and if so, how. Valid values are 'disabled' (default), 'generate', 'generate-and-submit', 'generate-and-upload' and 'download-and-submit'.
|
||||||
required: false
|
required: false
|
||||||
|
@ -71,6 +71,10 @@ export function isJobSummaryEnabled(): boolean {
|
|||||||
return getBooleanInput('generate-job-summary', true)
|
return getBooleanInput('generate-job-summary', true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isPRCommentEnabled(): boolean {
|
||||||
|
return getBooleanInput('add-pr-comment', false)
|
||||||
|
}
|
||||||
|
|
||||||
export function isDependencyGraphEnabled(): boolean {
|
export function isDependencyGraphEnabled(): boolean {
|
||||||
return getBooleanInput('generate-dependency-graph', true)
|
return getBooleanInput('generate-dependency-graph', true)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
|
import * as github from '@actions/github'
|
||||||
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'
|
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'
|
||||||
|
|
||||||
import * as params from './input-params'
|
import * as params from './input-params'
|
||||||
@ -10,6 +11,8 @@ export async function generateJobSummary(buildResults: BuildResult[], cacheListe
|
|||||||
const cachingReport = generateCachingReport(cacheListener)
|
const cachingReport = generateCachingReport(cacheListener)
|
||||||
|
|
||||||
if (shouldGenerateJobSummary()) {
|
if (shouldGenerateJobSummary()) {
|
||||||
|
core.info('Generating Job Summary')
|
||||||
|
|
||||||
core.summary.addRaw(summaryTable)
|
core.summary.addRaw(summaryTable)
|
||||||
core.summary.addRaw(cachingReport)
|
core.summary.addRaw(cachingReport)
|
||||||
await core.summary.write()
|
await core.summary.write()
|
||||||
@ -20,6 +23,39 @@ export async function generateJobSummary(buildResults: BuildResult[], cacheListe
|
|||||||
core.info(cachingReport)
|
core.info(cachingReport)
|
||||||
core.info('============================')
|
core.info('============================')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldAddPRComment()) {
|
||||||
|
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)}`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderSummaryTable(results: BuildResult[]): string {
|
function renderSummaryTable(results: BuildResult[]): string {
|
||||||
@ -28,10 +64,9 @@ function renderSummaryTable(results: BuildResult[]): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<h3>Gradle Builds</h3>
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Root Project</th>
|
<th>Gradle Root Project</th>
|
||||||
<th>Requested Tasks</th>
|
<th>Requested Tasks</th>
|
||||||
<th>Gradle Version</th>
|
<th>Gradle Version</th>
|
||||||
<th>Build Outcome</th>
|
<th>Build Outcome</th>
|
||||||
@ -84,3 +119,7 @@ function shouldGenerateJobSummary(): boolean {
|
|||||||
|
|
||||||
return params.isJobSummaryEnabled()
|
return params.isJobSummaryEnabled()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shouldAddPRComment(): boolean {
|
||||||
|
return params.isPRCommentEnabled()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user