diff --git a/.github/workflows/gemini-scheduled-issue-triage.yml b/.github/workflows/gemini-scheduled-issue-triage.yml index 9feb470ddd..a91b0cc60e 100644 --- a/.github/workflows/gemini-scheduled-issue-triage.yml +++ b/.github/workflows/gemini-scheduled-issue-triage.yml @@ -233,17 +233,38 @@ jobs: core.info(`Raw labels JSON: ${rawLabels}`); let parsedLabels; try { + // First, try to parse the raw output as JSON. + parsedLabels = JSON.parse(rawLabels); + } catch (jsonError) { + // If that fails, check for a markdown code block. + core.warning(`Direct JSON parsing failed: ${jsonError.message}. Trying to extract from a markdown block.`); const jsonMatch = rawLabels.match(/```json\s*([\s\S]*?)\s*```/); - if (!jsonMatch || !jsonMatch[1]) { - throw new Error("Could not find a ```json ... ``` block in the output."); + if (jsonMatch && jsonMatch[1]) { + try { + parsedLabels = JSON.parse(jsonMatch[1].trim()); + } catch (markdownError) { + core.setFailed(`Failed to parse JSON even after extracting from markdown block: ${markdownError.message}\nRaw output: ${rawLabels}`); + return; + } + } else { + // If no markdown block, try to find a raw JSON array in the output. + // The CLI may include debug/log lines (e.g. telemetry init, YOLO mode) + // before the actual JSON response. + const jsonArrayMatch = rawLabels.match(/(\[[\s\S]*\])/); + if (jsonArrayMatch) { + try { + parsedLabels = JSON.parse(jsonArrayMatch[0]); + } catch (extractError) { + core.setFailed(`Found JSON-like content but failed to parse: ${extractError.message}\nRaw output: ${rawLabels}`); + return; + } + } else { + core.setFailed(`Output is not valid JSON and does not contain extractable JSON.\nRaw output: ${rawLabels}`); + return; + } } - const jsonString = jsonMatch[1].trim(); - parsedLabels = JSON.parse(jsonString); - core.info(`Parsed labels JSON: ${JSON.stringify(parsedLabels)}`); - } catch (err) { - core.setFailed(`Failed to parse labels JSON from Gemini output: ${err.message}\nRaw output: ${rawLabels}`); - return; } + core.info(`Parsed labels JSON: ${JSON.stringify(parsedLabels)}`); for (const entry of parsedLabels) { const issueNumber = entry.issue_number;