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

pre-commit Git Hooks Implementation #47

Merged
merged 4 commits into from
Aug 18, 2023
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
35 changes: 18 additions & 17 deletions .github/workflows/android_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ on:
- development

jobs:
test:
name: Run Unit Tests
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set Up JDK 18
uses: actions/setup-java@v3
with:
java-version: '18'
distribution: 'temurin'
cache: gradle

- name: Unit tests
run: bash ./gradlew test --stacktrace
# Unit test implementation added through git hooks
# test:
# name: Run Unit Tests
# runs-on: ubuntu-latest
#
# steps:
# - name: Checkout
# uses: actions/checkout@v3
#
# - name: Set Up JDK 18
# uses: actions/setup-java@v3
# with:
# java-version: '18'
# distribution: 'temurin'
# cache: gradle
#
# - name: Unit tests
# run: bash ./gradlew test --stacktrace

build:
name: Build Project
Expand Down
23 changes: 23 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,26 @@ plugins {
alias(libs.plugins.kotlin.kapt) apply false
alias(libs.plugins.kotlin.ksp) apply false
}

tasks.register("copyGitHooks", Copy::class.java) {
description = "Copies the git hooks from /git-hooks to the .git folder."
group = "git hooks"
from("$rootDir/git-hooks/pre-commit")
into("$rootDir/.git/hooks/")
}

tasks.register("installGitHooks", Exec::class.java) {
description = "Installs the pre-commit git hooks from /git-hooks."
group = "git hooks"
workingDir = rootDir
commandLine = listOf("chmod")
args("-R", "+x", ".git/hooks/")
dependsOn("copyGitHooks")
doLast {
logger.info("Git hook installed successfully.")
}
}

afterEvaluate {
tasks.getByPath(":app:preBuild").dependsOn(":installGitHooks")
}
19 changes: 19 additions & 0 deletions git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
#
# pre-commit hook will run every time when we try to commit code through Android Studio IDE or Command Line
# - It will run the unit test before committing code
# It will return exit 0 if everything well and good else return other than zero with valid message to fix it.
#

echo "Running Unit Tests ..."
bash ./gradlew test --stacktrace

status=$?

if [ "$status" = 0 ] ; then
echo "Unit test passed successfully."
exit 0
else
echo 1>&2 "Unit test failed."
exit 1
fi