|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Close every open pull request that carries a given label, leaving an |
| 4 | +# explanatory comment. This is used to clear the backlog before Hacktoberfest. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# scripts/close_pull_requests_with_label.sh "<label>" ["<comment>"] |
| 8 | +# |
| 9 | +# Examples: |
| 10 | +# scripts/close_pull_requests_with_label.sh "require type hints" |
| 11 | +# DRY_RUN=1 scripts/close_pull_requests_with_label.sh "tests are failing" |
| 12 | +# |
| 13 | +# Environment variables: |
| 14 | +# DRY_RUN=1 Print the PRs that would be closed without closing them. |
| 15 | +# REPO Target repository (default: TheAlgorithms/Python). |
| 16 | +# SLEEP Seconds to wait between closes (default: 2) to avoid tripping |
| 17 | +# GitHub's secondary rate limits during bulk closes. |
| 18 | +# |
| 19 | +# On completion the script prints a machine-readable summary line: |
| 20 | +# CLOSED_COUNT=<n> CLOSED_PRS=<comma-separated PR numbers> |
| 21 | +# so the Hacktoberfest tracker can be updated from the output. |
| 22 | + |
| 23 | +set -euo pipefail |
| 24 | + |
| 25 | +label="${1:-}" |
| 26 | +if [[ -z "$label" ]]; then |
| 27 | + echo "error: missing label argument" >&2 |
| 28 | + echo "usage: $0 \"<label>\" [\"<comment>\"]" >&2 |
| 29 | + exit 2 |
| 30 | +fi |
| 31 | + |
| 32 | +repo="${REPO:-TheAlgorithms/Python}" |
| 33 | +sleep_seconds="${SLEEP:-2}" |
| 34 | +comment="${2:-Closing \"${label}\" PRs to prepare for Hacktoberfest}" |
| 35 | + |
| 36 | +# Filter by label server-side so we never miss PRs beyond an arbitrary --limit |
| 37 | +# cap (the repo can have ~900 open PRs). --limit is set high purely as a ceiling. |
| 38 | +prs=$(gh pr list --repo "$repo" --state open --label "$label" \ |
| 39 | + --json number,title --limit 1000) |
| 40 | + |
| 41 | +count=$(echo "$prs" | jq 'length') |
| 42 | +echo "Found $count open PR(s) with label \"$label\" in $repo" |
| 43 | + |
| 44 | +closed=() |
| 45 | +while read -r pr; do |
| 46 | + [[ -z "$pr" ]] && continue |
| 47 | + pr_number=$(echo "$pr" | jq -r '.number') |
| 48 | + pr_title=$(echo "$pr" | jq -r '.title') |
| 49 | + |
| 50 | + if [[ "${DRY_RUN:-0}" == "1" ]]; then |
| 51 | + echo "[dry-run] would close PR #$pr_number: $pr_title" |
| 52 | + closed+=("$pr_number") |
| 53 | + continue |
| 54 | + fi |
| 55 | + |
| 56 | + echo "Closing PR #$pr_number: $pr_title" |
| 57 | + gh pr close "$pr_number" --repo "$repo" --comment "$comment" |
| 58 | + closed+=("$pr_number") |
| 59 | + sleep "$sleep_seconds" |
| 60 | +done < <(echo "$prs" | jq -c '.[]') |
| 61 | + |
| 62 | +# Machine-readable summary for the tracker. |
| 63 | +IFS=,; echo "CLOSED_COUNT=${#closed[@]} CLOSED_PRS=${closed[*]:-}" |
0 commit comments