#!/usr/bin/env bash # # comment-on-duplicates.sh - Posts a duplicate issue comment with auto-close warning. # # Usage: # ./scripts/comment-on-duplicates.sh --base-issue 123 --potential-duplicates 456 789 # set -euo pipefail REPO="${GITHUB_REPOSITORY:-}" if [ -z "$REPO" ]; then echo "Error: GITHUB_REPOSITORY is not set" >&2 exit 1 fi BASE_ISSUE="" DUPLICATES=() # Parse arguments while [[ $# -gt 0 ]]; do case "$1" in --base-issue) BASE_ISSUE="$2" shift 2 ;; --potential-duplicates) shift while [[ $# -gt 0 && ! "$1" =~ ^-- ]]; do DUPLICATES+=("$1") shift done ;; *) echo "Error: Unknown argument: $1" >&2 exit 1 ;; esac done # Validate inputs if [ -z "$BASE_ISSUE" ]; then echo "Error: --base-issue is required" >&2 exit 1 fi if ! [[ "$BASE_ISSUE" =~ ^[0-9]+$ ]]; then echo "Error: --base-issue must be a number, got: $BASE_ISSUE" >&2 exit 1 fi if [ ${#DUPLICATES[@]} -eq 0 ]; then echo "Error: --potential-duplicates requires at least one issue number" >&2 exit 1 fi for dup in "${DUPLICATES[@]}"; do if ! [[ "$dup" =~ ^[0-9]+$ ]]; then echo "Error: duplicate issue must be a number, got: $dup" >&2 exit 1 fi done # Limit to 3 duplicates max if [ ${#DUPLICATES[@]} -gt 3 ]; then echo "Warning: Limiting to first 3 duplicates" >&2 DUPLICATES=("${DUPLICATES[@]:0:3}") fi # Validate that the base issue exists and is open if ! gh issue view "$BASE_ISSUE" --repo "$REPO" --json state -q '.state' | grep -qi 'open'; then echo "Error: Issue #$BASE_ISSUE is not open or does not exist" >&2 exit 1 fi # Build the duplicate links list LINKS="" COUNT=0 for dup in "${DUPLICATES[@]}"; do # Validate duplicate issue exists if gh issue view "$dup" --repo "$REPO" --json number -q '.number' > /dev/null 2>&1; then COUNT=$((COUNT + 1)) LINKS="${LINKS}${COUNT}. https://github.com/${REPO}/issues/${dup} " else echo "Warning: Issue #$dup does not exist, skipping" >&2 fi done if [ "$COUNT" -eq 0 ]; then echo "Error: None of the specified duplicate issues exist" >&2 exit 1 fi # Build and post the comment COMMENT="Found ${COUNT} possible duplicate issue(s): ${LINKS} This issue will be automatically closed as a duplicate in 3 days. - To prevent auto-closure, add a comment or react with :thumbsdown: on this comment." gh issue comment "$BASE_ISSUE" --repo "$REPO" --body "$COMMENT" # Add the duplicate label gh issue edit "$BASE_ISSUE" --repo "$REPO" --add-label "duplicate" echo "Posted duplicate comment on issue #$BASE_ISSUE with $COUNT potential duplicate(s)"