action-pr-comment/action.yml
GregoryDosh 877f1395e5
Some checks failed
Create Release / Create Release (push) Has been cancelled
Update Version / Update Version (push) Successful in 6s
Initial Dosh LLC Config 🌈🐻
2025-11-24 12:03:04 -06:00

101 lines
3.8 KiB
YAML

# SPDX-License-Identifier: AGPL-3.0-or-later
# SPDX-FileCopyrightText: 2025 Dosh LLC
---
name: Post/Update PR Comment
description: An action to add or update an existing comment anchored in a PR for test results, status messages, etc.
inputs:
pr-anchor:
description: "The unique identifier to anchor/update when posting a comment."
required: true
default: "${{ github.workflow }}"
path:
description: "A path to the file containing the comment to leave in the PR."
required: true
default: "${{github.workspace}}/.pr-comment.md"
github-token:
description: "A GitHub (or compatible) token."
required: true
default: "${{ env.GITHUB_TOKEN }}"
add-footer:
description: "Adds a footer to the comment containing the commit SHA and some other run information."
required: true
default: "true"
runs:
using: "composite"
steps:
- uses: https://github.com/actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
INPUT_PR_ANCHOR: "${{ inputs.pr-anchor }}"
INPUT_PATH: "${{ inputs.path }}"
INPUT_ADD_FOOTER: "${{ inputs.add-footer }}"
with:
github-token: ${{ inputs.github-token }}
script: |
core.info("Starting PR Comment Script")
const fs = require('fs')
const util = require('util')
const readFile = util.promisify(fs.readFile)
if (!process.env.INPUT_PR_ANCHOR) { core.setFailed('Missing input pr-anchor') }
if (!process.env.INPUT_PATH) { core.setFailed('Missing input path') }
if (!process.env.INPUT_ADD_FOOTER) { core.setFailed('Missing input add-footer') }
const anchor = `<!-- Bot Anchor - ${process.env.INPUT_PR_ANCHOR} -->`
core.info(`Param anchor: ${anchor}`)
const filePath = process.env.INPUT_PATH
core.info(`Param filePath: ${filePath}`)
const addFooter = process.env.INPUT_ADD_FOOTER === 'true'
core.info(`Param addFooter: ${addFooter}`)
let COMMENT = ""
try {
COMMENT = await readFile(filePath)
core.info(`Comment length: ${COMMENT.length}`)
}
catch (e) {
COMMENT = `⚠️ 📢 Plugin misconfigured. Please fix or validate parameters. 📢 ⚠️\n\n\`\`\`shell\n${e}\n\`\`\``
core.error(COMMENT)
core.setFailed(`Action failed with error ${e}`);
}
let footer = ""
if (addFooter) {
footer = `\n---\n**Commit:** ${ context.sha }\n**Actions:** [**\`#${ context.runNumber }\`**](${ context.serverUrl }/${ context.repo.owner }/${ context.repo.repo }/actions/runs/${ context.runNumber })`
}
core.info("Creating Templated Markdown comment")
const output = `${anchor}\n${COMMENT}\n${footer}`
core.info("Gathering existing comments")
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
core.info("Finding existing comment by anchor")
const existingComment = comments.find(comment => {
return comment.body.includes(anchor)
})
if (existingComment) {
console.info("Updating existing comment")
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: output
})
} else {
console.info("Creating new comment")
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}