@@ -108,25 +108,59 @@ jobs:
108108 uses : actions/github-script@v8
109109 env :
110110 AI_RESPONSE : ${{ steps.ai-triage.outputs.response }}
111+ AVAILABLE_LABELS : ${{ env.AVAILABLE_LABELS }}
111112 with :
112113 script : |
113114 const response = process.env.AI_RESPONSE;
115+ const availableLabelsStr = process.env.AVAILABLE_LABELS;
116+
114117 if (!response || response.trim() === '') {
115118 console.log('No labels selected by AI');
116119 return;
117120 }
118121
119- const labels = response.replaceAll('```', '').split(',')
122+ if (!availableLabelsStr) {
123+ console.log('No available labels found');
124+ return;
125+ }
126+
127+ // Parse available labels (normalized to lowercase)
128+ const availableLabels = availableLabelsStr.split(',')
129+ .map(l => l.trim().toLowerCase())
130+ .filter(l => l.length > 0);
131+
132+ // Parse AI suggested labels
133+ const suggestedLabels = response.replaceAll('```', '').split(',')
120134 .map(l => l.trim())
121135 .filter(l => l.length > 0);
122136
123- if (labels.length > 0) {
124- console.log(`Applying labels: ${labels.join(', ')}`);
137+ // Partition into valid and invalid in a single pass
138+ const { validLabels, invalidLabels } = suggestedLabels.reduce(
139+ (acc, label) => {
140+ if (availableLabels.includes(label.toLowerCase())) {
141+ acc.validLabels.push(label);
142+ } else {
143+ acc.invalidLabels.push(label);
144+ }
145+ return acc;
146+ },
147+ { validLabels: [], invalidLabels: [] }
148+ );
149+
150+ // Log any invalid labels
151+ if (invalidLabels.length > 0) {
152+ console.log(
153+ `Skipping invalid labels: ${invalidLabels.join(', ')}`
154+ );
155+ }
156+
157+ if (validLabels.length > 0) {
158+ console.log(`Applying labels: ${validLabels.join(', ')}`);
125159 await github.rest.issues.addLabels({
126160 owner: context.repo.owner,
127161 repo: context.repo.repo,
128162 issue_number: context.issue.number,
129- labels: labels
163+ labels: validLabels
130164 });
131165 } else {
132166 console.log('No valid labels to apply');
@@ -317,27 +351,60 @@ jobs:
317351 env :
318352 AI_RESPONSE : ${{ steps.ai-triage.outputs.response }}
319353 ITEM_NUMBER : ${{ inputs.issue_number }}
354+ AVAILABLE_LABELS : ${{ env.AVAILABLE_LABELS }}
320355 with :
321356 script : |
322357 const response = process.env.AI_RESPONSE;
323358 const itemNumber = parseInt(process.env.ITEM_NUMBER);
359+ const availableLabelsStr = process.env.AVAILABLE_LABELS;
324360
325361 if (!response || response.trim() === '') {
326362 console.log('No labels selected by AI');
327363 return;
328364 }
329365
330- const labels = response.replaceAll('```', '').split(',')
366+ if (!availableLabelsStr) {
367+ console.log('No available labels found');
368+ return;
369+ }
370+
371+ // Parse available labels (normalized to lowercase)
372+ const availableLabels = availableLabelsStr.split(',')
373+ .map(l => l.trim().toLowerCase())
374+ .filter(l => l.length > 0);
375+
376+ // Parse AI suggested labels
377+ const suggestedLabels = response.replaceAll('```', '').split(',')
331378 .map(l => l.trim())
332379 .filter(l => l.length > 0);
333380
334- if (labels.length > 0) {
335- console.log(`Applying labels: ${labels.join(', ')}`);
381+ // Partition into valid and invalid in a single pass
382+ const { validLabels, invalidLabels } = suggestedLabels.reduce(
383+ (acc, label) => {
384+ if (availableLabels.includes(label.toLowerCase())) {
385+ acc.validLabels.push(label);
386+ } else {
387+ acc.invalidLabels.push(label);
388+ }
389+ return acc;
390+ },
391+ { validLabels: [], invalidLabels: [] }
392+ );
393+
394+ // Log any invalid labels
395+ if (invalidLabels.length > 0) {
396+ console.log(
397+ `Skipping invalid labels: ${invalidLabels.join(', ')}`
398+ );
399+ }
400+
401+ if (validLabels.length > 0) {
402+ console.log(`Applying labels: ${validLabels.join(', ')}`);
336403 await github.rest.issues.addLabels({
337404 owner: context.repo.owner,
338405 repo: context.repo.repo,
339406 issue_number: itemNumber,
340- labels: labels
407+ labels: validLabels
341408 });
342409 } else {
343410 console.log('No valid labels to apply');
0 commit comments