-
-
Notifications
You must be signed in to change notification settings - Fork 184
feat: add device custom fields #6248
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
Merged
gustavosbarreto
merged 11 commits into
shellhub-io:master
from
danielgatis:feat/device-custom-fields
Apr 30, 2026
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8de06f4
feat(pkg): add custom_fields to Device model and DeviceUpdate request
danielgatis b8b9c15
feat(api): implement custom_fields storage and filtering for devices
danielgatis 1ebaab1
feat(openapi): add custom_fields to device schema and PUT request body
danielgatis f216236
fix(docker): add binutils-gold to Alpine images for ARM64 linker support
danielgatis c2cb388
feat(ui): display custom_fields on device detail page
danielgatis 2fa6436
feat(ui-react): add custom fields management to devices
danielgatis 8a7e9fb
test(api): add tests for custom_fields filtering and service update
danielgatis 8f1e938
test(ui-react): add tests for devices list and device details custom …
danielgatis 8473bdf
test(ui-react): update buildFilter tests to reflect custom_fields OR
danielgatis 3785db0
fix(api): address code review issues on custom_fields
danielgatis 0175231
fix(ui-react): gate custom fields edit on permission and fix stale name
danielgatis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/shellhub-io/shellhub/pkg/api/query" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.mongodb.org/mongo-driver/bson" | ||
| ) | ||
|
|
||
| func TestParseCustomFieldsFilter(t *testing.T) { | ||
| cases := []struct { | ||
| description string | ||
| fp *query.FilterProperty | ||
| wantOk bool | ||
| wantErr bool | ||
| checkResult func(t *testing.T, result bson.M) | ||
| }{ | ||
| { | ||
| description: "returns not-ok for unsupported operator eq", | ||
| fp: &query.FilterProperty{Name: "custom_fields", Operator: "eq", Value: "prod"}, | ||
| wantOk: false, | ||
| wantErr: false, | ||
| checkResult: func(t *testing.T, result bson.M) { | ||
| assert.Nil(t, result) | ||
| }, | ||
| }, | ||
| { | ||
| description: "returns error when value is not a string", | ||
| fp: &query.FilterProperty{Name: "custom_fields", Operator: "contains", Value: 42}, | ||
| wantOk: false, | ||
| wantErr: true, | ||
| checkResult: func(t *testing.T, result bson.M) { | ||
| assert.Nil(t, result) | ||
| }, | ||
| }, | ||
| { | ||
| description: "returns $expr condition for contains with string value", | ||
| fp: &query.FilterProperty{Name: "custom_fields", Operator: "contains", Value: "production"}, | ||
| wantOk: true, | ||
| wantErr: false, | ||
| checkResult: func(t *testing.T, result bson.M) { | ||
| require.NotNil(t, result) | ||
| // Top-level key must be $expr | ||
| exprRaw, ok := result["$expr"] | ||
| require.True(t, ok, "result must have $expr key") | ||
|
|
||
| expr, ok := exprRaw.(bson.M) | ||
| require.True(t, ok) | ||
|
|
||
| // $expr.$gt must exist | ||
| gtRaw, ok := expr["$gt"] | ||
| require.True(t, ok, "$expr must have $gt") | ||
|
|
||
| gt, ok := gtRaw.(bson.A) | ||
| require.True(t, ok) | ||
| require.Len(t, gt, 2) | ||
|
|
||
| // Second element of $gt must be 0 (threshold) | ||
| assert.Equal(t, 0, gt[1]) | ||
|
|
||
| // First element is the $size expression | ||
| sizeExpr, ok := gt[0].(bson.M) | ||
| require.True(t, ok) | ||
| _, hasSz := sizeExpr["$size"] | ||
| assert.True(t, hasSz, "$gt[0] must be a $size expression") | ||
| }, | ||
| }, | ||
| { | ||
| description: "regex contains the search value", | ||
| fp: &query.FilterProperty{Name: "custom_fields", Operator: "contains", Value: "team-a"}, | ||
| wantOk: true, | ||
| wantErr: false, | ||
| checkResult: func(t *testing.T, result bson.M) { | ||
| require.NotNil(t, result) | ||
| // Walk down to the $regexMatch input | ||
| expr := result["$expr"].(bson.M) | ||
| gt := expr["$gt"].(bson.A) | ||
| sizeExpr := gt[0].(bson.M) | ||
| filterExpr := sizeExpr["$size"].(bson.M) | ||
| filterMap := filterExpr["$filter"].(bson.M) | ||
| cond := filterMap["cond"].(bson.M) | ||
| regexMatch := cond["$regexMatch"].(bson.M) | ||
|
|
||
| assert.Equal(t, "team-a", regexMatch["regex"]) | ||
| assert.Equal(t, "i", regexMatch["options"]) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.description, func(t *testing.T) { | ||
| result, ok, err := ParseCustomFieldsFilter(tc.fp) | ||
| assert.Equal(t, tc.wantOk, ok) | ||
| if tc.wantErr { | ||
| require.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
| tc.checkResult(t, result) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.