Skip to content

Conversation

@RIT3shSapata
Copy link
Contributor

CBG-5101

Describe your PR here...

  • Add support for passing user contexts via the request body in Diagnostic API
  • Add test coverage for the same

Pre-review checklist

  • Removed debug logging (fmt.Print, log.Print, ...)
  • Logging sensitive data? Make sure it's tagged (e.g. base.UD(docID), base.MD(dbName))
  • Updated relevant information in the API specifications (such as endpoint descriptions, schemas, ...) in docs/api

Dependencies (if applicable)

  • Link upstream PRs
  • Update Go module dependencies when merged

Integration Tests

@RIT3shSapata RIT3shSapata requested a review from bbrks January 16, 2026 14:43
Copilot AI review requested due to automatic review settings January 16, 2026 14:43
Copy link
Contributor

Copilot AI left a 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 SyncDryRunUserCtx struct to accept user name, roles, and channels in the API request
  • Modified SyncFnDryrun function signature to accept an optional userCtx parameter
  • 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
Copy link

Copilot AI Jan 16, 2026

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment on lines +139 to +143
rolesMap := make(map[string]int)
for _, role := range syncDryRunPayload.UserCtx.Roles {
rolesMap[role] = 1
}
userCtx["roles"] = rolesMap
Copy link

Copilot AI Jan 16, 2026

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
requestDocID bool
expectedOutput SyncFnDryRun
expectedStatus int
userCtx SyncDryRunUserCtx
Copy link

Copilot AI Jan 16, 2026

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.

Copilot uses AI. Check for mistakes.
Copy link
Member

@bbrks bbrks left a 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.

@RIT3shSapata RIT3shSapata requested a review from bbrks January 16, 2026 16:17
@bbrks bbrks merged commit 8e8f23b into main Jan 16, 2026
44 checks passed
@bbrks bbrks deleted the CBG-5101 branch January 16, 2026 16:41
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.

3 participants