#!/bin/bash set +o histexpand # Query GitHub labels with perPage pagination support. # # Usage: ./query-labels.sh [OPTIONS] # # Options: # --owner OWNER Repository owner (required) # --repo REPO Repository name (required) # --per-page N Results per page: 1-100 (default: 10) # --page N Page number (default: 1) # --name-filter STR Case-insensitive substring filter on the label name # # Alternatively, inputs can be provided as environment variables using the # mcp-scripts INPUT_* convention (INPUT_OWNER, INPUT_REPO, INPUT_PERPAGE, # INPUT_PAGE, INPUT_NAMEFILTER). INPUT_PER_PAGE is also accepted for backward compatibility. # CLI arguments take precedence over environment variables. # # Calls the GitHub REST API: # GET /repos/{owner}/{repo}/labels?per_page={n}&page={n} # # Returns JSON: # { "labels": [...], "item_count": N, "per_page": N, "page": N } set -e # Defaults: pick up INPUT_* env vars (mcp-scripts convention) or fall back to # hardcoded defaults; CLI flags below will override. # INPUT_PERPAGE (camelCase perPage) is preferred; INPUT_PER_PAGE accepted for # backward compatibility with callers using the old snake_case convention. OWNER="${INPUT_OWNER:-}" REPO="${INPUT_REPO:-}" PER_PAGE="${INPUT_PERPAGE:-${INPUT_PER_PAGE:-10}}" PAGE="${INPUT_PAGE:-1}" NAME_FILTER="${INPUT_NAMEFILTER:-}" while [[ $# -gt 0 ]]; do case $1 in --owner) OWNER="$2" shift 2 ;; --repo) REPO="$2" shift 2 ;; --per-page) PER_PAGE="$2" shift 2 ;; --page) PAGE="$2" shift 2 ;; --name-filter) NAME_FILTER="$2" shift 2 ;; *) echo "Unknown option: $1" >&2 exit 1 ;; esac done if [[ -z "$OWNER" ]]; then echo '{"error": "owner is required"}' >&2 exit 1 fi if [[ -z "$REPO" ]]; then echo '{"error": "repo is required"}' >&2 exit 1 fi if ! [[ "$PER_PAGE" =~ ^[0-9]+$ ]] || [[ "$PER_PAGE" -lt 1 ]] || [[ "$PER_PAGE" -gt 100 ]]; then echo '{"error": "per_page must be between 1 and 100"}' >&2 exit 1 fi if [[ -n "$NAME_FILTER" ]]; then RESPONSE=$(gh api --paginate --slurp "repos/${OWNER}/${REPO}/labels?per_page=100" | jq '[.[][]]') else RESPONSE=$(gh api "repos/${OWNER}/${REPO}/labels?per_page=${PER_PAGE}&page=${PAGE}") fi echo "$RESPONSE" | jq \ --argjson per_page "$PER_PAGE" \ --argjson page "$PAGE" \ --arg name_filter "$NAME_FILTER" \ '(if $name_filter == "" then . else [.[] | select(.name | ascii_downcase | contains($name_filter | ascii_downcase))] | .[(($page - 1) * $per_page):($page * $per_page)] end) as $selected | { labels: [$selected[] | {id, node_id, url, name, color, default, description}], item_count: ($selected | length), per_page: $per_page, page: $page }'