diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..d65d02ad21 --- /dev/null +++ b/.pre-commit-config.yaml @@ -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.sh + files: \.kt \ No newline at end of file diff --git a/README.md b/README.md index 73eb8cd271..5ec4b62a63 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,17 @@ 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/). +### Pre-commit +The precommit hook use gradle to automatically apply detekt rules. +It will not be able to work if gradle is not in your $path variable + +#### With [pre-commit](https://pre-commit.com/) tool : (use python if u don't have python use the manual setup) +- install ```pip install pre-commit``` +- then run ```pre-commit install``` +#### Manual setup : +- copy the script in ```config/detekt/detekt.sh``` in your ```.git/pre-commit.sample``` file + + ### Code quality Code formatting and standard code quality checks are performed by [Detekt](https://detekt.github.io/detekt/index.html). diff --git a/config/detekt/detekt.sh b/config/detekt/detekt.sh new file mode 100644 index 0000000000..3536f71c84 --- /dev/null +++ b/config/detekt/detekt.sh @@ -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 5 ] || [ $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 \ No newline at end of file diff --git a/shared/src/main/kotlin/com/egm/stellio/shared/model/CompactedEntity.kt b/shared/src/main/kotlin/com/egm/stellio/shared/model/CompactedEntity.kt index d9dd982169..1d99fd599b 100644 --- a/shared/src/main/kotlin/com/egm/stellio/shared/model/CompactedEntity.kt +++ b/shared/src/main/kotlin/com/egm/stellio/shared/model/CompactedEntity.kt @@ -21,7 +21,7 @@ import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_PROPERTY_TERM import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_RELATIONSHIP_TERM import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_SYSATTRS_TERMS import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_VOCABPROPERTY_TERM -import java.util.Locale +import java.util.* typealias CompactedEntity = Map @@ -133,6 +133,7 @@ fun CompactedEntity.withoutSysAttrs(sysAttrToKeep: String?): Map { * * As a GeoJSON representation may have a null value for a key, returns a Map instead of CompactedEntity. */ + fun CompactedEntity.toFinalRepresentation( ngsiLdDataRepresentation: NgsiLdDataRepresentation ): Map =