#!/bin/bash
# Print the full body of one GitHub pull request comment, addressed by id.
#
# Usage:
#   get-pr-comment.sh <comment-id>
#   get-pr-comment.sh <comment-id> > /tmp/comment.md
#
# Motivation: edit-pr-comment.sh replaces a comment body whole, so a caller must
# hold the current body before editing, or the edit destroys whatever it did not
# retype. list-pr-comments.sh prints a truncated one-line preview, and raw gh is
# denied by the session's permission rules, so without this script the only
# comment you can safely edit is one you wrote yourself in the same session.
#
# That makes this the first step of the supported three-step correction:
#
#   get-pr-comment.sh <comment-id> > /tmp/comment.md   # get
#   $EDITOR /tmp/comment.md                            # edit the file
#   edit-pr-comment.sh <comment-id> --body-file /tmp/comment.md   # put
#
# Edit the file, never a retyped copy. Retyping a body from a rendered page is
# how a figure gets changed by accident, which is the failure this Skill's
# read-back verification exists to catch.
#
# The body is printed raw, with nothing added around it, so the redirect above
# produces a file that can go straight back to --body-file.
#
# Runs: gh api repos/{owner}/{repo}/issues/comments/<comment-id> --jq .body
# gh expands {owner} and {repo} from the git remote of the current repository,
# so neither is hardcoded. Get an id from list-pr-comments.sh <pr-number>.

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib.sh
source "$SCRIPT_DIR/lib.sh"

COMMENT_ID="$1"

if [ -z "$COMMENT_ID" ]; then
    echo "Error: missing comment id" >&2
    echo "Get it from: list-pr-comments.sh <pr-number>" >&2
    echo "Usage: $0 <comment-id>" >&2
    exit 1
fi

case "$COMMENT_ID" in
    *[!0-9]*)
        echo "Error: the first argument must be a numeric comment id, got: $COMMENT_ID" >&2
        echo "A comment id is not a PR number; get it from list-pr-comments.sh <pr-number>." >&2
        echo "Usage: $0 <comment-id>" >&2
        exit 1
        ;;
esac

if [ "$#" -gt 1 ]; then
    shift
    echo "Error: unexpected extra argument(s): $*" >&2
    echo "Usage: $0 <comment-id>" >&2
    exit 1
fi

if ! command -v gh &> /dev/null; then
    echo "Error: GitHub CLI (gh) is not installed" >&2
    echo "Install: https://cli.github.com/" >&2
    exit 1
fi

require_repo_context

# The body is captured rather than streamed so that a failed read prints nothing
# on stdout. gh writes the 404 response body to stdout, and a redirect that
# caught it would leave an error document in the file the caller is about to
# feed back to --body-file. Trailing newlines are dropped by the command
# substitution and one is restored on output; the read-back verification strips
# trailing newlines from both sides, so a round trip still compares equal.
# gh's stderr is captured so the failure can be attributed to the right cause.
# Blaming the comment id for every failure sends the reader hunting for a wrong
# id when the id was fine and something else broke, which is what this script
# did for anything that was not a 404.
GH_STDERR="$(mktemp)"
trap 'rm -f "$GH_STDERR"' EXIT

if ! BODY="$(gh api "repos/{owner}/{repo}/issues/comments/$COMMENT_ID" --jq '.body' 2>"$GH_STDERR")"; then
    cat "$GH_STDERR" >&2
    echo "Error: could not read comment $COMMENT_ID." >&2
    case "$(cat "$GH_STDERR")" in
        *"HTTP 404"*|*"Not Found"*)
            echo "No comment with that id exists in this repository. An id from another" >&2
            echo "repository fails the same way." >&2
            echo "Check the id with: list-pr-comments.sh <pr-number>" >&2
            ;;
        *)
            echo "The cause is the failure gh reported above, not the comment id." >&2
            ;;
    esac
    exit 1
fi

printf '%s\n' "$BODY"
