Skip to content

fix(action): scope exit-code capture instead of disabling errexit globally#13

Merged
cemililik merged 3 commits into
mainfrom
fix/action-errexit-scope
May 25, 2026
Merged

fix(action): scope exit-code capture instead of disabling errexit globally#13
cemililik merged 3 commits into
mainfrom
fix/action-errexit-scope

Conversation

@cemililik
Copy link
Copy Markdown
Collaborator

@cemililik cemililik commented May 25, 2026

Addresses the Sourcery review on #12.

#12's underlying change (the set +e fix for findings exiting 1 under bash -e) is already in main — it was merged via #11 and shipped in v1.6.0 — so #12 itself is redundant (and would conflict). This PR applies Sourcery's two valid follow-up suggestions to main:

  1. Don't disable errexit globally. action.yml now uses EXIT_CODE=0; leakwatch … || EXIT_CODE=$? instead of set +e, so -e stays on for the rest of the step (later command failures still fail fast) while findings (exit 1) are captured and mapped. The job-summary jq | head pipe is guarded with || true so a malformed SARIF can't abort the step before the exit-code mapping.
  2. Don't swallow real errors in the test. The cli-github-format job captures the exit code and fails on a hard error (>=2) while tolerating findings (0/1), instead of || true.

Verification

Reproduced under bash -e -o pipefail: the exit-code mapping runs and a subsequent failing command still aborts the step (errexit is no longer globally disabled). shellcheck (both run scripts) + actionlint clean.

🤖 Generated with Claude Code

Summary by Sourcery

Scope leakwatch exit-code capture to preserve errexit behavior while still honoring fail-on-findings configuration, and tighten tests to distinguish expected findings from hard errors.

Bug Fixes:

  • Ensure leakwatch findings exiting with code 1 do not abort the action step while still allowing later commands to fail fast.
  • Prevent malformed SARIF or job-summary generation errors from aborting the step before exit-code mapping.
  • Make the cli-github-format test fail on real leakwatch errors (exit code >= 2) while continuing to accept expected findings.

Tests:

  • Update cli-github-format workflow test to capture leakwatch exit codes explicitly and assert on hard errors versus expected findings.

Summary by CodeRabbit

  • Bug Fixes
    • Scan steps now handle nonzero exit codes more reliably: critical outcomes cause the step to fail and failure messages include the captured exit code.
    • Workflow summary generation is more resilient: failed findings rendering no longer breaks the step and a clear fallback message is shown when detailed rendering isn’t available.

Review Change Stack

…bally

Addresses the Sourcery review on #12 (the set +e change is already in main via #11,
released in v1.6.0):

- action.yml: replace `set +e` + direct call with
  `EXIT_CODE=0; leakwatch "${ARGS[@]}" || EXIT_CODE=$?`, so errexit stays enabled
  for the rest of the step (later failures still fail fast) while a findings exit
  (1) is captured and mapped. Guard the job-summary jq pipe with `|| true` so a
  malformed SARIF can't abort before the exit-code mapping.
- action-test.yml (cli-github-format): capture the exit code instead of `|| true`,
  failing on a real error (>=2) while tolerating findings (0/1).

Verified under `bash -e -o pipefail`: the mapping runs and a subsequent failing
command still aborts (errexit not globally disabled).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 25, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Scopes exit-code handling for the leakwatch scan and GitHub-format test so that bash errexit remains enabled globally while still correctly distinguishing expected findings (exit 1) from real errors, and ensures job-summary rendering failures do not mask scan results.

Flow diagram for cli-github-format test exit-code handling

flowchart TD
  A[Start cli-github-format job] --> B[Run leakwatch GitHub format scan and capture exit code]
  B --> C{EXIT_CODE value}
  C --> C0[EXIT_CODE=0: success]
  C --> C1[EXIT_CODE=1: findings tolerated]
  C --> C2[EXIT_CODE>=2: real error]

  C0 --> D[Job continues and passes]
  C1 --> D
  C2 --> E[Job fails to surface hard error]
Loading

File-Level Changes

Change Details Files
Scope leakwatch exit-code capture in the action entrypoint so errexit remains enabled for the rest of the step and summary rendering is non-fatal.
  • Replace global set +e around the leakwatch invocation with an EXIT_CODE variable initialized to 0 and set only when the scan command fails via `
Tighten the GitHub-format workflow test to distinguish expected findings from hard errors while running under bash -e.
  • Capture the leakwatch CLI exit status in a separate rc variable instead of swallowing it with `

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 46c4ec7c-7cb8-4038-8699-c5999311cbad

📥 Commits

Reviewing files that changed from the base of the PR and between 4f9fa2f and b4d5625.

📒 Files selected for processing (1)
  • action.yml

📝 Walkthrough

Walkthrough

Capture leakwatch exit codes without disabling errexit, add a non-fatal fallback for SARIF findings rendering, and update the workflow test to treat rc >= 2 as hard errors while validating expected inline ::error annotations.

Changes

Exit Code Handling and Error Resilience

Layer / File(s) Summary
Scan step exit code capture
action.yml
Initialize EXIT_CODE=0 and capture leakwatch failures with `leakwatch "${ARGS[@]}"
SARIF summary fallback
action.yml
Write jq SARIF table output to a temporary file, print its first 50 lines if present, otherwise print _(could not render the findings table; see the SARIF artifact)_, then clean up the temp file.
GitHub format test validation
.github/workflows/action-test.yml
Capture leakwatch exit status into rc, fail the test when rc >= 2, echo output, and assert the presence of a ^::error .*aws-access-key-id annotation (error message includes rc if annotation missing).

Sequence Diagram(s)

sequenceDiagram
  participant ActionRunner
  participant Leakwatch
  participant JobSummary
  ActionRunner->>Leakwatch: run leakwatch with ARGS
  Leakwatch-->>ActionRunner: exit code (0/1/>=2)
  ActionRunner->>ActionRunner: set EXIT_CODE or rc based on result
  ActionRunner->>JobSummary: jq render -> temp file -> head -50 or fallback
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I nibble bytes and tally codes with care,
I hop between exits — are they warnings or despair?
No masked set +e to hide the trail,
Now outputs speak and tests won't fail,
A rabbit cheers the action's clear air. 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: replacing global set +e with scoped exit-code capture to preserve errexit in subsequent commands, which is the primary focus across both modified files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/action-errexit-scope

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In the workflow test step, the pattern out="$(${RUNNER_TEMP}/leakwatch ...)" || rc=$? relies on the subtle behavior that the assignment’s exit status mirrors the command substitution; consider splitting this into an explicit if block (e.g. if ! out="$(${...})"; then rc=$?; fi) to make the error-handling semantics clearer and less surprising to future readers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the workflow test step, the pattern `out="$(${RUNNER_TEMP}/leakwatch ...)" || rc=$?` relies on the subtle behavior that the assignment’s exit status mirrors the command substitution; consider splitting this into an explicit `if` block (e.g. `if ! out="$(${...})"; then rc=$?; fi`) to make the error-handling semantics clearer and less surprising to future readers.

## Individual Comments

### Comment 1
<location path="action.yml" line_range="313" />
<code_context>
                 echo "| Level | Detector | Location |"
                 echo "| --- | --- | --- |"
-                jq -r '.runs[].results[] | "| \(.level) | \(.ruleId) | \((.locations[0].physicalLocation.artifactLocation.uri // "-"))\(if .locations[0].physicalLocation.region.startLine then ":" + (.locations[0].physicalLocation.region.startLine | tostring) else "" end) |"' "$OUT" 2>/dev/null | head -50
+                jq -r '.runs[].results[] | "| \(.level) | \(.ruleId) | \((.locations[0].physicalLocation.artifactLocation.uri // "-"))\(if .locations[0].physicalLocation.region.startLine then ":" + (.locations[0].physicalLocation.region.startLine | tostring) else "" end) |"' "$OUT" 2>/dev/null | head -50 || true
                 if [ "${total:-0}" -gt 50 ] 2>/dev/null; then
                   echo ""
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Using `|| true` here could over-mask genuine issues in the summary generation

This makes the summary step non-fatal for malformed/missing `$OUT`, but it also hides real failures (e.g., `jq` not installed, unexpected output shape) that we’d want to notice. To keep resilience without fully swallowing errors, consider logging a clear failure message instead (e.g., `... || echo "Failed to render findings summary from $OUT" >&2`).

```suggestion
                jq -r '.runs[].results[] | "| \(.level) | \(.ruleId) | \((.locations[0].physicalLocation.artifactLocation.uri // "-"))\(if .locations[0].physicalLocation.region.startLine then ":" + (.locations[0].physicalLocation.region.startLine | tostring) else "" end) |"' "$OUT" 2>/dev/null | head -50 || echo "Failed to render findings summary from \"${OUT:-<unset>}\" (jq exit code: $?)" >&2
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread action.yml Outdated
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors error handling in the action script by capturing the exit code of the leakwatch scan locally instead of disabling global errexit, ensuring subsequent commands still fail fast. It also adds a safety || true to the jq pipeline for the job summary. Feedback suggests further hardening the jq filter using the optional operator to handle potentially missing metadata in SARIF results.

Comment thread action.yml Outdated
echo "| Level | Detector | Location |"
echo "| --- | --- | --- |"
jq -r '.runs[].results[] | "| \(.level) | \(.ruleId) | \((.locations[0].physicalLocation.artifactLocation.uri // "-"))\(if .locations[0].physicalLocation.region.startLine then ":" + (.locations[0].physicalLocation.region.startLine | tostring) else "" end) |"' "$OUT" 2>/dev/null | head -50
jq -r '.runs[].results[] | "| \(.level) | \(.ruleId) | \((.locations[0].physicalLocation.artifactLocation.uri // "-"))\(if .locations[0].physicalLocation.region.startLine then ":" + (.locations[0].physicalLocation.region.startLine | tostring) else "" end) |"' "$OUT" 2>/dev/null | head -50 || true
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While adding || true prevents the script from aborting on jq or SIGPIPE errors, the jq filter itself is still fragile. If a SARIF result is missing the locations array or any nested property (e.g., for a global finding), jq will throw an error like Cannot index null with number and stop processing further results.

You can use the ? operator to make the filter more robust, ensuring the job summary table is populated even if some findings have incomplete metadata.

                jq -r '.runs[].results[] | "| \(.level) | \(.ruleId) | \((.locations[0].physicalLocation.artifactLocation.uri)? // "-")\(if (.locations[0].physicalLocation.region.startLine)? then ":" + (.locations[0].physicalLocation.region.startLine | tostring) else "" end) |"' "$OUT" 2>/dev/null | head -50 || true

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified this isn't needed for this filter: jq doesn't error on a result without locations because every accessed field is defaulted with // "-". Indexing null returns null in jq (not an error), and null // "-""-":

$ echo '{"runs":[{"results":[{"level":"warning","ruleId":"slack-token"}]}]}' | \
    jq -r '.runs[].results[] | "| \(.level) | \(.ruleId) | \((.locations[0].physicalLocation.artifactLocation.uri // "-"))… |"'
| warning | slack-token | - |        # exit 0

So the ? operator would be redundant. Keeping the filter as-is (the // "-" defaults already make it null-safe).

Follow-up to the PR #13 review:
- action.yml: replace the job-summary `|| true` with a visible fallback note so a
  (theoretical) render failure isn't silently swallowed. The jq filter already
  handles location-less findings via `// "-"` (verified jq returns "-" without
  erroring), so the `?` operator is unnecessary.
- action-test.yml (cli-github-format): capture the exit code with an explicit
  if/else to make the semantics obvious. (The reviewer's `if ! out=…; then rc=$?`
  form is incorrect — it captures 0, not the real exit code.)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@cemililik
Copy link
Copy Markdown
Collaborator Author

Thanks @sourcery-ai. On the test-step exit-code capture: switched to an explicit if/else in 4f9fa2f to make the semantics obvious:

if out="$("${RUNNER_TEMP}/leakwatch")"; then rc=0; else rc=$?; fi

Heads-up on the suggested if ! out="$(…)"; then rc=$?; fi form — that one is subtly incorrect: after ! cmd, $? is the inverted status, so rc becomes 0 instead of the real exit code. Verified:

rc=0; if ! out="$(bash -c 'exit 7')"; then rc=$?; fi; echo $rc   # -> 0  (wrong)
rc=0; if   out="$(bash -c 'exit 7')"; then rc=0; else rc=$?; fi; echo $rc   # -> 7 (correct)

The if/then/else form keeps the clarity you asked for without that pitfall.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@action.yml`:
- Line 313: The current pipeline "jq ... | head -50 || echo ..." can trigger the
fallback due to a broken pipe even when jq produced output; instead run jq to a
temporary file first (e.g., produce the table into a temp artifact), then
display the first 50 lines from that file with "head -n 50" and only emit the
fallback if the temp file is empty or head produced no output—update the command
that uses jq and head (the "jq -r '... | head -50 || echo ...'") to a two-step
approach: write jq output to a temp file, then run head -n 50 on that temp file
and conditionally echo the fallback if the file is empty.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d16dad14-38a0-45c0-b244-0d02548cdf31

📥 Commits

Reviewing files that changed from the base of the PR and between 8a276e8 and 4f9fa2f.

📒 Files selected for processing (2)
  • .github/workflows/action-test.yml
  • action.yml

Comment thread action.yml Outdated
…e fallback

Piping jq into `head -50` gives jq a SIGPIPE once head closes after 50 lines;
under pipefail that non-zero status tripped the `|| echo fallback` even when the
table rendered fine (reproducible once jq's output exceeds the ~64KB pipe buffer,
i.e. very many findings). Write jq output to a temp file, then `head -n 50` it,
and emit the fallback only when jq fails or the file is empty.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@cemililik cemililik merged commit fd0a361 into main May 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant