|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# gh-issue-add-subs - Add multiple sub-issues to a parent issue |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# gh-issue-add-subs <parent-issue-number> <child-issue-number>... |
| 7 | +# |
| 8 | +# Description: |
| 9 | +# This script adds multiple existing issues as sub-issues to a parent issue |
| 10 | +# using GitHub's GraphQL API. All relationships are created in a single |
| 11 | +# batched GraphQL request for efficiency. |
| 12 | +# |
| 13 | +# Examples: |
| 14 | +# # Add issues #52, #53, #54 as sub-issues of #31 |
| 15 | +# gh-issue-add-subs 31 52 53 54 |
| 16 | +# |
| 17 | +# # Add a single sub-issue |
| 18 | +# gh-issue-add-subs 31 52 |
| 19 | +# |
| 20 | +# Requirements: |
| 21 | +# - GitHub CLI (gh) installed and authenticated |
| 22 | +# - Issues must exist in the current repository |
| 23 | +# |
| 24 | +# Exit codes: |
| 25 | +# 0 - Success |
| 26 | +# 1 - Missing required arguments |
| 27 | +# 2 - Failed to get parent issue ID |
| 28 | +# 3 - Failed to get child issue ID |
| 29 | +# 4 - GraphQL mutation failed |
| 30 | + |
| 31 | +set -euo pipefail |
| 32 | + |
| 33 | +# Colors for output |
| 34 | +RED='\033[0;31m' |
| 35 | +GREEN='\033[0;32m' |
| 36 | +YELLOW='\033[1;33m' |
| 37 | +NC='\033[0m' # No Color |
| 38 | + |
| 39 | +# Print usage |
| 40 | +usage() { |
| 41 | + cat << EOF |
| 42 | +Usage: $0 <parent-issue-number> <child-issue-number>... |
| 43 | +
|
| 44 | +Add multiple sub-issues to a parent issue using GitHub's GraphQL API. |
| 45 | +
|
| 46 | +Arguments: |
| 47 | + parent-issue-number The issue number of the parent issue |
| 48 | + child-issue-number... One or more issue numbers to add as sub-issues |
| 49 | +
|
| 50 | +Examples: |
| 51 | + $0 31 52 53 54 |
| 52 | + $0 31 52 |
| 53 | +
|
| 54 | +EOF |
| 55 | +} |
| 56 | + |
| 57 | +# Check if GitHub CLI is available |
| 58 | +if ! command -v gh &> /dev/null; then |
| 59 | + echo -e "${RED}Error: GitHub CLI (gh) is not installed${NC}" >&2 |
| 60 | + echo "Install it from https://cli.github.com/" >&2 |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | + |
| 64 | +# Check arguments |
| 65 | +if [ $# -lt 2 ]; then |
| 66 | + echo -e "${RED}Error: Missing required arguments${NC}" >&2 |
| 67 | + usage |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +parent=$1 |
| 72 | +shift |
| 73 | +children=("$@") |
| 74 | + |
| 75 | +echo -e "${YELLOW}Adding ${#children[@]} sub-issue(s) to issue #${parent}...${NC}" |
| 76 | + |
| 77 | +# Get parent issue ID |
| 78 | +echo "Fetching parent issue #${parent}..." |
| 79 | +parent_id=$(gh issue view "$parent" --json id -q .id 2>/dev/null || true) |
| 80 | + |
| 81 | +if [ -z "$parent_id" ]; then |
| 82 | + echo -e "${RED}Error: Failed to get parent issue #${parent}${NC}" >&2 |
| 83 | + echo "Make sure the issue exists and you have access to it." >&2 |
| 84 | + exit 2 |
| 85 | +fi |
| 86 | + |
| 87 | +# Build GraphQL mutation with aliases for each child |
| 88 | +mutation="mutation {" |
| 89 | +i=1 |
| 90 | +child_ids=() |
| 91 | + |
| 92 | +for child in "${children[@]}"; do |
| 93 | + echo "Fetching child issue #${child}..." |
| 94 | + child_id=$(gh issue view "$child" --json id -q .id 2>/dev/null || true) |
| 95 | + |
| 96 | + if [ -z "$child_id" ]; then |
| 97 | + echo -e "${RED}Error: Failed to get child issue #${child}${NC}" >&2 |
| 98 | + echo "Make sure the issue exists and you have access to it." >&2 |
| 99 | + exit 3 |
| 100 | + fi |
| 101 | + |
| 102 | + child_ids+=("$child_id") |
| 103 | + |
| 104 | + # Add mutation with unique alias |
| 105 | + mutation+=" |
| 106 | + add$i: addSubIssue(input: { |
| 107 | + issueId: \"$parent_id\" |
| 108 | + subIssueId: \"$child_id\" |
| 109 | + }) { |
| 110 | + issue { |
| 111 | + number |
| 112 | + title |
| 113 | + } |
| 114 | + subIssue { |
| 115 | + number |
| 116 | + title |
| 117 | + } |
| 118 | + }" |
| 119 | + ((i++)) |
| 120 | +done |
| 121 | + |
| 122 | +mutation+=" |
| 123 | +}" |
| 124 | + |
| 125 | +# Execute GraphQL mutation |
| 126 | +echo -e "${YELLOW}Creating relationships...${NC}" |
| 127 | +response=$(gh api graphql -f query="$mutation" 2>&1) |
| 128 | + |
| 129 | +# Check for errors in response |
| 130 | +if echo "$response" | grep -q '"errors"'; then |
| 131 | + echo -e "${RED}Error: GraphQL mutation failed${NC}" >&2 |
| 132 | + echo "$response" | jq '.' >&2 || echo "$response" >&2 |
| 133 | + exit 4 |
| 134 | +fi |
| 135 | + |
| 136 | +# Parse and display results |
| 137 | +echo -e "${GREEN}✓ Successfully added sub-issues:${NC}" |
| 138 | +i=1 |
| 139 | +for child in "${children[@]}"; do |
| 140 | + result=$(echo "$response" | jq -r ".data.add$i.subIssue.number // empty" 2>/dev/null || echo "") |
| 141 | + if [ -n "$result" ]; then |
| 142 | + title=$(echo "$response" | jq -r ".data.add$i.subIssue.title // \"\"" 2>/dev/null || echo "") |
| 143 | + echo -e " ${GREEN}✓${NC} Issue #${child} → Sub-issue of #${parent}" |
| 144 | + if [ -n "$title" ]; then |
| 145 | + echo " \"$title\"" |
| 146 | + fi |
| 147 | + else |
| 148 | + echo -e " ${YELLOW}⚠${NC} Issue #${child} (may already be a sub-issue)" |
| 149 | + fi |
| 150 | + ((i++)) |
| 151 | +done |
| 152 | + |
| 153 | +echo -e "\n${GREEN}Done!${NC} View issue #${parent} on GitHub to see the relationships." |
| 154 | + |
0 commit comments