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:
|
runs:
|
||||||
using: 'composite'
|
using: 'composite'
|
||||||
steps:
|
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
|
- name: Create report
|
||||||
if: inputs.comment-on-pr == 'true' || inputs.generate-artifact == 'true'
|
if: inputs.comment-on-pr == 'true' || inputs.generate-artifact == 'true'
|
||||||
env:
|
env:
|
||||||
|
PR_ID: ${{ steps.pr-number.outputs.pr-number }}
|
||||||
COMMENT: ${{ inputs.comment-on-pr }}
|
COMMENT: ${{ inputs.comment-on-pr }}
|
||||||
|
COMMENT_ID: ${{ steps.find-comment.outputs.comment-id }}
|
||||||
ARTIFACT_NAME: ${{ inputs.artifact-name }}
|
ARTIFACT_NAME: ${{ inputs.artifact-name }}
|
||||||
DO_COMPARISON: ${{ inputs.do-comparison }}
|
DO_COMPARISON: ${{ inputs.do-comparison }}
|
||||||
BASE_BRANCH: ${{ inputs.base-branch }}
|
BASE_BRANCH: ${{ inputs.base-branch }}
|
||||||
|
|
|
@ -39,6 +39,8 @@ has_elements() {
|
||||||
# If BASE_REPORT is provided, a comparison will be made
|
# If BASE_REPORT is provided, a comparison will be made
|
||||||
markdown_from_report() {
|
markdown_from_report() {
|
||||||
cat <<-"EOF"
|
cat <<-"EOF"
|
||||||
|
<!-- AUTOGENERATED by nix-flake-outputs-size action -->
|
||||||
|
|
||||||
# Flake output sizes
|
# Flake output sizes
|
||||||
|
|
||||||
**Definitions:**
|
**Definitions:**
|
||||||
|
@ -116,28 +118,12 @@ if [ "${CI:-false}" != 'true' ]; then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
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
|
# 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"
|
warn "No PR created for this commit"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log "Retrieved index: $pr_number"
|
|
||||||
log "Expected PR URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pulls/$pr_number"
|
|
||||||
|
|
||||||
log 'Generating comment body'
|
log 'Generating comment body'
|
||||||
comment=$(markdown_from_report "$@")
|
comment=$(markdown_from_report "$@")
|
||||||
|
|
||||||
|
@ -150,9 +136,20 @@ group 'Request data'
|
||||||
log "$data"
|
log "$data"
|
||||||
endgroup
|
endgroup
|
||||||
|
|
||||||
curl -o - -X 'POST' \
|
if [ -z "$COMMENT_ID" ]; then
|
||||||
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" \
|
log 'Posting new comment'
|
||||||
-H 'accept: application/json' \
|
curl -o - -X 'POST' \
|
||||||
-H "Authorization: token $GITHUB_TOKEN" \
|
"$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_ID/comments" \
|
||||||
-H 'Content-Type: application/json' \
|
-H 'accept: application/json' \
|
||||||
-d "$data"
|
-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