action-pr-comment/action.yml

94 lines
3.4 KiB
YAML

# git.auengun.net/homelab/action-pr-comment
# Copyright (C) 2023 GregoryDosh
---
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: "${{ secrets.GITHUB_TOKEN }}"
addFooter:
description: "A GitHub (or compatible) token."
required: true
default: "true"
runs:
using: "composite"
steps:
- uses: https://github.com/actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
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)
const inputAnchor = core.getInput('pr-anchor', {required: true})
const anchor = `<!-- Bot Anchor - ${inputAnchor} -->`
core.info(`Param anchor: ${anchor}`)
const filePath = core.getInput('path', {required: true})
core.info(`Param filePath: ${filePath}`)
const addFooter = core.getBooleanInput('addFooter', { required: 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}\`\`\``
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
})
}