Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cb 6263 lefthook #3291

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .lefthook/pre-commit/license-checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

# Script is a pre-commit hook to check if the staged files
# has the license header from the file license-header.txt

set -eo pipefail

LICENSE_HEADER=${LICENSE_HEADER:-""}
INCLUDE_FILES_EXT="\.(ts|tsx)$"
STAGED_FILES=$(git diff --name-only --diff-filter=d --staged)

# read .license-checker.txt file if exists in the root directory
if [[ -f .license-checker.txt && -z "$LICENSE_HEADER" ]]; then
# read the file and set the LICENSE_HEADER variable
LICENSE_HEADER=$(cat .license-checker.txt)
fi


# error message function printing in red color
# usage: error "message"
error() {
local message=${1:-""}
echo -e "\033[0;31m${message}\033[0m"
}

# if the LICENSE_HEADER is empty, then exit
if [ -z "$LICENSE_HEADER" ]; then
error "The LICENSE_HEADER environment variable is empty."
error "Either create .license-checker.txt file in the root directory or set the LICENSE_HEADER environment variable."
exit 1
fi

LICENSE_HEADER=$(echo "$LICENSE_HEADER" | tr -d '\r' | sed 's/[[:space:]]*$//')
LICENSE_LINES=$(echo "$LICENSE_HEADER" | wc -l)

errorCount=0
# Check if the staged files contains the license header.
for file in $STAGED_FILES; do
# filter out files that ends with the included file extensions
# which described in the INCLUDE_FILES_EXT
if [[ ! $file =~ $INCLUDE_FILES_EXT ]] || [[ $file == *locales/* ]]; then
continue
fi

# check if the file contains the license header at the top of the file
# from the variable of LICENSE_HEADER

FILE_HEADER=$(head -n "$LICENSE_LINES" "$file" | tr -d '\r' | sed 's/[[:space:]]*$//')

if [[ "$FILE_HEADER" != "$LICENSE_HEADER" ]]; then
error "The file $file does not contain the license header or the license header is not correct."
errorCount=$((errorCount + 1))
fi
done

if [ $errorCount -gt 0 ]; then
error "\nPlease add the license header to the file(s) above"
exit 1
fi
7 changes: 7 additions & 0 deletions .license-checker.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* CloudBeaver - Cloud Database Manager
* Copyright (C) 2020-2025 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
Comment on lines +1 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets have license 1 entry point for vscode snippet and license checking via git hooks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, it will be strange to support both of them

5 changes: 5 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pre-commit:
parallel: true
scripts:
"license-checker.sh":
runner: bash
57 changes: 57 additions & 0 deletions webapp/.husky/_/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh

if [ "$LEFTHOOK_VERBOSE" = "1" -o "$LEFTHOOK_VERBOSE" = "true" ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we still have the .husky directory?

set -x
fi

if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi

call_lefthook()
{
if test -n "$LEFTHOOK_BIN"
then
"$LEFTHOOK_BIN" "$@"
elif lefthook -h >/dev/null 2>&1
then
lefthook "$@"
else
dir="$(git rev-parse --show-toplevel)"
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/;s/x86_64/x64/')
if test -f "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook"
then
"$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook" "$@"
elif test -f "$dir/node_modules/lefthook/bin/index.js"
then
"$dir/node_modules/lefthook/bin/index.js" "$@"

elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1
then
pnpm lefthook "$@"
elif swift package plugin lefthook >/dev/null 2>&1
then
swift package --disable-sandbox plugin lefthook "$@"
elif command -v mint >/dev/null 2>&1
then
mint run csjones/lefthook-plugin "$@"
else
echo "Can't find lefthook in PATH"
fi
fi
}

call_lefthook run "pre-commit" "$@"
57 changes: 57 additions & 0 deletions webapp/.husky/_/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh

if [ "$LEFTHOOK_VERBOSE" = "1" -o "$LEFTHOOK_VERBOSE" = "true" ]; then
set -x
fi

if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi

call_lefthook()
{
if test -n "$LEFTHOOK_BIN"
then
"$LEFTHOOK_BIN" "$@"
elif lefthook -h >/dev/null 2>&1
then
lefthook "$@"
else
dir="$(git rev-parse --show-toplevel)"
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/;s/x86_64/x64/')
if test -f "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook"
then
"$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook" "$@"
elif test -f "$dir/node_modules/lefthook/bin/index.js"
then
"$dir/node_modules/lefthook/bin/index.js" "$@"

elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1
then
pnpm lefthook "$@"
elif swift package plugin lefthook >/dev/null 2>&1
then
swift package --disable-sandbox plugin lefthook "$@"
elif command -v mint >/dev/null 2>&1
then
mint run csjones/lefthook-plugin "$@"
else
echo "Can't find lefthook in PATH"
fi
fi
}

call_lefthook run "prepare-commit-msg" "$@"
1 change: 0 additions & 1 deletion webapp/.husky/pre-commit

This file was deleted.

6 changes: 2 additions & 4 deletions webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
"lint": "eslint",
"validate-dependencies": "core-cli-validate-dependencies './packages/*/'",
"add-plugin": "core-cli-add-plugin",
"prepare": "cd .. && husky ./webapp/.husky",
"version:set": "yarn workspaces foreach -Ap --include '@cloudbeaver/product-*' run version:set",
"postinstall": "husky"
"version:set": "yarn workspaces foreach -Ap --include '@cloudbeaver/product-*' run version:set"
},
"devDependencies": {
"@cloudbeaver/core-cli": "workspace:*",
Expand All @@ -37,7 +35,7 @@
"@types/react-dom": "^19",
"concurrently": "^9",
"eslint": "^9.20.1",
"husky": "^9",
"lefthook": "^1",
Copy link
Contributor

@sergeyteleshev sergeyteleshev Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it didnt workout for me to have githooks after all install scripts. can we do it without need to install lefthook globally?

"mobx": "^6",
"mobx-react-lite": "^4",
"msw": "^2",
Expand Down
122 changes: 112 additions & 10 deletions webapp/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9917,7 +9917,7 @@ __metadata:
"@types/react-dom": "npm:^19"
concurrently: "npm:^9"
eslint: "npm:^9.20.1"
husky: "npm:^9"
lefthook: "npm:^1"
mobx: "npm:^6"
mobx-react-lite: "npm:^4"
msw: "npm:^2"
Expand Down Expand Up @@ -13014,15 +13014,6 @@ __metadata:
languageName: node
linkType: hard

"husky@npm:^9":
version: 9.1.7
resolution: "husky@npm:9.1.7"
bin:
husky: bin.js
checksum: 10c0/35bb110a71086c48906aa7cd3ed4913fb913823715359d65e32e0b964cb1e255593b0ae8014a5005c66a68e6fa66c38dcfa8056dbbdfb8b0187c0ffe7ee3a58f
languageName: node
linkType: hard

"hygen@npm:^6":
version: 6.2.11
resolution: "hygen@npm:6.2.11"
Expand Down Expand Up @@ -14699,6 +14690,117 @@ __metadata:
languageName: node
linkType: hard

"lefthook-darwin-arm64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-darwin-arm64@npm:1.10.10"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard

"lefthook-darwin-x64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-darwin-x64@npm:1.10.10"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard

"lefthook-freebsd-arm64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-freebsd-arm64@npm:1.10.10"
conditions: os=freebsd & cpu=arm64
languageName: node
linkType: hard

"lefthook-freebsd-x64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-freebsd-x64@npm:1.10.10"
conditions: os=freebsd & cpu=x64
languageName: node
linkType: hard

"lefthook-linux-arm64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-linux-arm64@npm:1.10.10"
conditions: os=linux & cpu=arm64
languageName: node
linkType: hard

"lefthook-linux-x64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-linux-x64@npm:1.10.10"
conditions: os=linux & cpu=x64
languageName: node
linkType: hard

"lefthook-openbsd-arm64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-openbsd-arm64@npm:1.10.10"
conditions: os=openbsd & cpu=arm64
languageName: node
linkType: hard

"lefthook-openbsd-x64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-openbsd-x64@npm:1.10.10"
conditions: os=openbsd & cpu=x64
languageName: node
linkType: hard

"lefthook-windows-arm64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-windows-arm64@npm:1.10.10"
conditions: os=win32 & cpu=arm64
languageName: node
linkType: hard

"lefthook-windows-x64@npm:1.10.10":
version: 1.10.10
resolution: "lefthook-windows-x64@npm:1.10.10"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard

"lefthook@npm:^1":
version: 1.10.10
resolution: "lefthook@npm:1.10.10"
dependencies:
lefthook-darwin-arm64: "npm:1.10.10"
lefthook-darwin-x64: "npm:1.10.10"
lefthook-freebsd-arm64: "npm:1.10.10"
lefthook-freebsd-x64: "npm:1.10.10"
lefthook-linux-arm64: "npm:1.10.10"
lefthook-linux-x64: "npm:1.10.10"
lefthook-openbsd-arm64: "npm:1.10.10"
lefthook-openbsd-x64: "npm:1.10.10"
lefthook-windows-arm64: "npm:1.10.10"
lefthook-windows-x64: "npm:1.10.10"
dependenciesMeta:
lefthook-darwin-arm64:
optional: true
lefthook-darwin-x64:
optional: true
lefthook-freebsd-arm64:
optional: true
lefthook-freebsd-x64:
optional: true
lefthook-linux-arm64:
optional: true
lefthook-linux-x64:
optional: true
lefthook-openbsd-arm64:
optional: true
lefthook-openbsd-x64:
optional: true
lefthook-windows-arm64:
optional: true
lefthook-windows-x64:
optional: true
bin:
lefthook: bin/index.js
checksum: 10c0/bd67c5582f7d3480db5f19c71ce7f5e6f0e0b0c8b6c94557cb5d45f535dabb3aa26315c2c328059499cf5d99ab71ce79257fd7ef32af8878d4e00e2710399618
languageName: node
linkType: hard

"less@npm:^4.2.0":
version: 4.2.1
resolution: "less@npm:4.2.1"
Expand Down
Loading