diff --git a/app/server/appsmith-git/src/main/java/com/appsmith/git/handler/ce/FSGitHandlerCEImpl.java b/app/server/appsmith-git/src/main/java/com/appsmith/git/handler/ce/FSGitHandlerCEImpl.java index 9e8b89e159..545fee5db2 100644 --- a/app/server/appsmith-git/src/main/java/com/appsmith/git/handler/ce/FSGitHandlerCEImpl.java +++ b/app/server/appsmith-git/src/main/java/com/appsmith/git/handler/ce/FSGitHandlerCEImpl.java @@ -1563,9 +1563,9 @@ public class FSGitHandlerCEImpl implements FSGitHandler { } @Override - public Mono getBranchTrackingStatus(Path repoPath, String branchName) { + public Mono getBranchTrackingStatus(Path repoSuffix, String branchName) { return Mono.using( - () -> Git.open(repoPath.toFile()), + () -> Git.open(createRepoPath(repoSuffix).toFile()), git -> Mono.fromCallable(() -> { Span jgitBranchTrackingSpan = observationHelper.createSpan(GitSpan.JGIT_BRANCH_TRACK); diff --git a/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/git/handler/FSGitHandler.java b/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/git/handler/FSGitHandler.java index 607821b098..dd86807c08 100644 --- a/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/git/handler/FSGitHandler.java +++ b/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/git/handler/FSGitHandler.java @@ -249,5 +249,5 @@ public interface FSGitHandler { Path createRepoPath(Path suffix); - Mono getBranchTrackingStatus(Path repoPath, String branchName); + Mono getBranchTrackingStatus(Path repoSuffix, String branchName); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/CentralGitServiceCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/CentralGitServiceCE.java index 5c84188dec..bd9db61b26 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/CentralGitServiceCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/CentralGitServiceCE.java @@ -15,6 +15,7 @@ import com.appsmith.server.dtos.GitConnectDTO; import com.appsmith.server.dtos.GitDocsDTO; import com.appsmith.server.dtos.GitMergeDTO; import com.appsmith.server.dtos.GitPullDTO; +import org.eclipse.jgit.lib.BranchTrackingStatus; import reactor.core.publisher.Mono; import java.util.List; @@ -39,13 +40,26 @@ public interface CentralGitServiceCE { Mono> listBranchForArtifact( String branchedArtifactId, ArtifactType artifactType, Boolean pruneBranches, GitType gitType); - Mono fetchRemoteChanges( + Mono fetchRemoteChanges( String referenceArtifactId, ArtifactType artifactType, boolean isFileLock, GitType gitType, RefType refType); + /** + * Fetches remote changes from remote git repository. + * This overloaded method is directly used for autocommit purpose + * @param baseArtifact : base artifact on which the repository was connected + * @param refArtifact : the reference/branch artifact for which remote changes are to be fetched + * @param isFileLock : would this require a redis file lock + * @param gitType : GitType of this operation + * @param refType : RefType for this operation + * @return : branchTrackingStatus, i.e., How many commits is local ahead and behind of remote + */ + Mono fetchRemoteChanges( + Artifact baseArtifact, Artifact refArtifact, boolean isFileLock, GitType gitType, RefType refType); + Mono mergeBranch( String branchedArtifactId, ArtifactType artifactType, GitMergeDTO gitMergeDTO, GitType gitType); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/CentralGitServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/CentralGitServiceCEImpl.java index 561a35b6f1..e4058d83e5 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/CentralGitServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/CentralGitServiceCEImpl.java @@ -1906,7 +1906,8 @@ public class CentralGitServiceCEImpl implements CentralGitServiceCE { }); } - public Mono fetchRemoteChanges( + @Override + public Mono fetchRemoteChanges( Artifact baseArtifact, Artifact refArtifact, boolean isFileLock, GitType gitType, RefType refType) { if (refArtifact == null @@ -1941,9 +1942,12 @@ public class CentralGitServiceCEImpl implements CentralGitServiceCE { GitHandlingService gitHandlingService = gitHandlingServiceResolver.getGitHandlingService(gitType); // current user mono has been zipped just to run in parallel. - Mono fetchRemoteMono = acquireGitLockMono + Mono fetchRemoteMono = acquireGitLockMono .then(Mono.defer(() -> gitHandlingService.fetchRemoteReferences( jsonTransformationDTO, baseArtifactGitData.getGitAuth(), FALSE))) + .flatMap(fetchedRemoteString -> { + return gitHandlingService.getBranchTrackingStatus(jsonTransformationDTO); + }) .flatMap(fetchedRemoteStatusString -> { return gitRedisUtils .releaseFileLock(artifactType, baseArtifactId, isFileLock) @@ -1960,14 +1964,17 @@ public class CentralGitServiceCEImpl implements CentralGitServiceCE { refArtifactGitData.getRefName(), gitType, throwable); - return Mono.error( - new AppsmithException(AppsmithError.GIT_ACTION_FAILED, "fetch", throwable.getMessage())); + + return gitRedisUtils + .releaseFileLock(artifactType, baseArtifactId, isFileLock) + .then(Mono.error(new AppsmithException( + AppsmithError.GIT_ACTION_FAILED, "fetch", throwable.getMessage()))); }) .elapsed() .zipWith(currUserMono) .flatMap(objects -> { Long elapsedTime = objects.getT1().getT1(); - String fetchRemote = objects.getT1().getT2(); + BranchTrackingStatus fetchRemote = objects.getT1().getT2(); User currentUser = objects.getT2(); return gitAnalyticsUtils .sendUnitExecutionTimeAnalyticsEvent( @@ -1995,7 +2002,7 @@ public class CentralGitServiceCEImpl implements CentralGitServiceCE { * @return Mono of {@link BranchTrackingStatus} */ @Override - public Mono fetchRemoteChanges( + public Mono fetchRemoteChanges( String refArtifactId, ArtifactType artifactType, boolean isFileLock, GitType gitType, RefType refType) { GitArtifactHelper artifactGitHelper = gitArtifactHelperResolver.getArtifactHelper(artifactType); AclPermission artifactEditPermission = artifactGitHelper.getArtifactEditPermission(); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/GitHandlingServiceCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/GitHandlingServiceCE.java index 3dd660ba6e..f3f155e400 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/GitHandlingServiceCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/central/GitHandlingServiceCE.java @@ -13,6 +13,7 @@ import com.appsmith.server.dtos.ArtifactExchangeJson; import com.appsmith.server.dtos.GitConnectDTO; import com.appsmith.server.dtos.GitMergeDTO; import com.appsmith.server.git.dtos.ArtifactJsonTransformationDTO; +import org.eclipse.jgit.lib.BranchTrackingStatus; import reactor.core.publisher.Mono; import reactor.util.function.Tuple2; @@ -94,6 +95,8 @@ public interface GitHandlingServiceCE { Mono fetchRemoteReferences( ArtifactJsonTransformationDTO jsonTransformationDTO, FetchRemoteDTO fetchRemoteDTO, GitAuth gitAuth); + Mono getBranchTrackingStatus(ArtifactJsonTransformationDTO artifactJsonTransformationDTO); + Mono mergeBranches(ArtifactJsonTransformationDTO jsonTransformationDTO, GitMergeDTO gitMergeDTO); Mono isBranchMergable(ArtifactJsonTransformationDTO JsonTransformationDTO, GitMergeDTO gitMergeDTO); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/common/CommonGitServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/common/CommonGitServiceCEImpl.java index 495771a1f9..7307b1cd9a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/common/CommonGitServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/common/CommonGitServiceCEImpl.java @@ -450,8 +450,9 @@ public class CommonGitServiceCEImpl implements CommonGitServiceCE { baseArtifactId, finalBranchName, throwable); - return Mono.error(new AppsmithException( - AppsmithError.GIT_ACTION_FAILED, "fetch", throwable.getMessage())); + return releaseFileLock(baseArtifactId, isFileLock) + .then(Mono.error(new AppsmithException( + AppsmithError.GIT_ACTION_FAILED, "fetch", throwable.getMessage()))); }); }) .elapsed() diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationControllerCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationControllerCE.java index e1ce18fea0..ee8000e15f 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationControllerCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationControllerCE.java @@ -28,6 +28,7 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.BooleanUtils; +import org.eclipse.jgit.lib.BranchTrackingStatus; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -142,7 +143,7 @@ public class GitApplicationControllerCE { @JsonView(Views.Public.class) @GetMapping("/{referencedApplicationId}/fetch/remote") - public Mono> fetchRemoteChanges( + public Mono> fetchRemoteChanges( @PathVariable String referencedApplicationId, @RequestHeader(required = false, defaultValue = "branch") RefType refType) { log.info("Going to compare with remote for default referencedApplicationId {}", referencedApplicationId); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/fs/GitFSServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/fs/GitFSServiceCEImpl.java index 8efeedd0ed..4ecd79d268 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/fs/GitFSServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/fs/GitFSServiceCEImpl.java @@ -42,6 +42,7 @@ import org.eclipse.jgit.api.errors.EmptyCommitException; import org.eclipse.jgit.api.errors.InvalidRemoteException; import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.errors.RepositoryNotFoundException; +import org.eclipse.jgit.lib.BranchTrackingStatus; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import reactor.core.observability.micrometer.Micrometer; @@ -674,6 +675,20 @@ public class GitFSServiceCEImpl implements GitHandlingServiceCE { return fsGitHandler.fetchRemote(repoSuffix, false, fetchRemoteDTO, publicKey, privateKey); } + @Override + public Mono getBranchTrackingStatus(ArtifactJsonTransformationDTO jsonTransformationDTO) { + String workspaceId = jsonTransformationDTO.getWorkspaceId(); + String baseArtifactId = jsonTransformationDTO.getBaseArtifactId(); + String repoName = jsonTransformationDTO.getRepoName(); + String refName = jsonTransformationDTO.getRefName(); + + ArtifactType artifactType = jsonTransformationDTO.getArtifactType(); + GitArtifactHelper gitArtifactHelper = gitArtifactHelperResolver.getArtifactHelper(artifactType); + Path repoSuffix = gitArtifactHelper.getRepoSuffixPath(workspaceId, baseArtifactId, repoName); + + return fsGitHandler.getBranchTrackingStatus(repoSuffix, refName); + } + @Override public Mono mergeBranches(ArtifactJsonTransformationDTO jsonTransformationDTO, GitMergeDTO gitMergeDTO) { String workspaceId = jsonTransformationDTO.getWorkspaceId();