Skip to content

Commit

Permalink
Avoid posting comment multiple times in same PR
Browse files Browse the repository at this point in the history
  • Loading branch information
florisvdg committed Jan 15, 2024
1 parent 2917476 commit 97cb4d3
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,37 @@ runs:
COMMENTS_URL: ${{ github.event.pull_request.comments_url }}
COMMITS_URL: ${{ github.event.pull_request.commits_url }}
run: |
if [[ -z "$COMMITS_URL" ]]; then
echo "❌ Could not find commits to check. Make sure the action is ran on `pull_request` or `pull_request_target`"
exit 1
fi
unsigned_commits="$(curl -s -H "Authorization: token $GITHUB_TOKEN" "$COMMITS_URL" | jq '.[] | select(.commit.verification.verified == false) | .commit.message')"
if [[ -n "$unsigned_commits" ]]; then
echo "Found unsigned commits:"
echo "$unsigned_commits"
if [[ -z "$unsigned_commits" ]]; then
echo "🎉 No unsigned commits found"
exit 0
fi
# Escape double quotes and newlines in comment
COMMENT_TEXT="$(echo "$COMMENT_TEXT" | sed 's/"/\\"/g' | awk '{printf "%s\\n", $0}')"
echo "🚨 Found unsigned commits:"
echo "$unsigned_commits"
curl -X POST $COMMENTS_URL \
-H "Content-Type: application/json" \
-H "Authorization: token $GITHUB_TOKEN" \
--data "{ \"body\": \"$COMMENT_TEXT\" }"
if [[ -n "$COMMENTS_URL" ]]; then
echo "⏱️ Checking if comment with commit signing instructions was already posted in PR"
exit 1
if curl -sf -H "Authorization: token $GITHUB_TOKEN" "$COMMENTS_URL" | jq -e --arg comment_text "$(echo "$COMMENT_TEXT" | tr -cd '[:alnum:]')" 'any(.[]; .body | gsub("[^[:alnum:]]"; "") == $comment_text)'; then
echo "✅ Comment already exists in PR, not posting it again"
else
echo "📤 Posting PR comment with commit signing instructions"
# Escape double quotes and newlines in comment
COMMENT_TEXT="$(echo "$COMMENT_TEXT" | sed 's/"/\\"/g' | awk '{printf "%s\\n", $0}')"
curl -X POST "$COMMENTS_URL" \
-H "Content-Type: application/json" \
-H "Authorization: token $GITHUB_TOKEN" \
--data "{ \"body\": \"$COMMENT_TEXT\" }"
fi
fi
echo "No unsigned commits found 🎉"
echo "❌ Returning with exit code 1 because of unsigned commits"
exit 1

0 comments on commit 97cb4d3

Please sign in to comment.