Skip to content

Commit

Permalink
Merge pull request #47 from swapnil-musale/git_hooks_pre-commit
Browse files Browse the repository at this point in the history
pre-commit Git Hooks Implementation
  • Loading branch information
swap-musale authored Aug 18, 2023
2 parents b9ce4aa + 34b6c15 commit bfa6d92
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
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

0 comments on commit bfa6d92

Please sign in to comment.