diff --git a/src/main/java/ru/ulstu/extractor/config/MvcConfiguration.java b/src/main/java/ru/ulstu/extractor/config/MvcConfiguration.java index 2355ca1..035f359 100644 --- a/src/main/java/ru/ulstu/extractor/config/MvcConfiguration.java +++ b/src/main/java/ru/ulstu/extractor/config/MvcConfiguration.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.config; import org.springframework.context.annotation.Configuration; @@ -5,12 +10,14 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import static ru.ulstu.extractor.controller.Route.LIST_INDEXED_REPOSITORIES; + @Configuration public class MvcConfiguration implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/{articlename:\\w+}"); - registry.addRedirectViewController("/", "/newRepo"); + registry.addRedirectViewController("/", LIST_INDEXED_REPOSITORIES); registry.addRedirectViewController("/default", "/home"); } diff --git a/src/main/java/ru/ulstu/extractor/controller/BranchController.java b/src/main/java/ru/ulstu/extractor/controller/BranchController.java index 8254210..031dc48 100644 --- a/src/main/java/ru/ulstu/extractor/controller/BranchController.java +++ b/src/main/java/ru/ulstu/extractor/controller/BranchController.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.controller; import org.springframework.stereotype.Controller; @@ -7,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.extractor.repository.BranchRepository; import ru.ulstu.extractor.repository.RepositoryRepository; +import static ru.ulstu.extractor.controller.Route.LIST_REPOSITORY_BRANCHES; + @Controller public class BranchController { private final RepositoryRepository repositoryRepository; @@ -17,12 +24,12 @@ public class BranchController { this.branchRepository = branchRepository; } - @GetMapping("/details") + @GetMapping(LIST_REPOSITORY_BRANCHES) public String indexBranch( Model model, @RequestParam int repositoryId) { model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId)); model.addAttribute("repository", repositoryRepository.findById(repositoryId).get()); - return "indexBranch"; + return LIST_REPOSITORY_BRANCHES; } } diff --git a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java index 85ef6aa..87df222 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.controller; import org.springframework.data.domain.Page; @@ -11,36 +16,24 @@ import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.OffsetablePageRequest; import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.service.FilteringService; -import ru.ulstu.extractor.service.IndexService; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.IntStream; +import static ru.ulstu.extractor.controller.Route.FILTER_COMMITS; + @Controller public class GitFilteringController { + private final static int DEFAULT_PAGE_SIZE = 20; private final FilteringService filteringService; - private final IndexService indexService; - public GitFilteringController(FilteringService filteringService, - IndexService indexService) { + public GitFilteringController(FilteringService filteringService) { this.filteringService = filteringService; - this.indexService = indexService; } - /* @PostMapping("/sendFilter") - public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) throws GitAPIException, IOException { - List list = gitRepositoryService.getCommits(filterForm.getUrl(), filterForm.getBranch()); - model.addAttribute("commits", list); - if (filterForm.getFilter() == null || filterForm.getFilter().isEmpty()) { - model.addAttribute("error", "'Строка' не должно быть пустым"); - return "filtering"; - } - return "resultRepo"; - }*/ - - @RequestMapping(value = "/filtering", method = RequestMethod.GET) + @RequestMapping(value = FILTER_COMMITS, method = RequestMethod.GET) public String listCommits( Model model, @ModelAttribute FilterForm filterForm, @@ -49,9 +42,11 @@ public class GitFilteringController { @RequestParam String repositoryUrl, @RequestParam String branchName) { int currentPage = page.orElse(1); - int pageSize = size.orElse(5); + int pageSize = size.orElse(DEFAULT_PAGE_SIZE); - Page commitsPage = indexService.getCommits(repositoryUrl, branchName, new OffsetablePageRequest(currentPage, pageSize)); + Page commitsPage = filteringService.getCommits(repositoryUrl, + branchName, + new OffsetablePageRequest(currentPage - 1, pageSize)); int totalPages = commitsPage.getTotalPages(); if (totalPages > 0) { List pageNumbers = IntStream.rangeClosed(1, totalPages) @@ -59,11 +54,11 @@ public class GitFilteringController { .collect(Collectors.toList()); model.addAttribute("pageNumbers", pageNumbers); } - filterForm = new FilterForm(); filterForm.setCommitsPage(commitsPage); filterForm.setBranch(branchName); filterForm.setUrl(repositoryUrl); model.addAttribute("filterForm", filterForm); - return "filtering"; + model.addAttribute("authors", filteringService.getRepositoryAuthors(repositoryUrl, branchName)); + return FILTER_COMMITS; } } diff --git a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java index 18ddb48..fb6f694 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java @@ -1,5 +1,13 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.controller; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -13,10 +21,15 @@ import ru.ulstu.extractor.model.mvc.RepoForm; import ru.ulstu.extractor.service.GitRepositoryService; import ru.ulstu.extractor.service.IndexService; +import java.io.IOException; import java.util.List; +import static ru.ulstu.extractor.controller.Route.FILTER_COMMITS; +import static ru.ulstu.extractor.controller.Route.INDEXING_NEW_REPOSITORY; + @Controller public class GitIndexingController { + private final static Logger LOG = LoggerFactory.getLogger(GitIndexingController.class); private final GitRepositoryService gitRepositoryService; private final IndexService indexService; @@ -26,40 +39,39 @@ public class GitIndexingController { this.indexService = indexService; } - @GetMapping("/newRepo") + @GetMapping(INDEXING_NEW_REPOSITORY) public String indexNewRepo(Model model) { model.addAttribute(new RepoForm()); - return "newRepo"; + return INDEXING_NEW_REPOSITORY; } - @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "send") + @RequestMapping(value = INDEXING_NEW_REPOSITORY, method = RequestMethod.POST, params = "send") public String getBranch(@ModelAttribute RepoForm repoForm, Model model) { try { - gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo()); - List branches = gitRepositoryService.getBranches(repoForm.getRepo()); + List branches = gitRepositoryService.getRemoteBranches(repoForm.getRepo()); model.addAttribute("branches", branches); - return "newRepo"; } catch (Exception ex) { model.addAttribute("error", ex.getMessage()); - return "newRepo"; } + return INDEXING_NEW_REPOSITORY; } - @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "next") - public String setBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) { + @RequestMapping(value = INDEXING_NEW_REPOSITORY, method = RequestMethod.POST, params = "next") + public String indexBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) { model.addAttribute("filterForm", new FilterForm(repoForm.getRepo())); if (repoForm.getBranch() == null) { - return "newRepo"; + return INDEXING_NEW_REPOSITORY; } else { try { indexService.index(repoForm.getRepo(), repoForm.getBranch()); - } catch (Exception ex) { + } catch (IOException | GitAPIException ex) { + ex.printStackTrace(); model.addAttribute("error", ex.getMessage()); - return "newRepo"; + return INDEXING_NEW_REPOSITORY; } redirectAttributes.addAttribute("repositoryUrl", repoForm.getRepo()); redirectAttributes.addAttribute("branchName", repoForm.getBranch()); - return "redirect:/filtering"; + return "redirect:/" + FILTER_COMMITS; } } } diff --git a/src/main/java/ru/ulstu/extractor/controller/RepoController.java b/src/main/java/ru/ulstu/extractor/controller/RepoController.java index 09ea5d5..98fafbf 100644 --- a/src/main/java/ru/ulstu/extractor/controller/RepoController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RepoController.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.controller; import org.eclipse.jgit.api.errors.GitAPIException; @@ -7,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.service.GitRepositoryService; +import ru.ulstu.extractor.service.IndexService; import java.io.IOException; import java.util.List; @@ -18,14 +24,12 @@ import static ru.ulstu.extractor.controller.RepoController.URL; public class RepoController { public static final String URL = "/"; private final GitRepositoryService gitRepositoryService; + private final IndexService indexService; - public RepoController(GitRepositoryService gitRepositoryService) { + public RepoController(GitRepositoryService gitRepositoryService, + IndexService indexService) { this.gitRepositoryService = gitRepositoryService; - } - - @GetMapping("clone") - public void cloneRepository(@RequestParam("url") String url) throws GitAPIException, IOException { - gitRepositoryService.cloneOrUpdateRepo(url); + this.indexService = indexService; } @GetMapping("commits") @@ -34,4 +38,11 @@ public class RepoController { return gitRepositoryService.getCommits(repositoryUrl, branchName); } + @GetMapping("index") + public Boolean indexRepository(@RequestParam("repositoryUrl") String repositoryUrl, + @RequestParam("branchName") String branchName) throws GitAPIException, IOException { + indexService.index(repositoryUrl, branchName); + return true; + } + } diff --git a/src/main/java/ru/ulstu/extractor/controller/BaseIndexingController.java b/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java similarity index 54% rename from src/main/java/ru/ulstu/extractor/controller/BaseIndexingController.java rename to src/main/java/ru/ulstu/extractor/controller/RepositoryController.java index e2a154a..9401504 100644 --- a/src/main/java/ru/ulstu/extractor/controller/BaseIndexingController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.controller; import org.springframework.stereotype.Controller; @@ -5,17 +10,19 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import ru.ulstu.extractor.repository.RepositoryRepository; +import static ru.ulstu.extractor.controller.Route.LIST_INDEXED_REPOSITORIES; + @Controller -public class BaseIndexingController { +public class RepositoryController { private final RepositoryRepository repositoryRepository; - public BaseIndexingController(RepositoryRepository repositoryRepository) { + public RepositoryController(RepositoryRepository repositoryRepository) { this.repositoryRepository = repositoryRepository; } - @GetMapping("/indexRepo") + @GetMapping(LIST_INDEXED_REPOSITORIES) public String indexNewRepo(Model model) { model.addAttribute("repositories", repositoryRepository.findAll()); - return "indexRepo"; + return LIST_INDEXED_REPOSITORIES; } } diff --git a/src/main/java/ru/ulstu/extractor/controller/Route.java b/src/main/java/ru/ulstu/extractor/controller/Route.java new file mode 100644 index 0000000..34853a5 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/controller/Route.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + +package ru.ulstu.extractor.controller; + +import org.springframework.stereotype.Component; + +@Component +public class Route { + public static final String LIST_INDEXED_REPOSITORIES = "listRepositories"; + public static final String LIST_REPOSITORY_BRANCHES = "listBranches"; + public static final String INDEXING_NEW_REPOSITORY = "indexNewRepository"; + public static final String FILTER_COMMITS = "filterCommits"; + + public static String getLIST_INDEXED_REPOSITORIES() { + return LIST_INDEXED_REPOSITORIES; + } + + public static String getLIST_REPOSITORY_BRANCHES() { + return LIST_REPOSITORY_BRANCHES; + } + + public static String getINDEXING_NEW_REPOSITORY() { + return INDEXING_NEW_REPOSITORY; + } + + public static String getFILTER_COMMITS() { + return FILTER_COMMITS; + } +} diff --git a/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java index 6180b13..0e01fd9 100644 --- a/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java +++ b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.model.mvc; import org.springframework.data.domain.Page; @@ -7,11 +12,20 @@ public class FilterForm { private String filter; private String url; private String branch; + private String author; private Page commitsPage; public FilterForm() { } + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + public FilterForm(String url) { this.url = url; } diff --git a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java index 504aaf2..a99a1b1 100644 --- a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.repository; import org.springframework.data.jpa.repository.JpaRepository; @@ -9,6 +14,6 @@ import ru.ulstu.extractor.model.Repository; import java.util.List; public interface AuthorRepository extends JpaRepository { - @Query("SELECT a FROM Commit c, Repository r, Branch b, Author a WHERE c.author = a AND c.branch = b AND r = b.repository AND r = :repository AND b.name = :branchName") - List findByRepositoryAndBranch(@Param("repository") Repository repository, @Param("branchName") String branchName); + @Query("SELECT DISTINCT a.name FROM Commit c, Repository r, Branch b, Author a WHERE c.author = a AND c.branch = b AND r = b.repository AND r = :repository AND b.name = :branchName AND a.name IS NOT NULL AND a.name <> '' ORDER BY a.name") + List findByRepositoryAndBranch(@Param("repository") Repository repository, @Param("branchName") String branchName); } diff --git a/src/main/java/ru/ulstu/extractor/service/FilteringService.java b/src/main/java/ru/ulstu/extractor/service/FilteringService.java index b6b7cf7..52cca2d 100644 --- a/src/main/java/ru/ulstu/extractor/service/FilteringService.java +++ b/src/main/java/ru/ulstu/extractor/service/FilteringService.java @@ -1,13 +1,61 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.service; +import com.sun.istack.NotNull; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.Commit; +import ru.ulstu.extractor.repository.AuthorRepository; +import ru.ulstu.extractor.repository.CommitRepository; +import ru.ulstu.extractor.repository.RepositoryRepository; + +import java.util.List; @Service public class FilteringService { + private final AuthorRepository authorRepository; + private final CommitRepository commitRepository; + private final RepositoryRepository repositoryRepository; + + public FilteringService(AuthorRepository authorRepository, + CommitRepository commitRepository, + RepositoryRepository repositoryRepository) { + this.authorRepository = authorRepository; + this.commitRepository = commitRepository; + this.repositoryRepository = repositoryRepository; + } - private final IndexService indexService; + public List getRepositoryAuthors(@NotNull String repositoryUrl, + @NotNull String branchName) { + return authorRepository.findByRepositoryAndBranch( + repositoryRepository.findByUrl(repositoryUrl), + branchName + ); + } - public FilteringService(IndexService indexService) { - this.indexService = indexService; + public Page getCommits(@NotNull String repositoryUrl, + @NotNull String branchName, + Pageable pageable) { + return commitRepository.findByRepositoryAndBranch( + pageable, + repositoryRepository.findByUrl(repositoryUrl), + branchName + ); } + + /* @PostMapping("/sendFilter") + public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) throws GitAPIException, IOException { + List list = gitRepositoryService.getCommits(filterForm.getUrl(), filterForm.getBranch()); + model.addAttribute("commits", list); + if (filterForm.getFilter() == null || filterForm.getFilter().isEmpty()) { + model.addAttribute("error", "'Строка' не должно быть пустым"); + return "filtering"; + } + return "resultRepo"; + }*/ } diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index 558b179..87ac342 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.service; import org.apache.commons.io.FileUtils; @@ -7,7 +12,6 @@ import org.eclipse.jgit.api.ListBranchCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.internal.storage.file.FileRepository; -import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.springframework.beans.factory.annotation.Value; @@ -21,6 +25,9 @@ import ru.ulstu.extractor.model.LineChange; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; import java.nio.file.Path; import java.time.Instant; import java.util.ArrayList; @@ -37,39 +44,32 @@ public class GitRepositoryService { @Value("${extractor.custom-projects-dir}") private String customProjectsDir; - public void cloneOrUpdateRepo(String url) throws GitAPIException, IOException { - Git git; - if (projectDirExists(getProjectDirectoryFile(url))) { - Repository localRepo = new FileRepository(getProjectGitDirectory(url)); - git = new Git(localRepo); - git.pull().call(); - localRepo.close(); - } else { - git = Git.cloneRepository() - .setURI(url) - .setDirectory(getProjectDirectoryFile(url)) - .call(); - } + public List getRemoteBranches(String url) throws GitAPIException, IOException { + cloneOrUpdateRepo(url); + Repository localRepo = new FileRepository(getProjectGitDirectory(url)); + Git git = new Git(localRepo); + List branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE) + .call() + .stream() + .map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, ""))) + .collect(Collectors.toList()); git.close(); + localRepo.close(); + return branches; } - private String getProjectDirectory(String url) { - return (isBlank(customProjectsDir) - ? System.getProperty("java.io.tmpdir") - : customProjectsDir) + url.substring(url.lastIndexOf('/')); - } - - private String getProjectGitDirectory(String url) { - return getProjectDirectory(url) + "/.git"; - } - - private File getProjectDirectoryFile(String url) { - return Path.of(getProjectDirectory(url)) - .toFile(); - } - - private boolean projectDirExists(File file) { - return file.exists(); + public List getLocalBranches(String url) throws GitAPIException, IOException { + cloneOrUpdateRepo(url); + Repository localRepo = new FileRepository(getProjectGitDirectory(url)); + Git git = new Git(localRepo); + List branches = git.branchList() + .call() + .stream() + .map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, ""))) + .collect(Collectors.toList()); + git.close(); + localRepo.close(); + return branches; } public List getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException { @@ -78,12 +78,18 @@ public class GitRepositoryService { Git git = new Git(localRepo); git.pull().call(); if (!localRepo.getBranch().equals(branchName)) { - Ref ref = git.checkout(). - setCreateBranch(true). - setName(branchName). - setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). - setStartPoint("origin/" + branchName). - call(); + 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 commits = new ArrayList<>(); git.log().call().forEach(commits::add); @@ -107,7 +113,58 @@ public class GitRepositoryService { return list; } - public List findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) { + public void remove(String repositoryUrl) throws IOException { + FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl)); + } + + private void cloneOrUpdateRepo(String url) throws GitAPIException, IOException { + Git git; + if (projectDirExists(getProjectDirectoryFile(url))) { + Repository localRepo = new FileRepository(getProjectGitDirectory(url)); + git = new Git(localRepo); + git.pull().call(); + localRepo.close(); + } else { + git = Git.cloneRepository() + .setURI(url) + .setDirectory(getProjectDirectoryFile(url)) + .call(); + } + git.close(); + } + + private String getProjectDirectory(String url) { + return (isBlank(customProjectsDir) + ? System.getProperty("java.io.tmpdir") + : customProjectsDir) + url.substring(url.lastIndexOf('/')); + } + + private String getProjectGitDirectory(String url) { + return getProjectDirectory(url) + "/.git"; + } + + private File getProjectDirectoryFile(String url) { + validateUrl(url); + return Path.of(getProjectDirectory(url)) + .toFile(); + } + + private void validateUrl(String url) { + if (url == null || url.isEmpty()) { + throw new RuntimeException("Repository url must not empty"); + } + try { + new URL(url).toURI(); + } catch (MalformedURLException | URISyntaxException e) { + throw new RuntimeException("Repository url not valid"); + } + } + + private boolean projectDirExists(File file) { + return file.exists(); + } + + private List findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) { if (laterCommit == null || earlierCommit == null) { return null; } @@ -205,22 +262,4 @@ public class GitRepositoryService { } return Optional.empty(); } - - public List getBranches(String url) throws GitAPIException, IOException { - cloneOrUpdateRepo(url); - Repository localRepo = new FileRepository(getProjectGitDirectory(url)); - Git git = new Git(localRepo); - List branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE) - .call() - .stream() - .map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, ""))) - .collect(Collectors.toList()); - git.close(); - localRepo.close(); - return branches; - } - - public void remove(String repositoryUrl) throws IOException { - FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl)); - } } diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java index 332a3e1..44b7414 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -1,15 +1,17 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.service; import com.sun.istack.NotNull; import org.eclipse.jgit.api.errors.GitAPIException; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; -import ru.ulstu.extractor.model.Author; +import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Repository; -import ru.ulstu.extractor.repository.AuthorRepository; import ru.ulstu.extractor.repository.BranchRepository; import ru.ulstu.extractor.repository.CommitRepository; import ru.ulstu.extractor.repository.RepositoryRepository; @@ -23,43 +25,32 @@ public class IndexService { private final RepositoryRepository repositoryRepository; private final BranchRepository branchRepository; private final CommitRepository commitRepository; - private final AuthorRepository authorRepository; - public IndexService(GitRepositoryService gitRepositoryService, RepositoryRepository repositoryRepository, BranchRepository branchRepository, - CommitRepository commitRepository, - AuthorRepository authorRepository) { + CommitRepository commitRepository) { this.gitRepositoryService = gitRepositoryService; this.repositoryRepository = repositoryRepository; this.branchRepository = branchRepository; this.commitRepository = commitRepository; - this.authorRepository = authorRepository; } + @Transactional public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { Repository repository = repositoryRepository.findByUrl(repositoryUrl); if (repository == null) { - repositoryRepository.save(new Repository(repositoryUrl)); + repository = repositoryRepository.save(new Repository(repositoryUrl)); } Branch branch = branchRepository.findByRepositoryAndName(repository, branchName); if (branch == null) { - branchRepository.save(new Branch(repository, branchName)); + branch = branchRepository.save(new Branch(repository, branchName)); } List commits = gitRepositoryService.getCommits(repositoryUrl, branchName); - commitRepository.deleteAll(branch.getCommits()); + List commitsToRemove = branch.getCommits(); branch.getCommits().clear(); + commitRepository.deleteAll(commitsToRemove); branch.setCommits(commits); branchRepository.save(branch); - gitRepositoryService.remove(repositoryUrl); - } - - public List getRepositoryAuthors(@NotNull String repositoryUrl, @NotNull String branchName) { - return authorRepository.findByRepositoryAndBranch(repositoryRepository.findByUrl(repositoryUrl), branchName); - } - - public Page getCommits(@NotNull String repositoryUrl, @NotNull String branchName, Pageable pageable) { - return commitRepository.findByRepositoryAndBranch(pageable, repositoryRepository.findByUrl(repositoryUrl), branchName); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 938472e..603e99a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,11 @@ +# +# Copyright (C) 2021 Anton Romanov - All Rights Reserved +# You may use, distribute and modify this code, please write to: romanov73@gmail.com. +# + spring.main.banner-mode=off server.port=8080 +server.jetty.connection-idle-timeout=1000s # Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF logging.level.ru.ulstu=DEBUG extractor.custom-projects-dir= diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 1db2a89..254f42c 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -27,10 +27,12 @@
diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index 0017912..ce4f465 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -1,18 +1,17 @@ + + - -

Ошибка

-

Страница: Page URL

@@ -34,11 +33,13 @@

${url}
-

-

${exception.message}

+
+ ${exception.message} +
    -
  • ${ste}
diff --git a/src/main/resources/templates/filterCommits.html b/src/main/resources/templates/filterCommits.html new file mode 100644 index 0000000..17c6039 --- /dev/null +++ b/src/main/resources/templates/filterCommits.html @@ -0,0 +1,105 @@ + + + + +
+ +
+
+
+ Автор +
+
+ +
+
+ Дата: +
+
+ +
+
+ - +
+
+ +
+
+
+
+ Искать по тексту: +
+
+ +
+
+ +
+
+ + + + + + + + + + + + + + + + + +
АвторДатаСообщение
+

+ +

+
+ Страницы: + +
+ diff --git a/src/main/resources/templates/filtering.html b/src/main/resources/templates/filtering.html deleted file mode 100644 index 365ac17..0000000 --- a/src/main/resources/templates/filtering.html +++ /dev/null @@ -1,58 +0,0 @@ - - - - Простая обработка формы на Spring MVC - - -
-
-

Фильтровать данные:

- По автору - - Дата с - - по - -

Строки:
- - -

-

- - - - - - - - - - - - - - - -
AuthorDateCommit
-

- -

-
-
- - diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html deleted file mode 100644 index 939171f..0000000 --- a/src/main/resources/templates/index.html +++ /dev/null @@ -1,32 +0,0 @@ - - - - Простая обработка формы на Spring MVC - - -
-

Форма

-
-

- - - - - - - - - - - -
Тема:
Кому:
Сообщение: