fix: Logic to delete stale git branch which are not in db (#10647)

This commit is contained in:
Anagh Hegde 2022-01-26 21:29:00 +05:30 committed by GitHub
parent c83f7fb3c1
commit e0e4a73650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 3 deletions

View File

@ -1537,9 +1537,13 @@ public class GitServiceCEImpl implements GitServiceCE {
.collect(Collectors.toList());
Mono<List<GitBranchDTO>> monoBranchList = Flux.fromIterable(localBranch)
.flatMap(gitBranch ->
applicationService.findByBranchNameAndDefaultApplicationId(gitBranch, defaultApplicationId, MANAGE_APPLICATIONS)
.flatMap(applicationPageService::deleteApplicationByResource)
.flatMap(gitBranch -> applicationService.findByBranchNameAndDefaultApplicationId(gitBranch, defaultApplicationId, MANAGE_APPLICATIONS)
.flatMap(application1 -> applicationPageService.deleteApplicationByResource(application1))
// Delete the branch that exists in local file system but not in DB
.onErrorResume(throwable -> {
log.warn(" No application exists in DB for the local branch of file system", throwable);
return Mono.empty();
})
.then(gitExecutor.deleteBranch(repoPath, gitBranch)))
.then(Mono.just(gitBranchListDTOS));

View File

@ -1181,6 +1181,47 @@ public class GitServiceTest {
.verifyComplete();
}
@Test
@WithUserDetails(value = "api_user")
public void listBranchForApplication_pruneBranchWithBranchNotExistsInDB_Success() throws IOException {
List<GitBranchDTO> branchList = new ArrayList<>();
GitBranchDTO gitBranchDTO = new GitBranchDTO();
gitBranchDTO.setBranchName("defaultBranch");
gitBranchDTO.setDefault(false);
branchList.add(gitBranchDTO);
gitBranchDTO = new GitBranchDTO();
gitBranchDTO.setBranchName("localBranchOnly");
gitBranchDTO.setDefault(false);
branchList.add(gitBranchDTO);
gitBranchDTO = new GitBranchDTO();
gitBranchDTO.setBranchName("origin/defaultBranch");
gitBranchDTO.setDefault(true);
branchList.add(gitBranchDTO);
Mockito.when(gitExecutor.listBranches(Mockito.any(Path.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), eq(true)))
.thenReturn(Mono.just(branchList));
Mockito.when(gitExecutor.cloneApplication(Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenReturn(Mono.just("defaultBranch"));
Mockito.when(gitFileUtils.checkIfDirectoryIsEmpty(Mockito.any(Path.class))).thenReturn(Mono.just(true));
Mockito.when(gitFileUtils.initializeGitRepo(Mockito.any(Path.class), Mockito.anyString(), Mockito.anyString()))
.thenReturn(Mono.just(Paths.get("textPath")));
Mockito.when(gitExecutor.fetchRemote(Mockito.any(Path.class), Mockito.anyString(), Mockito.anyString(), eq(false)))
.thenReturn(Mono.just("status"));
Mockito.when(gitExecutor.deleteBranch(Mockito.any(Path.class), Mockito.anyString()))
.thenReturn(Mono.just(true));
Application application1 = createApplicationConnectedToGit("listBranchForApplication_pruneBranchWithBranchNotExistsInDB_Success", "defaultBranch");
Mono<List<GitBranchDTO>> listMono = gitService.listBranchForApplication(application1.getId(), true, "defaultBranch");
StepVerifier
.create(listMono)
.assertNext(listBranch -> {
assertThat(listBranch).isNotEqualTo(branchList);
})
.verifyComplete();
}
@Test
@WithUserDetails(value = "api_user")
public void pullChanges_upstreamChangesAvailable_pullSuccess() throws IOException, GitAPIException {