|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | | -# react.sh - Add an emoji reaction to a PR comment |
| 3 | +# Check if PR number is provided |
| 4 | +if [ -z "$1" ]; then |
| 5 | + echo "Usage: gh react <pr_number>" |
| 6 | + exit 1 |
| 7 | +fi |
4 | 8 |
|
5 | | -# Usage: |
6 | | -# gh react pr 123 |
7 | | - |
8 | | -# Get PR number |
9 | 9 | PR_NUMBER=$1 |
10 | | - |
11 | | -# Fetch comments |
12 | 10 | echo "Fetching comments for PR #$PR_NUMBER..." |
13 | | -gh pr view "$PR_NUMBER" --comments |
| 11 | + |
| 12 | +OWNER=$(gh repo view --json owner -q ".owner.login") |
| 13 | +REPO=$(gh repo view --json name -q ".name") |
| 14 | + |
| 15 | +# Check if PR exists first |
| 16 | +echo "Checking if PR #$PR_NUMBER exists..." |
| 17 | +PR_EXISTS=$(gh pr view $PR_NUMBER --json number 2>/dev/null) |
| 18 | +if [ -z "$PR_EXISTS" ]; then |
| 19 | + echo "Error: PR #$PR_NUMBER does not exist or you don't have access to it." |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Use gh api to get raw comments for the issue/PR |
| 24 | +echo "Fetching comments..." |
| 25 | + |
| 26 | +# Get the PR description/body itself |
| 27 | +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 "") |
| 28 | + |
| 29 | +# Get issue comments (general PR comments) |
| 30 | +ISSUE_COMMENTS=$(gh api repos/$OWNER/$REPO/issues/$PR_NUMBER/comments --jq '.[] | "ISSUE|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "") |
| 31 | + |
| 32 | +# Get review comments (inline code review comments) |
| 33 | +REVIEW_COMMENTS=$(gh api repos/$OWNER/$REPO/pulls/$PR_NUMBER/comments --jq '.[] | "REVIEW|\(.id)|\(.user.login): \(.body)"' 2>/dev/null || echo "") |
| 34 | + |
| 35 | +# Get review summary comments (comments submitted with reviews) |
| 36 | +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 "") |
| 37 | + |
| 38 | +# Combine all comments |
| 39 | +ALL_COMMENTS="" |
| 40 | +if [ ! -z "$PR_BODY" ]; then |
| 41 | + ALL_COMMENTS="$ALL_COMMENTS$PR_BODY" |
| 42 | +fi |
| 43 | +if [ ! -z "$ISSUE_COMMENTS" ]; then |
| 44 | + if [ ! -z "$ALL_COMMENTS" ]; then |
| 45 | + ALL_COMMENTS="$ALL_COMMENTS |
| 46 | +" |
| 47 | + fi |
| 48 | + ALL_COMMENTS="$ALL_COMMENTS$ISSUE_COMMENTS" |
| 49 | +fi |
| 50 | +if [ ! -z "$REVIEW_COMMENTS" ]; then |
| 51 | + if [ ! -z "$ALL_COMMENTS" ]; then |
| 52 | + ALL_COMMENTS="$ALL_COMMENTS |
| 53 | +" |
| 54 | + fi |
| 55 | + ALL_COMMENTS="$ALL_COMMENTS$REVIEW_COMMENTS" |
| 56 | +fi |
| 57 | +if [ ! -z "$REVIEW_SUMMARY_COMMENTS" ]; then |
| 58 | + if [ ! -z "$ALL_COMMENTS" ]; then |
| 59 | + ALL_COMMENTS="$ALL_COMMENTS |
| 60 | +" |
| 61 | + fi |
| 62 | + ALL_COMMENTS="$ALL_COMMENTS$REVIEW_SUMMARY_COMMENTS" |
| 63 | +fi |
| 64 | + |
| 65 | +if [ -z "$ALL_COMMENTS" ]; then |
| 66 | + echo "No comments found for PR #$PR_NUMBER" |
| 67 | + exit 0 |
| 68 | +fi |
| 69 | + |
| 70 | +echo "$ALL_COMMENTS" |
14 | 71 |
|
15 | 72 | echo |
16 | 73 | read -p "Enter comment ID to react to: " COMMENT_ID |
17 | 74 | echo "Pick a reaction: (+1, -1, laugh, heart, hooray, rocket, eyes)" |
18 | 75 | read -p "Reaction: " REACTION |
19 | 76 |
|
20 | | -OWNER=$(gh repo view --json owner -q ".owner.login") |
21 | | -REPO=$(gh repo view --json name -q ".name") |
| 77 | +# Determine if it's an issue comment or review comment |
| 78 | +COMMENT_TYPE=$(echo "$ALL_COMMENTS" | grep "|$COMMENT_ID|" | cut -d'|' -f1) |
22 | 79 |
|
23 | 80 | echo "Sending reaction..." |
24 | | -gh api --method POST /repos/$OWNER/$REPO/issues/comments/$COMMENT_ID/reactions \ |
25 | | - -f "content=$REACTION" \ |
26 | | - -H "Accept: application/vnd.github+json" |
| 81 | +if [ "$COMMENT_TYPE" = "PR_BODY" ]; then |
| 82 | + # React to PR description/body |
| 83 | + gh api --method POST repos/$OWNER/$REPO/issues/$PR_NUMBER/reactions \ |
| 84 | + -f "content=$REACTION" \ |
| 85 | + -H "Accept: application/vnd.github+json" |
| 86 | +elif [ "$COMMENT_TYPE" = "ISSUE" ]; then |
| 87 | + # React to issue comment |
| 88 | + gh api --method POST repos/$OWNER/$REPO/issues/comments/$COMMENT_ID/reactions \ |
| 89 | + -f "content=$REACTION" \ |
| 90 | + -H "Accept: application/vnd.github+json" |
| 91 | +elif [ "$COMMENT_TYPE" = "REVIEW" ]; then |
| 92 | + # React to review comment (inline code comment) |
| 93 | + gh api --method POST repos/$OWNER/$REPO/pulls/comments/$COMMENT_ID/reactions \ |
| 94 | + -f "content=$REACTION" \ |
| 95 | + -H "Accept: application/vnd.github+json" |
| 96 | +elif [ "$COMMENT_TYPE" = "REVIEW_SUMMARY" ]; then |
| 97 | + # React to review summary comment |
| 98 | + gh api --method POST repos/$OWNER/$REPO/pulls/reviews/$COMMENT_ID/reactions \ |
| 99 | + -f "content=$REACTION" \ |
| 100 | + -H "Accept: application/vnd.github+json" |
| 101 | +else |
| 102 | + echo "Error: Could not determine comment type for ID $COMMENT_ID" |
| 103 | + exit 1 |
| 104 | +fi |
27 | 105 |
|
28 | 106 | echo "Done! 👍" |
0 commit comments