Skip to content

Move PostJSONWithTimeout to platform/http package and activity cleanup - #40561

Merged
getvictor merged 2 commits into
mainfrom
victor/38536-activity-cleanup
Feb 26, 2026
Merged

Move PostJSONWithTimeout to platform/http package and activity cleanup#40561
getvictor merged 2 commits into
mainfrom
victor/38536-activity-cleanup

Conversation

@getvictor

@getvictor getvictor commented Feb 25, 2026

Copy link
Copy Markdown
Member

Related issue: Resolves #38536

  • Moved PostJSONWithTimeout to platform/http
  • Created platform/errors package with only types needed by ctxerr. This way, ctxerr did not need to import fleethttp.
  • Made activity bounded context use PostJSONWithTimeout directly
  • Removed some activity types from legacy code that were no longer needed

Checklist for submitter

  • Changes file added for user-visible changes in changes/, orbit/changes/ or ee/fleetd-chrome/changes.
    • Changes file 38536-new-activity-bc already present, and this is just cleanup from that work.

Testing

  • Added/updated automated tests
  • QA'd all new/changed functionality manually

Summary by CodeRabbit

Release Notes

  • Refactor
    • Reorganized error handling utilities for improved clarity and decoupling.
    • Consolidated HTTP utilities to centralize JSON posting functionality with timeout support.
    • Simplified activity service initialization by removing unused internal parameters.
    • Cleaned up test utilities and removed webhook-related test scaffolding.

@getvictor

Copy link
Copy Markdown
Member Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Feb 25, 2026

Copy link
Copy Markdown
Contributor
✅ Actions performed

Full review triggered.

@codecov

codecov Bot commented Feb 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.67925% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.29%. Comparing base (e8152e5) to head (c351674).
⚠️ Report is 60 commits behind head on main.

Files with missing lines Patch % Lines
server/platform/http/post_json.go 79.31% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #40561      +/-   ##
==========================================
- Coverage   66.29%   66.29%   -0.01%     
==========================================
  Files        2466     2467       +1     
  Lines      197506   197488      -18     
  Branches     8655     8655              
==========================================
- Hits       130935   130920      -15     
+ Misses      54727    54723       -4     
- Partials    11844    11845       +1     
Flag Coverage Δ
backend 68.17% <88.67%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai

coderabbitai Bot commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This PR refactors the activity service architecture by removing the injected webhook send function from the activity bootstrap layer. The webhookSendFn parameter is removed from the bootstrap New constructor, and webhook delivery now directly calls platformhttp.PostJSONWithTimeout instead of using an injected callback. Supporting changes consolidate error types and HTTP utilities into platform packages: error classification interfaces (NotFoundError, ErrWithIsClientError) are moved from platform/http to a new platform/errors package, and HTTP utility functions are centralized in platform/http/post_json.go. Multiple callers—including bootstrap, service constructors, and test utilities—are updated to work with the simplified signatures. Several public types and interfaces are removed from server/fleet/activities.go including ActivityWebhookPayload, ActivityHosts, and optional activity interfaces. Test scaffolding related to webhook interactions is removed from activity testing files.

Possibly related PRs

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 68.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description is minimal but adequately addresses the purpose: moving PostJSONWithTimeout to platform/http and removing legacy activity types. However, it lacks critical details about testing and validation. Expand description to explain why these changes are beneficial, describe test coverage in detail, and confirm no regressions were found in manual QA.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately reflects the main changes: relocating PostJSONWithTimeout to platform/http and performing activity-related cleanup by removing webhook dependencies and simplifying the service architecture.
Linked Issues check ✅ Passed The PR addresses multiple objectives from issue #38536: moving PostJSONWithTimeout to platform (task 5), removing webhook-related code from activity service (cleanup), and simplifying the activity bounded context by removing custom webhook injection patterns.
Out of Scope Changes check ✅ Passed All changes align with the linked issue objectives. The refactoring moves HTTP utilities to platform/http, removes legacy webhook infrastructure from the activity service, and updates related error handling to use the new platform/errors package.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch victor/38536-activity-cleanup

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🧹 Nitpick comments (3)
server/platform/arch_test.go (1)

74-82: Comment label "Other infra packages" is slightly misleading

pkg/fleethttp (line 80) is grouped under "Other infra packages" but appears after the "Platform packages" block, which is fine structurally. Just a minor readability note — consider renaming the comment to "Infra packages" (matching the convention used in TestPlatformPackageDependencies and TestEndpointerPackageDependencies) for consistency.

✏️ Suggested comment rename
-		// Other infra packages
+		// Infra packages
 		m+"/pkg/fleethttp",
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/platform/arch_test.go` around lines 74 - 82, Rename the inline comment
"Other infra packages" above the IgnoreDeps block to "Infra packages" for
consistency with TestPlatformPackageDependencies and
TestEndpointerPackageDependencies; update the comment that precedes the
m+"/pkg/fleethttp" entry in the ShouldNotDependOn(...).IgnoreDeps(...) section
so it reads "Infra packages" and leaves the rest (including
m+"/server/platform/errors" and m+"/server/platform/http" and
m+"/pkg/fleethttp") unchanged.
server/activity/internal/service/new_activity_test.go (1)

218-221: Remove duplicate test helper.

newTestServiceWithWebhook is identical to newTestService; reusing the existing helper keeps this file easier to maintain.

♻️ Proposed simplification
-// newTestServiceWithWebhook creates a service configured for webhook delivery tests.
-func newTestServiceWithWebhook(ds types.Datastore, providers activity.DataProviders) *Service {
-	return NewService(&mockAuthorizer{}, ds, providers, slog.New(slog.DiscardHandler))
-}
@@
-	svc := newTestServiceWithWebhook(ds, providers)
+	svc := newTestService(ds, providers)
@@
-	svc := newTestServiceWithWebhook(ds, providers)
+	svc := newTestService(ds, providers)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/activity/internal/service/new_activity_test.go` around lines 218 -
221, The helper newTestServiceWithWebhook duplicates newTestService; delete the
newTestServiceWithWebhook function and update any tests referencing it to call
the existing newTestService helper instead (search for usages of
newTestServiceWithWebhook and replace them with newTestService), keeping the
same arguments and ensuring imports/constructor call remain identical
(NewService, mockAuthorizer{}, slog.New(slog.DiscardHandler), etc.).
server/platform/http/post_json.go (1)

39-39: A new HTTP client is created on every call, bypassing connection pooling.

fleethttp.NewClient allocates a fresh transport and connection pool each invocation. For a function that may be called at high frequency (e.g., per-activity webhook dispatch), this prevents keep-alive reuse and exhausts ephemeral ports under load. Consider initialising a package-level client once (or accepting a client as a dependency).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/platform/http/post_json.go` at line 39, The code currently calls
fleethttp.NewClient on every invocation (client := fleethttp.NewClient(...)),
which creates a fresh transport and prevents connection pooling; change this to
reuse a single client by either initializing a package-level variable (e.g., var
defaultClient = fleethttp.NewClient(fleethttp.WithTimeout(30*time.Second))) and
using that instead of per-call client, or modify the PostJSON function signature
to accept an injected *http.Client/fleethttp.Client and use the injected
instance; update all call sites accordingly and remove the per-call
fleethttp.NewClient allocation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@server/activity/internal/service/new_activity_test.go`:
- Around line 344-346: The test's async wait uses a fixed 1s timeout in the
select (the time.After(1 * time.Second) branch that calls t.Error("timeout
waiting for webhook")), which is too short and causes flakiness; update that
timeout to a larger value (e.g., 5 * time.Second) or replace with a named
constant (e.g., webhookWaitTimeout) and use that so CI/slower environments have
more headroom, keeping the rest of the select logic unchanged.

In `@server/platform/http/post_json.go`:
- Around line 54-58: The code currently calls io.ReadAll(resp.Body) which can
buffer arbitrarily large responses; replace this with reading from
io.LimitReader(resp.Body, 513) (or similar limit) to cap the amount read, handle
the returned error instead of ignoring it, and then convert the limited buffer
to bodyStr and truncate to 512 bytes if needed; update the logic around
resp.Body, io.ReadAll and bodyStr to use the limited reader and proper error
handling.

---

Nitpick comments:
In `@server/activity/internal/service/new_activity_test.go`:
- Around line 218-221: The helper newTestServiceWithWebhook duplicates
newTestService; delete the newTestServiceWithWebhook function and update any
tests referencing it to call the existing newTestService helper instead (search
for usages of newTestServiceWithWebhook and replace them with newTestService),
keeping the same arguments and ensuring imports/constructor call remain
identical (NewService, mockAuthorizer{}, slog.New(slog.DiscardHandler), etc.).

In `@server/platform/arch_test.go`:
- Around line 74-82: Rename the inline comment "Other infra packages" above the
IgnoreDeps block to "Infra packages" for consistency with
TestPlatformPackageDependencies and TestEndpointerPackageDependencies; update
the comment that precedes the m+"/pkg/fleethttp" entry in the
ShouldNotDependOn(...).IgnoreDeps(...) section so it reads "Infra packages" and
leaves the rest (including m+"/server/platform/errors" and
m+"/server/platform/http" and m+"/pkg/fleethttp") unchanged.

In `@server/platform/http/post_json.go`:
- Line 39: The code currently calls fleethttp.NewClient on every invocation
(client := fleethttp.NewClient(...)), which creates a fresh transport and
prevents connection pooling; change this to reuse a single client by either
initializing a package-level variable (e.g., var defaultClient =
fleethttp.NewClient(fleethttp.WithTimeout(30*time.Second))) and using that
instead of per-call client, or modify the PostJSON function signature to accept
an injected *http.Client/fleethttp.Client and use the injected instance; update
all call sites accordingly and remove the per-call fleethttp.NewClient
allocation.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between af4c112 and fb9915d.

📒 Files selected for processing (25)
  • cmd/fleet/serve.go
  • server/activity/arch_test.go
  • server/activity/bootstrap/bootstrap.go
  • server/activity/bootstrap/testing.go
  • server/activity/internal/service/new_activity.go
  • server/activity/internal/service/new_activity_test.go
  • server/activity/internal/service/service.go
  • server/activity/internal/service/service_test.go
  • server/activity/internal/tests/suite_test.go
  • server/activity/providers.go
  • server/contexts/ctxerr/ctxerr.go
  • server/contexts/logging/logging.go
  • server/datastore/mysql/in_house_apps_test.go
  • server/datastore/mysql/testing_utils.go
  • server/fleet/activities.go
  • server/fleet/datastore.go
  • server/fleet/errors.go
  • server/platform/arch_test.go
  • server/platform/errors/errors.go
  • server/platform/http/errors.go
  • server/platform/http/post_json.go
  • server/platform/mysql/errors.go
  • server/service/activities_test.go
  • server/service/testing_utils.go
  • server/utils.go
💤 Files with no reviewable changes (6)
  • server/activity/providers.go
  • cmd/fleet/serve.go
  • server/service/activities_test.go
  • server/service/testing_utils.go
  • server/activity/bootstrap/testing.go
  • server/fleet/activities.go

Comment thread server/activity/internal/service/new_activity_test.go
Comment thread server/platform/http/post_json.go Outdated

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

This PR continues the activity bounded-context migration by moving JSON POST helper logic into the platform layer, extracting transport-agnostic error classification into a dedicated platform package, and simplifying activity bootstrap wiring while adjusting tests accordingly.

Changes:

  • Moved PostJSONWithTimeout (and URL masking helpers) to server/platform/http and kept deprecated aliases in server/.
  • Introduced server/platform/errors for transport-agnostic error classification (Cause, NotFoundError, IsNotFound, ErrWithIsClientError) and updated call sites.
  • Removed injectable webhook send function from the activity service/bootstrap and migrated webhook tests into the activity bounded-context service tests.

Reviewed changes

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

Show a summary per file
File Description
server/utils.go Replaced in-package implementations with deprecated aliases to server/platform/http.
server/service/testing_utils.go Updated activity bootstrap wiring to new activity_bootstrap.New signature (no webhook send fn).
server/service/activities_test.go Removed service-layer webhook tests (now covered in activity service tests).
server/platform/mysql/errors.go Switched NotFoundError interface dependency from platform/http to platform/errors.
server/platform/http/post_json.go New home for PostJSONWithTimeout.
server/platform/http/errors.go Removed transport-agnostic primitives and switched to platform/errors for Cause.
server/platform/errors/errors.go New transport-agnostic error classification primitives package.
server/platform/arch_test.go Updated architecture dependency rules for new platform packages/deps.
server/fleet/errors.go Re-aliased ErrWithIsClientError and Cause to platform/errors.
server/fleet/datastore.go Re-aliased NotFoundError/IsNotFound to platform/errors.
server/fleet/activities.go Removed legacy webhook payload/context key and activity-related optional interfaces from fleet package.
server/datastore/mysql/testing_utils.go Updated activity test service creation for new bootstrap signature.
server/datastore/mysql/in_house_apps_test.go Removed use of legacy fleet.ActivityWebhookContextKey.
server/contexts/logging/logging.go Updated client-error classification interface import.
server/contexts/ctxerr/ctxerr.go Updated Cause and client-error classification interface import.
server/activity/providers.go Removed WebhookSendFunc from activity providers layer.
server/activity/internal/tests/suite_test.go Updated service construction for new NewService signature.
server/activity/internal/service/service_test.go Updated service construction for new NewService signature.
server/activity/internal/service/service.go Removed webhookSendFn from service struct and constructor.
server/activity/internal/service/new_activity_test.go Added webhook delivery tests directly against activity service.
server/activity/internal/service/new_activity.go Switched webhook delivery to platformhttp.PostJSONWithTimeout.
server/activity/bootstrap/bootstrap.go Updated bootstrap to construct service without webhook send fn injection.
server/activity/bootstrap/testing.go Deleted unit-test bootstrap helper that depended on injectable webhook send fn.
server/activity/arch_test.go Updated architecture dependency rules (platform/errors + fleethttp).
cmd/fleet/serve.go Updated activity bounded-context creation for new bootstrap signature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/platform/http/post_json.go
Comment thread server/activity/internal/service/new_activity_test.go Outdated
@getvictor
getvictor marked this pull request as ready for review February 25, 2026 23:37
@getvictor
getvictor requested a review from a team as a code owner February 25, 2026 23:37
@getvictor
getvictor merged commit 92bc1c6 into main Feb 26, 2026
49 checks passed
@getvictor
getvictor deleted the victor/38536-activity-cleanup branch February 26, 2026 23:39
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.

Activity bounded context: Complete write operations

3 participants