feat: implement replacing previous comments
Based on https://github.com/peter-evans/find-comment
This commit is contained in:
parent
7253c64753
commit
538f4706aa
2 changed files with 61 additions and 23 deletions
41
action.yml
41
action.yml
|
@ -67,10 +67,51 @@ outputs:
|
|||
runs:
|
||||
using: 'composite'
|
||||
steps:
|
||||
- name: Find PR (if it exists)
|
||||
id: pr-number
|
||||
if: inputs.comment-on-pr == 'true'
|
||||
run: |
|
||||
. "$GITHUB_ACTION_PATH/utils.sh"
|
||||
|
||||
log 'Determine head_ref'
|
||||
# For push & tag events it'll bet GITHUB_REF_NAME, for pull_request events it'll be GITHUB_HEAD_REF
|
||||
head_ref=${GITHUB_REF_NAME:-$GITHUB_HEAD_REF}
|
||||
|
||||
log "Get PR number for $head_ref"
|
||||
prs=$(curl -X 'GET' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?state=open&sort=recentupdate" \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'accept: application/json')
|
||||
|
||||
pr_number=$(echo "$prs" |
|
||||
jq --arg head_ref "$head_ref" '.[] | select(.head.ref == $head_ref) | .number')
|
||||
|
||||
# Protect against running before a PR is made or if it is triggered on the main branch
|
||||
if [ -z "$pr_number" ]; then
|
||||
warn "No PR created for this commit"
|
||||
echo "pr-number=" >> "$GIHUB_OUTPUT"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
log "Retrieved index: $pr_number"
|
||||
log "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number"
|
||||
|
||||
echo "pr-number=$pr_number" >> "$GIHUB_OUTPUT"
|
||||
- name: Find previous comment (if present)
|
||||
# We want to generate a comment, and we we able to fin the PR number
|
||||
if: inputs.comment-on-pr == 'true' && steps.pr-number.outputs.pr-number != ''
|
||||
id: find-comment
|
||||
uses: https://github.com/peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3
|
||||
with:
|
||||
issue-number: steps.pr-number.outputs.pr-number
|
||||
direction: first
|
||||
body-includes: "<!-- AUTOGENERATED by nix-flake-outputs-size action -->"
|
||||
- name: Create report
|
||||
if: inputs.comment-on-pr == 'true' || inputs.generate-artifact == 'true'
|
||||
env:
|
||||
PR_ID: ${{ steps.pr-number.outputs.pr-number }}
|
||||
COMMENT: ${{ inputs.comment-on-pr }}
|
||||
COMMENT_ID: ${{ steps.find-comment.outputs.comment-id }}
|
||||
ARTIFACT_NAME: ${{ inputs.artifact-name }}
|
||||
DO_COMPARISON: ${{ inputs.do-comparison }}
|
||||
BASE_BRANCH: ${{ inputs.base-branch }}
|
||||
|
|
|
@ -39,6 +39,8 @@ has_elements() {
|
|||
# If BASE_REPORT is provided, a comparison will be made
|
||||
markdown_from_report() {
|
||||
cat <<-"EOF"
|
||||
<!-- AUTOGENERATED by nix-flake-outputs-size action -->
|
||||
|
||||
# Flake output sizes
|
||||
|
||||
**Definitions:**
|
||||
|
@ -116,28 +118,12 @@ if [ "${CI:-false}" != 'true' ]; then
|
|||
exit 0
|
||||
fi
|
||||
|
||||
log 'Determine head_ref'
|
||||
# For push & tag events it'll bet GITHUB_REF_NAME, for pull_request events it'll be GITHUB_HEAD_REF
|
||||
head_ref=${GITHUB_REF_NAME:-$GITHUB_HEAD_REF}
|
||||
|
||||
log "Get PR number for $head_ref"
|
||||
prs=$(curl -X 'GET' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls?state=open&sort=recentupdate" \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'accept: application/json')
|
||||
|
||||
pr_number=$(echo "$prs" |
|
||||
jq --arg head_ref "$head_ref" '.[] | select(.head.ref == $head_ref) | .number')
|
||||
|
||||
# Protect against running before a PR is made or if it is triggered on the main branch
|
||||
if [ -z "$pr_number" ]; then
|
||||
if [ -z "$PR_ID" ]; then
|
||||
warn "No PR created for this commit"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
log "Retrieved index: $pr_number"
|
||||
log "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number"
|
||||
|
||||
log 'Generating comment body'
|
||||
comment=$(markdown_from_report "$@")
|
||||
|
||||
|
@ -150,9 +136,20 @@ group 'Request data'
|
|||
log "$data"
|
||||
endgroup
|
||||
|
||||
curl -o - -X 'POST' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" \
|
||||
-H 'accept: application/json' \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "$data"
|
||||
if [ -z "$COMMENT_ID" ]; then
|
||||
log 'Posting new comment'
|
||||
curl -o - -X 'POST' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_ID/comments" \
|
||||
-H 'accept: application/json' \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "$data"
|
||||
else
|
||||
log "Editing comment $COMMENT_ID"
|
||||
curl -o - -X 'PATCH' \
|
||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_ID/comments/$COMMENT_ID" \
|
||||
-H 'accept: application/json' \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d "$data"
|
||||
fi
|
||||
|
|
Loading…
Add table
Reference in a new issue