-
Notifications
You must be signed in to change notification settings - Fork 0
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
JQ filtering in client #20
Merged
+997
−23
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a8a4424
JQ filtering in client
gergas3 994af98
Test fix
gergas3 25afe6d
Apply formatting
gergas3 c6717da
Snapshot update
gergas3 1e580f6
Update all snapshots; change header name
gergas3 6b177c3
Change header name again
gergas3 747a9f4
Undo import order changes caused by prettier
gergas3 2a56aee
Use relative import in test
gergas3 5e06237
review comments
gergas3 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 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,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
yarn run lint-staged |
This file contains 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,114 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`json path filter returns only selected fields 1`] = ` | ||
{ | ||
"data": { | ||
"items": [ | ||
{ | ||
"metadata": { | ||
"name": "admin", | ||
}, | ||
}, | ||
{ | ||
"metadata": { | ||
"name": "aws-node", | ||
}, | ||
}, | ||
], | ||
"kind": "ClusterRoleList", | ||
}, | ||
"status": 200, | ||
} | ||
`; | ||
|
||
exports[`json path filter returns original input data on error 1`] = ` | ||
{ | ||
"data": { | ||
"apiVersion": "rbac.authorization.k8s.io/v1", | ||
"items": [ | ||
{ | ||
"metadata": { | ||
"managedFields": [ | ||
{ | ||
"manager": "clusterrole-aggregation-controller", | ||
"operation": "Apply", | ||
}, | ||
{ | ||
"manager": "kube-apiserver", | ||
"operation": "Update", | ||
}, | ||
], | ||
"name": "admin", | ||
"resourceVersion": "345", | ||
"uid": "4251609f-b3ca-4875-8ec6-76b40ab62748", | ||
}, | ||
}, | ||
{ | ||
"metadata": { | ||
"managedFields": [ | ||
{ | ||
"manager": "kubectl-client-side-apply", | ||
"operation": "Update", | ||
}, | ||
], | ||
"name": "aws-node", | ||
"resourceVersion": "273", | ||
"uid": "63a3bed8-a750-440d-bca9-b5f41c8786c2", | ||
}, | ||
}, | ||
], | ||
"kind": "ClusterRoleList", | ||
"metadata": { | ||
"resourceVersion": "4423772", | ||
}, | ||
}, | ||
"status": 200, | ||
"statusText": "OK", | ||
} | ||
`; | ||
|
||
exports[`json path filter returns original input data when no filter supplied 1`] = ` | ||
{ | ||
"data": { | ||
"apiVersion": "rbac.authorization.k8s.io/v1", | ||
"items": [ | ||
{ | ||
"metadata": { | ||
"managedFields": [ | ||
{ | ||
"manager": "clusterrole-aggregation-controller", | ||
"operation": "Apply", | ||
}, | ||
{ | ||
"manager": "kube-apiserver", | ||
"operation": "Update", | ||
}, | ||
], | ||
"name": "admin", | ||
"resourceVersion": "345", | ||
"uid": "4251609f-b3ca-4875-8ec6-76b40ab62748", | ||
}, | ||
}, | ||
{ | ||
"metadata": { | ||
"managedFields": [ | ||
{ | ||
"manager": "kubectl-client-side-apply", | ||
"operation": "Update", | ||
}, | ||
], | ||
"name": "aws-node", | ||
"resourceVersion": "273", | ||
"uid": "63a3bed8-a750-440d-bca9-b5f41c8786c2", | ||
}, | ||
}, | ||
], | ||
"kind": "ClusterRoleList", | ||
"metadata": { | ||
"resourceVersion": "4423772", | ||
}, | ||
}, | ||
"status": 200, | ||
"statusText": "OK", | ||
} | ||
`; |
This file contains 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,70 @@ | ||
import { jqTransform } from "../filter"; | ||
|
||
const exampleResponse = { | ||
status: 200, | ||
statusText: "OK", | ||
data: { | ||
kind: "ClusterRoleList", | ||
apiVersion: "rbac.authorization.k8s.io/v1", | ||
metadata: { | ||
resourceVersion: "4423772", | ||
}, | ||
items: [ | ||
{ | ||
metadata: { | ||
name: "admin", | ||
uid: "4251609f-b3ca-4875-8ec6-76b40ab62748", | ||
resourceVersion: "345", | ||
managedFields: [ | ||
{ | ||
manager: "clusterrole-aggregation-controller", | ||
operation: "Apply", | ||
}, | ||
{ | ||
manager: "kube-apiserver", | ||
operation: "Update", | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
metadata: { | ||
name: "aws-node", | ||
uid: "63a3bed8-a750-440d-bca9-b5f41c8786c2", | ||
resourceVersion: "273", | ||
managedFields: [ | ||
{ | ||
manager: "kubectl-client-side-apply", | ||
operation: "Update", | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
describe("json path filter", () => { | ||
it("returns only selected fields", async () => { | ||
const result = await jqTransform( | ||
exampleResponse, | ||
"{ status: .status, data: { kind: .data.kind, items: [.data.items[] | { metadata: { name: .metadata.name } }] } }" | ||
); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
describe("returns original input data", () => { | ||
it("on error", async () => { | ||
const result = await jqTransform(exampleResponse, "{ { }"); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
it("when no filter supplied", async () => { | ||
const result = await jqTransform(exampleResponse, undefined); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
This file contains 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,30 @@ | ||
import { isArray } from "lodash"; | ||
|
||
import { createLogger } from "../log"; | ||
|
||
const logger = createLogger({ name: "filter" }); | ||
|
||
// import { jq } from "node-jq"; | ||
// TODO fix node config because jq-node cannot be imported with the `import` syntax above | ||
// Error: | ||
// Module '"node-jq"' has no exported member 'jq'. | ||
const jq = require("node-jq"); | ||
|
||
export const jqTransform = async ( | ||
data: any, | ||
jqHeader: string | string[] | undefined | ||
): Promise<any> => { | ||
const query = isArray(jqHeader) ? jqHeader[0] : jqHeader; | ||
if (!query) { | ||
return data; | ||
} | ||
try { | ||
return await jq.run(query, data, { input: "json", output: "json" }); | ||
} catch (error: any) { | ||
logger.error( | ||
{ error, jpSelectQuery: query }, | ||
"Error running jq query, ignoring filters" | ||
); | ||
return data; | ||
} | ||
}; |
This file contains 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 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 |
---|---|---|
|
@@ -22,7 +22,9 @@ export const createLogger = <T extends LoggerOptions>( | |
// Redact the authorization header that may contain a secret token | ||
redact: [ | ||
"response.config.headers.authorization", // Axios intercepted response object | ||
"response.request.headers.authorization", // Axios intercepted response object contains the request as well | ||
"request.headers.authorization", // Axios intercepted request object + forwarded request object (JSON RPC request) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you also want just |
||
"headers.authorization", | ||
], | ||
formatters: { | ||
level: (label) => { | ||
|
This file contains 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 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,5 @@ | ||
module.exports = { | ||
importOrder: ["^(cli|testing)([/]|$)", "^[.][.]?([/]|$)"], | ||
importOrderSeparation: true, | ||
importOrderSortSpecifiers: true, | ||
}; |
This file contains 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.
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.
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.
Wouldn't you want to run this multiple times if there are multiple headers?
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.
You mean chain the header jq commands? The output of the first is the input of the second?
I don't know if that's helpful, I think chains should be expressible in one query.