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

요구 사항 수정 및 Ai-Server 연동 #5

Merged
merged 6 commits into from
Sep 11, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ public class AppController {
public final AppService service;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@ResponseStatus(HttpStatus.OK)
public ReadmeItem post(@Valid @RequestBody Create createDto) throws Exception {
return service.create(createDto);
}

@PostMapping("download")
@ResponseStatus(HttpStatus.OK)
public ObjectUrl download(@Valid @RequestBody Request requestDto) throws Exception {
return service.download(requestDto);
}

@PostMapping("pull-request")
@ResponseStatus(HttpStatus.CREATED)
@ResponseStatus(HttpStatus.OK)
public void pullRequest(@Valid @RequestBody Request requestDto) throws Exception {
service.pullRequest(requestDto);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kr.markdown.alreadyme.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class ExceptionController {
@ExceptionHandler({MethodArgumentNotValidException.class})
public ResponseEntity<?> badRequestHandler(MethodArgumentNotValidException e) {
Map<String, Object> error = new HashMap<>();
error.put("timestamp", new Date());
error.put("code", "400");
error.put("message", "This github URL is invalid");
return ResponseEntity.badRequest().body(error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface ReadmeItemDto {
@NoArgsConstructor
class Create {
@NotBlank
@Pattern(regexp = "(?:https://)github.com[:/](.*).git")
@Pattern(regexp = "(?:https://)github.com[:/](.*)")
private String githubOriginalUrl;
}

Expand Down
24 changes: 20 additions & 4 deletions src/main/java/kr/markdown/alreadyme/service/AiService.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package kr.markdown.alreadyme.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RequiredArgsConstructor
@Log4j2
public class AiService {

public String getReadmeText(String requestJsonData, String githubOriginalUrl) throws JsonProcessingException {
@Value("${ai-server.host}")
private String aiServerHost;

public String getReadmeText(String requestJsonData, String githubOriginalUrl) throws IOException {

//requestJsonData
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();

Expand All @@ -23,9 +35,13 @@ public String getReadmeText(String requestJsonData, String githubOriginalUrl) th
objectNode.set("data", jsonDataNode);

//httpClient
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(aiServerHost);
httpPost.setHeader("Content-type", "application/json");

//response.toString()
httpPost.setEntity(new StringEntity(objectNode.toString(), "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);

return "create readme.md success";
return EntityUtils.toString(httpResponse.getEntity());
}
}
15 changes: 9 additions & 6 deletions src/main/java/kr/markdown/alreadyme/service/AppService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kr.markdown.alreadyme.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import kr.markdown.alreadyme.domain.dto.ReadmeItemDto.Create;
import kr.markdown.alreadyme.domain.dto.ReadmeItemDto.Request;
import kr.markdown.alreadyme.domain.dto.ReadmeItemDto.ObjectUrl;
Expand Down Expand Up @@ -50,30 +52,31 @@ public class AppService {
@Transactional
public ReadmeItem create(Create createDto) throws Exception {

//GitFork
String githubBotUrl = GithubApiUtil.gitFork(createDto.getGithubOriginalUrl(), token);
String gitUrl = createDto.getGithubOriginalUrl()+".git";

//GitClone
Git git = JGitUtil.cloneRepository(githubBotUrl);
Git git = JGitUtil.cloneRepository(gitUrl);

//FileScan
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
File gitDirectory = git.getRepository().getDirectory();
String requestJsonData = FileScanUtil.createJson(
objectNode,
new File(gitDirectory.getPath() + File.separator + "..").getCanonicalPath(),
gitDirectory.getParentFile().getName()
);

//Delete Repository
JGitUtil.close(git);
FileUtils.deleteDirectory(new File(git.getRepository().getDirectory().getParentFile().getPath()));;
GithubApiUtil.gitDeleteRemoteRepository(githubBotUrl, token);

//Get readmeText by ai-server
String readmeText = aiService.getReadmeText(requestJsonData, createDto.getGithubOriginalUrl());
String readmeText = aiService.getReadmeText(requestJsonData, gitUrl);

//Create ReadmeItem
ReadmeItem readmeItem = ReadmeItem.builder()
.githubOriginalUrl(createDto.getGithubOriginalUrl())
.githubOriginalUrl(gitUrl)
.readmeText(readmeText)
.createdTime(LocalDateTime.now())
.build();
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/kr/markdown/alreadyme/utils/FileScanUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import java.util.Set;

public class FileScanUtil {
private static ObjectMapper objectMapper = new ObjectMapper();
private static ObjectNode objectNode = objectMapper.createObjectNode();
private static final Set<String> extSet = new HashSet<>(Arrays.asList(
".c", ".h", ".cs", ".cpp", ".hpp", ".c++", ".h++", ".cc", ".hh", ".C", ".H", ".java", ".js", ".lua", ".md", ".markdown", ".php", ".php3", ".php4", ".php5", ".phps", ".phpt", ".pl", ".pm", ".pod", ".perl", ".py", ".rb", ".rs", ".ts", ".tsx", ".vb"
".c", ".h", ".cs", ".cpp", ".hpp", ".c++", ".h++", ".cc", ".hh", ".C", ".H", ".java", ".js", ".lua", ".md", ".markdown", ".php", ".php3", ".php4", ".php5", ".phps", ".phpt", ".pl", ".pm", ".pod", ".perl", ".py", ".rb", ".rs", ".ts", ".tsx", ".vb", ".kt"
));

public static String createJson(String localDirPath, String directoryName) throws IOException {
public static String createJson(ObjectNode objectNode, String localDirPath, String directoryName) throws IOException {
String userDirPath = System.getProperty("user.dir");

File path = new File(localDirPath);
Expand All @@ -33,10 +31,11 @@ public static String createJson(String localDirPath, String directoryName) throw
.replace("\\", "/"),
FileUtils.readFileToString(fileList[i], "UTF-8")
);
} else if (fileList[i].isDirectory()){
createJson(fileList[i].getPath(), directoryName);
} else if (fileList[i].isDirectory()) {
createJson(objectNode, fileList[i].getPath(), directoryName);
}
}

return objectNode.toString();
}

Expand Down