|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
1 | 4 | # This gets all of the comments for a PR, helpful when writing with an agent. |
2 | 5 | # Expects your environment to have GITHUB_TOKEN with a PAT that can access the API. |
| 6 | + |
| 7 | +if [ -z "${GITHUB_TOKEN:-}" ]; then |
| 8 | + echo "Error: GITHUB_TOKEN environment variable is not set." >&2 |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +if ! command -v jq >/dev/null 2>&1; then |
| 13 | + echo "Error: jq is required but not installed." >&2 |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
3 | 17 | PROJECT="rancher/terraform-rancher2-aws" |
4 | | -PULL_ID=$1 |
| 18 | +PULL_ID=${1:-} |
| 19 | + |
| 20 | +if [ -z "$PULL_ID" ]; then |
| 21 | + echo "Error: PR ID argument is required." >&2 |
| 22 | + echo "Usage: $0 <pr-id>" >&2 |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +OWNER=$(echo "$PROJECT" | cut -d/ -f1) |
| 27 | +REPO=$(echo "$PROJECT" | cut -d/ -f2) |
| 28 | + |
| 29 | +# The GitHub REST API doesn't expose resolution status. We switch to GraphQL to filter by 'isResolved'. |
| 30 | +JSON_PAYLOAD=$(jq -n \ |
| 31 | + --arg q 'query($owner: String!, $name: String!, $pr: Int!) { repository(owner: $owner, name: $name) { pullRequest(number: $pr) { reviewThreads(first: 100) { nodes { isResolved comments(first: 50) { nodes { path line diffHunk body } } } } } } }' \ |
| 32 | + --arg owner "$OWNER" \ |
| 33 | + --arg name "$REPO" \ |
| 34 | + --argjson pr "${PULL_ID:-0}" \ |
| 35 | + '{ query: $q, variables: { owner: $owner, name: $name, pr: $pr } }') |
| 36 | + |
5 | 37 | curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \ |
6 | | - -H "Accept: application/vnd.github+json" \ |
7 | | - "https://api.github.com/repos/$PROJECT/pulls/${PULL_ID}/comments" | \ |
8 | | - jq -r '.[] | "File: \(.path)\nLine: \(.line)\nDiff:\n\(.diff_hunk)\n\nComment:\n\(.body)\n\n========================================\n"' |
| 38 | + -X POST -d "$JSON_PAYLOAD" \ |
| 39 | + "https://api.github.com/graphql" | \ |
| 40 | + jq -r '.data.repository.pullRequest.reviewThreads.nodes[]? | select(.isResolved == false) | .comments.nodes[]? | "File: \(.path)\nLine: \(.line)\nDiff:\n\(.diffHunk)\n\nComment:\n\(.body)\n\n========================================\n"' |
0 commit comments