From 48c774d054fcc01d2902b636ec7bcfe32b7681d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ida=20=C5=A0tambuk?= Date: Tue, 17 Dec 2024 11:56:02 +0100 Subject: [PATCH] Add precommit hook (#112) --- .drone.yml | 2 - .github/issue_commands.json | 66 ++++++++--------- .gitignore | 3 + README.md | 3 +- cspell.config.json | 16 ++--- lefthook.yml | 11 +++ package.json | 2 + src/components/SIGV4ConnectionConfig.test.tsx | 8 +-- src/components/SIGV4ConnectionConfig.tsx | 6 +- src/sql/ConfigEditor/ConfigSelect.test.tsx | 68 +++++++++--------- src/sql/QueryEditor/FormatSelect.test.tsx | 20 +++--- src/sql/QueryEditor/QueryEditorHeader.tsx | 4 +- src/sql/ResourceSelector.tsx | 27 +++---- src/types.ts | 4 +- yarn.lock | 71 +++++++++++++++++++ 15 files changed, 198 insertions(+), 113 deletions(-) create mode 100644 lefthook.yml diff --git a/.drone.yml b/.drone.yml index 6dd1e23..1d9d245 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,5 +41,3 @@ steps: --- kind: signature hmac: a2e180c09fe108e09838e4c44aa5648124fff138cb0372080677dcdd14e49f9f - -... diff --git a/.github/issue_commands.json b/.github/issue_commands.json index b62db38..8bd60e6 100644 --- a/.github/issue_commands.json +++ b/.github/issue_commands.json @@ -1,34 +1,34 @@ -[ - { - "type": "label", - "name": "grafana-aws-sdk-react", - "action": "addToProject", - "addToProject": { - "url": "https://github.com/orgs/grafana/projects/97" - } - }, - { - "type": "label", - "name": "grafana-aws-sdk-react", - "action": "removeFromProject", - "removeFromProject": { - "url": "https://github.com/orgs/grafana/projects/97" - } - }, - { - "type": "label", - "name": "type/docs", - "action": "addToProject", - "addToProject": { - "url": "https://github.com/orgs/grafana/projects/69" - } - }, - { - "type": "label", - "name": "type/docs", - "action": "removeFromProject", - "removeFromProject": { - "url": "https://github.com/orgs/grafana/projects/69" - } +[ + { + "type": "label", + "name": "grafana-aws-sdk-react", + "action": "addToProject", + "addToProject": { + "url": "https://github.com/orgs/grafana/projects/97" } -] \ No newline at end of file + }, + { + "type": "label", + "name": "grafana-aws-sdk-react", + "action": "removeFromProject", + "removeFromProject": { + "url": "https://github.com/orgs/grafana/projects/97" + } + }, + { + "type": "label", + "name": "type/docs", + "action": "addToProject", + "addToProject": { + "url": "https://github.com/orgs/grafana/projects/69" + } + }, + { + "type": "label", + "name": "type/docs", + "action": "removeFromProject", + "removeFromProject": { + "url": "https://github.com/orgs/grafana/projects/69" + } + } +] diff --git a/.gitignore b/.gitignore index d3adfb6..121236d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ cypress/screenshots/actual cypress/videos/ dist/ compiled/ + yarn-error.log .idea/ .DS_Store + +.eslintcache diff --git a/README.md b/README.md index 02182af..3db0ec7 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ export DRONE_TOKEN= drone --server https://drone.grafana.net sign --save grafana/grafana-aws-sdk-react ``` + ### Local Development -For local development instructions, please see the [contributing guide](https://github.com/grafana/grafana-aws-sdk-react/blob/main/CONTRIBUTING.md). \ No newline at end of file +For local development instructions, please see the [contributing guide](https://github.com/grafana/grafana-aws-sdk-react/blob/main/CONTRIBUTING.md). diff --git a/cspell.config.json b/cspell.config.json index 781a5d6..2bed475 100644 --- a/cspell.config.json +++ b/cspell.config.json @@ -1,21 +1,21 @@ { - "ignorePaths": [ - ".github/**", - "node_modules/**", - "dist/**", - "yarn.lock" - ], + "ignorePaths": [".github/**", "node_modules/**", "dist/**", "yarn.lock"], "words": [ "SIGV", "isob", "Creds", "CREDS", - "opensearch", + "opensearch", "esbuild", "testid", "Datasources", "Datasource", "typecheck", - "tibdex" + "tibdex", + "eslintcache", + "lefthook", + "gofmt", + "hmac", + "grabpl" ] } diff --git a/lefthook.yml b/lefthook.yml new file mode 100644 index 0000000..e527a60 --- /dev/null +++ b/lefthook.yml @@ -0,0 +1,11 @@ +pre-commit: + parallel: true + commands: + frontend-lint: + glob: '*.{js,ts,tsx}' + run: | + yarn lint {staged_files} + yarn prettier --write {staged_files} + stage_fixed: true + spellcheck: + run: yarn spellcheck {staged_files} diff --git a/package.json b/package.json index e8a2039..afe402a 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,9 @@ "identity-obj-proxy": "^3.0.0", "jest": "29.7.0", "jest-environment-jsdom": "^29.7.0", + "lefthook": "^1.9.2", "node-notifier": "^10.0.1", + "prettier": "^3.4.2", "react": "18.3.1", "react-dom": "18.3.1", "react-router-dom": "^6.27.0", diff --git a/src/components/SIGV4ConnectionConfig.test.tsx b/src/components/SIGV4ConnectionConfig.test.tsx index f1ae6be..22739ef 100644 --- a/src/components/SIGV4ConnectionConfig.test.tsx +++ b/src/components/SIGV4ConnectionConfig.test.tsx @@ -3,7 +3,7 @@ import React from 'react'; import { fireEvent, render, screen } from '@testing-library/react'; import { AwsAuthType } from '../types'; import { SIGV4ConnectionConfig } from './SIGV4ConnectionConfig'; -import {config} from '@grafana/runtime' +import { config } from '@grafana/runtime'; jest.mock('@grafana/runtime', () => ({ ...jest.requireActual('@grafana/runtime'), @@ -11,8 +11,8 @@ jest.mock('@grafana/runtime', () => ({ awsAllowedAuthProviders: [AwsAuthType.Credentials, AwsAuthType.Keys], awsAssumeRoleEnabled: true, featureToggles: { - awsDatasourcesTempCredentials: false - } + awsDatasourcesTempCredentials: false, + }, }, })); @@ -20,7 +20,7 @@ describe('SIGV4ConnectionConfig', () => { beforeEach(() => { config.awsAllowedAuthProviders = [AwsAuthType.Credentials, AwsAuthType.Keys]; config.awsAssumeRoleEnabled = true; - config.featureToggles.awsDatasourcesTempCredentials = false + config.featureToggles.awsDatasourcesTempCredentials = false; }); const setup = (onOptionsChange?: () => {}) => { const props: DataSourcePluginOptionsEditorProps = { diff --git a/src/components/SIGV4ConnectionConfig.tsx b/src/components/SIGV4ConnectionConfig.tsx index 7a7f027..0155bd7 100644 --- a/src/components/SIGV4ConnectionConfig.tsx +++ b/src/components/SIGV4ConnectionConfig.tsx @@ -6,11 +6,9 @@ import { AwsAuthDataSourceSecureJsonData, AwsAuthDataSourceJsonData, ConnectionC export interface SIGV4ConnectionConfigProps extends DataSourcePluginOptionsEditorProps { inExperimentalAuthComponent?: boolean; -}; +} -export const SIGV4ConnectionConfig: React.FC = ( - props: SIGV4ConnectionConfigProps -) => { +export const SIGV4ConnectionConfig: React.FC = (props: SIGV4ConnectionConfigProps) => { const { onOptionsChange, options } = props; // Map HttpSettings props to ConnectionConfigProps diff --git a/src/sql/ConfigEditor/ConfigSelect.test.tsx b/src/sql/ConfigEditor/ConfigSelect.test.tsx index c080eee..e621dc1 100644 --- a/src/sql/ConfigEditor/ConfigSelect.test.tsx +++ b/src/sql/ConfigEditor/ConfigSelect.test.tsx @@ -16,38 +16,38 @@ const props: ConfigSelectProps = { }; describe('ConfigSelect', () => { - it('should call onChange with the new value', async () => { - const fetch = jest.fn().mockResolvedValue(['bar']); - const onChange = jest.fn(); - const label = 'foo-id'; - render(); - - const selectEl = screen.getByLabelText(label); - expect(selectEl).toBeInTheDocument(); - expect(selectEl).not.toBeDisabled(); - await select(selectEl, 'bar', { container: document.body }); - expect(fetch).toHaveBeenCalled(); - expect(onChange).toHaveBeenCalledWith({ label: 'bar', value: 'bar' }); - }); - - it('should show disabled select if passed disabled as prop', () => { - const label = 'foo-id'; - render(); - const selectEl = screen.getByLabelText(label); - - expect(selectEl).toBeDisabled(); - }); - - it('should show disabled select if passed disabled=false as prop but jsonData.defaultRegion is not set', () => { - const propsWithoutDefaultRegion = { - ...props, - options: { ...props.options, jsonData: { ...props.options.jsonData } }, - }; - propsWithoutDefaultRegion.options.jsonData = {}; - const label = 'foo-id'; - render(); - const selectEl = screen.getByLabelText(label); - - expect(selectEl).toBeDisabled(); - }); + it('should call onChange with the new value', async () => { + const fetch = jest.fn().mockResolvedValue(['bar']); + const onChange = jest.fn(); + const label = 'foo-id'; + render(); + + const selectEl = screen.getByLabelText(label); + expect(selectEl).toBeInTheDocument(); + expect(selectEl).not.toBeDisabled(); + await select(selectEl, 'bar', { container: document.body }); + expect(fetch).toHaveBeenCalled(); + expect(onChange).toHaveBeenCalledWith({ label: 'bar', value: 'bar' }); + }); + + it('should show disabled select if passed disabled as prop', () => { + const label = 'foo-id'; + render(); + const selectEl = screen.getByLabelText(label); + + expect(selectEl).toBeDisabled(); + }); + + it('should show disabled select if passed disabled=false as prop but jsonData.defaultRegion is not set', () => { + const propsWithoutDefaultRegion = { + ...props, + options: { ...props.options, jsonData: { ...props.options.jsonData } }, + }; + propsWithoutDefaultRegion.options.jsonData = {}; + const label = 'foo-id'; + render(); + const selectEl = screen.getByLabelText(label); + + expect(selectEl).toBeDisabled(); + }); }); diff --git a/src/sql/QueryEditor/FormatSelect.test.tsx b/src/sql/QueryEditor/FormatSelect.test.tsx index 4cc3011..ddfd738 100644 --- a/src/sql/QueryEditor/FormatSelect.test.tsx +++ b/src/sql/QueryEditor/FormatSelect.test.tsx @@ -22,15 +22,15 @@ const props: FormatSelectProps = { }; describe('FormatSelect', () => { - it('should change the format mode', async () => { - render(); - expect(screen.getByText('Table')).toBeInTheDocument(); + it('should change the format mode', async () => { + render(); + expect(screen.getByText('Table')).toBeInTheDocument(); - const selectEl = screen.getByLabelText('Format data frames as'); - expect(selectEl).toBeInTheDocument(); - await select(selectEl, 'Time Series', { container: document.body }); + const selectEl = screen.getByLabelText('Format data frames as'); + expect(selectEl).toBeInTheDocument(); + await select(selectEl, 'Time Series', { container: document.body }); - expect(props.onChange).toHaveBeenCalledWith({ ...props.query, format: SQLOptions.TimeSeries }); - expect(props.onRunQuery).toHaveBeenCalled(); - }); - }); + expect(props.onChange).toHaveBeenCalledWith({ ...props.query, format: SQLOptions.TimeSeries }); + expect(props.onRunQuery).toHaveBeenCalled(); + }); +}); diff --git a/src/sql/QueryEditor/QueryEditorHeader.tsx b/src/sql/QueryEditor/QueryEditorHeader.tsx index 13dab5c..95557a8 100644 --- a/src/sql/QueryEditor/QueryEditorHeader.tsx +++ b/src/sql/QueryEditor/QueryEditorHeader.tsx @@ -8,7 +8,7 @@ import { RunQueryButtons } from '@grafana/async-query-data'; export interface Props< Datasource extends DataSourceApi, TQuery extends DataQuery, - JsonData extends DataSourceJsonData + JsonData extends DataSourceJsonData, > extends QueryEditorProps { showAsyncQueryButtons?: boolean; extraHeaderElementLeft?: React.JSX.Element; @@ -21,7 +21,7 @@ export interface Props< export function QueryEditorHeader< Datasource extends DataSourceApi, TQuery extends DataQuery, - JsonData extends DataSourceJsonData + JsonData extends DataSourceJsonData, >({ query, showAsyncQueryButtons, diff --git a/src/sql/ResourceSelector.tsx b/src/sql/ResourceSelector.tsx index e151cf7..5bce45b 100644 --- a/src/sql/ResourceSelector.tsx +++ b/src/sql/ResourceSelector.tsx @@ -114,17 +114,18 @@ export function ResourceSelector(props: ResourceSelectorProps) { } }; - return props.fetch && onClick()} + menuShouldPortal={true} + /> + ); } diff --git a/src/types.ts b/src/types.ts index 77dc741..021ec97 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,7 +9,7 @@ export enum AwsAuthType { * @deprecated use default */ ARN = 'arn', - GrafanaAssumeRole = 'grafana_assume_role' + GrafanaAssumeRole = 'grafana_assume_role', } export interface AwsAuthDataSourceJsonData extends DataSourceJsonData { @@ -31,7 +31,7 @@ export type AwsAuthDataSourceSettings = DataSourceSettings extends DataSourcePluginOptionsEditorProps { standardRegions?: string[]; loadRegions?: () => Promise; diff --git a/yarn.lock b/yarn.lock index 07bc80f..2c18829 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5894,6 +5894,72 @@ kleur@^3.0.3: resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e" integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w== +lefthook-darwin-arm64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-darwin-arm64/-/lefthook-darwin-arm64-1.9.2.tgz#7d280fe30b184e1cf209052df4497723ea34a412" + integrity sha512-kWcSqAdGu5MAtxdLBpxg8H+p4d/pO7tA7+3Q1Cj9NM+c0L3R2ZefGNStGF6Ftbu8grkpz9/jCD588O9PHjsVfw== + +lefthook-darwin-x64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-darwin-x64/-/lefthook-darwin-x64-1.9.2.tgz#837ee47ecb53eed1b3906d1e58194f7ec9c0354c" + integrity sha512-M4zspEhp0uXaIEVkcPFYayxElFzFhhSfEk29FMhxZOfbmZYPUSYaSsTHgb7oeR0O0/zcMTVDk4nNWsBp2Jpx5A== + +lefthook-freebsd-arm64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-freebsd-arm64/-/lefthook-freebsd-arm64-1.9.2.tgz#a80f4409e6ce9a9e05ccd19519d8f9569a817758" + integrity sha512-7Grb+wdY99Iuorw9aUu+zYKdkEaQs6OVGn4jgav+AyBSxJA0WZmXgdmZqd6+LuRVYn8JRO8bER8Tu4QUa1fSxg== + +lefthook-freebsd-x64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-freebsd-x64/-/lefthook-freebsd-x64-1.9.2.tgz#f28ab005b95e6bbe3b065e0644d107eef1df9b6b" + integrity sha512-tMBN//hqHMUJ9v7k9W3l9aMEBw//enEX+2eQLcvDJSuEE+J3AUbNg9YA5kTpVsDuafg10ziJXAuGD0kYh2ubww== + +lefthook-linux-arm64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-linux-arm64/-/lefthook-linux-arm64-1.9.2.tgz#847d28b7498cd8720488b292e0eb6c104987c56f" + integrity sha512-U16CUCaQSRQTbjqvrsJrwppCK4d/IQjMimfC/SwAEreXr7WkBiekV+Gv+fytEnVHJTCSb5MajJTV+WpBsg6jGg== + +lefthook-linux-x64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-linux-x64/-/lefthook-linux-x64-1.9.2.tgz#2707cfd6e4cac4fa1fbec3f8bbd81632355d4fb6" + integrity sha512-lkEes30MquDhiIrNQ61CfNvGLCN39d8PZ2S/dwLMJBtTH7qfVqXu8UR3KhKEDD1h43cEb4pnIeO2BJUZyn/FNw== + +lefthook-openbsd-arm64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-openbsd-arm64/-/lefthook-openbsd-arm64-1.9.2.tgz#998f579495d2dd5ab04b8583db2e4cd2d278a93f" + integrity sha512-UJKcL8Iiga8GetYqXqGXy6OeIhtNGim8PHQZH4nfR70s6TPvTvBEGvf48mvfwRyXt8t6aUvz1oyVvRQSdXETug== + +lefthook-openbsd-x64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-openbsd-x64/-/lefthook-openbsd-x64-1.9.2.tgz#c538554d6fed779395501cbd21a2b732c5500cfc" + integrity sha512-CLS119Fn91mZ2U7ug3fjYMfM7rvwB7gwpDUbuQlEPnVsHJOtaIpu+cmhnKKtU6b4B6r5ETMwhc5dDt/tS5curQ== + +lefthook-windows-arm64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-windows-arm64/-/lefthook-windows-arm64-1.9.2.tgz#b7e6d552295167edbcbc2ce6b04b56a86acc7e4d" + integrity sha512-MlN2EnYNF6RUMTGX4csJnHq/J+werlerQsUcYfZrs+TruXCm4MLjwO9Q7vShTws3bsCoeDZnacNTVESQpZG6Lg== + +lefthook-windows-x64@1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook-windows-x64/-/lefthook-windows-x64-1.9.2.tgz#459904fe083a78402fe5f50c387fd0e95c1c4ed8" + integrity sha512-7gl1L3qj6xWsqsYJ/ACRDUJruMWLqIRGcI66RiAEnQ3aSnnk0FLCVxIyMwecgW3IWY1O2C69EXV4Og7/0l9JVQ== + +lefthook@^1.9.2: + version "1.9.2" + resolved "https://registry.yarnpkg.com/lefthook/-/lefthook-1.9.2.tgz#0cac904e048f5e48a5157ae59df048e8cf72729e" + integrity sha512-boUWdXdT6MUPghZuuX64xXbG7u69fcgkD6vNEjoxh3Gll6o6MfY/ITpj/HrRAUVym1jWXB0Z298qL789OD+dZw== + optionalDependencies: + lefthook-darwin-arm64 "1.9.2" + lefthook-darwin-x64 "1.9.2" + lefthook-freebsd-arm64 "1.9.2" + lefthook-freebsd-x64 "1.9.2" + lefthook-linux-arm64 "1.9.2" + lefthook-linux-x64 "1.9.2" + lefthook-openbsd-arm64 "1.9.2" + lefthook-openbsd-x64 "1.9.2" + lefthook-windows-arm64 "1.9.2" + lefthook-windows-x64 "1.9.2" + lerc@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/lerc/-/lerc-3.0.0.tgz#36f36fbd4ba46f0abf4833799fff2e7d6865f5cb" @@ -6497,6 +6563,11 @@ prelude-ls@^1.2.1: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396" integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g== +prettier@^3.4.2: + version "3.4.2" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.4.2.tgz#a5ce1fb522a588bf2b78ca44c6e6fe5aa5a2b13f" + integrity sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ== + pretty-format@^27.0.2: version "27.5.1" resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-27.5.1.tgz#2181879fdea51a7a5851fb39d920faa63f01d88e"