diff --git a/.github/workflows/demo-pr-build-scan-comment.yml b/.github/workflows/demo-pr-build-scan-comment.yml index af08629..43d3a8d 100644 --- a/.github/workflows/demo-pr-build-scan-comment.yml +++ b/.github/workflows/demo-pr-build-scan-comment.yml @@ -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 }}' - }) diff --git a/action.yml b/action.yml index d5d1159..a6fd27a 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/input-params.ts b/src/input-params.ts index a6a4108..4947d8c 100644 --- a/src/input-params.ts +++ b/src/input-params.ts @@ -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) } diff --git a/src/job-summary.ts b/src/job-summary.ts index 393269b..ce1516f 100644 --- a/src/job-summary.ts +++ b/src/job-summary.ts @@ -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' @@ -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 { + 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 = `

Job Summary for gradle-build-action

+
${github.context.workflow} :: ${github.context.job}
+ +${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 ` -

Gradle Builds

- + @@ -84,3 +119,7 @@ function shouldGenerateJobSummary(): boolean { return params.isJobSummaryEnabled() } + +function shouldAddPRComment(): boolean { + return params.isPRCommentEnabled() +}
Root ProjectGradle Root Project Requested Tasks Gradle Version Build Outcome