Skip to content

Commit 583e51c

Browse files
committed
Add -R option as in other gh commands
Enables reacting from outside the repository
1 parent 922dd8c commit 583e51c

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

gh-react

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,47 @@ print_success() {
3535
echo -e "${GREEN}$1${NC}"
3636
}
3737

38-
# Check if PR number is provided
39-
if [ -z "$1" ]; then
40-
echo -e "${YELLOW}Usage:${NC} ${WHITE}gh react <pr_number>${NC}"
38+
usage() {
39+
echo -e "${YELLOW}Usage:${NC} ${WHITE}gh react [-R [HOST/]OWNER/REPO] <pr_number>${NC}"
4140
echo ""
4241
echo "Examples:"
4342
echo " gh react 23 # React to comments in PR #23"
4443
echo " gh react 156 # React to comments in PR #156"
4544
exit 1
45+
}
46+
47+
while getopts 'hR:' opt; do
48+
case "$opt" in
49+
h) usage; exit;;
50+
R)
51+
# canonicalize by removing the HOST part of [HOST/]OWNER/REPO
52+
# Note % and # expansions are noops if the string isn't an affix of
53+
# the parameter!
54+
repo="${OPTARG#"${OPTARG%/*/*}"/}";;
55+
*) usage; exit 1;;
56+
esac
57+
done
58+
shift $((OPTIND - 1))
59+
60+
# Check if PR number is provided
61+
if [ $# -ne 1 ]; then
62+
usage
4663
fi
4764

4865
PR_NUMBER=$1
66+
67+
if [ -z "$repo" ]; then
68+
OWNER=$(gh repo view --json owner -q ".owner.login")
69+
REPO=$(gh repo view --json name -q ".name")
70+
repo="$OWNER/$REPO"
71+
fi
72+
73+
4974
echo -e "${PURPLE}${COMMENT} GitHub PR Reaction Tool${NC}"
5075
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
5176
print_info "Fetching comments for PR #${WHITE}$PR_NUMBER${NC}..."
5277

53-
OWNER=$(gh repo view --json owner -q ".owner.login")
54-
REPO=$(gh repo view --json name -q ".name")
55-
56-
print_info "Repository: ${WHITE}$OWNER/$REPO${NC}"
78+
print_info "Repository: ${WHITE}$repo${NC}"
5779

5880
# Check if PR exists first
5981
print_info "Validating PR #${WHITE}$PR_NUMBER${NC}..."
@@ -71,16 +93,16 @@ echo ""
7193
print_info "Gathering all comments and content..."
7294

7395
# Get the PR description/body itself
74-
PR_BODY=$(gh api repos/$OWNER/$REPO/pulls/$PR_NUMBER --jq 'select(.body != null and .body != "") | "PR_BODY|\(.number)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
96+
PR_BODY=$(gh api repos/$repo/pulls/$PR_NUMBER --jq 'select(.body != null and .body != "") | "PR_BODY|\(.number)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
7597

7698
# Get issue comments (general PR comments)
77-
ISSUE_COMMENTS=$(gh api repos/$OWNER/$REPO/issues/$PR_NUMBER/comments --jq '.[] | "ISSUE|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
99+
ISSUE_COMMENTS=$(gh api repos/$repo/issues/$PR_NUMBER/comments --jq '.[] | "ISSUE|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
78100

79101
# Get review comments (inline code review comments)
80-
REVIEW_COMMENTS=$(gh api repos/$OWNER/$REPO/pulls/$PR_NUMBER/comments --jq '.[] | "REVIEW|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
102+
REVIEW_COMMENTS=$(gh api repos/$repo/pulls/$PR_NUMBER/comments --jq '.[] | "REVIEW|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
81103

82104
# Get review summary comments (comments submitted with reviews)
83-
REVIEW_SUMMARY_COMMENTS=$(gh api repos/$OWNER/$REPO/pulls/$PR_NUMBER/reviews --jq '.[] | select(.body != null and .body != "") | "REVIEW_SUMMARY|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
105+
REVIEW_SUMMARY_COMMENTS=$(gh api repos/$repo/pulls/$PR_NUMBER/reviews --jq '.[] | select(.body != null and .body != "") | "REVIEW_SUMMARY|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
84106

85107
# Combine all comments
86108
ALL_COMMENTS=""
@@ -138,7 +160,7 @@ COUNTER=1
138160
echo "$ALL_COMMENTS" | while IFS='|' read -r type id author_and_content; do
139161
author=$(echo "$author_and_content" | cut -d':' -f1)
140162
content=$(echo "$author_and_content" | cut -d':' -f2- | head -c 80)
141-
163+
142164
case $type in
143165
"PR_BODY")
144166
echo -e "${WHITE}[$COUNTER]${NC} ${PURPLE}📄 PR Description${NC} by ${CYAN}@$author${NC}"
@@ -210,22 +232,22 @@ echo ""
210232
print_info "Sending ${WHITE}$REACTION${NC} reaction to comment ${WHITE}$COMMENT_ID${NC}..."
211233
if [ "$COMMENT_TYPE" = "PR_BODY" ]; then
212234
# React to PR description/body
213-
RESPONSE=$(gh api --method POST repos/$OWNER/$REPO/issues/$PR_NUMBER/reactions \
235+
RESPONSE=$(gh api --method POST repos/$repo/issues/$PR_NUMBER/reactions \
214236
-f "content=$REACTION" \
215237
-H "Accept: application/vnd.github+json" 2>&1)
216238
elif [ "$COMMENT_TYPE" = "ISSUE" ]; then
217239
# React to issue comment
218-
RESPONSE=$(gh api --method POST repos/$OWNER/$REPO/issues/comments/$COMMENT_ID/reactions \
240+
RESPONSE=$(gh api --method POST repos/$repo/issues/comments/$COMMENT_ID/reactions \
219241
-f "content=$REACTION" \
220242
-H "Accept: application/vnd.github+json" 2>&1)
221243
elif [ "$COMMENT_TYPE" = "REVIEW" ]; then
222244
# React to review comment (inline code comment)
223-
RESPONSE=$(gh api --method POST repos/$OWNER/$REPO/pulls/comments/$COMMENT_ID/reactions \
245+
RESPONSE=$(gh api --method POST repos/$repo/pulls/comments/$COMMENT_ID/reactions \
224246
-f "content=$REACTION" \
225247
-H "Accept: application/vnd.github+json" 2>&1)
226248
elif [ "$COMMENT_TYPE" = "REVIEW_SUMMARY" ]; then
227249
# React to review summary comment
228-
RESPONSE=$(gh api --method POST repos/$OWNER/$REPO/pulls/reviews/$COMMENT_ID/reactions \
250+
RESPONSE=$(gh api --method POST repos/$repo/pulls/reviews/$COMMENT_ID/reactions \
229251
-f "content=$REACTION" \
230252
-H "Accept: application/vnd.github+json" 2>&1)
231253
else
@@ -238,8 +260,8 @@ if [ $? -eq 0 ]; then
238260
echo ""
239261
print_success "Reaction added successfully! 🎉"
240262
echo ""
241-
echo -e "${WHITE}🔗 View PR:${NC} https://github.com/$OWNER/$REPO/pull/$PR_NUMBER"
242-
263+
echo -e "${WHITE}🔗 View PR:${NC} https://github.com/$repo/pull/$PR_NUMBER"
264+
243265
# Show reaction emoji based on type
244266
case $REACTION in
245267
"+1") echo -e "${GREEN}👍 Added thumbs up!${NC}" ;;

0 commit comments

Comments
 (0)