Skip to content

Add stable exit-code contract and CI/CD documentation - #182

Closed
Mike Krüger (mkrueger) wants to merge 11 commits into
mainfrom
dev/mkrueger/setup-cosmosdb-shell-action
Closed

Mike Krüger (mkrueger) wants to merge 11 commits into
mainfrom
dev/mkrueger/setup-cosmosdb-shell-action

Conversation

@mkrueger

@mkrueger Mike Krüger (mkrueger) commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

Implements the exit-code contract and CI/CD documentation requested in #177, and includes the setup-cosmosdb-shell GitHub Action from the earlier work on this branch.

Exit-code contract (#176)

Non-interactive runs (-c, -k, or a script piped on stdin) now return stable, machine-readable process exit codes instead of always returning 1:

Code Meaning
0 Success
1 General failure
2 Authentication/authorization failure (401/403, failed Entra credential)
3 Connection/network error (socket failure, 503, request timeout)
4 Command/script syntax error (unparseable input, unrecognized CLI argument)
  • New ShellExitCode class centralizes the contract with FromException and FromCommandState classifiers.
  • Program.cs maps CLI parse errors, connect failures, and command/script results to these codes.
  • The interactive REPL still exits 0.

Documentation

docs/ci.md is expanded into a full CI/CD guide covering:

  • GitHub Actions workflow example (copy-paste ready) with azure/login OIDC
  • The formal exit-code contract
  • Authentication patterns for CI: workload identity federation (OIDC), managed identity, account key / static token
  • Failure-handling guidance: retry patterns, JSON output parsing, azd post-provision hooks, and binary caching

Linked from the README docs list.

Tests

  • ShellExitCodeTests — unit coverage for the classifier (auth, connection, syntax, general, nested exceptions, command-state mapping).
  • ShellProcessTests — end-to-end assertions that an unterminated string and an unrecognized CLI argument both exit with code 4.

Also included on this branch

  • setup-cosmosdb-shell composite GitHub Action (.github/actions/setup-cosmosdb-shell/) that downloads, verifies, caches, and PATH-adds the shell across Linux/macOS/Windows runners.

Validation

  • dotnet build CosmosDBShell.sln — 0 warnings, 0 errors
  • Exit-code and process tests pass (29 tests)

Addresses #177. Marketplace publishing under azure/setup-cosmosdb-shell (a separate repo) is documented but out of scope for this repo.

Composite action that resolves a release, downloads the per-runtime CosmosDBShell package, verifies its SHA256 digest, extracts the self-contained executable, caches it in the runner tool cache, and adds cosmosdbshell to PATH. Supports latest or a pinned version across Linux, macOS, and Windows on x64/arm64. Adds a CI/CD guide (docs/ci.md) and links it from the README.

Addresses #178
Introduce ShellExitCode with a stable, machine-readable process exit-code contract for non-interactive runs: 0 success, 1 general failure, 2 authentication/authorization failure, 3 connection/network error, 4 command/script syntax error. Program.cs now maps CLI parse errors, connect failures, and command/script results to these codes instead of always returning 1.

Document the contract, GitHub Actions usage, authentication patterns (OIDC, managed identity, key/token), and failure-handling guidance in docs/ci.md. Add unit tests for the classifier and end-to-end process tests for the syntax exit code. Update CHANGELOG.

Addresses #176
@github-code-quality

github-code-quality Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: C#

C# / code-coverage/dotnet

The overall coverage in the dev/mkrueger/setup-c... branch remains at 61%, unchanged from the main branch.

Show a code coverage summary of the most impacted files.
File main 4573ab3 dev/mkrueger/setup-c... 48c9c31 +/-
D:\a\CosmosDBSh...hell\Program.cs 72% 75% +3%
D:\a\CosmosDBSh...hellExitCode.cs 0% 97% +97%

Updated July 20, 2026 07:02 UTC
Code Coverage is in Public Preview. Learn more and provide us with your feedback.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a stable, machine-readable exit-code contract for non-interactive CosmosDBShell runs and introduces CI/CD-focused documentation and tooling (including an in-repo setup action) to make the shell easier to consume from automation.

Changes:

  • Introduces ShellExitCode and wires Program.cs to return stable exit codes for CLI parse errors, connection failures, and command/script execution results.
  • Adds a full CI/CD guide (GitHub Actions examples, auth patterns, retries, JSON parsing) and links it from the README and changelog.
  • Adds a setup-cosmosdb-shell composite GitHub Action plus unit/process tests for exit-code behavior.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
README.md Adds a docs link to the new CI/CD guide.
docs/ci.md New CI/CD guide documenting installation, auth patterns, exit codes, and scripting guidance.
CosmosDBShell/Program.cs Maps key non-interactive failure modes to stable exit codes via ShellExitCode.
CosmosDBShell/Azure.Data.Cosmos.Shell.Core/ShellExitCode.cs Centralized exit-code contract and classifiers (FromException, FromCommandState).
CosmosDBShell.Tests/Runtime/ShellExitCodeTests.cs Unit tests for exception/state-to-exit-code classification.
CosmosDBShell.Tests/Integration/ShellProcessTests.cs Process-level assertions for syntax/argument exit-code behavior.
CHANGELOG.md Documents the new exit-code contract and links to CI/CD guide.
.github/actions/setup-cosmosdb-shell/README.md Documents usage and behavior of the composite setup action.
.github/actions/setup-cosmosdb-shell/action.yml Implements the composite action to download/cache/install the shell from releases.
Comment thread CosmosDBShell/Program.cs Resolved
Comment thread docs/ci.md Resolved
- Map all CLI/argument validation failures (invalid --otel endpoint, mutually-exclusive -c/-k, invalid --mcp port) to the syntax exit code; classify MCP runtime failures via ShellExitCode.FromException.
- Add a gh preflight check to the setup action with an actionable self-hosted-runner message.
- Recommend pinning the action ref (tag/commit) in the action README and docs/ci.md.
- Add a process test asserting a malformed --otel endpoint returns exit code 4.
Copilot AI review requested due to automatic review settings July 16, 2026 12:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Comment thread docs/ci.md Resolved
Comment thread docs/ci.md Resolved
- Pin actions/checkout to v4 in both docs/ci.md workflow examples.
- Add a RequestFailedException 503/408/504 connection-error unit test for the exit-code classifier.
Copilot AI review requested due to automatic review settings July 16, 2026 12:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment thread docs/ci.md Resolved
Reword the OIDC section so endpoint-only connection is clearly the only requirement and the --connect-* options are optional for deterministic startup.
Copilot AI review requested due to automatic review settings July 16, 2026 12:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.

- action.yml: use github.token fallback via expressions (input default cannot evaluate expressions); drop the warning when a release asset has no digest.
- action README: soften SHA256 verification wording to reflect optional digest.
- ShellExitCode: classify Cosmos-like OperationCanceledException timeouts as ConnectionError, with unit tests.
Copilot AI review requested due to automatic review settings July 16, 2026 12:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread docs/ci.md Resolved
- docs/ci.md: clarify that only the -c/-k/stdin-free REPL always exits 0, and that -k preserves the initial command's exit code.
- action README: document PowerShell and gh runner prerequisites for self-hosted runners.
Copilot AI review requested due to automatic review settings July 16, 2026 13:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Use CosmosShell.Tests.Runtime to match the other tests under CosmosDBShell.Tests/Runtime.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread CosmosDBShell/Program.cs Resolved
Comment thread docs/ci.md Resolved
Copilot AI review requested due to automatic review settings July 16, 2026 13:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

- Program.cs: set GeneralFailure exit code when the initial --connect is cancelled so CI detects aborted startup.
- docs/ci.md: clarify REPL exits 0 only when reached and ending normally; startup failures still set non-zero codes.
- action README: correct the token input default (empty) and document the github.token fallback.
Copilot AI review requested due to automatic review settings July 16, 2026 14:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment thread docs/ci.md Resolved
Reference the COSMOSDB_SHELL_FORMAT env var and the per-command -f/--format option instead of the unrelated filter command.
Copilot AI review requested due to automatic review settings July 16, 2026 14:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 20, 2026 06:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

@mkrueger

Copy link
Copy Markdown
Collaborator Author

#173 already contains it.

Mike Krüger (mkrueger) added a commit to KhushviB/CosmosDBShell that referenced this pull request Aug 10, 2026
Fold the composite GitHub Action and docs/ci.md into the deterministic-output PR. Align the exit-code table with the unified 0-6 ShellExitCode contract, drop COSMOSDB_SHELL_FORMAT guidance, and link README/navigation/CHANGELOG.
@mkrueger

Copy link
Copy Markdown
Collaborator Author

Superseded by #173.

The exit-code contract now lives on #173 as a unified 0–6 map via ShellExitCode (usage=2, auth=3, connection=4, not-found=5, throttle=6). The setup-cosmosdb-shell action and docs/ci.md were ported onto that PR and rewritten against the new numbers (including dropping COSMOSDB_SHELL_FORMAT in favor of --output json).

Closing this PR to avoid thrashing Program.cs / README / tests with the older numbering. Tracking continues on #173.

@mkrueger

Copy link
Copy Markdown
Collaborator Author

Closing as superseded by #173 (deterministic output + unified 0-6 exit codes + setup action/CI docs).

Mike Krüger (mkrueger) added a commit that referenced this pull request Aug 25, 2026
…setup action (#173)

## Description

Implements the deterministic machine-output contract (Wave 1, P0 / #155)
plus the stable process exit-code contract and CI install path requested
in #176 / #177. Supersedes #182 by folding its remaining extras
(`setup-cosmosdb-shell` action + `docs/ci.md`) onto this branch with the
unified exit map.

The shell can be safely consumed by CI/CD pipelines, automation tools,
and agentic wrappers without output corruption or ambiguous failure
states.

## Key Changes

### 1. Global output controls
- **`--output` (`-o`)** global option with four formats: `user`, `json`,
`table`, `csv` (aliases `js`, `tbl`). Tokens are case-insensitive and
canonicalized in one place.
- **`--quiet` (`-q`)** global option that suppresses banners, connection
logs, and other informational chatter.
- **Per-command `--format`** is honored alongside the global option;
when a command sets it explicitly it wins, otherwise the session default
(from `--output`) is applied once at print time.
- **`COSMOSDB_SHELL_FORMAT`** remains supported as a format-only
fallback, checked after `--output`/`--format` when neither is supplied.
Unlike `--output`, setting it never enters machine mode on its own (no
banner/color suppression), so it's safe to export in an interactive
shell profile. See `docs/navigation.md`/README for the precedence order.

### 2. Output-rendering architecture (the core refactor)
Replaced the old single `IsPrinted` "already printed" flag with a
two-delegate rendering model on `CommandState`:
- **`RenderUser`** — optional human-facing renderer. Invoked **only**
for `user` format on an interactive terminal; never for redirected,
piped, or machine-mode output.
- **`RenderTabular`** — optional `TabularData` provider that feeds
**both** CSV and Table from one source.
- A structured `Result` (`ShellJson` / `ShellText`) is always available
for `json`/machine consumption independent of the interactive renderer.
- **~30 commands migrated** to this model (`list`, `dir`, `info`,
`sproc`, `udf`, `trigger`, `throughput`, `bucket`, `theme`, `connect`,
`disconnect`, `help`, `watch`, delete/create family, `pwd`, `cls`,
`exit`, `edit`, `ftab`, `filter`, and more).

### 3. Centralized policy
- **`OutputPolicy`** — shared machine-mode / structured-format decisions
for startup and runtime.
- **`OutputFormats.TryParse`** — canonical format vocabulary/aliases.
- Structured vs human: `json`/`csv` are machine formats; `user`/`table`
are human-facing.

### 4. Unified process exit codes (`ShellExitCode`)
Classification is centralized in `ShellExitCode.FromException` /
`FromCommandState` (used by `ErrorCommandState`,
`ParserErrorCommandState`, and `Program`):

| Code | Constant | Meaning |
| ---- | -------- | ------- |
| `0` | `Success` | OK |
| `1` | `GeneralFailure` | Catch-all |
| `2` | `UsageError` | CLI parse, script syntax, bad/unknown args |
| `3` | `AuthFailure` | `401`/`403`, `AuthenticationFailed`,
`CredentialUnavailable` |
| `4` | `ConnectionError` | HTTP/socket/timeout, `503`/`408`/`504`,
Cosmos timeout messages |
| `5` | `NotFound` | `404` |
| `6` | `Throttled` | `429` / RU budget exceeded |

Public contract: existing codes are never repurposed; new categories get
new codes. Codes propagate to `%ERRORLEVEL%` / `$LASTEXITCODE`.

### 5. Machine-mode protections
- Entered by `--quiet`, structured `--output json|csv`, or bare `-c`
(defaults to JSON).
- ANSI disabled; structured formats behave as quiet so STDOUT stays
clean.
- Startup/execution errors emit `{"status":"error","error":"..."}` on
**STDERR** in machine mode (honors `2>` redirection).

### 6. CI install path (ported from #182)
- **`.github/actions/setup-cosmosdb-shell`** composite action:
download/cache self-contained release binary, add `cosmosdbshell` to
`PATH` (no .NET SDK on the runner).
- **`docs/ci.md`** — install guide, exit-code contract (0–6), CI auth
patterns, retry guidance.
- README / navigation links to the CI guide and setup action.

## Tests
- **`OutputPolicyTests`** / expanded **`OutputFormatTests`**
- **`ShellExitCodeTests`** — unit coverage for the 0–6 classifier (auth
vs connection, not-found, throttle, usage, wrappers)
- **`ShellProcessTests`** — process-level exit codes, ANSI-free STDOUT,
JSON-on-STDERR
- **`OutputRedirectionTests`** — stdout/stderr redirection in machine
mode

## Docs
- `docs/navigation.md` — machine mode + exit table
- `docs/ci.md` — CI install, auth, failure handling
- `README.md` — `--output`/`--quiet`, exit table, setup action
- `.github/actions/setup-cosmosdb-shell/README.md`
- `CHANGELOG.md` Unreleased entries

## Validation
- [x] `-c` / structured `--output json|csv` produce parseable machine
output; bare `-c` defaults to JSON
- [x] No ANSI in machine modes
- [x] Stable exit codes 0–6 (usage=2, auth=3, connection=4, not-found=5,
throttle=6)
- [x] Parser / bad CLI args → exit 2
- [x] Connection failure in machine mode → exit 4 + JSON on STDERR
- [x] Setup action + CI docs land on this PR; #182 superseded

## Supersedes
Closes the remaining scope of #182 (setup action + CI docs). Exit-code
numbering here **replaces** #182's earlier 0–4 map — do not merge #182
after this.

## Follow-ups already applied on branch
- `RenderTabular` cleared at statement boundaries
- `connect <endpoint>` success payload normalized to `{ "connected":
true, "endpoint": "..." }`
- Merged latest `main`; welcome command reconciled with `RenderUser`
- Unified `ShellExitCode` helper (auth ≠ connection; keep
NotFound/Throttled)
Mike Krüger (mkrueger) added a commit that referenced this pull request Aug 25, 2026
## Summary

- stamp the changelog for `1.1.190-preview` on 2026-08-25
- add the missing entries for MCP destructive-command confirmation,
preview startup warnings, framework-dependent NuGet packaging, and
CodeQL default setup
- move the MCP elicitation entry from the historical `1.1.136-preview`
section into this release
- remove the superseded #182 reference

## Validation

- `git diff --check origin/main...HEAD`
- audited merged PRs since `1.1.150-preview`
@mkrueger
Mike Krüger (mkrueger) deleted the dev/mkrueger/setup-cosmosdb-shell-action branch September 2, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants