1818
1919# Script to create a sub-issue.
2020#
21- # Usage: ./create_sub_issue.sh <issue-title> <body-file> <parent-issue-number>
21+ # Usage: ./create_sub_issue.sh <issue-title> <body-file> <parent-issue-number> [<labels>]
2222#
2323# Arguments:
2424#
2525# issue-title Title for the new sub-issue.
2626# body-file Path to the file containing the issue body.
2727# parent-issue-number Number of the parent issue.
28+ # labels Optional comma-separated list of labels to apply to the new sub-issue.
2829#
2930# Environment variables:
3031#
@@ -42,6 +43,7 @@ set -o pipefail
4243issue_title=" $1 "
4344body_file=" $2 "
4445parent_issue_number=" $3 "
46+ labels=" $4 "
4547
4648# Repository information:
4749owner=" stdlib-js"
@@ -61,6 +63,14 @@ if [ ! -f "$body_file" ]; then
6163fi
6264issue_body=$( cat " $body_file " )
6365
66+ # Process labels into an array if provided:
67+ if [ -n " $labels " ]; then
68+ # Convert comma-separated string to JSON array...
69+ label_array=" [$( echo " $labels " | sed ' s/[[:space:]]*,[[:space:]]*/","/g' | sed ' s/.*/"&"/' ) ]"
70+ else
71+ label_array=" []"
72+ fi
73+
6474# FUNCTIONS #
6575
6676# Error handler.
95105 echo " $response " | jq -r ' .data.repository.id'
96106}
97107
98- # Creates a child issue.
108+ # Creates a child issue with labels .
99109#
100110# $1 - repository node ID
101111# $2 - issue body
@@ -108,11 +118,12 @@ create_child_issue() {
108118 -H " Content-Type: application/json" \
109119 --data @- << EOF
110120{
111- "query": "mutation CreateIssue(\$ repositoryId: ID!, \$ title: String!, \$ body: String!) { createIssue(input: {repositoryId: \$ repositoryId, title: \$ title, body: \$ body}) { issue { id number } } }",
121+ "query": "mutation CreateIssue(\$ repositoryId: ID!, \$ title: String!, \$ body: String!, \$ labelIds: [ID!] ) { createIssue(input: {repositoryId: \$ repositoryId, title: \$ title, body: \$ body, labelIds: \$ labelIds }) { issue { id number } } }",
112122 "variables": {
113123 "repositoryId": "${repo_id} ",
114124 "title": "${issue_title} ",
115- "body": $( echo " $issue_body " | jq -R -s ' .' )
125+ "body": $( echo " $issue_body " | jq -R -s ' .' ) ,
126+ "labelIds": ${label_array}
116127 }
117128}
118129EOF
140151 echo " $response " | jq -r ' .data.repository.issue.id'
141152}
142153
154+ # Fetches label IDs for given label names.
155+ fetch_label_ids () {
156+ if [ -z " $labels " ]; then
157+ echo " []"
158+ return
159+ fi
160+
161+ local label_names=" ${labels// ,/ \" ,\" } "
162+ local response
163+ response=$( curl -s -X POST ' https://api.github.com/graphql' \
164+ -H " Authorization: bearer ${github_token} " \
165+ -H " Content-Type: application/json" \
166+ --data @- << EOF
167+ {
168+ "query": "query(\$ owner: String!, \$ repo: String!) { repository(owner: \$ owner, name: \$ repo) { labels(first: 100) { nodes { id name } } } }",
169+ "variables": {
170+ "owner": "${owner} ",
171+ "repo": "${repo} "
172+ }
173+ }
174+ EOF
175+ )
176+
177+ # Extract and filter label IDs that match our requested labels...
178+ echo " $response " | jq --arg names " ${label_names} " '
179+ .data.repository.labels.nodes |
180+ map(select(.name as $n | [$names] | contains([$n]))) |
181+ map(.id)
182+ '
183+ }
184+
143185# Creates a sub-issue relationship.
144186#
145187# $1 - parent issue ID
@@ -175,6 +217,14 @@ main() {
175217 exit 1
176218 fi
177219
220+ if [ -n " $labels " ]; then
221+ echo " Fetching label IDs..."
222+ label_array=$( fetch_label_ids)
223+ if [ " $label_array " = " []" ]; then
224+ echo -e " Warning: No valid labels found for the provided label names."
225+ fi
226+ fi
227+
178228 echo " Creating child issue..."
179229 child_issue_response=$( create_child_issue " $repo_id " " $issue_body " )
180230
0 commit comments