Skip to content

Commit

Permalink
[BSVR-54] Spotless pre-commit hook 추가 (#4)
Browse files Browse the repository at this point in the history
* build: spotless plugin 추가

* docs : google java code style editorconfig 추가

* build(spotless): commit 찍을 때 자동으로 spotless 돌도록 설정

* build: spotless pre-commit register 수정

* test(pre-commit): spotless pre commit 테스트

* build: 사용하지 않는 build.gradle 블럭 삭제

* build: pre-commit script 수정

* docs: codeowners 추가

* fix: github workflow 디렉터리 이름 오타 수정
  • Loading branch information
EunjiShin authored Jul 1, 2024
1 parent e7f342e commit 44a65e4
Show file tree
Hide file tree
Showing 10 changed files with 1,413 additions and 30 deletions.
1,340 changes: 1,340 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

PROJECT_ROOT=$(git rev-parse --show-toplevel)

# part1
stagedFiles=$(git diff --staged --name-only)

# part2
echo "Running spotlessApply. Formatting code..."
cd $PROJECT_ROOT && ./gradlew spotlessApply

if [ $? -ne 0 ]; then
echo "Spotless apply failed!"
exit 1
fi

# part3
for file in $stagedFiles; do
if test -f "$file"; then
git add $file
fi
done
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @EunjiShin @wjdwnsdnjs13 @pminsung12
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.depromeet.spot.application;

import jakarta.annotation.PostConstruct;
import java.util.TimeZone;

import jakarta.annotation.PostConstruct;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

Expand All @@ -18,5 +20,4 @@ public static void main(String[] args) {
public void init() {
TimeZone.setDefault(TimeZone.getTimeZone(DEFAULT_TIMEZONE_KST));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan(
basePackages = {
"org.depromeet.spot.application"
}
)
@ComponentScan(basePackages = {"org.depromeet.spot.application"})
@Configuration
public class SpotApplicationConfig {

}

public class SpotApplicationConfig {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.depromeet.spot.application.sample.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.depromeet.spot.application.sample.controller.request.SampleRequest;
import org.depromeet.spot.application.sample.controller.response.SampleResponse;
import org.springframework.http.HttpStatus;
Expand All @@ -12,6 +9,10 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

// FIXME: swagger 확인용 샘플 컨트롤러 입니다. 이후 실제 작업 시작할 때 삭제 예정이에요!
@RestController
@RequiredArgsConstructor
Expand All @@ -22,11 +23,8 @@ public class SampleController {
@PostMapping
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "swagger 테스트용 API :: 주어진 메세지를 반환한다.")
public SampleResponse testSwagger(
@RequestBody SampleRequest sampleRequest
) {
public SampleResponse testSwagger(@RequestBody SampleRequest sampleRequest) {
var message = sampleRequest.message();
return new SampleResponse(message);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.depromeet.spot.application.sample.controller.request;

import jakarta.validation.constraints.NotBlank;

import org.hibernate.validator.constraints.Length;

public record SampleRequest(
@NotBlank(message = "메세지는 공백일 수 없습니다.")
@Length(min = 1, max = 30, message = "메세지는 1~30글자 사이여야 합니다.")
String message
) {

}
@NotBlank(message = "메세지는 공백일 수 없습니다.")
@Length(min = 1, max = 30, message = "메세지는 1~30글자 사이여야 합니다.")
String message) {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
package org.depromeet.spot.application.sample.controller.response;

public record SampleResponse(
String message
) {

}
public record SampleResponse(String message) {}
34 changes: 34 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("java")
id("org.springframework.boot") version "3.0.1"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id("com.diffplug.spotless") version "6.21.0"
}

allprojects {
Expand All @@ -18,6 +19,7 @@ subprojects {
apply(plugin = "java")
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management")
apply(plugin = "com.diffplug.spotless")

java {
sourceCompatibility = JavaVersion.VERSION_17
Expand All @@ -36,6 +38,38 @@ subprojects {
testImplementation("org.junit.jupiter:junit-jupiter")
}

// 코드 포맷터 spotless
spotless {
java {
// Google Java 포맷 적용
googleJavaFormat().aosp()
// 아래 순서로 import문 정렬
importOrder("java", "javax", "jakarta", "org", "com")
// 사용하지 않는 import 제거
removeUnusedImports()
// 각 라인 끝에 있는 공백을 제거
trimTrailingWhitespace()
// 파일 끝에 새로운 라인 추가
endWithNewline()
}
}

// git commit시 자동으로 spotless 적용되도록 설정
// 최초 적용시 && script 변경시 ./gradlew compileJava 한번 실행해주세요
tasks.register<Copy>("updateGitHooks") {
from(file("${rootProject.rootDir}/.githooks/pre-commit"))
into(file("${rootProject.rootDir}/.git/hooks"))
}

tasks.register<Exec>("makeGitHooksExecutable") {
commandLine("chmod", "+x", "${rootProject.rootDir}/.git/hooks/pre-commit")
dependsOn("updateGitHooks")
}

tasks.named<JavaCompile>("compileJava") {
dependsOn("makeGitHooksExecutable")
}

tasks.test {
useJUnitPlatform()
}
Expand Down

0 comments on commit 44a65e4

Please sign in to comment.