From e0e4a736500c6bed972367d271189ff5990e7176 Mon Sep 17 00:00:00 2001 From: Anagh Hegde Date: Wed, 26 Jan 2022 21:29:00 +0530 Subject: [PATCH] fix: Logic to delete stale git branch which are not in db (#10647) --- .../server/services/ce/GitServiceCEImpl.java | 10 +++-- .../server/services/GitServiceTest.java | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/GitServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/GitServiceCEImpl.java index df19bc7980..c0d6ddb18a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/GitServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/GitServiceCEImpl.java @@ -1537,9 +1537,13 @@ public class GitServiceCEImpl implements GitServiceCE { .collect(Collectors.toList()); Mono> 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)); diff --git a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/GitServiceTest.java b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/GitServiceTest.java index c2d21295a5..2f33473f16 100644 --- a/app/server/appsmith-server/src/test/java/com/appsmith/server/services/GitServiceTest.java +++ b/app/server/appsmith-server/src/test/java/com/appsmith/server/services/GitServiceTest.java @@ -1181,6 +1181,47 @@ public class GitServiceTest { .verifyComplete(); } + @Test + @WithUserDetails(value = "api_user") + public void listBranchForApplication_pruneBranchWithBranchNotExistsInDB_Success() throws IOException { + List 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> 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 {