Skip to content

Commit

Permalink
chore: Added changes for discard (#38152)
Browse files Browse the repository at this point in the history
## Description
- Added implementation for discard

Fixes #37438

## Automation

/ok-to-test tags="@tag.Git"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/12314835405>
> Commit: 0db248a
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12314835405&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Git`
> Spec:
> <hr>Fri, 13 Dec 2024 12:18:45 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced a method to discard changes associated with a specified
branched artifact.
  - Added functionality to recreate artifact JSON from the last commit.

- **Bug Fixes**
- Enhanced handling of lock requirements in Git operations, allowing for
null values.

- **Documentation**
- Updated method signatures and added descriptions for new
functionalities.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
sondermanish authored Dec 13, 2024
1 parent 32ed6f0 commit 813279b
Show file tree
Hide file tree
Showing 5 changed files with 105 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 @@ -31,4 +31,6 @@ Mono<String> fetchRemoteChanges(
ArtifactType artifactType,
GitType gitType,
RefType refType);

Mono<? extends Artifact> discardChanges(String branchedArtifactId, ArtifactType artifactType, GitType gitType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,84 @@ private Mono<? extends Artifact> updateArtifactWithGitMetadataGivenPermission(
.getArtifactHelper(artifact.getArtifactType())
.saveArtifact(artifact);
}

/**
* Resets the artifact to last commit, all uncommitted changes are lost in the process.
* @param branchedArtifactId : id of the branchedArtifact
* @param artifactType type of the artifact
* @param gitType what is the intended implementation type
* @return : a publisher of an artifact.
*/
@Override
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 Mono.just(branchedArtifact)
.doFinally(signalType -> gitRedisUtils.acquireGitLock(
branchedGitData.getDefaultArtifactId(),
GitConstants.GitCommandConstants.DISCARD,
TRUE));
})
.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 conflicts in the remote repository before proceeding."));
})
.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 -> gitAnalyticsUtils
.addAnalyticsForGitOperation(AnalyticsEvents.GIT_DISCARD_CHANGES, branchedArtifact, null)
.doFinally(signalType -> gitRedisUtils.releaseFileLock(
branchedArtifact.getGitArtifactMetadata().getDefaultArtifactId(), TRUE)))
.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 813279b

Please sign in to comment.