-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #352 from reflectoring/delete-branches
Add functionallity for deleting branches.
- Loading branch information
Showing
13 changed files
with
196 additions
and
5 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...dar-core/src/main/java/io/reflectoring/coderadar/projectadministration/domain/Branch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
.../io/reflectoring/coderadar/projectadministration/port/driven/branch/DeleteBranchPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.reflectoring.coderadar.projectadministration.port.driven.branch; | ||
|
||
import io.reflectoring.coderadar.projectadministration.domain.Branch; | ||
|
||
public interface DeleteBranchPort { | ||
|
||
/** | ||
* Deletes a branch in a project. Any commits that only exist as part of the branch are also | ||
* deleted. | ||
* | ||
* @param projectId The id of the project. | ||
* @param branch The branch to delete. | ||
*/ | ||
void delete(long projectId, Branch branch); | ||
} |
2 changes: 1 addition & 1 deletion
2
...river/branch/get/ListBranchesUseCase.java → ...iver/branch/list/ListBranchesUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...eflectoring/coderadar/graph/projectadministration/branch/adapter/DeleteBranchAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.reflectoring.coderadar.graph.projectadministration.branch.adapter; | ||
|
||
import io.reflectoring.coderadar.graph.analyzer.repository.CommitRepository; | ||
import io.reflectoring.coderadar.graph.analyzer.repository.MetricRepository; | ||
import io.reflectoring.coderadar.graph.projectadministration.branch.repository.BranchRepository; | ||
import io.reflectoring.coderadar.graph.projectadministration.domain.BranchEntity; | ||
import io.reflectoring.coderadar.graph.projectadministration.domain.CommitEntity; | ||
import io.reflectoring.coderadar.projectadministration.domain.Branch; | ||
import io.reflectoring.coderadar.projectadministration.port.driven.branch.DeleteBranchPort; | ||
import java.util.List; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class DeleteBranchAdapter implements DeleteBranchPort { | ||
|
||
private final BranchRepository branchRepository; | ||
private final CommitRepository commitRepository; | ||
private final MetricRepository metricRepository; | ||
|
||
public DeleteBranchAdapter( | ||
BranchRepository branchRepository, | ||
CommitRepository commitRepository, | ||
MetricRepository metricRepository) { | ||
this.branchRepository = branchRepository; | ||
this.commitRepository = commitRepository; | ||
this.metricRepository = metricRepository; | ||
} | ||
|
||
@Override | ||
public void delete(long projectId, Branch branch) { | ||
CommitEntity branchCommit = branchRepository.getBranchCommit(projectId, branch.getName()); | ||
BranchEntity branchEntity = | ||
branchRepository.findBranchInProjectByName(projectId, branch.getName()); | ||
branchRepository.delete(branchEntity); | ||
List<BranchEntity> branchesInProject = branchRepository.getBranchesInProject(projectId); | ||
deleteCommits(branchCommit, branchesInProject); | ||
} | ||
|
||
/** | ||
* Recursive method that walks the parents of the head of the branch and deletes them if: The | ||
* commit has no children. There is no branch pointer on the commit. | ||
* | ||
* <p>Also deletes any files that have been added or renamed as those are the only change types | ||
* for which new nodes are created and would therefore be no longer needed. | ||
* | ||
* <p>Metrics attached to the commit are also deleted if they exist. | ||
* | ||
* @param commit The commit to delete. | ||
* @param branchesInProject The currently available branches in the project. | ||
*/ | ||
private void deleteCommits(CommitEntity commit, List<BranchEntity> branchesInProject) { | ||
List<CommitEntity> children = commitRepository.getCommitChildren(commit.getId()); | ||
if (children != null | ||
&& commitRepository.getCommitChildren(commit.getId()).isEmpty() | ||
&& branchesInProject.stream().noneMatch(b -> b.getCommitHash().equals(commit.getName()))) { | ||
List<CommitEntity> parents = commitRepository.getCommitParents(commit.getId()); | ||
metricRepository.deleteMetricsForCommit(commit.getId()); | ||
commitRepository.deleteCommitAndAddedOrRenamedFiles(commit.getId()); | ||
for (CommitEntity parent : parents) { | ||
deleteCommits(parent, branchesInProject); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...adar-rest/src/main/java/io/reflectoring/coderadar/rest/branch/ListBranchesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
coderadar-test/src/test/java/io/reflectoring/coderadar/rest/branches/DeleteBranchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.reflectoring.coderadar.rest.branches; | ||
|
||
import io.reflectoring.coderadar.graph.analyzer.repository.CommitRepository; | ||
import io.reflectoring.coderadar.graph.analyzer.repository.FileRepository; | ||
import io.reflectoring.coderadar.projectadministration.domain.Branch; | ||
import io.reflectoring.coderadar.projectadministration.port.driven.branch.DeleteBranchPort; | ||
import io.reflectoring.coderadar.projectadministration.port.driven.branch.ListBranchesPort; | ||
import io.reflectoring.coderadar.projectadministration.port.driver.project.create.CreateProjectCommand; | ||
import io.reflectoring.coderadar.projectadministration.service.project.CreateProjectService; | ||
import io.reflectoring.coderadar.rest.ControllerTestTemplate; | ||
import java.net.URL; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class DeleteBranchTest extends ControllerTestTemplate { | ||
|
||
@Autowired private CreateProjectService createProjectService; | ||
|
||
@Autowired private FileRepository fileRepository; | ||
@Autowired private CommitRepository commitRepository; | ||
@Autowired private ListBranchesPort listBranchesPort; | ||
@Autowired private DeleteBranchPort deleteBranchPort; | ||
|
||
private Long projectId; | ||
private final Branch master = new Branch("master", "e9f7ff6fdd8c0863fdb5b24c9ed35a3651e20382"); | ||
private final Branch testBranch1 = | ||
new Branch("testBranch1", "d3272b3793bc4b2bc36a1a3a7c8293fcf8fe27df"); | ||
private final Branch testBranch2 = | ||
new Branch("testBranch2", "fcd9a0e7c34086fdb0aedc82497f8ddfa142e961"); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
URL testRepoURL = this.getClass().getClassLoader().getResource("test-repository"); | ||
projectId = | ||
createProjectService.createProject( | ||
new CreateProjectCommand( | ||
"testProject", null, null, testRepoURL.toString(), false, null, null)); | ||
} | ||
|
||
@Test | ||
void testDeleteBranch() { | ||
deleteBranchPort.delete(projectId, testBranch2); | ||
|
||
Assertions.assertThat(listBranchesPort.listBranchesInProject(projectId)) | ||
.containsExactlyInAnyOrder(testBranch1, master); | ||
|
||
Assertions.assertThat(commitRepository.findByProjectId(projectId)).hasSize(14); | ||
Assertions.assertThat(fileRepository.findAllinProject(projectId)).hasSize(8); | ||
} | ||
|
||
@Test | ||
void testDeleteBranchThatHasChildren() { | ||
deleteBranchPort.delete(projectId, testBranch1); | ||
|
||
Assertions.assertThat(listBranchesPort.listBranchesInProject(projectId)) | ||
.containsExactlyInAnyOrder(testBranch2, master); | ||
|
||
Assertions.assertThat(commitRepository.findByProjectId(projectId)).hasSize(15); | ||
Assertions.assertThat(fileRepository.findAllinProject(projectId)).hasSize(9); | ||
} | ||
|
||
@Test | ||
void testDeleteBranchWithFileOnlyModified() { | ||
deleteBranchPort.delete(projectId, master); | ||
|
||
Assertions.assertThat(listBranchesPort.listBranchesInProject(projectId)) | ||
.containsExactlyInAnyOrder(testBranch2, testBranch1); | ||
|
||
Assertions.assertThat(commitRepository.findByProjectId(projectId)).hasSize(14); | ||
Assertions.assertThat(fileRepository.findAllinProject(projectId)).hasSize(9); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters