From fffb9f76fb6c88dc0a7a6fb65dce797526c34949 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 23 Mar 2021 15:06:12 +0400 Subject: [PATCH] #6 - add Branch --- build.gradle | 1 - .../java/ru/ulstu/extractor/model/Branch.java | 14 ++++++ .../extractor/mvc/GitExtractorController.java | 47 ++++++++++++++----- .../ulstu/extractor/mvc/model/RepoForm.java | 10 ++++ .../service/GitRepositoryService.java | 14 ++++++ src/main/resources/templates/default.html | 2 +- src/main/resources/templates/filtering.html | 29 +++++++----- src/main/resources/templates/newRepo.html | 18 ++++++- 8 files changed, 106 insertions(+), 29 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/model/Branch.java diff --git a/build.gradle b/build.gradle index 703b519..4cd6c5e 100644 --- a/build.gradle +++ b/build.gradle @@ -64,7 +64,6 @@ dependencies { compile group: 'org.webjars', name: 'bootstrap-select', version: '1.13.8' compile group: 'org.webjars', name: 'font-awesome', version: '4.7.0' - testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test' } diff --git a/src/main/java/ru/ulstu/extractor/model/Branch.java b/src/main/java/ru/ulstu/extractor/model/Branch.java new file mode 100644 index 0000000..4f751d2 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/Branch.java @@ -0,0 +1,14 @@ +package ru.ulstu.extractor.model; + +public class Branch { + private String name; + + public Branch(String name) { + this.name = name; + } + + public String getName() { + return name; + } + +} diff --git a/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java index 54b66d6..e7ac514 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java +++ b/src/main/java/ru/ulstu/extractor/mvc/GitExtractorController.java @@ -5,11 +5,16 @@ 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 ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.mvc.model.FilterForm; import ru.ulstu.extractor.mvc.model.RepoForm; import ru.ulstu.extractor.service.FilteringService; import ru.ulstu.extractor.service.GitRepositoryService; +import java.util.List; + @Controller public class GitExtractorController { private final FilteringService filteringService; @@ -33,21 +38,15 @@ public class GitExtractorController { return "filtering"; } - @PostMapping("/sendRepo") + /*@PostMapping("/sendRepo") public String sendRepo(@ModelAttribute RepoForm repoForm, Model model) { - if (repoForm.getRepo() == null || repoForm.getRepo().isEmpty()) { - model.addAttribute("error", "'Git' не должно быть пустым"); - return "newRepo"; - } model.addAttribute("filterForm", new FilterForm(repoForm.getRepo())); - try { - gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo()); - } catch (Exception ex) { - model.addAttribute("error", ex.getMessage()); + if (repoForm.getBranch() == null) { return "newRepo"; + } else { + return "filtering"; } - return "filtering"; - } + }*/ @PostMapping("/sendFilter") public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) { @@ -55,7 +54,31 @@ public class GitExtractorController { model.addAttribute("error", "'Строка' не должно быть пустым"); return "filtering"; } - model.addAttribute("commits", filteringService.getCommits(filterForm.getFilter(), filterForm.getFilter())); +// model.addAttribute("commits", filteringService.getCommits(filterForm.getFilter(), filterForm.getFilter())); return "resultRepo"; } + + @RequestMapping(value = "/newRepo", method = RequestMethod.POST, params = "send") + public String getBranch(@ModelAttribute RepoForm repoForm, Model model) { + try { + gitRepositoryService.cloneOrUpdateRepo(repoForm.getRepo()); + // model.addAttribute("branches", gitRepositoryService.getBranches(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) { + model.addAttribute("filterForm", new FilterForm(repoForm.getRepo())); + if (repoForm.getBranch() == null) { + return "newRepo"; + } else { + return "filtering"; + } + } } 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 7f5766f..72b2b11 100644 --- a/src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java +++ b/src/main/java/ru/ulstu/extractor/mvc/model/RepoForm.java @@ -3,6 +3,8 @@ package ru.ulstu.extractor.mvc.model; public class RepoForm { private String repo; + private String branch; + public String getRepo() { return repo; } @@ -11,6 +13,14 @@ public class RepoForm { this.repo = repo; } + public String getBranch() { + return branch; + } + + public void setBranch(String branch) { + this.branch = branch; + } + @Override public String toString() { return "RepoForm{" + diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index 1102afa..1d20e44 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -1,6 +1,7 @@ package ru.ulstu.extractor.service; 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; @@ -8,6 +9,7 @@ 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.Branch; import ru.ulstu.extractor.model.Changes; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.FileChange; @@ -22,6 +24,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import static org.apache.logging.log4j.util.Strings.isBlank; @@ -188,4 +191,15 @@ 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); + return git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE) + .call() + .stream() + .map(r -> new Branch(r.getName())) + .collect(Collectors.toList()); + } } diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 98fbb34..c6c1237 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -6,7 +6,7 @@ app-name - + diff --git a/src/main/resources/templates/filtering.html b/src/main/resources/templates/filtering.html index 7490628..20a04cd 100644 --- a/src/main/resources/templates/filtering.html +++ b/src/main/resources/templates/filtering.html @@ -12,6 +12,10 @@

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

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

- - - - - - - - - - - -
AuthorDateCommit
Onions2.41yes
- + + + + + + + + + + + +

diff --git a/src/main/resources/templates/newRepo.html b/src/main/resources/templates/newRepo.html index 2239fd0..39d5457 100644 --- a/src/main/resources/templates/newRepo.html +++ b/src/main/resources/templates/newRepo.html @@ -8,15 +8,29 @@
-
+

Ваш git репозиторий:

- + +

+

Ветки:
+ +

+

+

+