Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit b47066d

Browse files
committed
PR comment functionality (draft)
1 parent e0b3f31 commit b47066d

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

.github/workflows/demo-job-summary.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: Demo Job Summary, for Gradle builds
33
on:
44
workflow_dispatch:
55
push:
6+
pull_request:
67

78
env:
89
GRADLE_BUILD_ACTION_CACHE_DEBUG_ENABLED: true
@@ -20,6 +21,8 @@ jobs:
2021
npm run build
2122
- name: Setup Gradle
2223
uses: ./
24+
with:
25+
add-pr-comment: true
2326
- name: Build kotlin-dsl project
2427
working-directory: .github/workflow-samples/kotlin-dsl
2528
run: ./gradlew assemble
@@ -60,6 +63,8 @@ jobs:
6063
touch ~/.gradle/caches/dummy.txt
6164
- name: Setup Gradle
6265
uses: ./
66+
with:
67+
add-pr-comment: true
6368
- name: Run build
6469
working-directory: .github/workflow-samples/groovy-dsl
6570
run: ./gradlew assemble

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ inputs:
6363
required: false
6464
default: true
6565

66+
add-pr-comment:
67+
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.
68+
required: false
69+
default: false
70+
6671
dependency-graph:
6772
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'.
6873
required: false

src/input-params.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export function isJobSummaryEnabled(): boolean {
7171
return getBooleanInput('generate-job-summary', true)
7272
}
7373

74+
export function isPRCommentEnabled(): boolean {
75+
return getBooleanInput('add-pr-comment', false)
76+
}
77+
7478
export function isDependencyGraphEnabled(): boolean {
7579
return getBooleanInput('generate-dependency-graph', true)
7680
}

src/job-summary.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as core from '@actions/core'
2+
import * as github from '@actions/github'
23
import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary'
34

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

1213
if (shouldGenerateJobSummary()) {
14+
core.info('Generating Job Summary')
15+
1316
core.summary.addRaw(summaryTable)
1417
core.summary.addRaw(cachingReport)
1518
await core.summary.write()
@@ -20,6 +23,36 @@ export async function generateJobSummary(buildResults: BuildResult[], cacheListe
2023
core.info(cachingReport)
2124
core.info('============================')
2225
}
26+
27+
if (shouldAddPRComment()) {
28+
await addPRComment(summaryTable)
29+
}
30+
}
31+
32+
async function addPRComment(content: string): Promise<void> {
33+
try {
34+
const github_token = params.getGithubToken()
35+
36+
const context = github.context
37+
if (context.payload.pull_request == null) {
38+
core.info('Not a PR')
39+
core.info(`Issue number: ${context.issue.number}`)
40+
core.info(`Context payload: ${JSON.stringify(context.payload)}`)
41+
return
42+
}
43+
44+
const pull_request_number = context.payload.pull_request.number
45+
core.info(`Adding Job Summary as comment to PR #${pull_request_number}.`)
46+
47+
const octokit = github.getOctokit(github_token)
48+
await octokit.rest.issues.createComment({
49+
...context.repo,
50+
issue_number: pull_request_number,
51+
body: content
52+
})
53+
} catch (error) {
54+
core.warning(`Failed to generate PR comment: ${String(error)}`)
55+
}
2356
}
2457

2558
function renderSummaryTable(results: BuildResult[]): string {
@@ -84,3 +117,7 @@ function shouldGenerateJobSummary(): boolean {
84117

85118
return params.isJobSummaryEnabled()
86119
}
120+
121+
function shouldAddPRComment(): boolean {
122+
return params.isPRCommentEnabled()
123+
}

0 commit comments

Comments
 (0)