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
|
@ -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
Add a link
Reference in a new issue