Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid posting comment multiple times in same PR #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
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
Loading