|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
4 | | -# Simple script to request a reviewer for a PR when invoked from GitHub Actions. |
5 | | -# Expects: GITHUB_EVENT_PATH and GITHUB_REPOSITORY to be set by Actions and |
6 | | -# a valid GITHUB_TOKEN in the environment. |
| 4 | +# Comment-based triage request script for PRs. |
| 5 | +# Aligns with issue #1721: post a comment to request triage reviewers |
| 6 | +# instead of assigning reviewers. Uses a triage list file or env var. |
| 7 | +# Safety: defaults to dry-run mode unless DRY_RUN is explicitly set to 'false'. |
7 | 8 |
|
8 | 9 | if [ -z "${GITHUB_EVENT_PATH:-}" ] || [ -z "${GITHUB_REPOSITORY:-}" ]; then |
9 | 10 | echo "This script is intended to run inside GitHub Actions (needs GITHUB_EVENT_PATH and GITHUB_REPOSITORY)." |
|
18 | 19 |
|
19 | 20 | OWNER=${GITHUB_REPOSITORY%%/*} |
20 | 21 | REPO=${GITHUB_REPOSITORY##*/} |
21 | | -REVIEWER="coderabbit" |
22 | 22 | TOKEN="${GITHUB_TOKEN:-}" |
23 | 23 |
|
24 | 24 | if [ -z "$TOKEN" ]; then |
25 | 25 | echo "GITHUB_TOKEN not set" |
26 | 26 | exit 1 |
27 | 27 | fi |
28 | 28 |
|
29 | | -echo "Checking existing requested reviewers for PR #$PR_NUMBER in $OWNER/$REPO" |
30 | | -existing=$(curl -sS -H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.github+json" \ |
31 | | - "https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER/requested_reviewers") |
32 | | - |
33 | | -if echo "$existing" | jq -e --arg r "$REVIEWER" '.users[]?.login == $r' >/dev/null 2>&1; then |
34 | | - echo "User $REVIEWER already requested for PR #$PR_NUMBER" |
35 | | - exit 0 |
| 29 | +# Dry-run guard: default to true to avoid accidental state changes. |
| 30 | +DRY_RUN=${DRY_RUN:-true} |
| 31 | +DRY_RUN_LC=$(echo "$DRY_RUN" | tr '[:upper:]' '[:lower:]') |
| 32 | +is_dry_run=true |
| 33 | +if [ "$DRY_RUN_LC" = "false" ] || [ "$DRY_RUN_LC" = "0" ] || [ "$DRY_RUN_LC" = "no" ]; then |
| 34 | + is_dry_run=false |
36 | 35 | fi |
37 | 36 |
|
38 | | -echo "Requesting reviewer $REVIEWER for PR #$PR_NUMBER" |
39 | | -resp=$(curl -sS -X POST -H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.github+json" \ |
40 | | - -d "{\"reviewers\":[\"$REVIEWER\"]}" \ |
41 | | - "https://api.github.com/repos/$OWNER/$REPO/pulls/$PR_NUMBER/requested_reviewers") |
| 37 | +# Determine triage reviewers: prefer file in repo, fallback to TRIAGE_REVIEWERS env var |
| 38 | +TRIAGE_FILE=".github/triage-reviewers.txt" |
| 39 | +if [ -f "$TRIAGE_FILE" ]; then |
| 40 | + TRIAGE_LIST=$(tr '\n' ' ' < "$TRIAGE_FILE" | xargs) |
| 41 | +else |
| 42 | + TRIAGE_LIST="${TRIAGE_REVIEWERS:-}" |
| 43 | +fi |
42 | 44 |
|
43 | | -if echo "$resp" | jq -e '.errors? // empty' >/dev/null 2>&1; then |
44 | | - echo "Request API returned an error: $resp" |
| 45 | +if [ -z "$TRIAGE_LIST" ]; then |
| 46 | + echo "No triage reviewers configured. Provide .github/triage-reviewers.txt or TRIAGE_REVIEWERS env var." |
45 | 47 | exit 1 |
46 | 48 | fi |
47 | 49 |
|
48 | | -echo "Requested $REVIEWER for PR #$PR_NUMBER successfully" |
| 50 | +# Prepare mentions: accept comma or space separated values |
| 51 | +mentions=$(echo "$TRIAGE_LIST" | sed -E 's/,/ /g') |
| 52 | + |
| 53 | +MARKER='<!-- triage-request -->' |
| 54 | +BODY_TEXT="${MARKER}\nRequesting triage review from: $mentions" |
| 55 | + |
| 56 | +echo "PR #$PR_NUMBER in $OWNER/$REPO — triage mentions: $mentions" |
| 57 | + |
| 58 | +# Idempotency: check existing comments for our marker |
| 59 | +comments=$(curl -fS -H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.github+json" \ |
| 60 | + "https://api.github.com/repos/$OWNER/$REPO/issues/$PR_NUMBER/comments") |
| 61 | + |
| 62 | +if echo "$comments" | jq -r '.[].body' | grep -Fq "$MARKER"; then |
| 63 | + echo "Triage comment already present; skipping." |
| 64 | + exit 0 |
| 65 | +fi |
| 66 | + |
| 67 | +if [ "$is_dry_run" = true ]; then |
| 68 | + echo "DRY RUN: would post comment to PR #$PR_NUMBER with body:" |
| 69 | + echo "---" |
| 70 | + echo -e "$BODY_TEXT" |
| 71 | + echo "---" |
| 72 | + exit 0 |
| 73 | +fi |
| 74 | + |
| 75 | +echo "Posting triage request comment to PR #$PR_NUMBER" |
| 76 | +resp=$(curl -fS -X POST -H "Authorization: Bearer $TOKEN" -H "Accept: application/vnd.github+json" -H "User-Agent: hiero-sdk-bot" \ |
| 77 | + -d "{\"body\": $(jq -n --arg b "$BODY_TEXT" '$b')}" \ |
| 78 | + "https://api.github.com/repos/$OWNER/$REPO/issues/$PR_NUMBER/comments") |
| 79 | + |
| 80 | +echo "Comment posted successfully" |
49 | 81 | exit 0 |
0 commit comments