Skip to content

fix(cli): scope global params from env#2827

Merged
mergify[bot] merged 2 commits into
mainfrom
fix/issue-2389-global-scope
Jun 7, 2026
Merged

fix(cli): scope global params from env#2827
mergify[bot] merged 2 commits into
mainfrom
fix/issue-2389-global-scope

Conversation

@tmchow

@tmchow tmchow commented Jun 7, 2026

Copy link
Copy Markdown
Owner

Summary

  • classify API-wide string scope parameters as env-backed global scope, including parameters that were already required in the source spec
  • generate endpoint commands that default required global scope flags from canonical env vars while preserving explicit flag precedence
  • inject the same env/default global scope values into generated sync user params so sync can populate scoped resources

Closes #2389

Verification

  • go test ./...
  • go test ./internal/generator -run TestGenerateGlobalScopeParamDefaultsFromEnv -count=1
  • go test ./internal/openapi ./internal/profiler -run 'TestPromoteGlobalScopeQueryParams|TestParsePreservesRequiredQueryParamsDuringGlobalFilter|TestProfile' -count=1\n- scripts/verify-generator-output.sh\n- scripts/golden.sh verify\n- go build -o ./cli-printing-press ./cmd/cli-printing-press\n- golangci-lint run ./...

@mergify

mergify Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 require-ready-label-and-ci

Wonderful, this rule succeeded.
  • #changes-requested-reviews-by = 0
  • #review-threads-unresolved = 0
  • check-success = build-and-test
  • check-success = generated-test
  • check-success = go-lint
  • check-success = golden
  • check-success = pr-title
  • check-success = test
  • any of:
    • label = ready-to-merge
    • all of:
      • head = release-please--branches--main
      • title ~= ^chore\(main\): release
  • any of:
    • -files ~= ^(\.github/workflows/|\.github/scripts/|scripts/|\.github/CODEOWNERS$)
    • author = tmchow
    • approved-reviews-by = mvanhorn
    • approved-reviews-by = tmchow
    • author = mvanhorn
  • any of:
    • check-success = Greptile Review
    • label = queued
    • check-neutral = Greptile Review
    • check-skipped = Greptile Review
    • head ~= ^mergify/merge-queue/
    • all of:
      • head = release-please--branches--main
      • title ~= ^chore\(main\): release

@greptile-apps

greptile-apps Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR promotes API-wide string scope parameters (e.g. workspaceId, TenantFilter) to env-backed global scope, allowing generated CLIs to satisfy required scope flags via canonical env vars while preserving explicit flag precedence. It also injects the same env defaults into sync's syncUserParams so that sync commands respect the same env-var convention.

  • classifyGlobalParams (parser.go): The counting pass now uses isPathSubstitutionParam instead of isGlobalFilterCandidate, so originally-required string params with scope-like names can reach the 50% threshold and be promoted; the filtering pass promotes them first, then falls through to isGlobalFilterCandidate only for the drop path.
  • command_endpoint.go.tmpl: Scope param flags are registered with globalScopeParamDefault(envName, fallback) as their default, and the required-flag guard adds && flagValue == \"\" so the env var silently satisfies the check without an explicit CLI flag.
  • helpers.go.tmpl / sync.go.tmpl: setGlobalDefault writes into flatGlobal (not trueGlobal), preserving the flat/dependent split — env-backed scope defaults apply to flat-list requests but are intentionally not injected into dependent path-scoped requests, preventing the Asana-style "duplicate scope param" rejection. The inline TestSyncGlobalScopeEnvDefaultsUseFlatGlobal test embedded in the integration test explicitly verifies this boundary.

Confidence Score: 5/5

Safe to merge. The flatGlobal/trueGlobal boundary is preserved correctly and is now explicitly tested by an embedded helper test.

The key concern from the prior review thread — that env-backed scope defaults might land in trueGlobal and silently break dependent-request calls on Asana-style APIs — is demonstrably addressed: setGlobalDefault writes into flatGlobal, and the new TestSyncGlobalScopeEnvDefaultsUseFlatGlobal helper test verifies that dependent requests do not receive the scope param from env defaults. The parser change widening the counting phase to include required params is intentional and covered by the updated tests. No credential-path reads are introduced.

No files require special attention.

Important Files Changed

Filename Overview
internal/openapi/parser.go Widens the counting phase from isGlobalFilterCandidate to isPathSubstitutionParam so originally-required scope-named params can be promoted to GlobalScope; filtering pass restructured to promote scope params first.
internal/generator/templates/helpers.go.tmpl Adds globalScopeParamDefault helper and setGlobalDefault method targeting flatGlobal (not trueGlobal), preserving the flat/dependent split.
internal/generator/templates/sync.go.tmpl Emits applySyncGlobalScopeEnvDefaults after parseSyncUserParams so explicit --param/--global-param flags take precedence over env-backed defaults.
internal/generator/templates/command_endpoint.go.tmpl Registers scope param flags with globalScopeParamDefault as default; required-flag guard updated to !Changed && value=='' so env var satisfies the check.
internal/generator/generator.go Adds globalScopeParams, globalScopeEnvName, globalScopeFallbackValue, and paramHasEnvDefault helpers; endpointHasRequiredInput skips env-default params.
internal/generator/global_scope_param_test.go End-to-end test covering env-default flag registration, missing-env error path, env-backed list and sync execution, with inline helper test verifying flatGlobal/trueGlobal boundary.
internal/openapi/parser_test.go Adds required workspaceId param to verify originally-required scope-named params are classified as GlobalScope.
internal/profiler/profiler.go Excludes env-backed GlobalScope string params from hasRequiredScopeParams so the profiler does not treat them as hard-required inputs.

Sequence Diagram

sequenceDiagram
    participant Env as OS Env
    participant Cmd as Generated CLI cmd
    participant Sync as sync RunE
    participant SP as syncUserParams
    participant API as HTTP API

    Note over Cmd: cobra flag registration (startup)
    Env-->>Cmd: os.Getenv(SCOPE_VAR)
    Cmd->>Cmd: globalScopeParamDefault sets flag default

    Note over Cmd: RunE (list command)
    Cmd->>Cmd: check !Changed and flagValue empty
    Cmd->>API: "GET /users?ScopeParam=tenant-a (flat)"

    Note over Sync: RunE (sync command)
    Sync->>SP: parseSyncUserParams
    Sync->>SP: applySyncGlobalScopeEnvDefaults
    SP->>Env: globalScopeParamDefault(SCOPE_VAR)
    Env-->>SP: tenant-a
    SP->>SP: setGlobalDefault into flatGlobal

    Sync->>API: "GET /resource?ScopeParam=tenant-a (isDependent=false)"
    Note over Sync,API: flatGlobal applies

    Sync->>API: "GET /parent/{id}/children (isDependent=true)"
    Note over Sync,API: flatGlobal skipped - no duplicate scope param
    Note over Sync,API: trueGlobal via --global-param still applies
Loading

Reviews (2): Last reviewed commit: "fix(cli): scope env sync defaults to fla..." | Re-trigger Greptile

Comment thread internal/generator/templates/helpers.go.tmpl
@tmchow tmchow added the ready-to-merge Allow Mergify to queue and merge this PR when protections pass label Jun 7, 2026
mergify Bot added a commit that referenced this pull request Jun 7, 2026
@mergify mergify Bot added the queued PR is in the Mergify merge queue label Jun 7, 2026
mergify Bot added a commit that referenced this pull request Jun 7, 2026
mergify Bot added a commit that referenced this pull request Jun 7, 2026
@mergify mergify Bot merged commit 95b099e into main Jun 7, 2026
31 checks passed
@mergify

mergify Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Merge Queue Status

This pull request spent 22 minutes 12 seconds in the queue, including 21 minutes 54 seconds running CI.

Required conditions to merge

@mergify mergify Bot deleted the fix/issue-2389-global-scope branch June 7, 2026 23:40
@mergify mergify Bot removed the queued PR is in the Mergify merge queue label Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Allow Mergify to queue and merge this PR when protections pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

generator: sync (and endpoint commands) must scope API-wide required params from config

1 participant