M-2 PR-C: Collapse Policy/Profile/Owner/Team services to ctx-first signatures

- Add ctx first param to 21 service-layer handler-interface methods
  across policy.go (6), profile.go (5), owner.go (5), team.go (5)
- Replace 24 context.Background() call sites with received ctx; use
  context.WithoutCancel(ctx) for subsidiary audit-recording ops to
  preserve fire-and-forget audit semantics without inheriting caller
  cancellation
- Add ctx first param to 21 handler-interface method signatures across
  policies.go (6), profiles.go (5), owners.go (5), teams.go (5)
- Thread r.Context() through 21 HTTP handler sites (ListPolicies,
  GetPolicy, CreatePolicy, UpdatePolicy, DeletePolicy, ListViolations,
  ListProfiles, GetProfile, CreateProfile, UpdateProfile, DeleteProfile,
  ListOwners, GetOwner, CreateOwner, UpdateOwner, DeleteOwner,
  ListTeams, GetTeam, CreateTeam, UpdateTeam, DeleteTeam)
- Update MockPolicyService/MockProfileService/MockOwnerService/
  MockTeamService mock method impls with _ context.Context first param
  (Fn fields unchanged — closures do not need ctx); update mock impls
  in integration/lifecycle_test.go for all four services
- Update 12 service-layer test callsites (policy_test.go ×2,
  owner_test.go ×5, team_test.go ×5, profile_test.go ×13) to pass
  context.Background() at the call site

Audit complete. Commit: 855124a9d9. Sections: 12. Findings: 2/7/10/4/6.
This commit is contained in:
Shankar
2026-04-18 01:10:06 +00:00
parent f7a668caa4
commit e5a7b4585c
17 changed files with 156 additions and 148 deletions
+12 -12
View File
@@ -230,7 +230,7 @@ func (s *PolicyService) ListViolationsWithContext(ctx context.Context, filter *r
}
// ListPolicies returns paginated policies (handler interface method).
func (s *PolicyService) ListPolicies(page, perPage int) ([]domain.PolicyRule, int64, error) {
func (s *PolicyService) ListPolicies(ctx context.Context, page, perPage int) ([]domain.PolicyRule, int64, error) {
if page < 1 {
page = 1
}
@@ -238,7 +238,7 @@ func (s *PolicyService) ListPolicies(page, perPage int) ([]domain.PolicyRule, in
perPage = 50
}
rules, err := s.policyRepo.ListRules(context.Background())
rules, err := s.policyRepo.ListRules(ctx)
if err != nil {
return nil, 0, fmt.Errorf("failed to list policies: %w", err)
}
@@ -264,12 +264,12 @@ func (s *PolicyService) ListPolicies(page, perPage int) ([]domain.PolicyRule, in
}
// GetPolicy returns a single policy (handler interface method).
func (s *PolicyService) GetPolicy(id string) (*domain.PolicyRule, error) {
return s.policyRepo.GetRule(context.Background(), id)
func (s *PolicyService) GetPolicy(ctx context.Context, id string) (*domain.PolicyRule, error) {
return s.policyRepo.GetRule(ctx, id)
}
// CreatePolicy creates a new policy (handler interface method).
func (s *PolicyService) CreatePolicy(policy domain.PolicyRule) (*domain.PolicyRule, error) {
func (s *PolicyService) CreatePolicy(ctx context.Context, policy domain.PolicyRule) (*domain.PolicyRule, error) {
if policy.ID == "" {
policy.ID = generateID("rule")
}
@@ -277,30 +277,30 @@ func (s *PolicyService) CreatePolicy(policy domain.PolicyRule) (*domain.PolicyRu
policy.CreatedAt = time.Now()
}
if err := s.policyRepo.CreateRule(context.Background(), &policy); err != nil {
if err := s.policyRepo.CreateRule(ctx, &policy); err != nil {
return nil, fmt.Errorf("failed to create policy: %w", err)
}
return &policy, nil
}
// UpdatePolicy modifies a policy (handler interface method).
func (s *PolicyService) UpdatePolicy(id string, policy domain.PolicyRule) (*domain.PolicyRule, error) {
func (s *PolicyService) UpdatePolicy(ctx context.Context, id string, policy domain.PolicyRule) (*domain.PolicyRule, error) {
policy.ID = id
policy.UpdatedAt = time.Now()
if err := s.policyRepo.UpdateRule(context.Background(), &policy); err != nil {
if err := s.policyRepo.UpdateRule(ctx, &policy); err != nil {
return nil, fmt.Errorf("failed to update policy: %w", err)
}
return &policy, nil
}
// DeletePolicy removes a policy (handler interface method).
func (s *PolicyService) DeletePolicy(id string) error {
return s.policyRepo.DeleteRule(context.Background(), id)
func (s *PolicyService) DeletePolicy(ctx context.Context, id string) error {
return s.policyRepo.DeleteRule(ctx, id)
}
// ListViolations returns policy violations with pagination (handler interface method).
func (s *PolicyService) ListViolations(policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error) {
func (s *PolicyService) ListViolations(ctx context.Context, policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error) {
if page < 1 {
page = 1
}
@@ -313,7 +313,7 @@ func (s *PolicyService) ListViolations(policyID string, page, perPage int) ([]do
PerPage: 1000, // Get all violations for the policy
}
violations, err := s.policyRepo.ListViolations(context.Background(), filter)
violations, err := s.policyRepo.ListViolations(ctx, filter)
if err != nil {
return nil, 0, fmt.Errorf("failed to list violations: %w", err)
}