spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / scripts / comment-on-duplicates.sh
95 lines · 2.5 KBRaw
1#!/usr/bin/env bash
2#
3# Comments on a GitHub issue with a list of potential duplicates.
4# Usage: ./comment-on-duplicates.sh --potential-duplicates 456 789 101
5#
6# The base issue number is read from the workflow event payload.
7#
8
9set -euo pipefail
10
11REPO="anthropics/claude-code"
12
13# Read from event payload so the issue number is bound to the triggering event.
14# Falls back to workflow_dispatch inputs for manual runs.
15BASE_ISSUE=$(jq -r '.issue.number // .inputs.issue_number // empty' "${GITHUB_EVENT_PATH:?GITHUB_EVENT_PATH not set}")
16if ! [[ "$BASE_ISSUE" =~ ^[0-9]+$ ]]; then
17 echo "Error: no issue number in event payload" >&2
18 exit 1
19fi
20
21DUPLICATES=()
22
23# Parse arguments
24while [[ $# -gt 0 ]]; do
25 case $1 in
26 --potential-duplicates)
27 shift
28 while [[ $# -gt 0 && ! "$1" =~ ^-- ]]; do
29 DUPLICATES+=("$1")
30 shift
31 done
32 ;;
33 *)
34 echo "Error: unknown argument (only --potential-duplicates is accepted)" >&2
35 exit 1
36 ;;
37 esac
38done
39
40# Validate duplicates
41if [[ ${#DUPLICATES[@]} -eq 0 ]]; then
42 echo "Error: --potential-duplicates requires at least one issue number" >&2
43 exit 1
44fi
45
46if [[ ${#DUPLICATES[@]} -gt 3 ]]; then
47 echo "Error: --potential-duplicates accepts at most 3 issues" >&2
48 exit 1
49fi
50
51for dup in "${DUPLICATES[@]}"; do
52 if ! [[ "$dup" =~ ^[0-9]+$ ]]; then
53 echo "Error: duplicate issue must be a number, got: $dup" >&2
54 exit 1
55 fi
56done
57
58# Validate that base issue exists
59if ! gh issue view "$BASE_ISSUE" --repo "$REPO" &>/dev/null; then
60 echo "Error: issue #$BASE_ISSUE does not exist in $REPO" >&2
61 exit 1
62fi
63
64# Validate that all duplicate issues exist
65for dup in "${DUPLICATES[@]}"; do
66 if ! gh issue view "$dup" --repo "$REPO" &>/dev/null; then
67 echo "Error: issue #$dup does not exist in $REPO" >&2
68 exit 1
69 fi
70done
71
72# Build comment body
73COUNT=${#DUPLICATES[@]}
74if [[ $COUNT -eq 1 ]]; then
75 HEADER="Found 1 possible duplicate issue:"
76else
77 HEADER="Found $COUNT possible duplicate issues:"
78fi
79
80BODY="$HEADER"$'\n\n'
81INDEX=1
82for dup in "${DUPLICATES[@]}"; do
83 BODY+="$INDEX. https://github.com/$REPO/issues/$dup"$'\n'
84 ((INDEX++))
85done
86
87BODY+=$'\n'"This issue will be automatically closed as a duplicate in 3 days."$'\n\n'
88BODY+="- If your issue is a duplicate, please close it and 👍 the existing issue instead"$'\n'
89BODY+="- To prevent auto-closure, add a comment or 👎 this comment"$'\n\n'
90BODY+="🤖 Generated with [Claude Code](https://claude.ai/code)"
91
92# Post the comment
93gh issue comment "$BASE_ISSUE" --repo "$REPO" --body "$BODY"
94
95echo "Posted duplicate comment on issue #$BASE_ISSUE"