From 38dad507b241e6ff877991d4f29a104e328fc6e5 Mon Sep 17 00:00:00 2001 From: gsehgal Date: Fri, 28 Nov 2025 07:58:53 +0000 Subject: [PATCH 1/7] fix #371 use JSON output of gemini cli --- action.yml | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/action.yml b/action.yml index cabafabf..50df5837 100644 --- a/action.yml +++ b/action.yml @@ -289,16 +289,14 @@ runs: # Keep track of whether we've failed FAILED=false - # Run Gemini CLI with the provided prompt, streaming responses in debug + # Run Gemini CLI with the provided prompt, using JSON output format if [[ "${DEBUG}" = true ]]; then echo "::warning::Gemini CLI debug logging is enabled. This will stream responses, which could reveal sensitive information if processed with untrusted inputs." - if ! { gemini --yolo --prompt "${PROMPT}" 2> >(tee "${TEMP_STDERR}" >&2) | tee "${TEMP_STDOUT}"; }; then - FAILED=true - fi - else - if ! gemini --yolo --prompt "${PROMPT}" 2> "${TEMP_STDERR}" 1> "${TEMP_STDOUT}"; then - FAILED=true - fi + fi + + # We capture stdout (JSON) to TEMP_STDOUT and stderr to TEMP_STDERR + if ! gemini --yolo --prompt "${PROMPT}" --output-format json 2> "${TEMP_STDERR}" 1> "${TEMP_STDOUT}"; then + FAILED=true fi # Create the artifacts directory and copy full logs @@ -312,19 +310,44 @@ runs: touch gemini-artifacts/telemetry.log fi + # Parse JSON output to extract response and errors + # If output is not valid JSON, RESPONSE will be empty and we'll rely on stderr for errors + if jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; then + RESPONSE=$(jq -r '.response // ""' "${TEMP_STDOUT}") + ERROR_JSON=$(jq -c '.error // empty' "${TEMP_STDOUT}") + else + RESPONSE="" + ERROR_JSON="" + # If FAILED is false but JSON is invalid, something is wrong + if [[ "${FAILED}" = false ]]; then + echo "::warning::Gemini CLI output was not valid JSON" + fi + fi + + # Set the captured response as a step output, supporting multiline echo "gemini_response<> "${GITHUB_OUTPUT}" - cat "${TEMP_STDOUT}" >> "${GITHUB_OUTPUT}" + echo "${RESPONSE}" >> "${GITHUB_OUTPUT}" echo "EOF" >> "${GITHUB_OUTPUT}" # Set the captured errors as a step output, supporting multiline echo "gemini_errors<> "${GITHUB_OUTPUT}" - cat "${TEMP_STDERR}" >> "${GITHUB_OUTPUT}" + if [[ -n "${ERROR_JSON}" ]]; then + echo "${ERROR_JSON}" >> "${GITHUB_OUTPUT}" + else + cat "${TEMP_STDERR}" >> "${GITHUB_OUTPUT}" + fi echo "EOF" >> "${GITHUB_OUTPUT}" if [[ "${FAILED}" = true ]]; then - LAST_LINE="$(tail -n1 "${TEMP_STDERR}")" - echo "::error title=Gemini CLI execution failed::${LAST_LINE}" + # If we have a structured error from JSON, use it for the error message + if [[ -n "${ERROR_JSON}" ]]; then + ERROR_MSG=$(echo "${ERROR_JSON}" | jq -r '.message // .') + echo "::error title=Gemini CLI execution failed::${ERROR_MSG}" + else + LAST_LINE="$(tail -n1 "${TEMP_STDERR}")" + echo "::error title=Gemini CLI execution failed::${LAST_LINE}" + fi echo "See logs for more details" exit 1 fi From 310816a59780b2502e5b00cb0153abf657f76e8d Mon Sep 17 00:00:00 2001 From: gsehgal Date: Mon, 1 Dec 2025 04:43:55 +0000 Subject: [PATCH 2/7] Addressed review comments --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 50df5837..dd8af23a 100644 --- a/action.yml +++ b/action.yml @@ -342,11 +342,11 @@ runs: if [[ "${FAILED}" = true ]]; then # If we have a structured error from JSON, use it for the error message if [[ -n "${ERROR_JSON}" ]]; then - ERROR_MSG=$(echo "${ERROR_JSON}" | jq -r '.message // .') + ERROR_MSG=$(jq -r '.message // .' <<< "${ERROR_JSON}") echo "::error title=Gemini CLI execution failed::${ERROR_MSG}" else LAST_LINE="$(tail -n1 "${TEMP_STDERR}")" - echo "::error title=Gemini CLI execution failed::${LAST_LINE}" + printf "::error title=Gemini CLI execution failed::%s\n" "${LAST_LINE}" fi echo "See logs for more details" exit 1 From 37041e218d4f1a1e2c18bd96b69edb9b44e77d56 Mon Sep 17 00:00:00 2001 From: gsehgal Date: Mon, 1 Dec 2025 04:49:26 +0000 Subject: [PATCH 3/7] Printing info in case of debug --- action.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/action.yml b/action.yml index dd8af23a..391747a2 100644 --- a/action.yml +++ b/action.yml @@ -299,6 +299,11 @@ runs: FAILED=true fi + if [[ "${DEBUG}" = true ]]; then + cat "${TEMP_STDOUT}" + cat "${TEMP_STDERR}" + fi + # Create the artifacts directory and copy full logs mkdir -p gemini-artifacts cp "${TEMP_STDOUT}" gemini-artifacts/stdout.log From 4a8b4867987ce43e511da205e105d1a2408088e1 Mon Sep 17 00:00:00 2001 From: gsehgal Date: Mon, 1 Dec 2025 10:27:19 +0000 Subject: [PATCH 4/7] Added review comments --- action.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/action.yml b/action.yml index 391747a2..676c137f 100644 --- a/action.yml +++ b/action.yml @@ -300,8 +300,12 @@ runs: fi if [[ "${DEBUG}" = true ]]; then + echo "::start-group::Gemini CLI STDOUT" cat "${TEMP_STDOUT}" + echo "::end-group::" + echo "::start-group::Gemini CLI STDERR" cat "${TEMP_STDERR}" + echo "::end-group::" fi # Create the artifacts directory and copy full logs @@ -323,10 +327,7 @@ runs: else RESPONSE="" ERROR_JSON="" - # If FAILED is false but JSON is invalid, something is wrong - if [[ "${FAILED}" = false ]]; then - echo "::warning::Gemini CLI output was not valid JSON" - fi + echo "::warning::Gemini CLI output was not valid JSON" fi @@ -349,11 +350,10 @@ runs: if [[ -n "${ERROR_JSON}" ]]; then ERROR_MSG=$(jq -r '.message // .' <<< "${ERROR_JSON}") echo "::error title=Gemini CLI execution failed::${ERROR_MSG}" - else - LAST_LINE="$(tail -n1 "${TEMP_STDERR}")" - printf "::error title=Gemini CLI execution failed::%s\n" "${LAST_LINE}" fi - echo "See logs for more details" + echo "::start-group::Gemini CLI STDERR" + cat "${TEMP_STDERR}" + echo "::end-group::" exit 1 fi env: From 9184bbf08be2f2741bf17662610e96d15a42c6f3 Mon Sep 17 00:00:00 2001 From: gsehgal Date: Mon, 1 Dec 2025 11:06:52 +0000 Subject: [PATCH 5/7] Refactor error and response output --- action.yml | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/action.yml b/action.yml index 676c137f..0a893768 100644 --- a/action.yml +++ b/action.yml @@ -300,12 +300,9 @@ runs: fi if [[ "${DEBUG}" = true ]]; then - echo "::start-group::Gemini CLI STDOUT" + echo "::: Start Gemini CLI STDOUT :::" cat "${TEMP_STDOUT}" - echo "::end-group::" - echo "::start-group::Gemini CLI STDERR" - cat "${TEMP_STDERR}" - echo "::end-group::" + echo "::: End Gemini CLI STDOUT :::"s fi # Create the artifacts directory and copy full logs @@ -321,19 +318,26 @@ runs: # Parse JSON output to extract response and errors # If output is not valid JSON, RESPONSE will be empty and we'll rely on stderr for errors + RESPONSE="" + ERROR_JSON="" if jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; then RESPONSE=$(jq -r '.response // ""' "${TEMP_STDOUT}") - ERROR_JSON=$(jq -c '.error // empty' "${TEMP_STDOUT}") - else - RESPONSE="" - ERROR_JSON="" + fi + if jq -e . "${TEMP_STDERR}" >/dev/null 2>&1; then + ERROR_JSON=$(jq -c '.error // empty' "${TEMP_STDERR}") + fi + if [[ -z "${RESPONSE}" && -z "${ERROR_JSON}" ]]; then echo "::warning::Gemini CLI output was not valid JSON" fi # Set the captured response as a step output, supporting multiline echo "gemini_response<> "${GITHUB_OUTPUT}" - echo "${RESPONSE}" >> "${GITHUB_OUTPUT}" + if [[ -n "${RESPONSE}" ]]; then + echo "${RESPONSE}" >> "${GITHUB_OUTPUT}" + else + cat "${TEMP_STDOUT}" >> "${GITHUB_OUTPUT}" + fi echo "EOF" >> "${GITHUB_OUTPUT}" # Set the captured errors as a step output, supporting multiline @@ -351,9 +355,9 @@ runs: ERROR_MSG=$(jq -r '.message // .' <<< "${ERROR_JSON}") echo "::error title=Gemini CLI execution failed::${ERROR_MSG}" fi - echo "::start-group::Gemini CLI STDERR" + echo "::: Start Gemini CLI STDERR :::" cat "${TEMP_STDERR}" - echo "::end-group::" + echo "::: End Gemini CLI STDERR :::" exit 1 fi env: From 51df6be466cffa7427acc4c7dd74d9bc5212b5e8 Mon Sep 17 00:00:00 2001 From: gsehgal Date: Tue, 2 Dec 2025 10:51:54 +0000 Subject: [PATCH 6/7] Change statement for readibility --- action.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/action.yml b/action.yml index 0a893768..feee457a 100644 --- a/action.yml +++ b/action.yml @@ -302,7 +302,7 @@ runs: if [[ "${DEBUG}" = true ]]; then echo "::: Start Gemini CLI STDOUT :::" cat "${TEMP_STDOUT}" - echo "::: End Gemini CLI STDOUT :::"s + echo "::: End Gemini CLI STDOUT :::" fi # Create the artifacts directory and copy full logs @@ -326,8 +326,8 @@ runs: if jq -e . "${TEMP_STDERR}" >/dev/null 2>&1; then ERROR_JSON=$(jq -c '.error // empty' "${TEMP_STDERR}") fi - if [[ -z "${RESPONSE}" && -z "${ERROR_JSON}" ]]; then - echo "::warning::Gemini CLI output was not valid JSON" + if ! { jq -e . "${TEMP_STDERR}" >/dev/null 2>&1 && jq -e . "${TEMP_STDOUT}" >/dev/null 2>&1; }; then + echo "::warning::Gemini CLI output was not valid JSON" fi From 2722bf0ce7092c06ea42c556a4452b9a1e059fd4 Mon Sep 17 00:00:00 2001 From: gsehgal Date: Wed, 3 Dec 2025 05:39:01 +0000 Subject: [PATCH 7/7] Fixed lint error --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index feee457a..9447689e 100644 --- a/action.yml +++ b/action.yml @@ -337,7 +337,7 @@ runs: echo "${RESPONSE}" >> "${GITHUB_OUTPUT}" else cat "${TEMP_STDOUT}" >> "${GITHUB_OUTPUT}" - fi + fi echo "EOF" >> "${GITHUB_OUTPUT}" # Set the captured errors as a step output, supporting multiline