Android Kotlin Code Quality Check using Detekt and Git Hooks Pre-push (Mac and Linux)

heri sulistiyanto
Binar Academy
Published in
3 min readOct 30, 2018
quality check

Lately on Android apps development in my team sometimes forget to check their code before push or raise merge request into protected branch. So I decide to put filter before they push their code into their feature branch using Git hooks.

As i mentioned before on my story, we use Kotlin Detekt as static code analyser (here) on gradle task. Sometimes member of the team forget to run it, and as a result I have to put several comment on their merge request which can be prevented before it raised.

Ok, so let’s begin. . .

So when we work with Git repo, in our project will have a directory named .git (it will showed up when you run ls -al on your root project directory). And inside of this folder will contain several Git properties and one of it is a directory named hooks (.git/hooks). If we proceed to look inside of it, it will contain several of Git lifecycle which ended with .sample, for example :

applypatch-msg.sample     post-update.sample        
pre-commit.sample pre-push.sample
pre-receive.sample update.sample
commit-msg.sample pre-applypatch.sample pre-rebase.sample prepare-commit-msg.sample

The .sample extension is work as a template and it can be override. So if we want to do something (as my plan is to intercept before push, so we will focus on pre-push).

The first step I did, I write a script named pre-push (without .sample at the end) and I save this script in path ${rootDir}/binar-common-config-android/hooks/

#!/bin/sh

echo "Running static analysis..."

# Validate Kotlin code with detekt
./gradlew app:detekt --stacktrace --no-daemon

status=$?

if [ "$status" = 0 ] ; then
echo "Static analysis found no issues. Proceeding with push."
exit 0
else
echo 1>&2 "Static analysis found issues you need to fix before pushing."
exit 1
fi

As you can see on the script above, it just a simple script to run task named detekt and it will check the status result. If there is 0 then no issue found and vice versa.

So how we can run this script and add this script to all of my team member?

The second step is, I write a gradle file named git-hooks.gradle which it looks like this :

As you can see on git-hooks.gradle above, I write 2 tasks:

copyGitHooks :

this task will copy our pre-push script into ${rootDir}/.git/hooks

installGitHooks :

This task will do something as the same as we run chmod +x to our pre-push script file

On after evaluate block, we can see that clean & assemble depending on installGitHooks, that mean if we run one of the task (./gradlew clean or ./gradlew assemble) it will install our pre-push script.

When the pre-push script already installed, every time we want to push our code into remote repo, it will check our code first by running detekt task before it can proceed to push step.

Ok, that’s it . . quite simple right?

bravo

If you find this article is helpful please give your warmest clap :)

Thanks

--

--