18
18
19
19
# Script to create a sub-issue.
20
20
#
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>]
22
22
#
23
23
# Arguments:
24
24
#
25
25
# issue-title Title for the new sub-issue.
26
26
# body-file Path to the file containing the issue body.
27
27
# parent-issue-number Number of the parent issue.
28
+ # labels Optional comma-separated list of labels to apply to the new sub-issue.
28
29
#
29
30
# Environment variables:
30
31
#
@@ -42,6 +43,7 @@ set -o pipefail
42
43
issue_title=" $1 "
43
44
body_file=" $2 "
44
45
parent_issue_number=" $3 "
46
+ labels=" $4 "
45
47
46
48
# Repository information:
47
49
owner=" stdlib-js"
@@ -61,6 +63,14 @@ if [ ! -f "$body_file" ]; then
61
63
fi
62
64
issue_body=$( cat " $body_file " )
63
65
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
+
64
74
# FUNCTIONS #
65
75
66
76
# Error handler.
95
105
echo " $response " | jq -r ' .data.repository.id'
96
106
}
97
107
98
- # Creates a child issue.
108
+ # Creates a child issue with labels .
99
109
#
100
110
# $1 - repository node ID
101
111
# $2 - issue body
@@ -108,11 +118,12 @@ create_child_issue() {
108
118
-H " Content-Type: application/json" \
109
119
--data @- << EOF
110
120
{
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 } } }",
112
122
"variables": {
113
123
"repositoryId": "${repo_id} ",
114
124
"title": "${issue_title} ",
115
- "body": $( echo " $issue_body " | jq -R -s ' .' )
125
+ "body": $( echo " $issue_body " | jq -R -s ' .' ) ,
126
+ "labelIds": ${label_array}
116
127
}
117
128
}
118
129
EOF
140
151
echo " $response " | jq -r ' .data.repository.issue.id'
141
152
}
142
153
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
+
143
185
# Creates a sub-issue relationship.
144
186
#
145
187
# $1 - parent issue ID
@@ -175,6 +217,14 @@ main() {
175
217
exit 1
176
218
fi
177
219
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
+
178
228
echo " Creating child issue..."
179
229
child_issue_response=$( create_child_issue " $repo_id " " $issue_body " )
180
230
0 commit comments