Skip to content

refactor: restructure packages, add chi router, and improve test coverage#26

Merged
sgtoj merged 3 commits intomainfrom
dev
Feb 18, 2026
Merged

refactor: restructure packages, add chi router, and improve test coverage#26
sgtoj merged 3 commits intomainfrom
dev

Conversation

@sgtoj
Copy link
Contributor

@sgtoj sgtoj commented Feb 18, 2026

Summary

  • Restructure internal packages: extract domain types/interfaces/errors into internal/domain/, move sync logic into internal/sync/, and consolidate the App factory into internal/app/factory.go
  • Replace custom Request/Response abstraction with chi router: all entry points (server, lambda, verify, sample) now construct stdlib *http.Request objects and route through a shared http.Handler returned by App.Handler(), with proper middleware for admin auth and panic recovery
  • Fix correctness bugs: domain error-to-HTTP mapping (errors.Is() instead of errors.HasType()), GetOrCreateTeam error classification, sync rule failure detection, PR review deduplication, Okta pagination, and token refresh race condition
  • Significantly improve test coverage: internal/app 24.5% → 55.8%, internal/config 14.8% → 74.1%, internal/notifiers 2.7% → 97.3%; add integration test hardening (dynamic ports, env restore, unexpected-call validation)

Commits

  • 4fa3862 — restructure packages, fix bugs, and improve test coverage
  • 2dc32b5 — improve dx (Dockerfile, compose.yaml, Makefile, dependabot)
  • 7a620fa — replace custom request/response types with chi router

Reorganize internal packages into domain/sync layers, fix multiple
correctness bugs, harden the integration test infrastructure, and
add comprehensive unit tests across all major packages.

Architecture:
- Extract shared App factory into internal/app/factory.go, removing
  duplicated newApp() from all four cmd/ entry points
- Move domain types/interfaces/errors into internal/domain/
- Move sync logic into internal/sync/ with full unit tests
- Add context.Context to OktaClient interface, remove stored-context
  anti-pattern
- Add compile-time interface assertions for GitHubClient and OktaClient
- Remove unused GitHubClientFactory and cross-installation code path

Bug fixes:
- Fix mapErrorResponse: errors.HasType() never matched errors.Mark()
  markers; switch to errors.Is() so domain errors return correct HTTP
  status codes (401/400/503/502) instead of always 500
- Fix GetOrCreateTeam: non-404 errors (403, 500) were misclassified as
  ErrTeamNotFound
- Fix sync all-rules-failed detection: compared against len(reports)
  instead of tracking enabled rule count separately
- Fix PR review deduplication: count only latest review per user
- Add GitHub Check Runs support alongside legacy commit statuses
- Fix token refresh TOCTOU race with double-check pattern
- Add Okta API pagination to ListGroups, GetGroupByName, GetGroupMembers
- Normalize username case in team membership comparisons

Test infrastructure:
- Replace hardcoded ports (9001-9003) with dynamic TLS listeners
- Add IP SANs to self-signed test certificates
- Add env var save/restore between integration test scenarios
- Add unexpected-destructive-call validation
- Fix test log handler Enabled() to respect verbose flag
- Add request body size limit (10MB) to HTTP server

Test coverage (before -> after):
- internal/app: 24.5% -> 55.8%
- internal/config: 14.8% -> 74.1%
- internal/notifiers: 2.7% -> 97.3%
- internal/sync: 82.8% -> 83.3%

Cleanup:
- Remove all fmt.Errorf usage, consistently use cockroachdb/errors
- Remove dead ErrOAuthTokenExpired sentinel
- Extract extractGroupName() helper in okta package
- Pre-compile regex in sync.go computeTeamName
- Trim leading/trailing dashes from generated team names
Eliminate the hand-rolled Request/Response abstraction layer in favor of
a standard net/http + chi router. All entry points (server, lambda,
verify, sample) now construct stdlib *http.Request objects and route them
through a shared chi handler returned by App.Handler(). This removes the
custom HandleRequest dispatcher, consolidates routing and middleware
(admin auth, panic recovery), and makes tests more realistic by
exercising the actual router via httptest.
@sgtoj sgtoj merged commit 137ae3a into main Feb 18, 2026
4 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