spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / scripts / edit-issue-labels.sh
84 lines · 1.9 KBRaw
1#!/usr/bin/env bash
2#
3# Edits labels on a GitHub issue.
4# Usage: ./edit-issue-labels.sh --add-label bug --add-label needs-triage --remove-label untriaged
5#
6# The issue number is read from the workflow event payload.
7#
8
9set -euo pipefail
10
11# Read from event payload so the issue number is bound to the triggering event.
12# Falls back to workflow_dispatch inputs for manual runs.
13ISSUE=$(jq -r '.issue.number // .inputs.issue_number // empty' "${GITHUB_EVENT_PATH:?GITHUB_EVENT_PATH not set}")
14if ! [[ "$ISSUE" =~ ^[0-9]+$ ]]; then
15 echo "Error: no issue number in event payload" >&2
16 exit 1
17fi
18
19ADD_LABELS=()
20REMOVE_LABELS=()
21
22# Parse arguments
23while [[ $# -gt 0 ]]; do
24 case $1 in
25 --add-label)
26 ADD_LABELS+=("$2")
27 shift 2
28 ;;
29 --remove-label)
30 REMOVE_LABELS+=("$2")
31 shift 2
32 ;;
33 *)
34 echo "Error: unknown argument (only --add-label and --remove-label are accepted)" >&2
35 exit 1
36 ;;
37 esac
38done
39
40if [[ ${#ADD_LABELS[@]} -eq 0 && ${#REMOVE_LABELS[@]} -eq 0 ]]; then
41 exit 1
42fi
43
44# Fetch valid labels from the repo
45VALID_LABELS=$(gh label list --limit 500 --json name --jq '.[].name')
46
47# Filter to only labels that exist in the repo
48FILTERED_ADD=()
49for label in "${ADD_LABELS[@]}"; do
50 if echo "$VALID_LABELS" | grep -qxF "$label"; then
51 FILTERED_ADD+=("$label")
52 fi
53done
54
55FILTERED_REMOVE=()
56for label in "${REMOVE_LABELS[@]}"; do
57 if echo "$VALID_LABELS" | grep -qxF "$label"; then
58 FILTERED_REMOVE+=("$label")
59 fi
60done
61
62if [[ ${#FILTERED_ADD[@]} -eq 0 && ${#FILTERED_REMOVE[@]} -eq 0 ]]; then
63 exit 0
64fi
65
66# Build gh command arguments
67GH_ARGS=("issue" "edit" "$ISSUE")
68
69for label in "${FILTERED_ADD[@]}"; do
70 GH_ARGS+=("--add-label" "$label")
71done
72
73for label in "${FILTERED_REMOVE[@]}"; do
74 GH_ARGS+=("--remove-label" "$label")
75done
76
77gh "${GH_ARGS[@]}"
78
79if [[ ${#FILTERED_ADD[@]} -gt 0 ]]; then
80 echo "Added: ${FILTERED_ADD[*]}"
81fi
82if [[ ${#FILTERED_REMOVE[@]} -gt 0 ]]; then
83 echo "Removed: ${FILTERED_REMOVE[*]}"
84fi