-
Notifications
You must be signed in to change notification settings - Fork 140
CBG-5101: Add support for User context in Diagnostic API #7974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
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 adds support for passing user context information via the request body in the Sync Function Diagnostic API (/_sync endpoint). Previously, the API only supported running sync functions with the authenticated user's context; now it can accept arbitrary user credentials for testing purposes.
Changes:
- Added
SyncDryRunUserCtxstruct to accept user name, roles, and channels in the API request - Modified
SyncFnDryrunfunction signature to accept an optionaluserCtxparameter - Added comprehensive test coverage for user context validation scenarios (correct/incorrect usernames, channels, and roles)
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| rest/diagnostic_doc_api.go | Added SyncDryRunUserCtx struct and logic to construct user context map from request payload, with validation for required user name |
| rest/diagnostic_doc_api_test.go | Added 6 new test cases covering various user context scenarios including valid/invalid usernames, channels, and roles |
| db/crud.go | Updated SyncFnDryrun to accept userCtx parameter and use it when provided, falling back to authenticated user context otherwise |
| return base.HTTPErrorf(http.StatusBadRequest, "no user name provided") | ||
| } | ||
| userCtx["name"] = syncDryRunPayload.UserCtx.Name | ||
| userCtx["channels"] = syncDryRunPayload.UserCtx.Channels |
Copilot
AI
Jan 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When syncDryRunPayload.UserCtx.Channels is nil or empty, this will set userCtx['channels'] to nil/empty slice. However, the MakeUserCtx function returns channels.AllKeys() which returns a slice of strings. For consistency with the existing behavior and to match the structure expected by the sync function runtime, this should be set to an empty slice []string{} when nil/empty, similar to how roles are handled with a map structure.
| userCtx["channels"] = syncDryRunPayload.UserCtx.Channels | |
| channels := syncDryRunPayload.UserCtx.Channels | |
| if len(channels) == 0 { | |
| // Ensure channels is always represented as a non-nil slice for consistency | |
| // with MakeUserCtx and the expectations of the sync function runtime. | |
| channels = []string{} | |
| } | |
| userCtx["channels"] = channels |
| rolesMap := make(map[string]int) | ||
| for _, role := range syncDryRunPayload.UserCtx.Roles { | ||
| rolesMap[role] = 1 | ||
| } | ||
| userCtx["roles"] = rolesMap |
Copilot
AI
Jan 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The roles are transformed into a map[string]int structure here, but looking at MakeUserCtx, it uses user.RoleNames() which likely returns a different structure. To maintain consistency with how user context is constructed elsewhere in the codebase, verify that the sync function runtime expects this map structure. If the sync function expects the same format as MakeUserCtx produces, consider documenting why this transformation is necessary or aligning the formats.
| rolesMap := make(map[string]int) | |
| for _, role := range syncDryRunPayload.UserCtx.Roles { | |
| rolesMap[role] = 1 | |
| } | |
| userCtx["roles"] = rolesMap | |
| // Use the roles slice directly to keep the user context format consistent with MakeUserCtx. | |
| userCtx["roles"] = syncDryRunPayload.UserCtx.Roles |
rest/diagnostic_doc_api_test.go
Outdated
| requestDocID bool | ||
| expectedOutput SyncFnDryRun | ||
| expectedStatus int | ||
| userCtx SyncDryRunUserCtx |
Copilot
AI
Jan 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The userCtx field is added to the test case struct but is never used in any of the existing test cases (lines 970-1247). The new test cases starting at line 1248 populate the request.UserCtx field directly rather than using this struct field. Either remove this unused field or update the test execution logic to use it.
bbrks
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a nitpick about naming of syncOptions just to keep the dry run and real code more consistent but functionally it looks good.
CBG-5101
Describe your PR here...
Pre-review checklist
fmt.Print,log.Print, ...)base.UD(docID),base.MD(dbName))docs/apiDependencies (if applicable)
Integration Tests
GSI=true,xattrs=truehttps://jenkins.sgwdev.com/job/SyncGatewayIntegration/227/