|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 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. |
| 7 | + |
| 8 | +if [ -z "${GITHUB_EVENT_PATH:-}" ] || [ -z "${GITHUB_REPOSITORY:-}" ]; then |
| 9 | + echo "This script is intended to run inside GitHub Actions (needs GITHUB_EVENT_PATH and GITHUB_REPOSITORY)." |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +PR_NUMBER=$(jq -r .pull_request.number < "$GITHUB_EVENT_PATH") |
| 14 | +if [ "$PR_NUMBER" = "null" ] || [ -z "$PR_NUMBER" ]; then |
| 15 | + echo "No pull_request.number found in event payload" |
| 16 | + exit 0 |
| 17 | +fi |
| 18 | + |
| 19 | +OWNER=${GITHUB_REPOSITORY%%/*} |
| 20 | +REPO=${GITHUB_REPOSITORY##*/} |
| 21 | +REVIEWER="coderabbit" |
| 22 | +TOKEN="${GITHUB_TOKEN:-}" |
| 23 | + |
| 24 | +if [ -z "$TOKEN" ]; then |
| 25 | + echo "GITHUB_TOKEN not set" |
| 26 | + exit 1 |
| 27 | +fi |
| 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 |
| 36 | +fi |
| 37 | + |
| 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") |
| 42 | + |
| 43 | +if echo "$resp" | jq -e '.errors? // empty' >/dev/null 2>&1; then |
| 44 | + echo "Request API returned an error: $resp" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +echo "Requested $REVIEWER for PR #$PR_NUMBER successfully" |
| 49 | +exit 0 |
0 commit comments