chore(NA): removes auto install of pre-commit hook (#83566) (#85026)

* chore(NA): remove kibana pre-commit hook installation from bootstrap

* chore(NA): add support for git ref flag on run precommit hook script

* chore(NA): integrate quick commit checks within the CI

* chore(NA): introduce logging trap to warn about quick commit checks failure and how to reproduce it

* chore(NA): update quick commit checks message

* fix(NA): quick commit checks function def

* chore(NA): fix quick commit checks message quotes

* chore(NA): fix functional call

* chore(NA): fix script to run

* chore(NA): add unexpected debugger statement to test quick commit checks

* chore(NA): update message to log before quick commit checks

* chore(NA): remove extra debugger statement

* chore(NA): add echo message inline with script execution

* chore(NA): add unexpected debugger statement to test quick commit checks

* chore(NA): remove extra usage of debug statement

* chore(NA): wrapping quick commit checks in a func

* chore(NA): export function to use later

* chore(NA): export function to use later

* chore(NA): use child bash script on github checks reporter

* chore(NA): define dir context for commit_check_runner.sh

* fix(NA): permissions for commit_check_runner.sh

* fix(NA): permissions for commit.sh

* chore(NA): format message to log

* chore(NA): add unexpected debugger statement to test quick commit checks

* chore(NA): remove extra usage of debug statement

* chore(NA): format runner message

* chore(NA): replace log.info by log.warning

* docs(NA): include docs for removing the pre-commit hook auto installation

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Tiago Costa 2020-12-04 17:52:14 +00:00 committed by GitHub
parent d17299e805
commit ff03b5b30d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 8 deletions

View file

@ -110,6 +110,20 @@ View all available options by running `yarn start --help`
Read about more advanced options for <<running-kibana-advanced>>.
[discrete]
=== Install pre-commit hook (optional)
In case you want to run a couple of checks like linting or check the file casing of the files to commit, we provide
a way to install a pre-commit hook. To configure it you just need to run the following:
[source,bash]
----
node scripts/register_git_hook
----
After the script completes the pre-commit hook will be created within the file `.git/hooks/pre-commit`.
If you choose to not install it, don't worry, we still run a quick ci check to provide feedback earliest as we can about the same checks.
[discrete]
=== Code away!

View file

@ -65,7 +65,7 @@
"kbn:watch": "node scripts/kibana --dev --logging.json=false",
"build:types": "rm -rf ./target/types && tsc --p tsconfig.types.json",
"docs:acceptApiChanges": "node --max-old-space-size=6144 scripts/check_published_api_changes.js --accept",
"kbn:bootstrap": "node scripts/build_ts_refs && node scripts/register_git_hook",
"kbn:bootstrap": "node scripts/build_ts_refs",
"spec_to_console": "node scripts/spec_to_console",
"storybook": "node scripts/storybook"
},

View file

@ -27,13 +27,13 @@ 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
* @param {String} gitRef
* @return {Promise<Array<File>>}
*/
export async function getFilesForCommit() {
export async function getFilesForCommit(gitRef) {
const simpleGit = new SimpleGit(REPO_ROOT);
const output = await fcb((cb) => simpleGit.diff(['--name-status', '--cached'], cb));
const gitRefForDiff = gitRef ? gitRef : '--cached';
const output = await fcb((cb) => simpleGit.diff(['--name-status', gitRefForDiff], cb));
return (
output

View file

@ -17,16 +17,30 @@
* under the License.
*/
import { run, combineErrors } from '@kbn/dev-utils';
import { run, combineErrors, createFlagError } from '@kbn/dev-utils';
import * as Eslint from './eslint';
import * as Sasslint from './sasslint';
import { getFilesForCommit, checkFileCasing } from './precommit_hook';
run(
async ({ log, flags }) => {
const files = await getFilesForCommit();
const files = await getFilesForCommit(flags.ref);
const errors = [];
const maxFilesCount = flags['max-files']
? Number.parseInt(String(flags['max-files']), 10)
: undefined;
if (maxFilesCount !== undefined && (!Number.isFinite(maxFilesCount) || maxFilesCount < 1)) {
throw createFlagError('expected --max-files to be a number greater than 0');
}
if (maxFilesCount && files.length > maxFilesCount) {
log.warning(
`--max-files is set to ${maxFilesCount} and ${files.length} were discovered. The current script execution will be skipped.`
);
return;
}
try {
await checkFileCasing(log, files);
} catch (error) {
@ -52,15 +66,18 @@ run(
},
{
description: `
Run checks on files that are staged for commit
Run checks on files that are staged for commit by default
`,
flags: {
boolean: ['fix'],
string: ['max-files', 'ref'],
default: {
fix: false,
},
help: `
--fix Execute eslint in --fix mode
--max-files Max files number to check against. If exceeded the script will skip the execution
--ref Run checks against any git ref files (example HEAD or <commit_sha>) instead of running against staged ones
`,
},
}

View file

@ -0,0 +1,11 @@
#!/usr/bin/env bash
source src/dev/ci_setup/setup_env.sh
# Runs pre-commit hook script for the files touched in the last commit.
# That way we can ensure a set of quick commit checks earlier as we removed
# the pre-commit hook installation by default.
# If files are more than 200 we will skip it and just use
# the further ci steps that already check linting and file casing for the entire repo.
checks-reporter-with-killswitch "Quick commit checks" \
"$(dirname "${0}")/commit_check_runner.sh"

View file

@ -0,0 +1,13 @@
#!/usr/bin/env bash
run_quick_commit_checks() {
echo "!!!!!!!! ATTENTION !!!!!!!!
That check is intended to provide earlier CI feedback after we remove the automatic install for the local pre-commit hook.
If you want, you can still manually install the pre-commit hook locally by running 'node scripts/register_git_hook locally'
!!!!!!!!!!!!!!!!!!!!!!!!!!!
"
node scripts/precommit_hook.js --ref HEAD~1..HEAD --max-files 200 --verbose
}
run_quick_commit_checks

View file

@ -4,6 +4,7 @@ def call(List<Closure> closures) {
def check() {
tasks([
kibanaPipeline.scriptTask('Quick Commit Checks', 'test/scripts/checks/commit/commit.sh'),
kibanaPipeline.scriptTask('Check Telemetry Schema', 'test/scripts/checks/telemetry.sh'),
kibanaPipeline.scriptTask('Check TypeScript Projects', 'test/scripts/checks/ts_projects.sh'),
kibanaPipeline.scriptTask('Check Jest Configs', 'test/scripts/checks/jest_configs.sh'),