refactor(managementapi): let remote rows reach the statistics format handlers (S17 repository statistics on remote repositories plan: 4/8)

Summary

  • Problem: the repository statistics route answers 404 for remote repositories. Its first check keeps the kind as a hosted bool, so it cannot let remote rows through without also letting virtual rows through.
  • Impact: nothing changes for users yet. Every remote answer is still the same 404, byte for byte. This MR prepares Steps 5, 6, and 7, which turn on remote figures one format at a time.
  • Change:
    • A new type, repositoryStatisticsKind, holds three values: refused, hosted, and remote. Its zero value is refused. This matters because hosted is kind 0, so an empty value would otherwise read as hosted. Only repositoryStatisticsScopeFor sets it.
    • The first check refuses only the zero value, so remote rows now reach the format handlers.
    • Each format handler (serveContainerRepositoryStatistics, writeMavenRepositoryStatistics, writeNpmRepositoryStatistics) now starts with a switch on the kind. Hosted runs today's code, which moved unchanged into a *Hosted* function. Remote calls writeRepositoryNotFound. Any other value answers the contract-violation 500.
    • One answer changes. A remote row with an unknown format now gets the contract-violation 500, as a hosted row already did. The check_repositories_format constraint keeps such rows out of the database.
  • Downsides: 115 source lines (added plus removed), against the plan's forecast of about 50. Most of it is the three small kind switches and the moved function headers.

Checklist

  • Ran it: n/a, because no client sees a change: every remote answer is the same 404 as before. I ran the package unit suite and the statistics integration tests against PostgreSQL 17. TestRepositoryStatisticsIntegration_KindGate sends a real remote Maven row through the router, and it still gets the byte-identical 404.
  • Interfaces and data: none change. No API, configuration, schema, or storage format. The only new answer is the 500 above, for a row the database does not allow. The spec already describes remote statistics. The API text changes in Step 8.
  • Failure: a format handler that gets a kind it does not serve logs a contract violation with the repository id and answers 500. Only a scope built without the constructor can carry such a kind, and the first check stops the zero value before any handler runs.
  • Rollout: nothing to coordinate. No schema or configuration change.
  • Hot path: no new query. One extra switch per request.
  • Database: none.
  • Security: no new input, route, or permission. The route keeps read_repository. A remote row still gets the existence-hiding 404, byte for byte, and no count runs for it, so nothing new is disclosed.
  • ADRs: ADR-009 (the statistics route has no kind limit), ADR-021 (statistics need read_repository), and ADR-007 (the remote cache tables, which this MR does not read yet). The change holds all three. scripts/adr-freshness.sh reports the mirror one handbook commit behind: the Go modules amendment to ADR-004, ADR-007, ADR-009, and ADR-010. That amendment does not touch statistics.
  • E2E: none. e2e.maven.setup.repository-statistics still expects the remote 404, which this MR keeps. Step 5 changes that row.
  • Simplicity: I rejected a second remote bool next to hosted, because two bools can say "both", which means nothing. I also rejected an empty hosted case with early returns for the rest, because Steps 5 to 7 would then grow their remote code inside a case.

Tests

  • statistics_repository_internal_test.go (new) checks how the constructor maps each kind and that the zero scope is refused. It also calls each format handler with the zero value, and with a value no constant names, and expects the 500.
  • The container and npm suites now compare their remote and virtual answers byte for byte with assertMatchesStatisticsGateBaseline, as the Maven suite already did.
  • TestRepositoryStatistics_UnmappedFormat_Returns500 gets a remote row. It is the test that fails if the first check refuses remote again.
  • The remote rows leave TestRepositoryStatistics_KindGate and TestRepositoryStatistics_UnmappedKind_IsTheOnlyKindThatLogs, as the plan says. Each format's own suite covers remote.
  • I broke each new branch in a scratch copy and ran the suite. All 12 changes failed it: the first check refusing remote, the constructor dropping remote or passing virtual, each of the 3 handlers losing its default, each of the 3 keeping only the zero-value case, and each of the 3 remote cases running the hosted code.

Beyond the plan's file list

  • TestStatisticsContainerRepository_NonHostedKindsNeverReachTheArm is renamed to TestStatisticsContainerRepository_NonHostedKindsNeverReachTheCounts, because remote rows now reach the arm.
  • Two case names in contract_test.go and one message in statistics_maven_repository_test.go called the gate hosted-only. They now say what the gate does.

Merge order

None needed. No format serves remote before its own step lands. Steps 5, 6, and 7 wait for this MR. Of the open MRs, only one other touches these files (handler.go), and it merges cleanly with this branch.

Closes #1670 (closed)

Merge request reports

Loading
Loading