Skip to content

Commit 4abf699

Browse files
committed
Use jq more extensively for output massaging
1 parent e8d4de3 commit 4abf699

File tree

1 file changed

+47
-75
lines changed

1 file changed

+47
-75
lines changed

gh-react

Lines changed: 47 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -126,58 +126,29 @@ print_status "PR #$PR_NUMBER found!"
126126
echo ""
127127
print_info "Gathering all comments and content..."
128128

129-
# Get the PR description/body itself
130-
PR_BODY=$(ghGet pulls/"$PR_NUMBER" '
131-
select(.body != null and .body != "")
132-
| "PR_BODY|\(.number)|\(.user.login): \(.body)"')
133-
134-
# Get issue comments (general PR comments)
135-
ISSUE_COMMENTS=$(ghGet issues/"$PR_NUMBER"/comments '
136-
.[] | "ISSUE|\(.id)|\(.user.login): \(.body)"')
137-
138-
# Get review comments (inline code review comments)
139-
REVIEW_COMMENTS=$(ghGet pulls/"$PR_NUMBER"/comments '
140-
.[] | "REVIEW|\(.id)|\(.user.login): \(.body)")
129+
commentsFile="$(mktemp)"
130+
trap 'rm -- "$commentsFile"' EXIT
141131

142-
# Get review summary comments (comments submitted with reviews)
143-
REVIEW_SUMMARY_COMMENTS=$(ghGet pulls/"$PR_NUMBER"/reviews '
132+
>"$commentsFile" jq -s . \
133+
<( # Get the PR description/body itself
134+
ghGet pulls/"$PR_NUMBER" '
135+
select(.body != null and .body != "")
136+
| {type: "PR_BODY", id: .number, author: .user.login, body}') \
137+
<( # Get issue comments (general PR comments)
138+
ghGet issues/"$PR_NUMBER"/comments '
139+
.[]
140+
| {type: "ISSUE", id, author: .user.login, body}') \
141+
<( # Get review comments (inline code review comments)
142+
ghGet pulls/"$PR_NUMBER"/comments '
143+
.[] | {type: "REVIEW", id, author: .user.login, body}') \
144+
<( # Get review summary comments (comments submitted with reviews)
145+
ghGet pulls/"$PR_NUMBER"/reviews '
144146
.[] | select(.body != null and .body != "")
145-
| "REVIEW_SUMMARY|\(.id)|\(.user.login): \(.body)"')
146-
147-
# Combine all comments
148-
ALL_COMMENTS=""
149-
COMMENT_COUNT=0
147+
| {type: "REVIEW_SUMMARY", id, author: .user.login, body}')
150148

151-
if [ ! -z "$PR_BODY" ]; then
152-
ALL_COMMENTS="$ALL_COMMENTS$PR_BODY"
153-
COMMENT_COUNT=$((COMMENT_COUNT + 1))
154-
fi
155-
if [ ! -z "$ISSUE_COMMENTS" ]; then
156-
if [ ! -z "$ALL_COMMENTS" ]; then
157-
ALL_COMMENTS="$ALL_COMMENTS
158-
"
159-
fi
160-
ALL_COMMENTS="$ALL_COMMENTS$ISSUE_COMMENTS"
161-
COMMENT_COUNT=$((COMMENT_COUNT + $(echo "$ISSUE_COMMENTS" | wc -l)))
162-
fi
163-
if [ ! -z "$REVIEW_COMMENTS" ]; then
164-
if [ ! -z "$ALL_COMMENTS" ]; then
165-
ALL_COMMENTS="$ALL_COMMENTS
166-
"
167-
fi
168-
ALL_COMMENTS="$ALL_COMMENTS$REVIEW_COMMENTS"
169-
COMMENT_COUNT=$((COMMENT_COUNT + $(echo "$REVIEW_COMMENTS" | wc -l)))
170-
fi
171-
if [ ! -z "$REVIEW_SUMMARY_COMMENTS" ]; then
172-
if [ ! -z "$ALL_COMMENTS" ]; then
173-
ALL_COMMENTS="$ALL_COMMENTS
174-
"
175-
fi
176-
ALL_COMMENTS="$ALL_COMMENTS$REVIEW_SUMMARY_COMMENTS"
177-
COMMENT_COUNT=$((COMMENT_COUNT + $(echo "$REVIEW_SUMMARY_COMMENTS" | wc -l)))
178-
fi
149+
COMMENT_COUNT="$(jq length "$commentsFile")"
179150

180-
if [ -z "$ALL_COMMENTS" ]; then
151+
if [ "$COMMENT_COUNT" -eq 0 ]; then
181152
die "${err[NOCOMMS]}" "No comments found for PR #$PR_NUMBER" <<-EOF
182153
183154
$(tip) This PR doesn't have any comments yet. Try adding a comment first!
@@ -189,29 +160,32 @@ echo ""
189160
print_info "$(hl 📋 Available content:)"
190161
echo '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'
191162

192-
COUNTER=1
193-
194163
# Format and display comments with numbers
195-
echo "$ALL_COMMENTS" | while IFS='|' read -r type _ author_and_content; do
196-
author=$(echo "$author_and_content" | cut -d':' -f1)
197-
content=$(echo "$author_and_content" | cut -d':' -f2- | head -c 80)
198-
199-
case $type in
200-
PR_BODY) label="${PURPLE}📄 PR Description${NC}" ;;
201-
ISSUE) label="${GREEN}💬 Comment${NC}" ;;
202-
REVIEW) label="${YELLOW}🔍 Code Review${NC}" ;;
203-
REVIEW_SUMMARY) label="${BLUE}📝 Review Summary${NC}" ;;
204-
esac
205-
printf "$(hl [$COUNTER]) $label by $(user $author)"
206-
printf " $(hl └─) $(echo "$content" | tr '\n' ' ')..."
207-
echo ""
208-
COUNTER=$((COUNTER + 1))
209-
done
210-
211-
# Store the comment data in temporary file for later retrieval
212-
commentsFile="$(mktemp)"
213-
trap 'rm -- "$commentsFile"' EXIT
214-
echo "$ALL_COMMENTS" > "$commentsFile"
164+
<"$commentsFile" jq \
165+
--arg nc "$NC" \
166+
--argjson color \
167+
"$(jq --null-input '$ARGS.named' \
168+
--arg PR_BODY "$PURPLE" \
169+
--arg ISSUE "$GREEN" \
170+
--arg REVIEW "$YELLOW" \
171+
--arg REVIEW_SUMMARY "$BLUE" \
172+
--arg USER "$CYAN"
173+
)" \
174+
-r '
175+
def renderType: .type
176+
| "\($color[.])" + {
177+
PR_BODY: "📄 PR Description",
178+
ISSUE: "💬 Comment",
179+
REVIEW: "🔍 Code Review",
180+
REVIEW_SUMMARY: "📝 Review Summary",
181+
}[.] + $nc;
182+
def renderUser: .author
183+
| "\($color["USER"])\(.)\($nc)";
184+
def renderBody: .body | gsub("\\s"; " ")
185+
| "\t└─\(.[:80])\(if length > 80 then "..." else "" end)";
186+
to_entries[] | {ix: .key + 1} + .value
187+
| "[\(.ix)] \(renderType) by \(renderUser)\n\(renderBody)"
188+
'
215189

216190
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
217191
echo ""
@@ -226,11 +200,9 @@ while read -rp "� Enter number (1-$COMMENT_COUNT): " SELECTION; do
226200
print_error "Invalid selection: $SELECTION"
227201
done
228202

229-
# Get the selected comment data
230-
SELECTED_LINE=$(sed -n "${SELECTION}p" "$commentsFile")
231-
COMMENT_TYPE=$(echo "$SELECTED_LINE" | cut -d'|' -f1)
232-
COMMENT_ID=$(echo "$SELECTED_LINE" | cut -d'|' -f2)
233-
SELECTED_AUTHOR=$(echo "$SELECTED_LINE" | cut -d'|' -f3 | cut -d':' -f1)
203+
COMMENT_TYPE=$(jq -r ".[$SELECTION-1].type" "$commentsFile")
204+
COMMENT_ID=$(jq ".[$SELECTION-1].id" "$commentsFile")
205+
SELECTED_AUTHOR=$(jq -r ".[$SELECTION-1].author" "$commentsFile")
234206

235207
echo ""
236208
print_info "Selected: $(hl "$COMMENT_TYPE") by $(user "$SELECTED_AUTHOR")"

0 commit comments

Comments
 (0)