From a2220d31df769bdf626dd2b505dae4a08a40387e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rk=20K=C5=91v=C3=A1ri?= Date: Sun, 14 Jul 2024 13:00:06 +0200 Subject: [PATCH] feat(lint): add instructions and setup script for maven checklist --- README.md | 17 ++++++++++++++++ setup-pre-commit-hook.sh | 18 +++++++++++++++++ utils/pre-commit | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100755 setup-pre-commit-hook.sh create mode 100755 utils/pre-commit diff --git a/README.md b/README.md index 7af1611e..d0343f8c 100644 --- a/README.md +++ b/README.md @@ -19,3 +19,20 @@ It was created with Vite npm install vite@latest ``` +## Contributing + +### Precommit-hooks + +This project uses precommit-hooks to ensure that the code is formatted correctly and that the tests pass before committing. To enable this, you need to install the precommit-hooks: + +[:warning: **Important**] You need to have `maven` being installed in your machine to run the pre-commit hooks. +Although the project is using `maven-wrapper`, the pre-commit hooks are using the `mvn` command directly. +If the binary cannot be found please follow the instructions of the pre-commit hook to install `maven`. + +To install the pre-commit hooks, run the following commands: + +```bash +chmod +x ./setup-pre-commit-hook.sh +sh ./setup-pre-commit-hook.sh +``` + diff --git a/setup-pre-commit-hook.sh b/setup-pre-commit-hook.sh new file mode 100755 index 00000000..fc90f798 --- /dev/null +++ b/setup-pre-commit-hook.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# This script is used to install a git pre-commit hook that will run the + +if [ -f .git/hooks/pre-commit ]; then + echo "Pre-commit hook already exists, good job!" + echo "If you want to re-install the hook, please remove the .git/hooks/pre-commit file first" + exit 1 +fi + +# Copy the pre-commit script to the .git/hooks directory +cp ./utils/pre-commit .git/hooks/pre-commit +# Make the script executable +chmod +x .git/hooks/pre-commit + +echo "Pre-commit hook installed, you should be good to go" + +# Currently it is for backend, but we will add frontend as well. \ No newline at end of file diff --git a/utils/pre-commit b/utils/pre-commit new file mode 100755 index 00000000..8dd3faee --- /dev/null +++ b/utils/pre-commit @@ -0,0 +1,42 @@ +#!/bin/sh + +## This script is used to run tests and checkstyle before commiting the code +## If the tests or checkstyle fails, the commit will be aborted + +# Change to the backend directory +cd backend + +# Check if there are any changes to commit + +if [ -z "$(git status --porcelain)" ]; then + # Working directory clean, nothing to commit, not running things + echo "No changes to commit in backend" +else + # Working directory not clean, running tests and checkstyle + ## Looking for maven binary + if ! [ -x "$(command -v mvn)" ]; then + ## If maven not found, print error message and exit + echo 'Error: maven is not installed.' >&2 + echo 'Please install maven (with the recommended package managers) and try again' >&2 + echo 'On mac, you can install maven using brew: brew install maven + brew -> https://brew.sh/ + maven -> brew install maven' >&2 + echo 'on linux, you can install maven using apt: sudo apt install maven' >&2 + echo 'on windows, you can install maven using chocolatey: choco install maven\ + chocolatey -> https://chocolatey.org/ + maven -> choco install maven' >&2 + exit 1 + else + ## If maven is found, run tests and checkstyle + echo "Running tests and checkstyle" + local maven_result = $(mvn clean install checkstyle:check) + if [ $? -eq 0 ]; then + echo "Tests and checkstyle passed, hopefully the watchdog will be happy too" + else + echo "Tests or checkstyle failed" + echo "Please fix the issues and try again" + echo "Aborting commit" + exit 1 + fi + fi +fi \ No newline at end of file