Skip to content

Commit

Permalink
[OE] Adds dev doc script to precommit hook (opensearch-project#6585)
Browse files Browse the repository at this point in the history
* Adds doc generation to pre commit hook

Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>

* Add check hook to pre commit

Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>

* Changeset file for PR opensearch-project#6585 created/updated

* Update error message

Signed-off-by: Ashwin P Chandran <ashwinpc1993@gmail.com>

* Improve error message

Co-authored-by: Miki <amoo_miki@yahoo.com>
Signed-off-by: Ashwin P Chandran <ashwinpc1993@gmail.com>

* fixes lint issue

Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>

---------

Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>
Signed-off-by: Ashwin P Chandran <ashwinpc1993@gmail.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Co-authored-by: Miki <amoo_miki@yahoo.com>
Signed-off-by: yujin-emma <yujin.emma.work@gmail.com>
  • Loading branch information
3 people authored and yujin-emma committed May 16, 2024
1 parent 57ca1ab commit 9560ca0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 15 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6585.yml
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions scripts/precommit_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
*/

require('@osd/optimizer').registerNodeAutoTranspilation();
require('./generate_docs_sidebar');
require('../src/dev/run_precommit_hook');
18 changes: 18 additions & 0 deletions src/dev/precommit_hook/check_dev_docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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(
`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.`
);
}
});
}
42 changes: 29 additions & 13 deletions src/dev/precommit_hook/get_files_for_commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<File>>}
*/
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)
Expand All @@ -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<Array<File>>}
*/
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<Array<File>>}
*/
export async function getUnstagedFiles() {
const simpleGit = new SimpleGit(REPO_ROOT);

const unstaged = await fcb((cb) => simpleGit.diff(['--name-status'], cb));

return getFileList(unstaged);
}
3 changes: 2 additions & 1 deletion src/dev/precommit_hook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
15 changes: 14 additions & 1 deletion src/dev/run_precommit_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9560ca0

Please sign in to comment.