From ed8c6edfe2cffbfd665dce42b92053769263aa93 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 24 Mar 2021 15:22:53 +0400 Subject: [PATCH 01/15] #10 - add table --- build.gradle | 1 + .../extractor/mvc/GitExtractorController.java | 58 ++++++++++++++++--- .../extractor/service/CommitService.java | 40 +++++++++++++ src/main/resources/templates/filtering.html | 36 +++++++----- src/main/resources/templates/newRepo.html | 2 - 5 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/service/CommitService.java diff --git a/build.gradle b/build.gradle index 4cd6c5e..0d2a506 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,7 @@ dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect' + compile group: 'org.springframework.data', name: 'spring-data-commons' compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc' diff --git a/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java index 676e7f2..77b4c1f 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java +++ b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java @@ -1,5 +1,8 @@ package ru.ulstu.extractor.mvc; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -7,23 +10,34 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.mvc.model.FilterForm; import ru.ulstu.extractor.mvc.model.RepoForm; +import ru.ulstu.extractor.service.CommitService; import ru.ulstu.extractor.service.FilteringService; import ru.ulstu.extractor.service.GitRepositoryService; +import java.io.IOException; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; @Controller public class GitExtractorController { private final FilteringService filteringService; private final GitRepositoryService gitRepositoryService; + private final CommitService commitService; public GitExtractorController(FilteringService filteringService, - GitRepositoryService gitRepositoryService) { + GitRepositoryService gitRepositoryService, + CommitService commitService) { this.filteringService = filteringService; this.gitRepositoryService = gitRepositoryService; + this.commitService = commitService; } @GetMapping("/newRepo") @@ -32,14 +46,16 @@ public class GitExtractorController { return "newRepo"; } - @GetMapping("/filtering") - public String filter(Model model) { - model.addAttribute("filterForm", new FilterForm()); - return "filtering"; - } +// @GetMapping("/filtering") +// public String filter(Model model) { +// model.addAttribute("filterForm", new FilterForm()); +// return "filtering"; +// } @PostMapping("/sendFilter") - public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) { + public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) throws GitAPIException, IOException { + List list = gitRepositoryService.getCommits(filterForm.getUrl()); + model.addAttribute("commits", list); if (filterForm.getFilter() == null || filterForm.getFilter().isEmpty()) { model.addAttribute("error", "'Строка' не должно быть пустым"); return "filtering"; @@ -61,12 +77,36 @@ public class GitExtractorController { } @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "next") - public String setBranch(@ModelAttribute RepoForm repoForm, Model model) { + public String setBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) { model.addAttribute("filterForm", new FilterForm(repoForm.getRepo())); if (repoForm.getBranch() == null) { return "newRepo"; } else { - return "filtering"; + redirectAttributes.addAttribute("url", repoForm.getRepo()); + redirectAttributes.addAttribute("branch", repoForm.getBranch()); + return "redirect:/filtering"; } } + + @RequestMapping(value = "/filtering", method = RequestMethod.GET) + public String listCommits( + Model model, + @ModelAttribute FilterForm filterForm, + @RequestParam("page") Optional page, + @RequestParam("size") Optional size, + @RequestParam String url, + @RequestParam String branch) throws GitAPIException, IOException { + int currentPage = page.orElse(1); + int pageSize = size.orElse(5); + Page commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), url); + model.addAttribute("commitPage", commitPage); + int totalPages = commitPage.getTotalPages(); + if (totalPages > 0) { + List pageNumbers = IntStream.rangeClosed(1, totalPages) + .boxed() + .collect(Collectors.toList()); + model.addAttribute("pageNumbers", pageNumbers); + } + return "filtering"; + } } diff --git a/src/main/java/ru/ulstu/extractor/service/CommitService.java b/src/main/java/ru/ulstu/extractor/service/CommitService.java new file mode 100644 index 0000000..4f7b143 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/CommitService.java @@ -0,0 +1,40 @@ +package ru.ulstu.extractor.service; + +import org.eclipse.jgit.api.errors.GitAPIException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.Commit; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +@Service +public class CommitService { + private final GitRepositoryService gitRepositoryService; + + public CommitService(GitRepositoryService gitRepositoryService) { + this.gitRepositoryService = gitRepositoryService; + } + + public Page findPaginated(Pageable pageable, String url) throws GitAPIException, IOException { + int pageSize = pageable.getPageSize(); + int currentPage = pageable.getPageNumber(); + int startItem = currentPage * pageSize; + List commits = gitRepositoryService.getCommits(url); + ; + + if (commits.size() < startItem) { + commits = Collections.emptyList(); + } else { + int toIndex = Math.min(startItem + pageSize, commits.size()); + commits = commits.subList(startItem, toIndex); + } + return new PageImpl<>(commits, PageRequest.of(currentPage, pageSize), commits.size()); + } +} + + diff --git a/src/main/resources/templates/filtering.html b/src/main/resources/templates/filtering.html index 0899636..88676d5 100644 --- a/src/main/resources/templates/filtering.html +++ b/src/main/resources/templates/filtering.html @@ -7,7 +7,6 @@
-

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

По автору @@ -30,23 +29,30 @@

- - - - - - - - - - - - + + + + + + + + + + + + + + + +
AuthorDateCommit

- - +
+ diff --git a/src/main/resources/templates/newRepo.html b/src/main/resources/templates/newRepo.html index 39d5457..5406ca6 100644 --- a/src/main/resources/templates/newRepo.html +++ b/src/main/resources/templates/newRepo.html @@ -7,7 +7,6 @@
-

Ваш git репозиторий:
@@ -28,7 +27,6 @@

-
From 11f3b93b9d12086f628a42dceaf528d3439b5a4e Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 27 Mar 2021 11:13:42 +0400 Subject: [PATCH 05/15] #15 -- Fix collapsible --- .../ulstu/extractor/mvc/model/RepoForm.java | 4 +- src/main/resources/templates/newRepo.html | 65 ++++++++++++------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java b/src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java index 72b2b11..73a6b8a 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java +++ b/src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java @@ -2,7 +2,6 @@ package ru.ulstu.extractor.mvc.model; public class RepoForm { private String repo; - private String branch; public String getRepo() { @@ -24,7 +23,8 @@ public class RepoForm { @Override public String toString() { return "RepoForm{" + - "subject='" + repo + + "repo='" + repo + '\'' + + ", branch='" + branch + '\'' + '}'; } } diff --git a/src/main/resources/templates/newRepo.html b/src/main/resources/templates/newRepo.html index 8d0e811..b2a2062 100644 --- a/src/main/resources/templates/newRepo.html +++ b/src/main/resources/templates/newRepo.html @@ -9,32 +9,49 @@

-
- -
-
- -
-
- + +
+
+
+ +
+
+ +
+
+ +
+
-
- -
-
- -
-
- +
+
+
+ +
+
+ +
+
+ +
+
@@ -49,5 +66,7 @@ + + From 4b56058835706a81930ad6f0bfbf0d7ab6a0028b Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 27 Mar 2021 11:58:07 +0400 Subject: [PATCH 06/15] #15 -- Fix filtering table --- ...oller.java => GitFilteringController.java} | 39 +------------ .../extractor/mvc/GitIndexingController.java | 55 +++++++++++++++++++ .../extractor/service/CommitService.java | 1 - src/main/resources/templates/filtering.html | 10 ++-- 4 files changed, 62 insertions(+), 43 deletions(-) rename src/main/java/ru/ulstu/extractor/mvc/{GitExtractorController.java => GitFilteringController.java} (64%) create mode 100644 src/main/java/ru/ulstu/extractor/mvc/GitIndexingController.java diff --git a/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java b/src/main/java/ru/ulstu/extractor/mvc/GitFilteringController.java similarity index 64% rename from src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java rename to src/main/java/ru/ulstu/extractor/mvc/GitFilteringController.java index 463c468..53320f5 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java +++ b/src/main/java/ru/ulstu/extractor/mvc/GitFilteringController.java @@ -5,17 +5,13 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.servlet.mvc.support.RedirectAttributes; -import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.mvc.model.FilterForm; -import ru.ulstu.extractor.mvc.model.RepoForm; import ru.ulstu.extractor.service.CommitService; import ru.ulstu.extractor.service.FilteringService; import ru.ulstu.extractor.service.GitRepositoryService; @@ -27,12 +23,12 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; @Controller -public class GitExtractorController { +public class GitFilteringController { private final FilteringService filteringService; private final GitRepositoryService gitRepositoryService; private final CommitService commitService; - public GitExtractorController(FilteringService filteringService, + public GitFilteringController(FilteringService filteringService, GitRepositoryService gitRepositoryService, CommitService commitService) { this.filteringService = filteringService; @@ -40,12 +36,6 @@ public class GitExtractorController { this.commitService = commitService; } - @GetMapping("/newRepo") - public String indexNewRepo(Model model) { - model.addAttribute("repoForm", new RepoForm()); - return "newRepo"; - } - @PostMapping("/sendFilter") public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) throws GitAPIException, IOException { List list = gitRepositoryService.getCommits(filterForm.getUrl()); @@ -57,31 +47,6 @@ public class GitExtractorController { return "resultRepo"; } - @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "send") - public String getBranch(@ModelAttribute RepoForm repoForm, Model model) { - try { - gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo()); - List list = gitRepositoryService.getBranches(repoForm.getRepo()); - model.addAttribute("branches", list); - return "newRepo"; - } catch (Exception ex) { - model.addAttribute("error", ex.getMessage()); - return "newRepo"; - } - } - - @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "next") - public String setBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) { - model.addAttribute("filterForm", new FilterForm(repoForm.getRepo())); - if (repoForm.getBranch() == null) { - return "newRepo"; - } else { - redirectAttributes.addAttribute("url", repoForm.getRepo()); - redirectAttributes.addAttribute("branch", repoForm.getBranch()); - return "redirect:/filtering"; - } - } - @RequestMapping(value = "/filtering", method = RequestMethod.GET) public String listCommits( Model model, diff --git a/src/main/java/ru/ulstu/extractor/mvc/GitIndexingController.java b/src/main/java/ru/ulstu/extractor/mvc/GitIndexingController.java new file mode 100644 index 0000000..bfd61f6 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/mvc/GitIndexingController.java @@ -0,0 +1,55 @@ +package ru.ulstu.extractor.mvc; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.mvc.model.FilterForm; +import ru.ulstu.extractor.mvc.model.RepoForm; +import ru.ulstu.extractor.service.GitRepositoryService; + +import java.util.List; + +@Controller +public class GitIndexingController { + private final GitRepositoryService gitRepositoryService; + + public GitIndexingController(GitRepositoryService gitRepositoryService) { + this.gitRepositoryService = gitRepositoryService; + } + + @GetMapping("/newRepo") + public String indexNewRepo(Model model) { + model.addAttribute(new RepoForm()); + return "newRepo"; + } + + @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "send") + public String getBranch(@ModelAttribute RepoForm repoForm, Model model) { + try { + gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo()); + List branches = gitRepositoryService.getBranches(repoForm.getRepo()); + model.addAttribute("branches", branches); + return "newRepo"; + } catch (Exception ex) { + model.addAttribute("error", ex.getMessage()); + return "newRepo"; + } + } + + @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "next") + public String setBranch(@ModelAttribute RepoForm repoForm, Model model, RedirectAttributes redirectAttributes) { + model.addAttribute("filterForm", new FilterForm(repoForm.getRepo())); + if (repoForm.getBranch() == null) { + return "newRepo"; + } else { + redirectAttributes.addAttribute("url", repoForm.getRepo()); + redirectAttributes.addAttribute("branch", repoForm.getBranch()); + return "redirect:/filtering"; + } + } +} diff --git a/src/main/java/ru/ulstu/extractor/service/CommitService.java b/src/main/java/ru/ulstu/extractor/service/CommitService.java index 4f7b143..05ec123 100644 --- a/src/main/java/ru/ulstu/extractor/service/CommitService.java +++ b/src/main/java/ru/ulstu/extractor/service/CommitService.java @@ -25,7 +25,6 @@ public class CommitService { int currentPage = pageable.getPageNumber(); int startItem = currentPage * pageSize; List commits = gitRepositoryService.getCommits(url); - ; if (commits.size() < startItem) { commits = Collections.emptyList(); diff --git a/src/main/resources/templates/filtering.html b/src/main/resources/templates/filtering.html index 88676d5..8a8bca1 100644 --- a/src/main/resources/templates/filtering.html +++ b/src/main/resources/templates/filtering.html @@ -29,12 +29,12 @@

- - +
+ - - - + + + From 0ab674e36626aacdc09d55876bbfd2a2a5a86c01 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 27 Mar 2021 11:59:01 +0400 Subject: [PATCH 07/15] #15 -- Fix empty strings --- src/main/resources/templates/newRepo.html | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/resources/templates/newRepo.html b/src/main/resources/templates/newRepo.html index b2a2062..8e7a21d 100644 --- a/src/main/resources/templates/newRepo.html +++ b/src/main/resources/templates/newRepo.html @@ -59,14 +59,5 @@ From 736ee1b3d8f836c217f19f443325cf3850d88500 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 27 Mar 2021 12:13:07 +0400 Subject: [PATCH 08/15] #15 -- Some classes refactoring --- .../{mvc => config}/GlobalDefaultExceptionHandler.java | 2 +- .../ulstu/extractor/{mvc => config}/MvcConfiguration.java | 2 +- .../extractor/{mvc => config}/TemplateConfiguration.java | 2 +- .../{mvc => controller}/GitFilteringController.java | 4 ++-- .../{mvc => controller}/GitIndexingController.java | 6 +++--- .../extractor/{mvc/model => model/mvc}/FilterForm.java | 2 +- .../ulstu/extractor/{mvc/model => model/mvc}/RepoForm.java | 2 +- .../extractor/{db => repository}/CommitRepository.java | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) rename src/main/java/ru/ulstu/extractor/{mvc => config}/GlobalDefaultExceptionHandler.java (96%) rename src/main/java/ru/ulstu/extractor/{mvc => config}/MvcConfiguration.java (96%) rename src/main/java/ru/ulstu/extractor/{mvc => config}/TemplateConfiguration.java (97%) rename src/main/java/ru/ulstu/extractor/{mvc => controller}/GitFilteringController.java (97%) rename src/main/java/ru/ulstu/extractor/{mvc => controller}/GitIndexingController.java (93%) rename src/main/java/ru/ulstu/extractor/{mvc/model => model/mvc}/FilterForm.java (93%) rename src/main/java/ru/ulstu/extractor/{mvc/model => model/mvc}/RepoForm.java (93%) rename src/main/java/ru/ulstu/extractor/{db => repository}/CommitRepository.java (97%) diff --git a/src/main/java/ru/ulstu/extractor/mvc/GlobalDefaultExceptionHandler.java b/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java similarity index 96% rename from src/main/java/ru/ulstu/extractor/mvc/GlobalDefaultExceptionHandler.java rename to src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java index 7a1ea5b..49f1501 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/GlobalDefaultExceptionHandler.java +++ b/src/main/java/ru/ulstu/extractor/config/GlobalDefaultExceptionHandler.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.mvc; +package ru.ulstu.extractor.config; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; diff --git a/src/main/java/ru/ulstu/extractor/mvc/MvcConfiguration.java b/src/main/java/ru/ulstu/extractor/config/MvcConfiguration.java similarity index 96% rename from src/main/java/ru/ulstu/extractor/mvc/MvcConfiguration.java rename to src/main/java/ru/ulstu/extractor/config/MvcConfiguration.java index c25a506..2355ca1 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/MvcConfiguration.java +++ b/src/main/java/ru/ulstu/extractor/config/MvcConfiguration.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.mvc; +package ru.ulstu.extractor.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; diff --git a/src/main/java/ru/ulstu/extractor/mvc/TemplateConfiguration.java b/src/main/java/ru/ulstu/extractor/config/TemplateConfiguration.java similarity index 97% rename from src/main/java/ru/ulstu/extractor/mvc/TemplateConfiguration.java rename to src/main/java/ru/ulstu/extractor/config/TemplateConfiguration.java index 7798224..1a638d2 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/TemplateConfiguration.java +++ b/src/main/java/ru/ulstu/extractor/config/TemplateConfiguration.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.mvc; +package ru.ulstu.extractor.config; import nz.net.ultraq.thymeleaf.LayoutDialect; import org.springframework.beans.factory.annotation.Value; diff --git a/src/main/java/ru/ulstu/extractor/mvc/GitFilteringController.java b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java similarity index 97% rename from src/main/java/ru/ulstu/extractor/mvc/GitFilteringController.java rename to src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java index 53320f5..5203c7d 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/GitFilteringController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.mvc; +package ru.ulstu.extractor.controller; import org.eclipse.jgit.api.errors.GitAPIException; import org.springframework.data.domain.Page; @@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.extractor.model.Commit; -import ru.ulstu.extractor.mvc.model.FilterForm; +import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.service.CommitService; import ru.ulstu.extractor.service.FilteringService; import ru.ulstu.extractor.service.GitRepositoryService; diff --git a/src/main/java/ru/ulstu/extractor/mvc/GitIndexingController.java b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java similarity index 93% rename from src/main/java/ru/ulstu/extractor/mvc/GitIndexingController.java rename to src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java index bfd61f6..964bc4a 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/GitIndexingController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.mvc; +package ru.ulstu.extractor.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -8,8 +8,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import ru.ulstu.extractor.model.Branch; -import ru.ulstu.extractor.mvc.model.FilterForm; -import ru.ulstu.extractor.mvc.model.RepoForm; +import ru.ulstu.extractor.model.mvc.FilterForm; +import ru.ulstu.extractor.model.mvc.RepoForm; import ru.ulstu.extractor.service.GitRepositoryService; import java.util.List; diff --git a/src/main/java/ru/ulstu/extractor/mvc/model/FilterForm.java b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java similarity index 93% rename from src/main/java/ru/ulstu/extractor/mvc/model/FilterForm.java rename to src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java index 16e52b8..67f2194 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/model/FilterForm.java +++ b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.mvc.model; +package ru.ulstu.extractor.model.mvc; public class FilterForm { private String filter; diff --git a/src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java b/src/main/java/ru/ulstu/extractor/model/mvc/RepoForm.java similarity index 93% rename from src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java rename to src/main/java/ru/ulstu/extractor/model/mvc/RepoForm.java index 73a6b8a..e0ca0aa 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java +++ b/src/main/java/ru/ulstu/extractor/model/mvc/RepoForm.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.mvc.model; +package ru.ulstu.extractor.model.mvc; public class RepoForm { private String repo; diff --git a/src/main/java/ru/ulstu/extractor/db/CommitRepository.java b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java similarity index 97% rename from src/main/java/ru/ulstu/extractor/db/CommitRepository.java rename to src/main/java/ru/ulstu/extractor/repository/CommitRepository.java index 86a924e..e185761 100644 --- a/src/main/java/ru/ulstu/extractor/db/CommitRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.db; +package ru.ulstu.extractor.repository; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; From 99415a1a22f06d2b329c0c06174fdfc0a36b2f9e Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 29 Mar 2021 11:38:27 +0400 Subject: [PATCH 09/15] #16 -- Add JPA. Create entity classes, repositories and database changelogs. --- build.gradle | 5 +- .../controller/GitFilteringController.java | 8 +- .../controller/GitIndexingController.java | 12 ++- .../extractor/controller/RepoController.java | 5 +- .../java/ru/ulstu/extractor/model/Author.java | 23 +++++ .../ru/ulstu/extractor/model/BaseEntity.java | 86 +++++++++++++++++++ .../java/ru/ulstu/extractor/model/Branch.java | 49 ++++++++++- .../java/ru/ulstu/extractor/model/Commit.java | 69 +++++++++++---- .../ru/ulstu/extractor/model/FileChange.java | 26 +++++- .../ru/ulstu/extractor/model/LineChange.java | 23 ++++- .../ru/ulstu/extractor/model/Repository.java | 23 +++++ .../ulstu/extractor/model/mvc/FilterForm.java | 9 ++ .../repository/BranchRepository.java | 11 +++ .../repository/CommitRepository.java | 39 +-------- .../repository/RepositoryRepository.java | 10 +++ .../extractor/service/CommitService.java | 4 +- .../service/GitRepositoryService.java | 30 ++++--- .../ulstu/extractor/service/IndexService.java | 41 +++++++++ src/main/resources/application.properties | 3 + .../db/changelog-20210329_120000-schema.xml | 38 ++++++++ src/main/resources/db/changelog-master.xml | 1 + 21 files changed, 437 insertions(+), 78 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/model/Author.java create mode 100644 src/main/java/ru/ulstu/extractor/model/BaseEntity.java create mode 100644 src/main/java/ru/ulstu/extractor/model/Repository.java create mode 100644 src/main/java/ru/ulstu/extractor/repository/BranchRepository.java create mode 100644 src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java create mode 100644 src/main/java/ru/ulstu/extractor/service/IndexService.java create mode 100644 src/main/resources/db/changelog-20210329_120000-schema.xml diff --git a/build.gradle b/build.gradle index 0d2a506..9ef6c17 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ buildscript { ext { - versionSpringBoot = '2.3.8.RELEASE' + versionSpringBoot = '2.3.9.RELEASE' } repositories { @@ -47,11 +47,10 @@ dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-web' compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' + compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa' compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect' - compile group: 'org.springframework.data', name: 'spring-data-commons' compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' - compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc' compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212' compile group: 'org.liquibase', name: 'liquibase-core', version: '4.3.1' diff --git a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java index 5203c7d..f4dcde8 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java @@ -38,7 +38,7 @@ public class GitFilteringController { @PostMapping("/sendFilter") public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) throws GitAPIException, IOException { - List list = gitRepositoryService.getCommits(filterForm.getUrl()); + List list = gitRepositoryService.getCommits(filterForm.getUrl(), filterForm.getBranch()); model.addAttribute("commits", list); if (filterForm.getFilter() == null || filterForm.getFilter().isEmpty()) { model.addAttribute("error", "'Строка' не должно быть пустым"); @@ -53,11 +53,11 @@ public class GitFilteringController { @ModelAttribute FilterForm filterForm, @RequestParam("page") Optional page, @RequestParam("size") Optional size, - @RequestParam String url, - @RequestParam String branch) throws GitAPIException, IOException { + @RequestParam String repositoryUrl, + @RequestParam String branchName) throws GitAPIException, IOException { int currentPage = page.orElse(1); int pageSize = size.orElse(5); - Page commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), url); + Page commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), repositoryUrl, branchName); model.addAttribute("commitPage", commitPage); int totalPages = commitPage.getTotalPages(); if (totalPages > 0) { diff --git a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java index 964bc4a..00beb20 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java @@ -11,15 +11,19 @@ import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.model.mvc.RepoForm; import ru.ulstu.extractor.service.GitRepositoryService; +import ru.ulstu.extractor.service.IndexService; import java.util.List; @Controller public class GitIndexingController { private final GitRepositoryService gitRepositoryService; + private final IndexService indexService; - public GitIndexingController(GitRepositoryService gitRepositoryService) { + public GitIndexingController(GitRepositoryService gitRepositoryService, + IndexService indexService) { this.gitRepositoryService = gitRepositoryService; + this.indexService = indexService; } @GetMapping("/newRepo") @@ -47,6 +51,12 @@ public class GitIndexingController { if (repoForm.getBranch() == null) { return "newRepo"; } else { + try { + indexService.index(repoForm.getRepo(), repoForm.getBranch()); + } catch (Exception ex) { + model.addAttribute("error", ex.getMessage()); + return "newRepo"; + } redirectAttributes.addAttribute("url", repoForm.getRepo()); redirectAttributes.addAttribute("branch", repoForm.getBranch()); return "redirect:/filtering"; diff --git a/src/main/java/ru/ulstu/extractor/controller/RepoController.java b/src/main/java/ru/ulstu/extractor/controller/RepoController.java index 2d30d9a..09ea5d5 100644 --- a/src/main/java/ru/ulstu/extractor/controller/RepoController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RepoController.java @@ -29,8 +29,9 @@ public class RepoController { } @GetMapping("commits") - public List getCommits(@RequestParam("url") String url) throws GitAPIException, IOException { - return gitRepositoryService.getCommits(url); + public List getCommits(@RequestParam("repositoryUrl") String repositoryUrl, + @RequestParam("branchName") String branchName) throws GitAPIException, IOException { + return gitRepositoryService.getCommits(repositoryUrl, branchName); } } diff --git a/src/main/java/ru/ulstu/extractor/model/Author.java b/src/main/java/ru/ulstu/extractor/model/Author.java new file mode 100644 index 0000000..2462d5b --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/Author.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.model; + +import javax.persistence.Entity; + +@Entity +public class Author extends BaseEntity { + private String name; + + public Author() { + } + + public Author(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/ru/ulstu/extractor/model/BaseEntity.java b/src/main/java/ru/ulstu/extractor/model/BaseEntity.java new file mode 100644 index 0000000..0859807 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/BaseEntity.java @@ -0,0 +1,86 @@ +package ru.ulstu.extractor.model; + +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.persistence.Version; +import java.io.Serializable; + +@MappedSuperclass +public abstract class BaseEntity implements Serializable, Comparable { + @Id + @GeneratedValue(strategy = GenerationType.TABLE) + private Integer id; + + @Version + private Integer version; + + public BaseEntity() { + } + + public BaseEntity(Integer id, Integer version) { + this.id = id; + this.version = version; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getVersion() { + return version; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!getClass().isAssignableFrom(obj.getClass())) { + return false; + } + BaseEntity other = (BaseEntity) obj; + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (id == null ? 0 : id.hashCode()); + return result; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "{" + + "id=" + id + + ", version=" + version + + '}'; + } + + @Override + public int compareTo(Object o) { + return id != null ? id.compareTo(((BaseEntity) o).getId()) : -1; + } + + public void reset() { + this.id = null; + this.version = null; + } +} diff --git a/src/main/java/ru/ulstu/extractor/model/Branch.java b/src/main/java/ru/ulstu/extractor/model/Branch.java index 4f751d2..02a6349 100644 --- a/src/main/java/ru/ulstu/extractor/model/Branch.java +++ b/src/main/java/ru/ulstu/extractor/model/Branch.java @@ -1,14 +1,61 @@ package ru.ulstu.extractor.model; -public class Branch { +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import java.util.List; + +@Entity +public class Branch extends BaseEntity { private String name; + @ManyToOne + private Repository repository; + + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @JoinColumn(name = "branch_id", unique = true) + @Fetch(FetchMode.SUBSELECT) + private List commits; + + public Branch() { + } + public Branch(String name) { this.name = name; } + public Branch(Repository repository, String branchName) { + this.repository = repository; + this.name = branchName; + } + public String getName() { return name; } + public void setName(String name) { + this.name = name; + } + + public Repository getRepository() { + return repository; + } + + public void setRepository(Repository repository) { + this.repository = repository; + } + + public List getCommits() { + return commits; + } + + public void setCommits(List commits) { + this.commits = commits; + } } diff --git a/src/main/java/ru/ulstu/extractor/model/Commit.java b/src/main/java/ru/ulstu/extractor/model/Commit.java index 9e8e6f4..e7d6795 100644 --- a/src/main/java/ru/ulstu/extractor/model/Commit.java +++ b/src/main/java/ru/ulstu/extractor/model/Commit.java @@ -1,18 +1,37 @@ package ru.ulstu.extractor.model; +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import java.util.ArrayList; import java.util.Date; +import java.util.List; -public class Commit { - private String message; +@Entity +public class Commit extends BaseEntity { + private String hash; private Date date; - private String author; - private Changes changes; + private String message; + @ManyToOne + private Author author; + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @JoinColumn(name = "commit_id", unique = true) + @Fetch(FetchMode.SUBSELECT) + private List fileChanges = new ArrayList<>(); - public Commit(String message, String author, Date date) { + public Commit() { + } + + public Commit(String message, Author author, Date date) { this.message = message; this.author = author; this.date = date; - } public String getMessage() { @@ -23,15 +42,35 @@ public class Commit { return date; } - public String getAuthor() { + public String getHash() { + return hash; + } + + public void setHash(String hash) { + this.hash = hash; + } + + public void setDate(Date date) { + this.date = date; + } + + public void setMessage(String message) { + this.message = message; + } + + public void setAuthor(Author author) { + this.author = author; + } + + public List getFileChanges() { + return fileChanges; + } + + public void setFileChanges(List fileChanges) { + this.fileChanges = fileChanges; + } + + public Author getAuthor() { return author; } - - public Changes getChanges() { - return changes; - } - - public void setChanges(Changes changes) { - this.changes = changes; - } } diff --git a/src/main/java/ru/ulstu/extractor/model/FileChange.java b/src/main/java/ru/ulstu/extractor/model/FileChange.java index fb0ec6b..2493da6 100644 --- a/src/main/java/ru/ulstu/extractor/model/FileChange.java +++ b/src/main/java/ru/ulstu/extractor/model/FileChange.java @@ -1,12 +1,28 @@ package ru.ulstu.extractor.model; +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.Transient; import java.util.ArrayList; import java.util.List; -public class FileChange { +@Entity +public class FileChange extends BaseEntity { private String file; + @Transient private boolean removed; + @Transient private boolean added; + + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @JoinColumn(name = "file_change_id", unique = true) + @Fetch(FetchMode.SUBSELECT) private List lineChanges = new ArrayList<>(); public FileChange() { @@ -47,4 +63,12 @@ public class FileChange { public void setLineChanges(List lineChanges) { this.lineChanges = lineChanges; } + + public boolean isRemoved() { + return removed; + } + + public boolean isAdded() { + return added; + } } diff --git a/src/main/java/ru/ulstu/extractor/model/LineChange.java b/src/main/java/ru/ulstu/extractor/model/LineChange.java index 7f1bab3..7901322 100644 --- a/src/main/java/ru/ulstu/extractor/model/LineChange.java +++ b/src/main/java/ru/ulstu/extractor/model/LineChange.java @@ -1,7 +1,13 @@ package ru.ulstu.extractor.model; -public class LineChange { +import javax.persistence.Entity; +import javax.persistence.Transient; + +@Entity +public class LineChange extends BaseEntity { + @Transient private boolean added; + @Transient private boolean removed; private String lineFrom; private String lineTo; @@ -42,4 +48,19 @@ public class LineChange { return lineTo; } + public boolean isAdded() { + return added; + } + + public void setAdded(boolean added) { + this.added = added; + } + + public boolean isRemoved() { + return removed; + } + + public void setRemoved(boolean removed) { + this.removed = removed; + } } diff --git a/src/main/java/ru/ulstu/extractor/model/Repository.java b/src/main/java/ru/ulstu/extractor/model/Repository.java new file mode 100644 index 0000000..651bfca --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/Repository.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.model; + +import javax.persistence.Entity; + +@Entity +public class Repository extends BaseEntity { + private String url; + + public Repository() { + } + + public Repository(String repositoryUrl) { + url = repositoryUrl; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } +} 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 67f2194..4946871 100644 --- a/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java +++ b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java @@ -3,6 +3,7 @@ package ru.ulstu.extractor.model.mvc; public class FilterForm { private String filter; private String url; + private String branch; public FilterForm() { } @@ -27,6 +28,14 @@ public class FilterForm { this.url = url; } + public String getBranch() { + return branch; + } + + public void setBranch(String branch) { + this.branch = branch; + } + @Override public String toString() { return "FilterForm{" + diff --git a/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java b/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java new file mode 100644 index 0000000..f982d1e --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java @@ -0,0 +1,11 @@ +package ru.ulstu.extractor.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.model.Repository; + +import java.util.Optional; + +public interface BranchRepository extends JpaRepository { + Optional findByRepositoryAndName(Repository repository, String name); +} diff --git a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java index e185761..fd386d4 100644 --- a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java @@ -1,42 +1,7 @@ package ru.ulstu.extractor.repository; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; -import org.springframework.stereotype.Repository; +import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.extractor.model.Commit; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; - -@Repository -public class CommitRepository { - private final static String SQL_SELECT_COMMITS = "SELECT * FROM commit"; - private final static String SQL_INSERT_COMMITS = "INSERT INTO commit (hash, date, author, message) " + - "VALUES (?, ?, ?, ?)"; - private final JdbcTemplate jdbcTemplate; - - public CommitRepository(JdbcTemplate jdbcTemplate) { - this.jdbcTemplate = jdbcTemplate; - } - - private static class CommitRowMapper implements RowMapper { - - @Override - public Commit mapRow(ResultSet rs, int rowNum) throws SQLException { - return new Commit(rs.getString("message"), - rs.getString("author"), - rs.getDate("date")); - } - } - - public List getCommits() { - return jdbcTemplate.query(SQL_SELECT_COMMITS, new CommitRowMapper()); - } - - public void saveCommit(Commit commit) { - jdbcTemplate.update(SQL_INSERT_COMMITS, "", commit.getDate(), - commit.getAuthor(), - commit.getMessage()); - } +public interface CommitRepository extends JpaRepository { } diff --git a/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java b/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java new file mode 100644 index 0000000..cedaa75 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java @@ -0,0 +1,10 @@ +package ru.ulstu.extractor.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.extractor.model.Repository; + +import java.util.Optional; + +public interface RepositoryRepository extends JpaRepository { + Optional findByUrl(String url); +} diff --git a/src/main/java/ru/ulstu/extractor/service/CommitService.java b/src/main/java/ru/ulstu/extractor/service/CommitService.java index 05ec123..62073d6 100644 --- a/src/main/java/ru/ulstu/extractor/service/CommitService.java +++ b/src/main/java/ru/ulstu/extractor/service/CommitService.java @@ -20,11 +20,11 @@ public class CommitService { this.gitRepositoryService = gitRepositoryService; } - public Page findPaginated(Pageable pageable, String url) throws GitAPIException, IOException { + public Page findPaginated(Pageable pageable, String repositoryUrl, String branchName) throws GitAPIException, IOException { int pageSize = pageable.getPageSize(); int currentPage = pageable.getPageNumber(); int startItem = currentPage * pageSize; - List commits = gitRepositoryService.getCommits(url); + List commits = gitRepositoryService.getCommits(repositoryUrl, branchName); if (commits.size() < startItem) { commits = Collections.emptyList(); diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index 1d20e44..93c9aab 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -1,16 +1,18 @@ package ru.ulstu.extractor.service; +import org.eclipse.jgit.api.CreateBranchCommand; import org.eclipse.jgit.api.Git; 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; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.Author; import ru.ulstu.extractor.model.Branch; -import ru.ulstu.extractor.model.Changes; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.FileChange; import ru.ulstu.extractor.model.LineChange; @@ -68,13 +70,19 @@ public class GitRepositoryService { return file.exists(); } - public List getCommits(String url) throws GitAPIException, IOException { - cloneOrUpdateRepo(url); - Repository localRepo = new FileRepository(getProjectGitDirectory(url)); + public List getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException { + cloneOrUpdateRepo(repositoryUrl); + Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); Git git = new Git(localRepo); git.pull().call(); + Ref ref = git.checkout(). + setCreateBranch(true). + setName(branchName). + setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). + setStartPoint("origin/" + branchName). + call(); + List commits = new ArrayList<>(); - //TODO: сделать преобразование в коллекцию "наших объектов" git.log().call().forEach(commits::add); List list = new ArrayList<>(); @@ -82,10 +90,10 @@ public class GitRepositoryService { for (RevCommit revCommit : commits) { Commit commit = new Commit( revCommit.getFullMessage(), - revCommit.getAuthorIdent().getName(), + new Author(revCommit.getAuthorIdent().getName()), Date.from(Instant.ofEpochSecond(revCommit.getCommitTime()))); if (prevCommit != null) { - commit.setChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo)); + commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo)); } list.add(commit); prevCommit = revCommit; @@ -93,7 +101,7 @@ public class GitRepositoryService { return list; } - public Changes findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) { + public List findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) { if (laterCommit == null || earlierCommit == null) { return null; } @@ -109,8 +117,8 @@ public class GitRepositoryService { return parseOutputDiff(output); } - private Changes parseOutputDiff(String output) { - Changes changes = new Changes(); + private List parseOutputDiff(String output) { + List changes = new ArrayList<>(); String[] strings = output.split("\n"); FileChange fileChange = new FileChange(); int stringsLength = strings.length - 1; @@ -121,7 +129,7 @@ public class GitRepositoryService { fileChange = new FileChange(); fileChange.setFile(maybeFileName.get()); /// вытащить другие изменения из коммита - changes.getFileChanges().add(fileChange); + changes.add(fileChange); } LineChange lineChange = new LineChange(); if (strings[i].startsWith("-")) { diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java new file mode 100644 index 0000000..113a70b --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -0,0 +1,41 @@ +package ru.ulstu.extractor.service; + +import com.sun.istack.NotNull; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.model.Commit; +import ru.ulstu.extractor.model.Repository; +import ru.ulstu.extractor.repository.BranchRepository; +import ru.ulstu.extractor.repository.CommitRepository; +import ru.ulstu.extractor.repository.RepositoryRepository; + +import java.io.IOException; +import java.util.List; + +@Service +public class IndexService { + private final GitRepositoryService gitRepositoryService; + private final RepositoryRepository repositoryRepository; + private final BranchRepository branchRepository; + private final CommitRepository commitRepository; + + public IndexService(GitRepositoryService gitRepositoryService, + RepositoryRepository repositoryRepository, + BranchRepository branchRepository, + CommitRepository commitRepository) { + this.gitRepositoryService = gitRepositoryService; + this.repositoryRepository = repositoryRepository; + this.branchRepository = branchRepository; + this.commitRepository = commitRepository; + } + + public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { + Repository repository = repositoryRepository.findByUrl(repositoryUrl) + .orElse(repositoryRepository.save(new Repository(repositoryUrl))); + Branch branch = branchRepository.findByRepositoryAndName(repository, branchName) + .orElse(branchRepository.save(new Branch(repository, branchName))); + List commits = gitRepositoryService.getCommits(repositoryUrl, branchName); + branch.setCommits(commits); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f061be5..938472e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,6 +11,9 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/repo spring.datasource.username=postgres spring.datasource.password=postgres spring.datasource.driverclassName=org.postgresql.Driver +spring.jpa.hibernate.ddl-auto=validate +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect +spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false # Liquibase Settings spring.liquibase.drop-first=false spring.liquibase.enabled=true diff --git a/src/main/resources/db/changelog-20210329_120000-schema.xml b/src/main/resources/db/changelog-20210329_120000-schema.xml new file mode 100644 index 0000000..78b1cc4 --- /dev/null +++ b/src/main/resources/db/changelog-20210329_120000-schema.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 90a3c94..0ddc390 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -5,4 +5,5 @@ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> + From 14a7983f49cb8aaaf0d6f6f08705fbd7094f8880 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 29 Mar 2021 12:04:52 +0400 Subject: [PATCH 10/15] #16 -- Checkout specific branch --- .../java/ru/ulstu/extractor/service/GitRepositoryService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index 93c9aab..e99e700 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -32,6 +32,7 @@ import static org.apache.logging.log4j.util.Strings.isBlank; @Service public class GitRepositoryService { + private static final String BRANCH_PREFIX = "refs/remotes/origin/"; @Value("${extractor.custom-projects-dir}") private String customProjectsDir; @@ -207,7 +208,7 @@ public class GitRepositoryService { return git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE) .call() .stream() - .map(r -> new Branch(r.getName())) + .map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, ""))) .collect(Collectors.toList()); } } From cf9f4ea82b4f4d4f2ff23166614470a349b62d79 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 29 Mar 2021 14:40:20 +0400 Subject: [PATCH 11/15] #16 -- Save commits to DB --- .../controller/GitIndexingController.java | 4 ++-- .../java/ru/ulstu/extractor/model/Commit.java | 5 +++-- .../extractor/repository/BranchRepository.java | 4 +--- .../repository/RepositoryRepository.java | 4 +--- .../service/GitRepositoryService.java | 18 ++++++++++-------- .../ulstu/extractor/service/IndexService.java | 13 +++++++++---- 6 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java index 00beb20..18ddb48 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java @@ -57,8 +57,8 @@ public class GitIndexingController { model.addAttribute("error", ex.getMessage()); return "newRepo"; } - redirectAttributes.addAttribute("url", repoForm.getRepo()); - redirectAttributes.addAttribute("branch", repoForm.getBranch()); + redirectAttributes.addAttribute("repositoryUrl", repoForm.getRepo()); + redirectAttributes.addAttribute("branchName", repoForm.getBranch()); return "redirect:/filtering"; } } diff --git a/src/main/java/ru/ulstu/extractor/model/Commit.java b/src/main/java/ru/ulstu/extractor/model/Commit.java index e7d6795..916f81f 100644 --- a/src/main/java/ru/ulstu/extractor/model/Commit.java +++ b/src/main/java/ru/ulstu/extractor/model/Commit.java @@ -18,7 +18,7 @@ public class Commit extends BaseEntity { private String hash; private Date date; private String message; - @ManyToOne + @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Author author; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "commit_id", unique = true) @@ -28,10 +28,11 @@ public class Commit extends BaseEntity { public Commit() { } - public Commit(String message, Author author, Date date) { + public Commit(String message, Author author, Date date, String hash) { this.message = message; this.author = author; this.date = date; + this.hash = hash; } public String getMessage() { diff --git a/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java b/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java index f982d1e..f893bb6 100644 --- a/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java @@ -4,8 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Repository; -import java.util.Optional; - public interface BranchRepository extends JpaRepository { - Optional findByRepositoryAndName(Repository repository, String name); + Branch findByRepositoryAndName(Repository repository, String name); } diff --git a/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java b/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java index cedaa75..21263f4 100644 --- a/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java @@ -3,8 +3,6 @@ package ru.ulstu.extractor.repository; import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.extractor.model.Repository; -import java.util.Optional; - public interface RepositoryRepository extends JpaRepository { - Optional findByUrl(String url); + Repository findByUrl(String url); } diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index e99e700..ef26349 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -76,13 +76,14 @@ public class GitRepositoryService { Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); Git git = new Git(localRepo); git.pull().call(); - Ref ref = git.checkout(). - setCreateBranch(true). - setName(branchName). - setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). - setStartPoint("origin/" + branchName). - call(); - + if (!localRepo.getBranch().equals(branchName)) { + Ref ref = git.checkout(). + setCreateBranch(true). + setName(branchName). + setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). + setStartPoint("origin/" + branchName). + call(); + } List commits = new ArrayList<>(); git.log().call().forEach(commits::add); @@ -92,7 +93,8 @@ public class GitRepositoryService { Commit commit = new Commit( revCommit.getFullMessage(), new Author(revCommit.getAuthorIdent().getName()), - Date.from(Instant.ofEpochSecond(revCommit.getCommitTime()))); + Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())), + revCommit.getName()); if (prevCommit != null) { commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo)); } diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java index 113a70b..413685f 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -31,11 +31,16 @@ public class IndexService { } public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { - Repository repository = repositoryRepository.findByUrl(repositoryUrl) - .orElse(repositoryRepository.save(new Repository(repositoryUrl))); - Branch branch = branchRepository.findByRepositoryAndName(repository, branchName) - .orElse(branchRepository.save(new Branch(repository, branchName))); + Repository repository = repositoryRepository.findByUrl(repositoryUrl); + if (repository == null) { + repositoryRepository.save(new Repository(repositoryUrl)); + } + Branch branch = branchRepository.findByRepositoryAndName(repository, branchName); + if (branch == null) { + branchRepository.save(new Branch(repository, branchName)); + } List commits = gitRepositoryService.getCommits(repositoryUrl, branchName); branch.setCommits(commits); + branchRepository.save(branch); } } From c89a27d49541ab0d5ac218b278b0da59dad6f318 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 29 Mar 2021 15:38:54 +0400 Subject: [PATCH 12/15] #16 -- Show commits page --- build.gradle | 1 + .../controller/GitFilteringController.java | 34 ++++--- .../java/ru/ulstu/extractor/model/Commit.java | 10 ++ .../model/OffsetablePageRequest.java | 93 +++++++++++++++++++ .../ulstu/extractor/model/mvc/FilterForm.java | 12 +++ .../repository/AuthorRepository.java | 14 +++ .../repository/CommitRepository.java | 7 ++ .../extractor/service/FilteringService.java | 25 +---- .../service/GitRepositoryService.java | 4 + .../ulstu/extractor/service/IndexService.java | 19 +++- src/main/resources/templates/filtering.html | 6 +- 11 files changed, 181 insertions(+), 44 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/model/OffsetablePageRequest.java create mode 100644 src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java diff --git a/build.gradle b/build.gradle index 9ef6c17..c356675 100644 --- a/build.gradle +++ b/build.gradle @@ -53,6 +53,7 @@ dependencies { compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212' compile group: 'org.liquibase', name: 'liquibase-core', version: '4.3.1' + implementation group: 'commons-io', name: 'commons-io', version: '2.6' compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0' diff --git a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java index f4dcde8..85ef6aa 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java @@ -1,22 +1,18 @@ package ru.ulstu.extractor.controller; -import org.eclipse.jgit.api.errors.GitAPIException; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.extractor.model.Commit; +import ru.ulstu.extractor.model.OffsetablePageRequest; import ru.ulstu.extractor.model.mvc.FilterForm; -import ru.ulstu.extractor.service.CommitService; import ru.ulstu.extractor.service.FilteringService; -import ru.ulstu.extractor.service.GitRepositoryService; +import ru.ulstu.extractor.service.IndexService; -import java.io.IOException; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -25,18 +21,15 @@ import java.util.stream.IntStream; @Controller public class GitFilteringController { private final FilteringService filteringService; - private final GitRepositoryService gitRepositoryService; - private final CommitService commitService; + private final IndexService indexService; public GitFilteringController(FilteringService filteringService, - GitRepositoryService gitRepositoryService, - CommitService commitService) { + IndexService indexService) { this.filteringService = filteringService; - this.gitRepositoryService = gitRepositoryService; - this.commitService = commitService; + this.indexService = indexService; } - @PostMapping("/sendFilter") + /* @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); @@ -45,7 +38,7 @@ public class GitFilteringController { return "filtering"; } return "resultRepo"; - } + }*/ @RequestMapping(value = "/filtering", method = RequestMethod.GET) public String listCommits( @@ -54,18 +47,23 @@ public class GitFilteringController { @RequestParam("page") Optional page, @RequestParam("size") Optional size, @RequestParam String repositoryUrl, - @RequestParam String branchName) throws GitAPIException, IOException { + @RequestParam String branchName) { int currentPage = page.orElse(1); int pageSize = size.orElse(5); - Page commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), repositoryUrl, branchName); - model.addAttribute("commitPage", commitPage); - int totalPages = commitPage.getTotalPages(); + + Page commitsPage = indexService.getCommits(repositoryUrl, branchName, new OffsetablePageRequest(currentPage, pageSize)); + int totalPages = commitsPage.getTotalPages(); if (totalPages > 0) { List pageNumbers = IntStream.rangeClosed(1, totalPages) .boxed() .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"; } } diff --git a/src/main/java/ru/ulstu/extractor/model/Commit.java b/src/main/java/ru/ulstu/extractor/model/Commit.java index 916f81f..1bbe22f 100644 --- a/src/main/java/ru/ulstu/extractor/model/Commit.java +++ b/src/main/java/ru/ulstu/extractor/model/Commit.java @@ -24,6 +24,8 @@ public class Commit extends BaseEntity { @JoinColumn(name = "commit_id", unique = true) @Fetch(FetchMode.SUBSELECT) private List fileChanges = new ArrayList<>(); + @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private Branch branch; public Commit() { } @@ -74,4 +76,12 @@ public class Commit extends BaseEntity { public Author getAuthor() { return author; } + + public Branch getBranch() { + return branch; + } + + public void setBranch(Branch branch) { + this.branch = branch; + } } diff --git a/src/main/java/ru/ulstu/extractor/model/OffsetablePageRequest.java b/src/main/java/ru/ulstu/extractor/model/OffsetablePageRequest.java new file mode 100644 index 0000000..e96673b --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/OffsetablePageRequest.java @@ -0,0 +1,93 @@ +package ru.ulstu.extractor.model; + +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; + +import java.io.Serializable; + +public class OffsetablePageRequest implements Pageable, Serializable { + private final int offset; + private final int count; + private final Sort sort; + + public OffsetablePageRequest(int page, long pageSize) { + this(pageSize * page, pageSize, Sort.by("id")); + } + + public OffsetablePageRequest(long offset, long count, Sort sort) { + if (offset < 0) { + throw new IllegalArgumentException("Offset value must not be less than zero!"); + } + if (count < 1) { + throw new IllegalArgumentException("Count value must not be less than one!"); + } + this.offset = (int) offset; + this.count = (int) count; + this.sort = sort; + } + + @Override + public Sort getSort() { + return sort; + } + + @Override + public int getPageSize() { + return count; + } + + @Override + public int getPageNumber() { + return offset / count; + } + + @Override + public long getOffset() { + return offset; + } + + @Override + public boolean hasPrevious() { + return offset > 0; + } + + @Override + public Pageable next() { + return new OffsetablePageRequest(getOffset() + getPageSize(), getPageSize(), getSort()); + } + + @Override + public Pageable previousOrFirst() { + return hasPrevious() ? previous() : first(); + } + + public Pageable previous() { + return getOffset() == 0 ? this : new OffsetablePageRequest(getOffset() - getPageSize(), getPageSize(), getSort()); + } + + @Override + public Pageable first() { + return new OffsetablePageRequest(0, getPageSize(), getSort()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + final OffsetablePageRequest other = (OffsetablePageRequest) obj; + return this.offset == other.offset && this.count == other.count; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + offset; + result = prime * result + count; + return result; + } +} 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 4946871..6180b13 100644 --- a/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java +++ b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java @@ -1,9 +1,13 @@ package ru.ulstu.extractor.model.mvc; +import org.springframework.data.domain.Page; +import ru.ulstu.extractor.model.Commit; + public class FilterForm { private String filter; private String url; private String branch; + private Page commitsPage; public FilterForm() { } @@ -36,6 +40,14 @@ public class FilterForm { this.branch = branch; } + public Page getCommitsPage() { + return commitsPage; + } + + public void setCommitsPage(Page commitsPage) { + this.commitsPage = commitsPage; + } + @Override public String toString() { return "FilterForm{" + diff --git a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java new file mode 100644 index 0000000..504aaf2 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java @@ -0,0 +1,14 @@ +package ru.ulstu.extractor.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import ru.ulstu.extractor.model.Author; +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); +} diff --git a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java index fd386d4..3c32439 100644 --- a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java @@ -1,7 +1,14 @@ package ru.ulstu.extractor.repository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import ru.ulstu.extractor.model.Commit; +import ru.ulstu.extractor.model.Repository; public interface CommitRepository extends JpaRepository { + @Query("SELECT c FROM Commit c, Repository r, Branch b WHERE c.branch = b AND r = b.repository AND r = :repository AND b.name = :branchName") + Page findByRepositoryAndBranch(Pageable pageable, @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 254437d..b6b7cf7 100644 --- a/src/main/java/ru/ulstu/extractor/service/FilteringService.java +++ b/src/main/java/ru/ulstu/extractor/service/FilteringService.java @@ -1,32 +1,13 @@ package ru.ulstu.extractor.service; import org.springframework.stereotype.Service; -import ru.ulstu.extractor.model.Commit; - -import java.util.List; -import java.util.stream.Collectors; @Service public class FilteringService { - private final GitRepositoryService gitRepositoryService; + private final IndexService indexService; - public FilteringService(GitRepositoryService gitRepositoryService) { - this.gitRepositoryService = gitRepositoryService; - } - - public List getCommits(String urlRepo) { - return getCommits(urlRepo, null); - } - - public List getCommits(String urlRepo, String filterCommitMessage) { - if (filterCommitMessage != null) { - return getCommits(urlRepo) - .stream() - .filter(commit -> commit.getMessage().contains(filterCommitMessage)) - .collect(Collectors.toList()); - } else { - return getCommits(urlRepo); - } + public FilteringService(IndexService indexService) { + this.indexService = indexService; } } diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index ef26349..63082d0 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -213,4 +213,8 @@ public class GitRepositoryService { .map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, ""))) .collect(Collectors.toList()); } + + 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 413685f..581921e 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -2,10 +2,14 @@ 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 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; @@ -19,15 +23,19 @@ 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) { + CommitRepository commitRepository, + AuthorRepository authorRepository) { this.gitRepositoryService = gitRepositoryService; this.repositoryRepository = repositoryRepository; this.branchRepository = branchRepository; this.commitRepository = commitRepository; + this.authorRepository = authorRepository; } public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { @@ -42,5 +50,14 @@ public class IndexService { List commits = gitRepositoryService.getCommits(repositoryUrl, branchName); 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/templates/filtering.html b/src/main/resources/templates/filtering.html index 8a8bca1..365ac17 100644 --- a/src/main/resources/templates/filtering.html +++ b/src/main/resources/templates/filtering.html @@ -38,8 +38,8 @@ - - + + @@ -50,7 +50,7 @@

-
AuthorDateCommitAuthorDateCommit