Skip to content
Merged
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
38 changes: 38 additions & 0 deletions .git-hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Git Hooks

This directory contains Git hooks for the project. These hooks help maintain code quality and ensure that only properly tested code gets committed and pushed to the repository.

## Available Hooks

### Pre-commit Hook
This hook runs automatically before each commit and:
- Lints JavaScript files with ESLint to catch code quality issues
- Formats code with Prettier to ensure consistent style
- Runs tests related to the changed files

### Pre-push Hook
This hook runs automatically before each push and:
- Runs all tests with coverage
- Ensures code coverage meets minimum thresholds (80%)
- Checks for skipped tests (tests marked with .skip or .only)

## How to Enable These Hooks

To use these hooks, you need to configure Git to look for hooks in this directory instead of the default `.git/hooks` directory. Run the following command from the root of the repository:

```bash
git config core.hooksPath .git-hooks
```

This is a one-time setup per clone of the repository.

## Bypassing Hooks

In emergency situations, you can bypass the hooks using the `--no-verify` flag:

```bash
git commit --no-verify
git push --no-verify
```

However, this is not recommended as it bypasses the quality checks.
50 changes: 50 additions & 0 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh

# This is the pre-commit hook that runs before each commit
# It ensures that code quality standards are met before allowing the commit to proceed

# Exit immediately if a command exits with a non-zero status
set -e

# Get the root directory of the git repository
ROOT_DIR="$(git rev-parse --show-toplevel)"

# Check if the staged files include JavaScript files in the javascriptapp directory
STAGED_JS_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '^javascriptapp/.*\.js$' || true)

# If no JavaScript files in the javascriptapp directory are staged, exit early
if [ -z "$STAGED_JS_FILES" ]; then
echo "No JavaScript files in javascriptapp directory were staged. Skipping pre-commit hooks."
exit 0
fi

# Change directory to the javascriptapp directory
cd "$ROOT_DIR/javascriptapp"

# Extract just the filenames without the javascriptapp/ prefix
JS_FILES_WITHOUT_PREFIX=$(echo "$STAGED_JS_FILES" | sed 's|^javascriptapp/||')

echo "Running pre-commit hooks for JavaScript files..."

# Run ESLint on staged JavaScript files
echo "Running ESLint..."
npx eslint --fix $JS_FILES_WITHOUT_PREFIX

# Run Prettier on staged JavaScript files
echo "Running Prettier..."
npx prettier --write $JS_FILES_WITHOUT_PREFIX

# Run Jest tests related to the changed files
echo "Running Jest tests..."
npx jest --findRelatedTests --passWithNoTests $JS_FILES_WITHOUT_PREFIX

# Add the fixed files back to the staging area
cd "$ROOT_DIR"
for file in $STAGED_JS_FILES; do
git add "$file"
done

echo "Pre-commit hooks completed successfully!"

# If all checks pass, the commit will proceed
# If any check fails, the commit will be aborted
40 changes: 40 additions & 0 deletions .git-hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

# This is the pre-push hook that runs before each push to the remote repository
# It ensures that all tests pass and meet coverage thresholds before allowing the push to proceed

# Exit immediately if a command exits with a non-zero status
set -e

# Get the root directory of the git repository
ROOT_DIR="$(git rev-parse --show-toplevel)"

# Check if we're pushing changes to the javascriptapp directory
CHANGED_FILES=$(git diff --name-only HEAD@{1} HEAD 2>/dev/null | grep -E '^javascriptapp/' || true)

# If no files in the javascriptapp directory are changed, exit early
if [ -z "$CHANGED_FILES" ]; then
echo "No changes to javascriptapp directory. Skipping pre-push hooks."
exit 0
fi

# Change directory to the javascriptapp directory
cd "$ROOT_DIR/javascriptapp"

echo "Running pre-push hooks for javascriptapp..."

# Run Jest tests with coverage
echo "Running Jest tests with coverage..."
npx jest --coverage --coverageThreshold='{"global":{"statements":80,"branches":80,"functions":80,"lines":80}}'

# Check for skipped tests (tests marked with .skip or .only)
echo "Checking for skipped or focused tests..."
if grep -r --include="*.test.js" --include="*.spec.js" -E "(describe|it|test)\.only|\.skip" tests/; then
echo "Error: Found skipped or focused tests. Please remove .skip or .only from your tests."
exit 1
fi

echo "Pre-push hooks completed successfully!"

# If all checks pass, the push will proceed
# If any check fails, the push will be aborted
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ This repository contains various projects and examples used for AI experimentati

- **javascriptapp/**: A simple JavaScript application with tests and coverage reports.
- **mermaid_diagrams/**: Examples of using Mermaid to create diagrams for various architectures and workflows.
- **.git-hooks/**: Git hooks for code quality and testing. See [Git Hooks](#git-hooks) section below.

## Git Hooks

This repository includes Git hooks to ensure code quality and proper testing. The hooks are located in the `.git-hooks` directory.

To enable these hooks, run the following command from the root of the repository:

```bash
git config core.hooksPath .git-hooks
```

For more information about the available hooks and how to use them, see the [.git-hooks/README.md](.git-hooks/README.md) file.

## Note

Expand Down
7 changes: 6 additions & 1 deletion javascriptapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ git push --no-verify
```

### How the Hooks Work
The hooks are implemented as shell scripts in the `.git/hooks` directory:
The hooks are implemented as shell scripts in the `.git-hooks` directory at the root of the repository:
- They only run on JavaScript files in the javascriptapp directory
- The pre-commit hook runs ESLint, Prettier, and related tests
- The pre-push hook runs all tests with coverage checks

To enable these hooks, run the following command from the root of the repository:
```
git config core.hooksPath .git-hooks
```

## Technologies Used

Express.js - Backend server
Expand Down
2 changes: 1 addition & 1 deletion javascriptapp/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const API_KEY = process.env.OPENWEATHER_API_KEY;
// Serve static files from the "public" folder
app.use(express.static(path.join(__dirname, 'public')));

// GET /api/weather?city=London - Endpoint to fetch weather data
// GET /api/weather?city=London - Endpoint to fetch weather data from OpenWeather API
app.get('/api/weather', async (req, res) => {
try {
const city = req.query.city;
Expand Down