From 601010de82246d0970f793a5fa5f309986546849 Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Mon, 22 Apr 2024 08:41:43 +0000 Subject: [PATCH 1/6] Adds doc generation to pre commit hook Signed-off-by: Ashwin P Chandran --- scripts/precommit_hook.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/precommit_hook.js b/scripts/precommit_hook.js index 1d5485c64905..c53480e5ddf1 100644 --- a/scripts/precommit_hook.js +++ b/scripts/precommit_hook.js @@ -30,3 +30,4 @@ require('@osd/optimizer').registerNodeAutoTranspilation(); require('../src/dev/run_precommit_hook'); +require('./generate_docs_sidebar'); From 57c8c4fed0cd39bde867f87e8dd53681feb466cb Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Mon, 22 Apr 2024 09:26:35 +0000 Subject: [PATCH 2/6] Add check hook to pre commit Signed-off-by: Ashwin P Chandran --- docs/_sidebar.md | 8 ++++ scripts/precommit_hook.js | 2 +- src/dev/precommit_hook/check_dev_docs.js | 16 +++++++ .../precommit_hook/get_files_for_commit.js | 42 +++++++++++++------ src/dev/precommit_hook/index.js | 3 +- src/dev/run_precommit_hook.js | 15 ++++++- 6 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 src/dev/precommit_hook/check_dev_docs.js diff --git a/docs/_sidebar.md b/docs/_sidebar.md index db1e0a09b57c..264197994180 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -14,18 +14,22 @@ - [Data_persistence](plugins/data_persistence.md) - Saved_objects - [Saved_object_repository_factory_design](saved_objects/saved_object_repository_factory_design.md) + - Telemetry + - [Telemetry](telemetry/telemetry.md) - src - [Core](../src/core/README.md) - dev - [Build](../src/dev/build/README.md) - [I18n](../src/dev/i18n/README.md) - plugins + - [Application_config](../src/plugins/application_config/README.md) - [Bfetch](../src/plugins/bfetch/README.md) - [Charts](../src/plugins/charts/README.md) - console - public - application - [Hooks](../src/plugins/console/public/application/hooks/README.md) + - [Csp_handler](../src/plugins/csp_handler/README.md) - [Dashboard](../src/plugins/dashboard/README.md) - [Data](../src/plugins/data/README.md) - [Data_explorer](../src/plugins/data_explorer/README.md) @@ -77,6 +81,7 @@ - [Vis_type_xy](../src/plugins/vis_type_xy/README.md) - [Visualizations](../src/plugins/visualizations/README.md) - [Visualize](../src/plugins/visualize/README.md) + - [Workspace](../src/plugins/workspace/README.md) - examples - [Dashboard_embeddable_examples](../examples/dashboard_embeddable_examples/README.md) - [Developer_examples](../examples/developer_examples/README.md) @@ -84,6 +89,7 @@ - public - [Todo](../examples/embeddable_examples/public/todo/README.md) - [Expressions_example](../examples/expressions_example/README.md) + - [Multiple_data_source_examples](../examples/multiple_data_source_examples/README.md) - [Routing_example](../examples/routing_example/README.md) - [Search_examples](../examples/search_examples/README.md) - [Ui_action_examples](../examples/ui_action_examples/README.md) @@ -136,6 +142,7 @@ - [Opensearch dashboards.release notes 1.3.12](../release-notes/opensearch-dashboards.release-notes-1.3.12.md) - [Opensearch dashboards.release notes 1.3.13](../release-notes/opensearch-dashboards.release-notes-1.3.13.md) - [Opensearch dashboards.release notes 1.3.14](../release-notes/opensearch-dashboards.release-notes-1.3.14.md) + - [Opensearch dashboards.release notes 1.3.15](../release-notes/opensearch-dashboards.release-notes-1.3.15.md) - [Opensearch dashboards.release notes 1.3.2](../release-notes/opensearch-dashboards.release-notes-1.3.2.md) - [Opensearch dashboards.release notes 1.3.3](../release-notes/opensearch-dashboards.release-notes-1.3.3.md) - [Opensearch dashboards.release notes 1.3.5](../release-notes/opensearch-dashboards.release-notes-1.3.5.md) @@ -151,6 +158,7 @@ - [Opensearch dashboards.release notes 2.11.0](../release-notes/opensearch-dashboards.release-notes-2.11.0.md) - [Opensearch dashboards.release notes 2.11.1](../release-notes/opensearch-dashboards.release-notes-2.11.1.md) - [Opensearch dashboards.release notes 2.12.0](../release-notes/opensearch-dashboards.release-notes-2.12.0.md) + - [Opensearch dashboards.release notes 2.13.0](../release-notes/opensearch-dashboards.release-notes-2.13.0.md) - [Opensearch dashboards.release notes 2.2.0](../release-notes/opensearch-dashboards.release-notes-2.2.0.md) - [Opensearch dashboards.release notes 2.2.1](../release-notes/opensearch-dashboards.release-notes-2.2.1.md) - [Opensearch dashboards.release notes 2.3.0](../release-notes/opensearch-dashboards.release-notes-2.3.0.md) diff --git a/scripts/precommit_hook.js b/scripts/precommit_hook.js index c53480e5ddf1..eb5347071386 100644 --- a/scripts/precommit_hook.js +++ b/scripts/precommit_hook.js @@ -29,5 +29,5 @@ */ require('@osd/optimizer').registerNodeAutoTranspilation(); -require('../src/dev/run_precommit_hook'); require('./generate_docs_sidebar'); +require('../src/dev/run_precommit_hook'); diff --git a/src/dev/precommit_hook/check_dev_docs.js b/src/dev/precommit_hook/check_dev_docs.js new file mode 100644 index 000000000000..1ce7793ea998 --- /dev/null +++ b/src/dev/precommit_hook/check_dev_docs.js @@ -0,0 +1,16 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +const SIDEBAR_PATH = 'docs/_sidebar.md'; + +export async function checkDevDocs(log, files) { + files.map((file) => { + const path = file.getRelativePath(); + + if (path === SIDEBAR_PATH) { + throw Error(`Please stage the ${SIDEBAR_PATH} to update the developer docs`); + } + }); +} diff --git a/src/dev/precommit_hook/get_files_for_commit.js b/src/dev/precommit_hook/get_files_for_commit.js index b0a18f650f70..7691df3c0fa7 100644 --- a/src/dev/precommit_hook/get_files_for_commit.js +++ b/src/dev/precommit_hook/get_files_for_commit.js @@ -34,20 +34,9 @@ import { fromNode as fcb } from 'bluebird'; import { REPO_ROOT } from '@osd/utils'; import { File } from '../file'; -/** - * Get the files that are staged for commit (excluding deleted files) - * as `File` objects that are aware of their commit status. - * - * @param {String} repoPath - * @return {Promise>} - */ -export async function getFilesForCommit() { - const simpleGit = new SimpleGit(REPO_ROOT); - - const output = await fcb((cb) => simpleGit.diff(['--name-status', '--cached'], cb)); - +function getFileList(diffText) { return ( - output + diffText .split('\n') // Ignore blank lines .filter((line) => line.trim().length > 0) @@ -69,3 +58,30 @@ export async function getFilesForCommit() { .filter(Boolean) ); } + +/** + * Get the files that are staged for commit (excluding deleted files) + * as `File` objects that are aware of their commit status. + * + * @return {Promise>} + */ +export async function getFilesForCommit() { + const simpleGit = new SimpleGit(REPO_ROOT); + + const staged = await fcb((cb) => simpleGit.diff(['--name-status', '--cached'], cb)); // staged + + return getFileList(staged); +} + +/** + * Get the unstaged files as `File` objects that are aware of their commit status. + * + * @return {Promise>} + */ +export async function getUnstagedFiles() { + const simpleGit = new SimpleGit(REPO_ROOT); + + const unstaged = await fcb((cb) => simpleGit.diff(['--name-status'], cb)); + + return getFileList(unstaged); +} diff --git a/src/dev/precommit_hook/index.js b/src/dev/precommit_hook/index.js index 14bf1af2a34b..898b4393d860 100644 --- a/src/dev/precommit_hook/index.js +++ b/src/dev/precommit_hook/index.js @@ -29,4 +29,5 @@ */ export { checkFileCasing } from './check_file_casing'; -export { getFilesForCommit } from './get_files_for_commit'; +export { getFilesForCommit, getUnstagedFiles } from './get_files_for_commit'; +export { checkDevDocs } from './check_dev_docs'; diff --git a/src/dev/run_precommit_hook.js b/src/dev/run_precommit_hook.js index 86a279166aca..b840fca99752 100644 --- a/src/dev/run_precommit_hook.js +++ b/src/dev/run_precommit_hook.js @@ -31,13 +31,26 @@ import { run, combineErrors } from '@osd/dev-utils'; import * as Eslint from './eslint'; import * as Stylelint from './stylelint'; -import { getFilesForCommit, checkFileCasing } from './precommit_hook'; +import { + getFilesForCommit, + getUnstagedFiles, + checkFileCasing, + checkDevDocs, +} from './precommit_hook'; run( async ({ log, flags }) => { const files = await getFilesForCommit(); + const unstagedFiles = await getUnstagedFiles(); const errors = []; + try { + // Check if the dev docs sidebar has been updated but not staged + await checkDevDocs(log, unstagedFiles); + } catch (error) { + errors.push(error); + } + try { await checkFileCasing(log, files); } catch (error) { From 2b2ff7aa3abaa102ef055619616572890501632b Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:31:19 +0000 Subject: [PATCH 3/6] Changeset file for PR #6585 created/updated --- changelogs/fragments/6585.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/6585.yml diff --git a/changelogs/fragments/6585.yml b/changelogs/fragments/6585.yml new file mode 100644 index 000000000000..311fa8da3abc --- /dev/null +++ b/changelogs/fragments/6585.yml @@ -0,0 +1,2 @@ +chore: +- Adds a git pre commit hook to ensure that developer docs are always updated ([#6585](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6585)) \ No newline at end of file From 69797d5c69dd3b14c98e30deb697532030f07be6 Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Tue, 23 Apr 2024 09:41:14 -0700 Subject: [PATCH 4/6] Update error message Signed-off-by: Ashwin P Chandran --- src/dev/precommit_hook/check_dev_docs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev/precommit_hook/check_dev_docs.js b/src/dev/precommit_hook/check_dev_docs.js index 1ce7793ea998..d0300ef11c31 100644 --- a/src/dev/precommit_hook/check_dev_docs.js +++ b/src/dev/precommit_hook/check_dev_docs.js @@ -10,7 +10,7 @@ export async function checkDevDocs(log, files) { const path = file.getRelativePath(); if (path === SIDEBAR_PATH) { - throw Error(`Please stage the ${SIDEBAR_PATH} to update the developer docs`); + throw Error(`Please git add the ${SIDEBAR_PATH} before committing your changes to update the developer docs`); } }); } From 453613c9f1979031b7bfaeef0de2f3ecca4ffb97 Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Tue, 23 Apr 2024 10:00:36 -0700 Subject: [PATCH 5/6] Improve error message Co-authored-by: Miki Signed-off-by: Ashwin P Chandran --- src/dev/precommit_hook/check_dev_docs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev/precommit_hook/check_dev_docs.js b/src/dev/precommit_hook/check_dev_docs.js index d0300ef11c31..e5a9d616f310 100644 --- a/src/dev/precommit_hook/check_dev_docs.js +++ b/src/dev/precommit_hook/check_dev_docs.js @@ -10,7 +10,7 @@ export async function checkDevDocs(log, files) { const path = file.getRelativePath(); if (path === SIDEBAR_PATH) { - throw Error(`Please git add the ${SIDEBAR_PATH} before committing your changes to update the developer docs`); + throw Error(`The ${SIDEBAR_PATH} file of the developer docs has been modified but is not ready to be committed. This can be done by performing "git add ${SIDEBAR_PATH}" and committing the changes.`); } }); } From 9d73dd1b764108c99e6171d27c3fcd5617aba329 Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Wed, 24 Apr 2024 15:13:30 +0000 Subject: [PATCH 6/6] fixes lint issue Signed-off-by: Ashwin P Chandran --- src/dev/precommit_hook/check_dev_docs.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/dev/precommit_hook/check_dev_docs.js b/src/dev/precommit_hook/check_dev_docs.js index e5a9d616f310..3a134dffb2b3 100644 --- a/src/dev/precommit_hook/check_dev_docs.js +++ b/src/dev/precommit_hook/check_dev_docs.js @@ -10,7 +10,9 @@ export async function checkDevDocs(log, files) { const path = file.getRelativePath(); if (path === SIDEBAR_PATH) { - throw Error(`The ${SIDEBAR_PATH} file of the developer docs has been modified but is not ready to be committed. This can be done by performing "git add ${SIDEBAR_PATH}" and committing the changes.`); + throw Error( + `The ${SIDEBAR_PATH} file of the developer docs has been modified but is not ready to be committed. This can be done by performing "git add ${SIDEBAR_PATH}" and committing the changes.` + ); } }); }