Setting the file. One moment.
Query Labels · GitHub Labels Query · github/gh-aw · Skills Docs
ContentsBack to the top of the page
Raw file
query-labels.sh
Shell · 101 lines · 3 KB
16 # mcp-scripts INPUT_* convention (INPUT_OWNER, INPUT_REPO, INPUT_PERPAGE,
17 # INPUT_PAGE, INPUT_NAMEFILTER). INPUT_PER_PAGE is also accepted for backward compatibility.
18 # CLI arguments take precedence over environment variables.
19 #
20 # Calls the GitHub REST API:
21 # GET /repos/{owner}/{repo}/labels?per_page={n}&page={n}
22 #
23 # Returns JSON:
24 # { "labels": [...], "item_count": N, "per_page": N, "page": N }
25
26 set -e
27
28 # Defaults: pick up INPUT_* env vars (mcp-scripts convention) or fall back to
29 # hardcoded defaults; CLI flags below will override.
30 # INPUT_PERPAGE (camelCase perPage) is preferred; INPUT_PER_PAGE accepted for
31 # backward compatibility with callers using the old snake_case convention.
32 OWNER = "${ INPUT_OWNER :- }"
33 REPO = "${ INPUT_REPO :- }"
34 PER_PAGE = "${ INPUT_PERPAGE :- ${ INPUT_PER_PAGE :- 10 }}"
35 PAGE = "${ INPUT_PAGE :- 1 }"
36 NAME_FILTER = "${ INPUT_NAMEFILTER :- }"
37
38 while [[ $# -gt 0 ]]; do
39 case $1 in
40 --owner )
41 OWNER = " $2 "
42 shift 2
43 ;;
44 --repo )
45 REPO = " $2 "
46 shift 2
47 ;;
48 --per-page )
49 PER_PAGE = " $2 "
50 shift 2
51 ;;
52 --page )
53 PAGE = " $2 "
54 shift 2
55 ;;
56 --name-filter )
57 NAME_FILTER = " $2 "
58 shift 2
59 ;;
60 *)
61 echo "Unknown option: $1 " >&2
62 exit 1
63 ;;
64 esac
65 done
66
67 if [[ -z " $OWNER " ]]; then
68 echo '{"error": "owner is required"}' >&2
69 exit 1
70 fi
71
72 if [[ -z " $REPO " ]]; then
73 echo '{"error": "repo is required"}' >&2
74 exit 1
75 fi
76
77 if ! [[ " $PER_PAGE " =~ ^[0-9]+$ ]] || [[ " $PER_PAGE " -lt 1 ]] || [[ " $PER_PAGE " -gt 100 ]]; then
78 echo '{"error": "per_page must be between 1 and 100"}' >&2
79 exit 1
80 fi
81
82 if [[ -n " $NAME_FILTER " ]]; then
83 RESPONSE = $( gh api --paginate --slurp "repos/${ OWNER }/${ REPO }/labels?per_page=100" | jq '[.[][]]' )
84 else
85 RESPONSE = $( gh api "repos/${ OWNER }/${ REPO }/labels?per_page=${ PER_PAGE }&page=${ PAGE }" )
86 fi
87
88 echo " $RESPONSE " | jq \
89 --argjson per_page " $PER_PAGE " \
90 --argjson page " $PAGE " \
91 --arg name_filter " $NAME_FILTER " \
92 '(if $name_filter == "" then .
93 else [.[] | select(.name | ascii_downcase | contains($name_filter | ascii_downcase))]
94 | .[(($page - 1) * $per_page):($page * $per_page)]
95 end) as $selected
96 | {
97 labels: [$selected[] | {id, node_id, url, name, color, default, description}],
98 item_count: ($selected | length),
99 per_page: $per_page,
100 page: $page
101 }'