Skip to content

Commit

Permalink
added changes for discard
Browse files Browse the repository at this point in the history
  • Loading branch information
sondermanish committed Dec 13, 2024
1 parent 6eb44a1 commit 90d6fb8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Mono<Boolean> releaseFileLock(String defaultApplicationId) {
* @return : Boolean for whether the lock is acquired
*/
// TODO @Manish add artifactType reference in incoming prs.
public Mono<Boolean> acquireGitLock(String baseArtifactId, String commandName, boolean isLockRequired) {
public Mono<Boolean> acquireGitLock(String baseArtifactId, String commandName, Boolean isLockRequired) {
if (!Boolean.TRUE.equals(isLockRequired)) {
return Mono.just(Boolean.TRUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,85 @@ private Mono<? extends Artifact> updateArtifactWithGitMetadataGivenPermission(
.getArtifactHelper(artifact.getArtifactType())
.saveArtifact(artifact);
}

/**
*
* @param branchedArtifactId : id of the branchedArtifact
* @param artifactType type of the artifact
* @param gitType
* @return
*/
public Mono<? extends Artifact> discardChanges(
String branchedArtifactId, ArtifactType artifactType, GitType gitType) {

if (!hasText(branchedArtifactId)) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ARTIFACT_ID));
}

GitArtifactHelper<?> gitArtifactHelper = gitArtifactHelperResolver.getArtifactHelper(artifactType);
AclPermission artifactEditPermission = gitArtifactHelper.getArtifactEditPermission();

Mono<? extends Artifact> branchedArtifactMonoCached =
gitArtifactHelper.getArtifactById(branchedArtifactId, artifactEditPermission);

Mono<? extends Artifact> recreatedArtifactFromLastCommit;

// Rehydrate the artifact from local file system
recreatedArtifactFromLastCommit = branchedArtifactMonoCached
.flatMap(branchedArtifact -> {
GitArtifactMetadata branchedGitData = branchedArtifact.getGitArtifactMetadata();
if (branchedGitData == null || !hasText(branchedGitData.getDefaultArtifactId())) {
return Mono.error(
new AppsmithException(AppsmithError.INVALID_GIT_CONFIGURATION, GIT_CONFIG_ERROR));
}

return gitRedisUtils
.acquireGitLock(
branchedGitData.getDefaultArtifactId(),
GitConstants.GitCommandConstants.DISCARD,
TRUE)
.thenReturn(branchedArtifact);
})
.flatMap(branchedArtifact -> {
GitArtifactMetadata branchedGitData = branchedArtifact.getGitArtifactMetadata();
ArtifactJsonTransformationDTO jsonTransformationDTO = new ArtifactJsonTransformationDTO();
// Because this operation is only valid for branches
jsonTransformationDTO.setArtifactType(artifactType);
jsonTransformationDTO.setRefType(RefType.BRANCH);
jsonTransformationDTO.setWorkspaceId(branchedArtifact.getWorkspaceId());
jsonTransformationDTO.setBaseArtifactId(branchedGitData.getDefaultArtifactId());
jsonTransformationDTO.setRefName(branchedGitData.getRefName());
jsonTransformationDTO.setRepoName(branchedGitData.getRepoName());

GitHandlingService gitHandlingService = gitHandlingServiceResolver.getGitHandlingService(gitType);

return gitHandlingService
.recreateArtifactJsonFromLastCommit(jsonTransformationDTO)
.onErrorResume(throwable -> {
log.error("Git recreate ArtifactJsonFailed", throwable.getMessage());
return Mono.error(
new AppsmithException(
AppsmithError.GIT_ACTION_FAILED,
"discard changes",
"Please create a new branch and resolve the conflicts on remote repository before proceeding ahead."));
})
.flatMap(artifactExchangeJson -> importService.importArtifactInWorkspaceFromGit(
branchedArtifact.getWorkspaceId(),
branchedArtifact.getId(),
artifactExchangeJson,
branchedGitData.getBranchName()))
// Update the last deployed status after the rebase
.flatMap(importedArtifact -> gitArtifactHelper.publishArtifact(importedArtifact, true));
})
.flatMap(branchedArtifact -> gitRedisUtils
.releaseFileLock(
branchedArtifact.getGitArtifactMetadata().getDefaultArtifactId(), TRUE)
.then(gitAnalyticsUtils.addAnalyticsForGitOperation(
AnalyticsEvents.GIT_DISCARD_CHANGES, branchedArtifact, null)))
.name(GitSpan.OPS_DISCARD_CHANGES)
.tap(Micrometer.observation(observationRegistry));

return Mono.create(sink ->
recreatedArtifactFromLastCommit.subscribe(sink::success, sink::error, null, sink.currentContext()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ Mono<Tuple2<? extends Artifact, String>> commitArtifact(
Artifact branchedArtifact, CommitDTO commitDTO, ArtifactJsonTransformationDTO jsonTransformationDTO);

Mono<String> fetchRemoteChanges(ArtifactJsonTransformationDTO jsonTransformationDTO, GitAuth gitAuth);

Mono<? extends ArtifactExchangeJson> recreateArtifactJsonFromLastCommit(
ArtifactJsonTransformationDTO jsonTransformationDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,23 @@ public Mono<String> fetchRemoteChanges(ArtifactJsonTransformationDTO jsonTransfo

return checkoutBranchMono.then(Mono.defer(() -> fetchRemoteMono));
}

@Override
public Mono<? extends ArtifactExchangeJson> recreateArtifactJsonFromLastCommit(
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.rebaseBranch(repoSuffix, refName).flatMap(rebaseStatus -> {
return commonGitFileUtils.reconstructArtifactExchangeJsonFromGitRepoWithAnalytics(
workspaceId, baseArtifactId, repoName, refName, artifactType);
});
}
}

0 comments on commit 90d6fb8

Please sign in to comment.