#25-Fixing bug

This commit is contained in:
Anton Romanov 2021-04-22 13:01:52 +04:00
parent 4b4194e188
commit c684d53b15
2 changed files with 62 additions and 36 deletions

View File

@ -5,46 +5,39 @@
package ru.ulstu.extractor.heuristic.controller;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
import ru.ulstu.extractor.heuristic.model.EntityUnit;
import ru.ulstu.extractor.heuristic.model.ResourceUnit;
import ru.ulstu.extractor.heuristic.service.StructuralUnitService;
import ru.ulstu.extractor.service.GitRepositoryService;
import java.io.File;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("StructuralUnitController")
public class StructuralUnitController {
private final StructuralUnitService structuralUnitService;
private final GitRepositoryService gitRepositoryService;
public StructuralUnitController(StructuralUnitService structuralUnitService,
GitRepositoryService gitRepositoryService) {
this.structuralUnitService = structuralUnitService;
public StructuralUnitController(GitRepositoryService gitRepositoryService) {
this.gitRepositoryService = gitRepositoryService;
}
@GetMapping("get-entities")
public List<EntityUnit> getEntities(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
return structuralUnitService.getEntities(rootPath);
public List<EntityUnit> getEntities(String repositoryUrl, String branchName) throws IOException, GitAPIException {
return gitRepositoryService.getEntities(repositoryUrl, branchName);
}
@GetMapping("get-business-logic")
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
return structuralUnitService.getBusinessLogic(rootPath);
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl, String branchName) throws IOException, GitAPIException {
return gitRepositoryService.getBusinessLogic(repositoryUrl, branchName);
}
@GetMapping("get-resources")
public List<ResourceUnit> getResource(String repositoryUrl) throws IOException {
File rootPath = gitRepositoryService.getProjectDirectoryFile(repositoryUrl);
return structuralUnitService.getResource(rootPath);
public List<ResourceUnit> getResource(String repositoryUrl, String branchName) throws IOException, GitAPIException {
return gitRepositoryService.getResource(repositoryUrl, branchName);
}
}

View File

@ -20,6 +20,9 @@ import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit;
import ru.ulstu.extractor.heuristic.model.EntityUnit;
import ru.ulstu.extractor.heuristic.model.ResourceUnit;
import ru.ulstu.extractor.heuristic.service.StructuralUnitService;
import ru.ulstu.extractor.model.Author;
import ru.ulstu.extractor.model.Branch;
@ -85,24 +88,10 @@ public class GitRepositoryService {
}
public List<Commit> getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException {
cloneOrUpdateRepo(repositoryUrl);
cloneOrUpdateRepo(repositoryUrl, branchName);
Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
Git git = new Git(localRepo);
git.pull().call();
if (!localRepo.getBranch().equals(branchName)) {
if (getLocalBranches(repositoryUrl).stream().anyMatch(localBranch -> localBranch.getName().contains(branchName))) {
git.checkout()
.setName(branchName)
.call();
} else {
git.checkout()
.setCreateBranch(true)
.setName(branchName)
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
.setStartPoint("origin/" + branchName)
.call();
}
}
List<RevCommit> commits = new ArrayList<>();
git.log().call().forEach(commits::add);
@ -125,32 +114,76 @@ public class GitRepositoryService {
return list;
}
private void checkoutBranch(String repositoryUrl, Git git, Repository localRepo, String branchName) throws GitAPIException, IOException {
git.pull().call();
if (!localRepo.getBranch().equals(branchName)) {
if (getLocalBranches(repositoryUrl).stream().anyMatch(localBranch -> localBranch.getName().contains(branchName))) {
git.checkout()
.setName(branchName)
.call();
} else {
git.checkout()
.setCreateBranch(true)
.setName(branchName)
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
.setStartPoint("origin/" + branchName)
.call();
}
}
git.pull().call();
}
public File getProjectDirectoryFile(String url) {
validateUrl(url);
return Path.of(getProjectDirectory(url))
.toFile();
}
public List<EntityUnit> getEntities(String repositoryUrl, String branchName) throws IOException, GitAPIException {
cloneOrUpdateRepo(repositoryUrl, branchName);
return structuralUnitService.getEntities(getProjectDirectoryFile(repositoryUrl));
}
public List<BusinessLogicUnit> getBusinessLogic(String repositoryUrl, String branchName) throws IOException, GitAPIException {
cloneOrUpdateRepo(repositoryUrl, branchName);
return structuralUnitService.getBusinessLogic(getProjectDirectoryFile(repositoryUrl));
}
public List<ResourceUnit> getResource(String repositoryUrl, String branchName) throws IOException, GitAPIException {
cloneOrUpdateRepo(repositoryUrl, branchName);
return structuralUnitService.getResource(getProjectDirectoryFile(repositoryUrl));
}
public void remove(String repositoryUrl) throws IOException {
FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl));
}
private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException {
private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException {
Git git;
if (projectDirExists(getProjectDirectoryFile(url))) {
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
Repository localRepo;
if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) {
localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
git = new Git(localRepo);
git.pull().call();
localRepo.close();
} else {
git = Git.cloneRepository()
.setURI(url)
.setDirectory(getProjectDirectoryFile(url))
.setURI(repositoryUrl)
.setDirectory(getProjectDirectoryFile(repositoryUrl))
.call();
localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
}
if (branchName != null && !branchName.isEmpty()) {
checkoutBranch(repositoryUrl, git, localRepo, branchName);
}
git.close();
}
private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException {
cloneOrUpdateRepo(url, null);
}
private String getProjectDirectory(String url) {
return (isBlank(customProjectsDir)
? System.getProperty("java.io.tmpdir")