Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions sources/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@actions/glob": "0.6.1",
"@actions/http-client": "4.0.0",
"@actions/tool-cache": "4.0.0",
"@octokit/graphql-schema": "15.26.1",
"@octokit/webhooks-types": "7.6.1",
"cheerio": "1.2.0",
"semver": "7.7.4",
Expand Down
4 changes: 4 additions & 0 deletions sources/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ export class SummaryConfig {
return this.shouldAddJobSummary(this.getJobSummaryOption(), hasFailure)
}

canAddPRComment(): boolean {
return this.getPRCommentOption() !== JobSummaryOption.Never
}

shouldAddPRComment(hasFailure: boolean): boolean {
return this.shouldAddJobSummary(this.getPRCommentOption(), hasFailure)
}
Expand Down
68 changes: 66 additions & 2 deletions sources/src/job-summary.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Repository} from '@octokit/graphql-schema'

import {BuildResult} from './build-results'
import {SummaryConfig, getActionId, getGithubToken} from './configuration'
import {DependencyGraphConfig, getActionId, getGithubToken, getJobMatrix, SummaryConfig} from './configuration'
import {Deprecation, getDeprecations, getErrors} from './deprecation-collector'

export async function generateJobSummary(
Expand Down Expand Up @@ -33,6 +34,10 @@ export async function generateJobSummary(
core.info('============================')
}

if (config.canAddPRComment()) {
await minimizeObsoletePRComments()
}

if (config.shouldAddPRComment(hasFailure)) {
await addPRComment(summaryTable)
}
Expand All @@ -48,7 +53,8 @@ async function addPRComment(jobSummary: string): Promise<void> {
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</h3>
const prComment = `${jobMarker(context)}
<h3>Job Summary for Gradle</h3>
<a href="${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}" target="_blank">
<h5>${context.workflow} :: <em>${context.job}</em></h5>
</a>
Expand All @@ -57,6 +63,7 @@ ${jobSummary}`

const github_token = getGithubToken()
const octokit = github.getOctokit(github_token)

try {
await octokit.rest.issues.createComment({
...context.repo,
Expand Down Expand Up @@ -201,3 +208,60 @@ function truncateString(str: string, maxLength: number): string {
return str
}
}

async function minimizeObsoletePRComments(): Promise<void> {
const context = github.context
if (context.payload.pull_request == null) {
core.info('No pull_request trigger detected: not minimizing obsolete PR comments')
return
}

const prNumber = context.payload.pull_request.number
core.info(`Minimizing obsolete Job Summary comments on PR #${prNumber}.`)

const marker = jobMarker(context)
const octokit = github.getOctokit(getGithubToken())
const {owner, repo} = context.repo

const query = `
query($owner: String!, $repo: String!, $prNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
comments(last: 100) {
nodes { id body isMinimized url }
}
}
}
}
`
let comments
try {
const {repository} = await octokit.graphql<{repository: Repository}>(query, {owner, repo, prNumber})
comments = repository.pullRequest?.comments?.nodes?.filter((c): c is NonNullable<typeof c> => c !== null) ?? []
} catch (error) {
return core.warning(`Failed to fetch comments: ${error}`)
}

const mutation = `
mutation($id: ID!) {
minimizeComment(input: {subjectId: $id, classifier: OUTDATED}) {
clientMutationId
}
}
`

const commentsToMinimize = comments
.filter(c => !c.isMinimized && c.body.includes(marker))
.map(async c =>
octokit
.graphql(mutation, {id: c.id})
.then(() => core.info(`Successfully minimized (id:${c.id}, url:${c.url})`))
.catch(e => core.warning(`Failed to minimize (id:${c.id}, url:${c.url}, error:${e?.message || e})`))
)
await Promise.allSettled(commentsToMinimize)
}

function jobMarker(context: typeof github.context): string {
const jobCorrelator = DependencyGraphConfig.constructJobCorrelator(context.workflow, context.job, getJobMatrix())
return `<!-- gradle-job-summary: ${jobCorrelator} -->`
}
Loading