Skip to content

Restrict Issue Comments #8

Restrict Issue Comments

Restrict Issue Comments #8

# .github/workflows/restrict-issue-comments-alt.yml
name: Restrict Issue Comments - Alternative
on:
issue_comment:
types: [created]
permissions:
issues: write
contents: write
jobs:
moderate-comments:
runs-on: ubuntu-latest
steps:
- name: Check permissions and moderate
env:
GH_TOKEN: ${{ github.token }}
run: |
# Get issue and comment details
ISSUE_NUMBER=${{ github.event.issue.number }}
COMMENT_ID=${{ github.event.comment.id }}
COMMENT_USER="${{ github.event.comment.user.login }}"
ISSUE_CREATOR="${{ github.event.issue.user.login }}"
ORG_NAME="ChatAndBuild"
# Check if commenter is issue creator
if [ "$COMMENT_USER" = "$ISSUE_CREATOR" ]; then
echo "✅ Allowing comment from issue creator: $COMMENT_USER"
exit 0
fi
# Check if commenter is organization member
if gh api orgs/$ORG_NAME/members/$COMMENT_USER > /dev/null 2>&1; then
echo "✅ Allowing comment from org member: $COMMENT_USER"
exit 0
fi
echo "🚫 Restricting comment from: $COMMENT_USER"
# Delete comment
gh api -X DELETE /repos/${{ github.repository }}/issues/comments/$COMMENT_ID
# Lock issue
gh api -X PUT /repos/${{ github.repository }}/issues/$ISSUE_NUMBER/lock
# Add warning
gh api -X POST /repos/${{ github.repository }}/issues/$ISSUE_NUMBER/comments \
-f body="⚠️ **Comment Restricted**\n\nOnly @$ORG_NAME organization members and the issue creator (@$ISSUE_CREATOR) can comment on this issue."