1616# See the License for the specific language governing permissions and
1717# limitations under the License.
1818
19- # Script to create a sub-issue for linting failures
19+ # Script to create a sub-issue.
2020#
21- # Usage: ./create_lint_sub_issue .sh <workflow-url > <lint-type > <parent-issue-number>
21+ # Usage: ./create_sub_issue .sh <issue-title > <body-file > <parent-issue-number> [<labels>]
2222#
2323# Arguments:
2424#
25- # workflow-url URL of the workflow run .
26- # lint-type Type of linting failure .
25+ # issue-title Title for the new sub-issue .
26+ # body-file Path to the file containing the issue body .
2727# parent-issue-number Number of the parent issue.
28- # error-log-file Path to the error log file.
29- #
28+ # labels Optional comma-separated list of labels to apply to the new sub-issue.
3029#
3130# Environment variables:
3231#
@@ -41,10 +40,10 @@ set -o pipefail
4140# VARIABLES #
4241
4342# Assign command line arguments to variables:
44- workflow_url =" $1 "
45- lint_type =" $2 "
43+ issue_title =" $1 "
44+ body_file =" $2 "
4645parent_issue_number=" $3 "
47- error_log_file =" $4 "
46+ labels =" $4 "
4847
4948# Repository information:
5049owner=" stdlib-js"
@@ -57,28 +56,20 @@ if [ -z "$github_token" ]; then
5756 exit 1
5857fi
5958
60- # Read and format the error log
61- if [ ! -f " $error_log_file " ]; then
62- echo -e " Error log file not found: ${error_log_file } "
59+ # Read and validate the body file:
60+ if [ ! -f " $body_file " ]; then
61+ echo -e " ERROR: Body file not found: ${body_file } "
6362 exit 1
6463fi
65- error_log_content=$( cat " $error_log_file " )
66-
67- # Create issue body with formatted error log
68- issue_body=" ## ${lint_type} Linting Failures
69-
70- Linting failures were detected in the automated lint workflow run.
71-
72- ### Workflow Details
73- - Run: ${workflow_url}
74- - Type: ${lint_type} Linting
75- - Date: $( date -u +" %Y-%m-%d %H:%M:%S UTC" )
76-
77- ### Error Details
78- \`\`\`
79- ${error_log_content}
80- \`\`\`
81- "
64+ issue_body=$( cat " $body_file " )
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
8273
8374# FUNCTIONS #
8475
114105 echo " $response " | jq -r ' .data.repository.id'
115106}
116107
117- # Creates a child issue.
108+ # Creates a child issue with labels .
118109#
119110# $1 - repository node ID
120111# $2 - issue body
@@ -127,11 +118,12 @@ create_child_issue() {
127118 -H " Content-Type: application/json" \
128119 --data @- << EOF
129120{
130- "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 } } }",
131122 "variables": {
132123 "repositoryId": "${repo_id} ",
133- "title": "Fix ${lint_type} lint errors",
134- "body": $( echo " $issue_body " | jq -R -s ' .' )
124+ "title": "${issue_title} ",
125+ "body": $( echo " $issue_body " | jq -R -s ' .' ) ,
126+ "labelIds": ${label_array}
135127 }
136128}
137129EOF
159151 echo " $response " | jq -r ' .data.repository.issue.id'
160152}
161153
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+
162185# Creates a sub-issue relationship.
163186#
164187# $1 - parent issue ID
@@ -194,7 +217,15 @@ main() {
194217 exit 1
195218 fi
196219
197- echo " Creating child issue for ${lint_type} lint failures..."
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+
228+ echo " Creating child issue..."
198229 child_issue_response=$( create_child_issue " $repo_id " " $issue_body " )
199230
200231 child_issue_id=$( echo " $child_issue_response " | jq -r ' .data.createIssue.issue.id' )
0 commit comments