Skip to content

Commit

Permalink
Merge pull request #2352 from Agenta-AI/add-pre-commit-hook
Browse files Browse the repository at this point in the history
Add pre commit hook
  • Loading branch information
jp-agenta authored Dec 19, 2024
2 parents 835bc3e + 7d6c356 commit e9225cf
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
37 changes: 37 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -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
77 changes: 77 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions agenta-web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion agenta-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
Expand Down

0 comments on commit e9225cf

Please sign in to comment.