Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
85 changes: 85 additions & 0 deletions .github/actions/comment-pull-request/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Comment on Pull Request
description: Create or update a comment on a pull request, optionally keyed by a tag

inputs:
message:
description: Comment body
required: false
comment-tag:
description: A tag embedded in the comment that is used to find and update an existing comment instead of creating duplicates
required: false
pr-number:
description: Number of the pull request to comment on
required: false
default: ${{ github.event.pull_request.number || github.event.issue.number || github.event.workflow_run.pull_requests[0].number }}
github-token:
description: GitHub token used to authenticate with the API
required: false
default: ${{ github.token }}

runs:
using: composite
steps:
- name: Create or update comment
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
env:
MESSAGE: ${{ inputs.message }}
COMMENT_TAG: ${{ inputs.comment-tag }}
PR_NUMBER: ${{ inputs.pr-number }}
with:
github-token: ${{ inputs.github-token }}
script: |
const message = process.env.MESSAGE;
const commentTag = process.env.COMMENT_TAG || '';
const prNumber = Number(process.env.PR_NUMBER);

if (!prNumber) {
core.setFailed('No pull request number provided (pr-number input) and none could be inferred from context.');
return;
}
if (!message) {
core.setFailed("The 'message' input must be provided.");
return;
}

const { owner, repo } = context.repo;

// Keep the same marker format as the previously used
// thollander/actions-comment-pull-request action so existing comments
// are updated instead of duplicated.
const tagPattern = commentTag
? `<!-- thollander/actions-comment-pull-request "${commentTag}" -->`
: '';
const body = tagPattern ? `${message}\n${tagPattern}` : message;

let commentId;
if (tagPattern) {
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: prNumber,
per_page: 100,
});
const existing = comments.find((comment) => comment.body.includes(tagPattern));
if (existing) {
commentId = existing.id;
}
}

if (commentId) {
core.info(`Updating comment ${commentId} on PR #${prNumber}`);
await github.rest.issues.updateComment({
owner,
repo,
comment_id: commentId,
body,
});
} else {
core.info(`Creating comment on PR #${prNumber}`);
await github.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body,
});
}
22 changes: 19 additions & 3 deletions .github/workflows/leave-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,26 @@ jobs:
echo "EOF"
} >> "$GITHUB_OUTPUT"

- name: Add Comment to PR
# `github.event.workflow_run.pull_requests` is only populated for PRs from
# branches in this repository; for fork PRs it is empty, so we resolve the
# PR number from the head repository and branch instead.
- name: Resolve PR number
id: pr
if: steps.combine.outputs.combined
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
env:
GH_TOKEN: ${{ github.token }}
HEAD_REPO: ${{ github.event.workflow_run.head_repository.login }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
run: |
PR_NUMBER=$(gh api \
"repos/${GITHUB_REPOSITORY}/pulls?head=${HEAD_REPO}:${HEAD_BRANCH}" \
--jq '[.[] | select(.state == "open")][0].number // .[0].number // empty')
echo "number=${PR_NUMBER:-}" >> "$GITHUB_OUTPUT"

- name: Add Comment to PR
if: steps.combine.outputs.combined && steps.pr.outputs.number
uses: ./.github/actions/comment-pull-request
with:
comment-tag: compared
message: ${{ steps.combine.outputs.combined }}
pr-number: ${{ github.event.workflow_run.pull_requests[0].number }}
pr-number: ${{ steps.pr.outputs.number }}
Loading