diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000000..f0481db762 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,37 @@ +# Define the directories to check +DIRECTORIES=("agenta-backend" "agenta-cli") +ORIGINAL_DIR=$(pwd) + +# Check for changes in frontend directory +if git diff --cached --name-only | grep -q '^agenta-web/'; then + cd agenta-web || exit + + # Run Prettier check + if ! npm run format; then + echo '⚠️ Formatting issues detected. Running Prettier to fix them...' + npm run format-fix + + echo '✅ Formatting issues fixed. Please stage the changes and commit the code again' + exit 1 + fi + + cd "$ORIGINAL_DIR" || exit +fi + +# Check for changes in backend directory +for DIR in "${DIRECTORIES[@]}"; do + if git diff --cached --name-only | grep -q "^$DIR/"; then + cd "$DIR" || exit + + # Run black for formatting + if ! black --check .; then + echo "⚠️ Formatting issues detected in $DIR. Running black to fix them..." + black . + + echo "✅ Formatting issues fixed Please stage the changes and commit the code again $DIR." + exit 1 + fi + + cd "$ORIGINAL_DIR" || exit + fi +done diff --git a/.husky/pre-push b/.husky/pre-push new file mode 100644 index 0000000000..7780c3ac7a --- /dev/null +++ b/.husky/pre-push @@ -0,0 +1,77 @@ +# Define the directories to check +DIRECTORIES=("agenta-backend" "agenta-cli") +ORIGINAL_DIR=$(pwd) +BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) +FIRST_PUSH_FILE=".git/first_push_detected" + +# Function to run frontend checks +run_frontend_checks() { + cd agenta-web || exit + + # Run ESLint check + if ! npm run lint; then + echo '❌ ESLint issues detected. Please fix them before pushing.' + exit 1 + fi + + # Run TypeScript type check + if ! npm run types:check; then + echo '❌ TypeScript type check failed.' + exit 1 + fi + + echo '🎉 Frontend checks passed!' + cd "$ORIGINAL_DIR" || exit +} + +# Function to run backend checks +run_backend_checks() { + local DIR=$1 + cd "$DIR" || exit + + # Run pylint checks + if ! pylint --recursive=y --errors-only .; then + echo "❌ pylint issues detected in $DIR. Please fix them before pushing." + exit 1 + fi + + echo "🎉 Backend checks passed for $DIR." + cd "$ORIGINAL_DIR" || exit +} + +# Check if this is the first push +is_first_push() { + # Check if the branch exists locally + if ! git rev-parse --verify "$BRANCH_NAME" >/dev/null 2>&1; then + return 0 # First push, since the branch doesn't exist locally + fi + + # Check if the branch exists remotely + if ! git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then + return 0 # First push, since the branch doesn't exist remotely + fi + + return 1 # Not the first push +} + +# If: First-time push: Run all checks +# Else: Check directory-specific changes for existing branch +if is_first_push; then + echo "🚀 First-time push detected for branch: $BRANCH_NAME" + run_frontend_checks + + # for DIR in "${DIRECTORIES[@]}"; do + # run_backend_checks "$DIR" + # done +else + echo "🔍 Checking directory changes for existing branch: $BRANCH_NAME" + if git diff --name-only --cached origin/"$BRANCH_NAME" | grep -q '^agenta-web/'; then + run_frontend_checks + fi + + # for DIR in "${DIRECTORIES[@]}"; do + # if git diff --name-only --cached origin/"$BRANCH_NAME" | grep -q "^$DIR/"; then + # run_backend_checks "$DIR" + # fi + # done +fi diff --git a/agenta-web/package-lock.json b/agenta-web/package-lock.json index 5f32a7e5d8..5b5a627212 100644 --- a/agenta-web/package-lock.json +++ b/agenta-web/package-lock.json @@ -87,6 +87,7 @@ "@swc/core": "^1.4.15", "@types/node": "^20.8.10", "cypress": "^13.15.0", + "husky": "^9.1.7", "node-mocks-http": "^1.12.2", "prettier": "^3.2.5" }, @@ -11093,6 +11094,21 @@ "node": ">=10.17.0" } }, + "node_modules/husky": { + "version": "9.1.7", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.7.tgz", + "integrity": "sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA==", + "dev": true, + "bin": { + "husky": "bin.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/hyphenate-style-name": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/hyphenate-style-name/-/hyphenate-style-name-1.1.0.tgz", diff --git a/agenta-web/package.json b/agenta-web/package.json index d5643b1702..deb7c42abb 100644 --- a/agenta-web/package.json +++ b/agenta-web/package.json @@ -18,7 +18,8 @@ "format": "prettier --check .", "format-fix": "prettier --write .", "types:check": "tsc", - "types:watch": "tsc -w" + "types:watch": "tsc -w", + "prepare": "cd .. && husky && if [ \"$FEATURE_FLAG\" = \"cloud-dev\" ]; then cd .. && husky; fi || true" }, "dependencies": { "@ag-grid-community/core": "^31.3.4", @@ -100,6 +101,7 @@ "@swc/core": "^1.4.15", "@types/node": "^20.8.10", "cypress": "^13.15.0", + "husky": "^9.1.7", "node-mocks-http": "^1.12.2", "prettier": "^3.2.5" }