From fb1106be159af781c8ab45cab6dc2ebd35574244 Mon Sep 17 00:00:00 2001 From: Felix Kling Date: Tue, 9 Jul 2024 09:40:12 +0200 Subject: [PATCH] fix(svelte): Respect cody ignore settings (#63677) This commit adds support for cody ignore settings to the SvelteKit app. When cody is disabled on the instance or for the user, the cody button and sidebar are not shown anymore. Likewise if the repository is excluded from cody, the buttons and sidebar are not shown. The implementation was inspired by `useCodyIngore.ts`, but simplified for use in the SvelteKit app. It seems that the file exclusion logic is not actually used for the sidebar and thus was omitted. It also seems that for the sidebar itself we are only checking whether the current repository is excluded or not, which lets us simplify the whole setup and simply pass a boolean (store) from the data loader, indicating whether cody is enabled or not. Furthermore I introduced zod to validate that the value of `codyContextFilters.raw`, which is typed as `JSONValue`, has the expected shape. We've run into issues in the past where such values have just been cast to the expected Typescript type. zod adds runtime validation. Note that we use JSON schema base validation (with `ajv`) in some places, but that requires importing and sending the whole JSON schema to the client, which is something I'd like to avoid. The advantage JSON schema is that we also use it for generating Go code. We should find a way to use JSON schema but generate specific validators at build time. There are also other libraries that do runtime validation and are smaller but they don't necessarily allow asynchronous validation (which we want to do because we only want to import the `re2js` library when necessary; of course we could organize the code differently but it's nice to be able to encapsulate this logic) ## Test plan Manual testing and new integration tests. --- client/web-sveltekit/BUILD.bazel | 3 + client/web-sveltekit/package.json | 4 +- .../src/lib/cody/CodySidebar.svelte | 23 ++- .../web-sveltekit/src/lib/cody/config.test.ts | 31 ++++ client/web-sveltekit/src/lib/cody/config.ts | 58 ++++++++ .../src/lib/repo/OpenCodyAction.svelte | 3 +- .../(validrev)/(code)/+layout.svelte | 4 +- .../(validrev)/(code)/+layout.ts | 66 ++++++++- .../(code)/-/blob/[...path]/FileView.svelte | 5 +- .../(code)/-/tree/[...path]/+page.svelte | 5 +- .../(validrev)/(code)/layout.gql | 8 + .../(validrev)/(code)/page.spec.ts | 137 ++++++++++++++++++ .../web-sveltekit/src/testing/integration.ts | 7 + pnpm-lock.yaml | 10 ++ 14 files changed, 351 insertions(+), 13 deletions(-) create mode 100644 client/web-sveltekit/src/lib/cody/config.test.ts create mode 100644 client/web-sveltekit/src/lib/cody/config.ts diff --git a/client/web-sveltekit/BUILD.bazel b/client/web-sveltekit/BUILD.bazel index c6209943140..ca2ae9ef127 100644 --- a/client/web-sveltekit/BUILD.bazel +++ b/client/web-sveltekit/BUILD.bazel @@ -62,6 +62,7 @@ BUILD_DEPS = [ "//:node_modules/@mdi/js", "//:node_modules/@reach/combobox", "//:node_modules/@reach/menu-button", + "//:node_modules/@sourcegraph/cody-context-filters-test-dataset", "//:node_modules/@types/lodash", "//:node_modules/@types/node", "//:node_modules/@types/react", @@ -115,6 +116,7 @@ BUILD_DEPS = [ ":node_modules/hotkeys-js", ":node_modules/mermaid", ":node_modules/prismjs", + ":node_modules/re2js", ":node_modules/sass", ":node_modules/signale", ":node_modules/svelte", @@ -124,6 +126,7 @@ BUILD_DEPS = [ ":node_modules/vite", ":node_modules/vite-plugin-inspect", ":node_modules/wonka", + ":node_modules/zod", ] + glob([ "dev/**/*.cjs", "dev/**/*.ts", diff --git a/client/web-sveltekit/package.json b/client/web-sveltekit/package.json index d15efefa2d5..db33a5f37f2 100644 --- a/client/web-sveltekit/package.json +++ b/client/web-sveltekit/package.json @@ -99,8 +99,10 @@ "hotkeys-js": "^3.13.7", "mermaid": "^10.9.1", "prismjs": "^1.29.0", + "re2js": "^0.4.1", "ts-key-enum": "^2.0.12", - "wonka": "^6.3.4" + "wonka": "^6.3.4", + "zod": "^3.23.8" }, "msw": { "workerDirectory": "static" diff --git a/client/web-sveltekit/src/lib/cody/CodySidebar.svelte b/client/web-sveltekit/src/lib/cody/CodySidebar.svelte index e4e75ff719e..0fd125b7931 100644 --- a/client/web-sveltekit/src/lib/cody/CodySidebar.svelte +++ b/client/web-sveltekit/src/lib/cody/CodySidebar.svelte @@ -1,3 +1,9 @@ + + -
+