-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget-pr-comments.sh
More file actions
executable file
·40 lines (32 loc) · 1.53 KB
/
get-pr-comments.sh
File metadata and controls
executable file
·40 lines (32 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env bash
set -euo pipefail
# This gets all of the comments for a PR, helpful when writing with an agent.
# Expects your environment to have GITHUB_TOKEN with a PAT that can access the API.
if [ -z "${GITHUB_TOKEN:-}" ]; then
echo "Error: GITHUB_TOKEN environment variable is not set." >&2
exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
echo "Error: jq is required but not installed." >&2
exit 1
fi
PROJECT="rancher/terraform-rancher2-aws"
PULL_ID=${1:-}
if [ -z "$PULL_ID" ]; then
echo "Error: PR ID argument is required." >&2
echo "Usage: $0 <pr-id>" >&2
exit 1
fi
OWNER=$(echo "$PROJECT" | cut -d/ -f1)
REPO=$(echo "$PROJECT" | cut -d/ -f2)
# The GitHub REST API doesn't expose resolution status. We switch to GraphQL to filter by 'isResolved'.
JSON_PAYLOAD=$(jq -n \
--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 } } } } } } }' \
--arg owner "$OWNER" \
--arg name "$REPO" \
--argjson pr "${PULL_ID:-0}" \
'{ query: $q, variables: { owner: $owner, name: $name, pr: $pr } }')
curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-X POST -d "$JSON_PAYLOAD" \
"https://api.github.com/graphql" | \
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"'