Unverified Commit 34a07dce authored by daz's avatar daz
Browse files

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
parent 24e9e9dc
Loading
Loading
Loading
Loading
+2 −11
Changes for .github/workflows/demo-pr-build-scan-comment.yml: 2 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -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 for action.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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
+4
Changes for src/input-params.ts: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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)
}
+41 −2
Changes for src/job-summary.ts: 41 added lines, 2 removed lines.
Original line number Diff line number Diff line
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'
@@ -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()
@@ -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 {
@@ -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>
@@ -84,3 +119,7 @@ function shouldGenerateJobSummary(): boolean {

    return params.isJobSummaryEnabled()
}

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