| #!/usr/bin/env bash |
| set -euo pipefail |
|
|
| |
| PRS_NEEDING_COMMENT="" |
|
|
| |
| process_pr() { |
| if [[ -z "${GITHUB_REPOSITORY:-}" ]]; then |
| echo "โผ๏ธ Missing \$GITHUB_REPOSITORY - this must be run from GitHub Actions" |
| return 1 |
| fi |
|
|
| if [[ -z "${GITHUB_OUTPUT:-}" ]]; then |
| echo "โผ๏ธ Missing \$GITHUB_OUTPUT - this must be run from GitHub Actions" |
| return 1 |
| fi |
|
|
| local PR_NUMBER=$1 |
| echo "๐ Processing PR #${PR_NUMBER}" |
|
|
| |
| local PR_BODY |
| if ! PR_BODY=$(gh pr view "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --json body -q .body 2>/dev/null); then |
| echo " โ ๏ธ Could not fetch PR #${PR_NUMBER} details" |
| return 1 |
| fi |
|
|
| |
| local ISSUE_NUMBER="" |
|
|
| |
| if [[ -z "${ISSUE_NUMBER}" ]]; then |
| ISSUE_NUMBER=$(echo "${PR_BODY}" | grep -oE '#[0-9]+' | head -1 | sed 's/#//' 2>/dev/null || echo "") |
| fi |
|
|
| |
| if [[ -z "${ISSUE_NUMBER}" ]]; then |
| ISSUE_NUMBER=$(echo "${PR_BODY}" | grep -iE '(closes?|fixes?|resolves?) #[0-9]+' | grep -oE '#[0-9]+' | head -1 | sed 's/#//' 2>/dev/null || echo "") |
| fi |
|
|
| if [[ -z "${ISSUE_NUMBER}" ]]; then |
| echo "โ ๏ธ No linked issue found for PR #${PR_NUMBER}, adding status/need-issue label" |
| if ! gh pr edit "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --add-label "status/need-issue" 2>/dev/null; then |
| echo " โ ๏ธ Failed to add label (may already exist or have permission issues)" |
| fi |
| |
| if [[ -z "${PRS_NEEDING_COMMENT}" ]]; then |
| PRS_NEEDING_COMMENT="${PR_NUMBER}" |
| else |
| PRS_NEEDING_COMMENT="${PRS_NEEDING_COMMENT},${PR_NUMBER}" |
| fi |
| echo "needs_comment=true" >> "${GITHUB_OUTPUT}" |
| else |
| echo "๐ Found linked issue #${ISSUE_NUMBER}" |
|
|
| |
| if ! gh pr edit "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --remove-label "status/need-issue" 2>/dev/null; then |
| echo " status/need-issue label not present or could not be removed" |
| fi |
|
|
| |
| echo "๐ฅ Fetching labels from issue #${ISSUE_NUMBER}" |
| local ISSUE_LABELS="" |
| if ! ISSUE_LABELS=$(gh issue view "${ISSUE_NUMBER}" --repo "${GITHUB_REPOSITORY}" --json labels -q '.labels[].name' 2>/dev/null | tr '\n' ',' | sed 's/,$//' || echo ""); then |
| echo " โ ๏ธ Could not fetch issue #${ISSUE_NUMBER} (may not exist or be in different repo)" |
| ISSUE_LABELS="" |
| fi |
|
|
| |
| echo "๐ฅ Fetching labels from PR #${PR_NUMBER}" |
| local PR_LABELS="" |
| if ! PR_LABELS=$(gh pr view "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --json labels -q '.labels[].name' 2>/dev/null | tr '\n' ',' | sed 's/,$//' || echo ""); then |
| echo " โ ๏ธ Could not fetch PR labels" |
| PR_LABELS="" |
| fi |
|
|
| echo " Issue labels: ${ISSUE_LABELS}" |
| echo " PR labels: ${PR_LABELS}" |
|
|
| |
| local ISSUE_LABEL_ARRAY PR_LABEL_ARRAY |
| IFS=',' read -ra ISSUE_LABEL_ARRAY <<< "${ISSUE_LABELS}" |
| IFS=',' read -ra PR_LABEL_ARRAY <<< "${PR_LABELS}" |
|
|
| |
| local LABELS_TO_ADD="" |
| for label in "${ISSUE_LABEL_ARRAY[@]}"; do |
| if [[ -n "${label}" ]] && [[ " ${PR_LABEL_ARRAY[*]} " != *" ${label} "* ]]; then |
| if [[ -z "${LABELS_TO_ADD}" ]]; then |
| LABELS_TO_ADD="${label}" |
| else |
| LABELS_TO_ADD="${LABELS_TO_ADD},${label}" |
| fi |
| fi |
| done |
|
|
| |
| local LABELS_TO_REMOVE="" |
| for label in "${PR_LABEL_ARRAY[@]}"; do |
| if [[ -n "${label}" ]] && [[ " ${ISSUE_LABEL_ARRAY[*]} " != *" ${label} "* ]]; then |
| |
| if [[ "${label}" != "status/need-issue" ]]; then |
| if [[ -z "${LABELS_TO_REMOVE}" ]]; then |
| LABELS_TO_REMOVE="${label}" |
| else |
| LABELS_TO_REMOVE="${LABELS_TO_REMOVE},${label}" |
| fi |
| fi |
| fi |
| done |
|
|
| |
| if [[ -n "${LABELS_TO_ADD}" ]]; then |
| echo "โ Adding labels: ${LABELS_TO_ADD}" |
| if ! gh pr edit "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --add-label "${LABELS_TO_ADD}" 2>/dev/null; then |
| echo " โ ๏ธ Failed to add some labels" |
| fi |
| fi |
|
|
| if [[ -n "${LABELS_TO_REMOVE}" ]]; then |
| echo "โ Removing labels: ${LABELS_TO_REMOVE}" |
| if ! gh pr edit "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" --remove-label "${LABELS_TO_REMOVE}" 2>/dev/null; then |
| echo " โ ๏ธ Failed to remove some labels" |
| fi |
| fi |
|
|
| if [[ -z "${LABELS_TO_ADD}" ]] && [[ -z "${LABELS_TO_REMOVE}" ]]; then |
| echo "โ
Labels already synchronized" |
| fi |
| echo "needs_comment=false" >> "${GITHUB_OUTPUT}" |
| fi |
| } |
|
|
| |
| if [[ -n "${PR_NUMBER:-}" ]]; then |
| if ! process_pr "${PR_NUMBER}"; then |
| echo "โ Failed to process PR #${PR_NUMBER}" |
| exit 1 |
| fi |
| else |
| |
| |
| echo "๐ฅ Getting all open pull requests..." |
| if ! PR_NUMBERS=$(gh pr list --repo "${GITHUB_REPOSITORY}" --state open --limit 1000 --json number -q '.[].number' 2>/dev/null); then |
| echo "โ Failed to fetch PR list" |
| exit 1 |
| fi |
|
|
| if [[ -z "${PR_NUMBERS}" ]]; then |
| echo "โ
No open PRs found" |
| else |
| |
| PR_COUNT=$(echo "${PR_NUMBERS}" | wc -w | tr -d ' ') |
| echo "๐ Found ${PR_COUNT} open PRs to process" |
|
|
| for pr_number in ${PR_NUMBERS}; do |
| if ! process_pr "${pr_number}"; then |
| echo "โ ๏ธ Failed to process PR #${pr_number}, continuing with next PR..." |
| continue |
| fi |
| done |
| fi |
| fi |
|
|
| |
| if [[ -z "${PRS_NEEDING_COMMENT}" ]]; then |
| echo "prs_needing_comment=[]" >> "${GITHUB_OUTPUT}" |
| else |
| echo "prs_needing_comment=[${PRS_NEEDING_COMMENT}]" >> "${GITHUB_OUTPUT}" |
| fi |
|
|
| echo "โ
PR triage completed" |
|
|