Skip to content

Commit 9df0829

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

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

gh-react

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,49 @@ 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}"
40+
echo ""
41+
echo "Flags:"
42+
echo "-R [HOST/]OWNER/REPO Select another repository using the [HOST/]OWNER/REPO format"
4143
echo ""
4244
echo "Examples:"
4345
echo " gh react 23 # React to comments in PR #23"
4446
echo " gh react 156 # React to comments in PR #156"
45-
exit 1
47+
}
48+
49+
while getopts 'hR:' opt; do
50+
case "$opt" in
51+
h) usage; exit;;
52+
R)
53+
# canonicalize by removing the HOST part of [HOST/]OWNER/REPO
54+
# Note % and # expansions are noops if the string isn't an affix of
55+
# the parameter!
56+
repo="${OPTARG#"${OPTARG%/*/*}"/}";;
57+
*) usage; exit 1;;
58+
esac
59+
done
60+
shift $((OPTIND - 1))
61+
62+
# Check if PR number is provided
63+
if [ $# -ne 1 ]; then
64+
usage
4665
fi
4766

4867
PR_NUMBER=$1
68+
69+
if [ -z "$repo" ]; then
70+
OWNER=$(gh repo view --json owner -q ".owner.login")
71+
REPO=$(gh repo view --json name -q ".name")
72+
repo="$OWNER/$REPO"
73+
fi
74+
75+
4976
echo -e "${PURPLE}${COMMENT} GitHub PR Reaction Tool${NC}"
5077
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
5178
print_info "Fetching comments for PR #${WHITE}$PR_NUMBER${NC}..."
5279

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}"
80+
print_info "Repository: ${WHITE}$repo${NC}"
5781

5882
# Check if PR exists first
5983
print_info "Validating PR #${WHITE}$PR_NUMBER${NC}..."
@@ -71,16 +95,16 @@ echo ""
7195
print_info "Gathering all comments and content..."
7296

7397
# 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 "")
98+
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 "")
7599

76100
# 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 "")
101+
ISSUE_COMMENTS=$(gh api repos/$repo/issues/$PR_NUMBER/comments --jq '.[] | "ISSUE|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
78102

79103
# 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 "")
104+
REVIEW_COMMENTS=$(gh api repos/$repo/pulls/$PR_NUMBER/comments --jq '.[] | "REVIEW|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "")
81105

82106
# 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 "")
107+
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 "")
84108

85109
# Combine all comments
86110
ALL_COMMENTS=""
@@ -138,7 +162,7 @@ COUNTER=1
138162
echo "$ALL_COMMENTS" | while IFS='|' read -r type id author_and_content; do
139163
author=$(echo "$author_and_content" | cut -d':' -f1)
140164
content=$(echo "$author_and_content" | cut -d':' -f2- | head -c 80)
141-
165+
142166
case $type in
143167
"PR_BODY")
144168
echo -e "${WHITE}[$COUNTER]${NC} ${PURPLE}📄 PR Description${NC} by ${CYAN}@$author${NC}"
@@ -210,22 +234,22 @@ echo ""
210234
print_info "Sending ${WHITE}$REACTION${NC} reaction to comment ${WHITE}$COMMENT_ID${NC}..."
211235
if [ "$COMMENT_TYPE" = "PR_BODY" ]; then
212236
# React to PR description/body
213-
RESPONSE=$(gh api --method POST repos/$OWNER/$REPO/issues/$PR_NUMBER/reactions \
237+
RESPONSE=$(gh api --method POST repos/$repo/issues/$PR_NUMBER/reactions \
214238
-f "content=$REACTION" \
215239
-H "Accept: application/vnd.github+json" 2>&1)
216240
elif [ "$COMMENT_TYPE" = "ISSUE" ]; then
217241
# React to issue comment
218-
RESPONSE=$(gh api --method POST repos/$OWNER/$REPO/issues/comments/$COMMENT_ID/reactions \
242+
RESPONSE=$(gh api --method POST repos/$repo/issues/comments/$COMMENT_ID/reactions \
219243
-f "content=$REACTION" \
220244
-H "Accept: application/vnd.github+json" 2>&1)
221245
elif [ "$COMMENT_TYPE" = "REVIEW" ]; then
222246
# React to review comment (inline code comment)
223-
RESPONSE=$(gh api --method POST repos/$OWNER/$REPO/pulls/comments/$COMMENT_ID/reactions \
247+
RESPONSE=$(gh api --method POST repos/$repo/pulls/comments/$COMMENT_ID/reactions \
224248
-f "content=$REACTION" \
225249
-H "Accept: application/vnd.github+json" 2>&1)
226250
elif [ "$COMMENT_TYPE" = "REVIEW_SUMMARY" ]; then
227251
# React to review summary comment
228-
RESPONSE=$(gh api --method POST repos/$OWNER/$REPO/pulls/reviews/$COMMENT_ID/reactions \
252+
RESPONSE=$(gh api --method POST repos/$repo/pulls/reviews/$COMMENT_ID/reactions \
229253
-f "content=$REACTION" \
230254
-H "Accept: application/vnd.github+json" 2>&1)
231255
else
@@ -238,8 +262,8 @@ if [ $? -eq 0 ]; then
238262
echo ""
239263
print_success "Reaction added successfully! 🎉"
240264
echo ""
241-
echo -e "${WHITE}🔗 View PR:${NC} https://github.com/$OWNER/$REPO/pull/$PR_NUMBER"
242-
265+
echo -e "${WHITE}🔗 View PR:${NC} https://github.com/$repo/pull/$PR_NUMBER"
266+
243267
# Show reaction emoji based on type
244268
case $REACTION in
245269
"+1") echo -e "${GREEN}👍 Added thumbs up!${NC}" ;;

0 commit comments

Comments
 (0)