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

Add pre commit hook #2352

Merged
merged 21 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6a7743d
feat(frontend) added pre-commit hook for format check
ashrafchowdury Dec 8, 2024
4757025
feat(frontend): added lint and tsc check on pre-commit hook
ashrafchowdury Dec 8, 2024
8c80a32
enhance(frontend): added condition to run hooks efficiantly
ashrafchowdury Dec 8, 2024
2eb926a
feat(bacckend): added pre-commit checks for backend
ashrafchowdury Dec 8, 2024
143587c
refactor(frontend): moved .husky folder in the root directory
ashrafchowdury Dec 9, 2024
a03962a
enhance(workflow): added pre-push hooks to reduce time from pre-commit
ashrafchowdury Dec 10, 2024
f1f3548
enhane(workflow): added cli directory
ashrafchowdury Dec 10, 2024
58bbce2
Merge branch 'main' of github.com:agenta-ai/agenta into add-pre-commi…
ashrafchowdury Dec 11, 2024
a1abe95
refactor(workflow): removed duplicate code
ashrafchowdury Dec 11, 2024
b331812
Merge branch 'main' of github.com:agenta-ai/agenta into add-pre-commi…
ashrafchowdury Dec 11, 2024
8cf5173
fix(workflow): pre-push hook failed
ashrafchowdury Dec 11, 2024
114652e
temp(workflow): temporarily commented out the pylint checks
ashrafchowdury Dec 11, 2024
a809830
fix(workflow): eslint build faile
ashrafchowdury Dec 11, 2024
f791aa7
Merge branch 'dev' of github.com:agenta-ai/agenta into add-pre-commit…
ashrafchowdury Dec 11, 2024
467aa78
fix(prettire): format
ashrafchowdury Dec 11, 2024
cf2a9a6
fix(prettier): my system prettier lib version
ashrafchowdury Dec 11, 2024
114a6c3
fix(workflow): pre-push hook for first time push
ashrafchowdury Dec 11, 2024
986b959
fix(workflow): temporarily disabled pylint check
ashrafchowdury Dec 13, 2024
1ddcf2e
fix(workflow): temporarily disabled auto commit
ashrafchowdury Dec 13, 2024
d3dc0b1
Merge branch 'dev' of github.com:agenta-ai/agenta into add-pre-commit…
ashrafchowdury Dec 16, 2024
7d6c356
Merge branch 'dev' into add-pre-commit-hook
bekossy Dec 19, 2024
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
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
Loading