Skip to content
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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

# Gen Commiting Application
# Commmit Craft API

<img src="src/main/resources/images/craft.jpg" alt="Craft" width="300"/>

## Requirements
- Java 17+
Expand Down Expand Up @@ -29,12 +31,12 @@

3. If you want to build the Docker image, use the following command:
```bash
docker build -t gen-commiting .
docker build -t commmit-craft .
```

4. Run the Docker container:
```bash
docker run -d -p 9000:9000 --name gen-commiting gen-commiting
docker run -d -p 8090:8090 --name commmit-craft commmit-craft
```

5. The application will be available at `http://localhost:8090`.
Expand All @@ -56,7 +58,7 @@ The `translate` module integrates with DeepL for machine translation. To use thi

## Configuration

You can specify different profiles for the application. For example, to use the `kam` profile:
You can specify different profiles for the application. For example, to use the `dev` profile:

1. In `application.yml`:
```properties
Expand Down
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.modelmapper:modelmapper:3.2.2'
implementation 'org.hibernate.validator:hibernate-validator:8.0.1.Final'
implementation 'org.glassfish:jakarta.el:4.0.2'
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
testImplementation 'org.mockito:mockito-core:4.5.1'
testImplementation 'org.mockito:mockito-junit-jupiter:4.5.1'
}

dependencyManagement {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rootProject.name = 'gen'
rootProject.name = 'commit-craft'
include 'translate'

13 changes: 13 additions & 0 deletions src/main/java/pl/commit/craft/CommitCraftApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pl.commit.craft;

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

@SpringBootApplication(scanBasePackages = {"pl.commit.craft"})
public class CommitCraftApplication {

public static void main(String[] args) {
SpringApplication.run(CommitCraftApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package pl.commit.craft.controller;

import org.springframework.web.bind.annotation.*;
import pl.commit.craft.service.CommitTranslateService;

@RestController
@RequestMapping("/api/v1/commit-translate")
public class CommitTranslateController {

private final CommitTranslateService commitTranslateService;

public CommitTranslateController(CommitTranslateService commitTranslateService) {
this.commitTranslateService = commitTranslateService;
}

@PostMapping("/craft")
public String generateCommit(@RequestBody CommitTranslateRequest commitTranslateRequest) {
return commitTranslateService.generateTranslateCommit(
commitTranslateRequest.major(),
commitTranslateRequest.type(),
commitTranslateRequest.component(),
commitTranslateRequest.changeDescription(),
commitTranslateRequest.details(),
commitTranslateRequest.wholeGitCommand(),
commitTranslateRequest.language()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package pl.commit.gen.controller;
package pl.commit.craft.controller;

public record CommitTranslateRequest(
String major,
String type,
String component,
String changeDescription,
String details,
boolean wholeGitCommand
boolean wholeGitCommand,
String language
) {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.commit.gen.error;
package pl.commit.craft.error;

import lombok.Getter;
import lombok.Setter;
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/pl/commit/craft/error/RestExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package pl.commit.craft.error;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;

@ControllerAdvice
public class RestExceptionHandler {

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request) {
ErrorResponseCommiting errorResponseCommiting = new ErrorResponseCommiting("INVALID_ARGUMENT", ex.getMessage());
return new ResponseEntity<>(errorResponseCommiting, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<Object> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("status", HttpStatus.BAD_REQUEST.value());
body.put("error", "Invalid JSON request");
body.put("message", ex.getLocalizedMessage());
body.put("path", "");

return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleGeneralException(Exception ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
body.put("error", "Internal Server Error");
body.put("message", ex.getLocalizedMessage());
body.put("path", "");

return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package pl.commit.gen.flow;
package pl.commit.craft.flow;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pl.commit.gen.service.CommitService;
import pl.commit.craft.service.CommitTranslateService;

@RestController
@RequestMapping("/api/commit-flow")
@RequestMapping("/api/v1/commit-flow")
public class CommitFlowController {

private final CommitService commitService;
private final CommitTranslateService commitTranslateService;

public CommitFlowController(CommitService commitService) {
this.commitService = commitService;
public CommitFlowController(CommitTranslateService commitTranslateService) {
this.commitTranslateService = commitTranslateService;
}

@PostMapping("/generate")
@PostMapping("/craft")
public String generateCommit(@RequestBody CommitFlowRequest commitFlowRequest) {
return commitService.generateFlowCommit(
return commitTranslateService.generateFlowCommit(
commitFlowRequest.major(),
commitFlowRequest.type(),
commitFlowRequest.component(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.commit.gen.flow;
package pl.commit.craft.flow;

record CommitFlowRequest(
String major,
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/pl/commit/craft/pattern/BasicModelPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pl.commit.craft.pattern;

sealed class BasicModelPattern permits CommitModelPattern {
private static final String DEFAULT_TARGET_LANG = "EN";

protected BasicModelPattern() {
throw new IllegalStateException("Utility class");
}

public static String getTargetLanguage(String language) {
return language == null ? DEFAULT_TARGET_LANG : language;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.commit.gen.pattern;
package pl.commit.craft.pattern;

public final class CommitModelPattern extends BasicModelPattern {
private static final String GIT_COMMAND = "git commit -m";
Expand Down Expand Up @@ -46,11 +46,12 @@ private static String getCommittingWorkPatternWithoutComponentAndDetails() {
}

/**
* Główna metoda wybierająca odpowiedni wzorzec na podstawie flagi `wholeGitCommand`, `component` i `details`.
* @param wholeGitCommand - jeśli true, zwraca wzorzec z pełnym git commit.
* @param component - nazwa komponentu, może być pusta.
* @param details - szczegóły, mogą być puste.
* @return odpowiedni wzorzec.
* Main method for selecting the appropriate pattern based on the `wholeGitCommand`, `component`, and `details` flags.
*
* @param wholeGitCommand - if true, returns a pattern with the full Git commit command.
* @param component - the name of the component, may be empty.
* @param details - additional details, may be empty.
* @return the appropriate pattern.
*/
public static String getPattern(boolean wholeGitCommand, String component, String details) {
if (wholeGitCommand) {
Expand All @@ -61,10 +62,12 @@ public static String getPattern(boolean wholeGitCommand, String component, Strin
}

/**
* Zwraca odpowiedni wzorzec z pełnym poleceniem Git, w zależności od tego, czy `component` i `details` są puste.
* @param component - nazwa komponentu.
* @param details - szczegóły.
* @return wzorzec z pełnym poleceniem Git.
* Returns the appropriate pattern with the full Git command,
* depending on whether `component` and `details` are empty or not.
*
* @param component - the name of the component.
* @param details - additional details.
* @return a pattern with the full Git commit command.
*/
private static String getPatternWithGitCommand(String component, String details) {
if (component.isEmpty() && details.isEmpty()) {
Expand All @@ -79,10 +82,12 @@ private static String getPatternWithGitCommand(String component, String details)
}

/**
* Zwraca odpowiedni wzorzec bez pełnego polecenia Git, w zależności od tego, czy `component` i `details` są puste.
* @param component - nazwa komponentu.
* @param details - szczegóły.
* @return wzorzec bez pełnego polecenia Git.
* Returns the appropriate pattern without the full Git command,
* depending on whether `component` and `details` are empty or not.
*
* @param component - the name of the component.
* @param details - additional details.
* @return a pattern without the full Git commit command.
*/
private static String getPatternWithoutGitCommand(String component, String details) {
if (component.isEmpty() && details.isEmpty()) {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/pl/commit/craft/quick/CommitQuickController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pl.commit.craft.quick;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/commit-quick")
@RequiredArgsConstructor
public class CommitQuickController {

final CommitQuickService commitQuickService;

@PostMapping("/craft")
public String generateCommit(@RequestBody CommitQuickRequest commitQuickRequest) {
return commitQuickService.generateQuickCommit(
commitQuickRequest.topicScope(),
commitQuickRequest.isGitCommand()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.commit.gen.quick;
package pl.commit.craft.quick;

record QuickCommitRequest(
record CommitQuickRequest(
String topicScope,
boolean isGitCommand
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package pl.commit.gen.quick;
package pl.commit.craft.quick;

import org.springframework.stereotype.Service;

@Service
class QuickCommitService {
class CommitQuickService {
private static final String AUDIT_COMMIT = "audit: Audit fix";
private static final String PR_FIX_COMMIT = "fix: Pull request comments improved";
private static final String TEST_FIX_COMMIT = "test: Fixed tests";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package pl.commit.gen.service;
package pl.commit.craft.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import pl.commit.gen.pattern.CommitModelPattern;
import pl.commit.translate.TranslateCommiting;
import pl.commit.craft.pattern.CommitModelPattern;
import pl.commit.craft.translate.TranslateCommitCraft;

@Service
@RequiredArgsConstructor
public class CommitService {
private final TranslateCommiting translateCommiting;
public class CommitTranslateService {
private final TranslateCommitCraft translateCommitCraft;

public String generateTranslateCommit(String major, String type, String component, String changeDescription, String details, boolean wholeGitCommand) {
public String generateTranslateCommit(String major, String type, String component, String changeDescription, String details, boolean wholeGitCommand, String language) {
if (isValidType(type)) {
throw new IllegalArgumentException("Invalid commit type: " + type);
}

MajorNumber majorNumber = MajorNumberPreparer.of(major).getMajorNumber();
String changeDescriptionTranslated = getChangeDescriptionTranslated(changeDescription);
String detailsTranslated = !details.isEmpty() ? getChangeDescriptionTranslated(details) : "";
String changeDescriptionTranslated = getChangeDescriptionTranslated(changeDescription, language);
String detailsTranslated = !details.isEmpty() ? getChangeDescriptionTranslated(details, language) : "";
String pattern = CommitModelPattern.getPattern(wholeGitCommand, component, detailsTranslated);

return String.format(
Expand All @@ -33,7 +33,7 @@ public String generateTranslateCommit(String major, String type, String componen

public String generateFlowCommit(String major, String type, String component, String changeDescription, String details, boolean wholeGitCommand) {
if (isValidType(type)) {
throw new IllegalArgumentException("Invalid commit type: " + type);
throw new IllegalArgumentException(String.format("Invalid commit type: %s", type));
}

MajorNumber majorNumber = MajorNumberPreparer.of(major).getMajorNumber();
Expand All @@ -50,8 +50,8 @@ public String generateFlowCommit(String major, String type, String component, St
).trim();
}

private String getChangeDescriptionTranslated(String changeDescription) {
return translateCommiting.translate(changeDescription, CommitModelPattern.getTargetLanguage());
private String getChangeDescriptionTranslated(String changeDescription, String language) {
return translateCommitCraft.translate(changeDescription, CommitModelPattern.getTargetLanguage(language));
}

private boolean isValidType(String type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.commit.gen.service;
package pl.commit.craft.service;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package pl.commit.gen.service;
package pl.commit.craft.service;

record MajorNumber(String issueNumber) { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.commit.gen.service;
package pl.commit.craft.service;

import lombok.Getter;

Expand Down
Loading