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

feat: precommit with detekt #1181

Merged
merged 5 commits into from
Jun 25, 2024
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
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repos:
- repo: local
hooks:
- id: detekt
name: detekt check
description: Runs `detekt` on modified .kt files.
language: script
entry: config/detekt/detekt_auto_correct.sh
files: \.kt
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ If you want to build only one of the services, you can launch:

Commits follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/).


### Code quality

Code formatting and standard code quality checks are performed by [Detekt](https://detekt.github.io/detekt/index.html).
Expand All @@ -144,6 +145,17 @@ that applies changed code refactoring and optimized imports on a save.

* You can enable Detekt support with the [Detekt plugin](https://github.com/detekt/detekt-intellij-plugin).

* You can also setup a precommit hook to run detekt autocorrect automatically

### Pre-commit
#### Automatic setup with [pre-commit](https://pre-commit.com/) tool
(if you don't have Python installed, use the manual setup below)
* install ```pip install pre-commit```
* then run ```pre-commit install```
#### Manual setup
* copy the script in ```config/detekt/detekt_auto_correct.sh``` in your ```.git/pre-commit``` file


### Working locally with Docker images

To work locally with a Docker image of a service without publishing it to Docker Hub, you can follow the below instructions:
Expand Down
65 changes: 65 additions & 0 deletions config/detekt/detekt_auto_correct.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash
echo "Running detekt check..."
OUTPUT="/tmp/detekt-$(date +%s)"
COUNTER=0
EXIT_CODE=1




until [ $COUNTER -gt 3 ] || [ $EXIT_CODE -eq 0 ]
do
./gradlew detekt --auto-correct > $OUTPUT
EXIT_CODE=$?
echo "execute $COUNTER time"
cat $OUTPUT
COUNTER=$((COUNTER+1))
echo "detekt return $EXIT_CODE"
done
rm $OUTPUT

if [ $EXIT_CODE -eq 0 ] && [ $COUNTER -eq 1 ]; then
echo "***********************************************"
echo " Validation succeeded "
echo "***********************************************"
echo ""
exit 0
fi

if [ $EXIT_CODE -eq 0 ] ; then
echo "*************************************************"
echo " Validation failed "
echo " Fix succeeded "
echo ""
echo " The fix were successfully applied "
echo "You can retry the commit with the applied changes"
echo "*************************************************"
echo ""
exit 1
fi

if [ $EXIT_CODE -eq 1 ]; then
echo "***********************************************"
echo " detekt failed "
echo " unexpected error "
echo "***********************************************"
echo ""
exit 1
fi

if [ $EXIT_CODE -eq 2 ]; then
echo "***********************************************"
echo " detekt failed "
echo " Please fix the above issues before committing "
echo "***********************************************"
exit 1
fi

if [ $EXIT_CODE -eq 2 ]; then
echo "***********************************************"
echo " detekt failed "
echo " Invalid detekt configuration file "
echo "***********************************************"
echo ""
exit 1
fi
Loading