Skip to content

Commit

Permalink
Automatically add Job Summary as PR comment
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bigdaz committed Jan 2, 2024
1 parent 24e9e9d commit 34a07dc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
13 changes: 2 additions & 11 deletions .github/workflows/demo-pr-build-scan-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@ jobs:
uses: actions/checkout@v4
- name: Setup Gradle
uses: ./
with:
add-pr-comment: true
- name: Run build with Gradle wrapper
id: gradle
working-directory: .github/workflow-samples/kotlin-dsl
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 }}'
})
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ inputs:
required: false
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:
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
Expand Down
4 changes: 4 additions & 0 deletions src/input-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export function isJobSummaryEnabled(): boolean {
return getBooleanInput('generate-job-summary', true)
}

export function isPRCommentEnabled(): boolean {
return getBooleanInput('add-pr-comment', false)
}

export function isDependencyGraphEnabled(): boolean {
return getBooleanInput('generate-dependency-graph', true)
}
Expand Down
43 changes: 41 additions & 2 deletions src/job-summary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'

import * as params from './input-params'
Expand All @@ -10,6 +11,8 @@ export async function generateJobSummary(buildResults: BuildResult[], cacheListe
const cachingReport = generateCachingReport(cacheListener)

if (shouldGenerateJobSummary()) {
core.info('Generating Job Summary')

core.summary.addRaw(summaryTable)
core.summary.addRaw(cachingReport)
await core.summary.write()
Expand All @@ -20,6 +23,39 @@ export async function generateJobSummary(buildResults: BuildResult[], cacheListe
core.info(cachingReport)
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 {
Expand All @@ -28,10 +64,9 @@ function renderSummaryTable(results: BuildResult[]): string {
}

return `
<h3>Gradle Builds</h3>
<table>
<tr>
<th>Root Project</th>
<th>Gradle Root Project</th>
<th>Requested Tasks</th>
<th>Gradle Version</th>
<th>Build Outcome</th>
Expand Down Expand Up @@ -84,3 +119,7 @@ function shouldGenerateJobSummary(): boolean {

return params.isJobSummaryEnabled()
}

function shouldAddPRComment(): boolean {
return params.isPRCommentEnabled()
}

0 comments on commit 34a07dc

Please sign in to comment.