Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added interface changes to the server #38258

Merged
merged 6 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.appsmith.external.constants.ErrorReferenceDocUrl;
import com.appsmith.external.dtos.GitBranchDTO;
import com.appsmith.external.dtos.GitLogDTO;
import com.appsmith.external.dtos.GitRefDTO;
import com.appsmith.external.dtos.GitStatusDTO;
import com.appsmith.external.dtos.MergeStatusDTO;
import com.appsmith.external.git.constants.GitSpan;
import com.appsmith.external.git.constants.ce.RefType;
import com.appsmith.external.git.handler.FSGitHandler;
import com.appsmith.external.helpers.Stopwatch;
import com.appsmith.git.configurations.GitServiceConfig;
Expand Down Expand Up @@ -344,6 +346,52 @@ public Mono<String> createAndCheckoutToBranch(Path repoSuffix, String branchName
.subscribeOn(scheduler);
}

private String createAndCheckoutBranch(Git git, GitRefDTO gitRefDTO) throws GitAPIException, IOException {
String branchName = gitRefDTO.getRefName();
git.checkout()
.setCreateBranch(TRUE)
.setName(branchName)
.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
.call();

repositoryHelper.updateRemoteBranchTrackingConfig(branchName, git);
return git.getRepository().getBranch();
}

private String createTag(Git git, GitRefDTO gitRefDTO) throws GitAPIException {
String tagName = gitRefDTO.getRefName();
String message = gitRefDTO.getMessage();
return git.tag().setName(tagName).setMessage(message).call().getName();
}

@Override
public Mono<String> createAndCheckoutReference(Path repoSuffix, GitRefDTO gitRefDTO) {
RefType refType = gitRefDTO.getRefType();
String refName = gitRefDTO.getRefName();

return Mono.using(
() -> Git.open(createRepoPath(repoSuffix).toFile()),
git -> Mono.fromCallable(() -> {
log.info(
"{} : Creating reference of type {} and name {} for the repo {}",
Thread.currentThread().getName(),
refType.name(),
refName,
repoSuffix);

if (RefType.TAG.equals(refType)) {
return createTag(git, gitRefDTO);
}

return createAndCheckoutBranch(git, gitRefDTO);
})
.timeout(Duration.ofMillis(Constraint.TIMEOUT_MILLIS))
.name(GitSpan.FS_CREATE_BRANCH)
.tap(Micrometer.observation(observationRegistry)),
Git::close)
.subscribeOn(scheduler);
}

@Override
public Mono<Boolean> deleteBranch(Path repoSuffix, String branchName) {
// We can safely assume that repo has been already initialised either in commit or clone flow and can directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class GitRefDTO {

RefType refType;

/**
* for tags, while tagging we require messages.
*/
String message;

boolean isDefault;

boolean createdFromLocal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.appsmith.external.dtos.GitBranchDTO;
import com.appsmith.external.dtos.GitLogDTO;
import com.appsmith.external.dtos.GitRefDTO;
import com.appsmith.external.dtos.GitStatusDTO;
import com.appsmith.external.dtos.MergeStatusDTO;
import org.eclipse.jgit.api.errors.GitAPIException;
Expand Down Expand Up @@ -80,6 +81,8 @@ Mono<String> pushApplication(
*/
Mono<String> createAndCheckoutToBranch(Path repoSuffix, String branchName);

Mono<String> createAndCheckoutReference(Path repoSuffix, GitRefDTO gitRefDTO);

/**
* Delete a branch in the local repo
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.appsmith.server.applications.git;

import com.appsmith.external.git.constants.ce.RefType;
import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.actioncollections.base.ActionCollectionService;
import com.appsmith.server.applications.base.ApplicationService;
Expand Down Expand Up @@ -322,4 +323,16 @@ public Application getNewArtifact(String workspaceId, String repoName) {
public Mono<Application> publishArtifactPostCommit(Artifact committedArtifact) {
return publishArtifact(committedArtifact, true);
}

@Override
public Mono<Application> publishArtifactPostRefCreation(
Artifact artifact, RefType refType, Boolean isPublishedManually) {
// TODO: create publish for ref type creation.
Application application = (Application) artifact;
if (RefType.TAG.equals(refType)) {
return Mono.just(application);
}

return Mono.just(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,17 +419,21 @@ protected Mono<? extends Artifact> checkoutReference(
});
} else {
// TODO refactor method to account for RefName as well
checkedOutArtifactMono = Mono.defer(() -> gitArtifactHelper
.getArtifactByBaseIdAndBranchName(
baseArtifactId, finalRefName, gitArtifactHelper.getArtifactReadPermission())
checkedOutArtifactMono = gitHandlingService
.checkoutArtifact(jsonTransformationDTO)
.flatMap(isCheckedOut -> gitArtifactHelper.getArtifactByBaseIdAndBranchName(
baseArtifactId, finalRefName, gitArtifactHelper.getArtifactReadPermission()))
.flatMap(artifact -> gitAnalyticsUtils.addAnalyticsForGitOperation(
AnalyticsEvents.GIT_CHECKOUT_BRANCH,
artifact,
artifact.getGitArtifactMetadata().getIsRepoPrivate())));
artifact.getGitArtifactMetadata().getIsRepoPrivate()));
}

return checkedOutArtifactMono
.doFinally(signalType -> gitRedisUtils.releaseFileLock(baseArtifactId, addFileLock))
return acquireFileLock
.then(checkedOutArtifactMono)
.flatMap(checkedOutArtifact -> gitRedisUtils
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sondermanish doesn't look like this one has a release in the chain

.releaseFileLock(baseArtifactId, addFileLock)
.thenReturn(checkedOutArtifact))
.tag(GitConstants.GitMetricConstants.CHECKOUT_REMOTE, FALSE.toString())
.name(GitSpan.OPS_CHECKOUT_BRANCH)
.tap(Micrometer.observation(observationRegistry))
Expand Down Expand Up @@ -522,6 +526,9 @@ public Mono<? extends Artifact> createReference(
FieldName.BRANCH_NAME));
}

Mono<Boolean> refCreationValidationMono = isValidationForRefCreationComplete(
baseArtifact, parentArtifact, gitType, refType);

Mono<? extends ArtifactExchangeJson> artifactExchangeJsonMono =
exportService.exportByArtifactId(
parentArtifact.getId(), VERSION_CONTROL, artifactType);
Expand All @@ -531,15 +538,24 @@ public Mono<? extends Artifact> createReference(
gitArtifactHelper.createNewArtifactForCheckout(
parentArtifact, refDTO.getRefName());

return Mono.zip(newArtifactFromSourceMono, artifactExchangeJsonMono);
return refCreationValidationMono.flatMap(isOkayToProceed -> {
if (!TRUE.equals(isOkayToProceed)) {
return Mono.error(new AppsmithException(
AppsmithError.GIT_ACTION_FAILED,
"ref creation",
"status unclean"));
}

return Mono.zip(newArtifactFromSourceMono, artifactExchangeJsonMono);
});
}))
.flatMap(tuple -> {
ArtifactExchangeJson exportedJson = tuple.getT2();
Artifact newRefArtifact = tuple.getT1();

Mono<String> refCreationMono = gitHandlingService
.createGitReference(jsonTransformationDTO)
// TODO: ths error could be shipped to handling layer as well?
.createGitReference(jsonTransformationDTO, refDTO)
// TODO: this error could be shipped to handling layer as well?
.onErrorResume(error -> Mono.error(new AppsmithException(
AppsmithError.GIT_ACTION_FAILED,
"ref creation preparation",
Expand All @@ -553,25 +569,44 @@ public Mono<? extends Artifact> createReference(
exportedJson,
refDTO.getRefName());
})
// after the branch is created, we need to reset the older branch to initial
// commit
.flatMap(importedArtifact -> {
return gitArtifactHelper.publishArtifactPostRefCreation(
importedArtifact, refType, TRUE);
})
// after the branch is created, we need to reset the older branch to the
// clean status, i.e. last commit
.doOnSuccess(newImportedArtifact ->
discardChanges(parentArtifact.getId(), artifactType, gitType));
});
})
.flatMap(newImportedArtifact -> gitAnalyticsUtils
.addAnalyticsForGitOperation(
.flatMap(newImportedArtifact -> gitRedisUtils
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we release in case of errors?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No release in chain

.releaseFileLock(
newImportedArtifact.getGitArtifactMetadata().getDefaultArtifactId(), TRUE)
.then(gitAnalyticsUtils.addAnalyticsForGitOperation(
AnalyticsEvents.GIT_CREATE_BRANCH,
newImportedArtifact,
newImportedArtifact.getGitArtifactMetadata().getIsRepoPrivate())
.doFinally(signalType -> gitRedisUtils.releaseFileLock(
newImportedArtifact.getGitArtifactMetadata().getDefaultArtifactId(), TRUE)))
newImportedArtifact.getGitArtifactMetadata().getIsRepoPrivate())))
.name(GitSpan.OPS_CREATE_BRANCH)
.tap(Micrometer.observation(observationRegistry));

return Mono.create(sink -> createBranchMono.subscribe(sink::success, sink::error, null, sink.currentContext()));
}

protected Mono<Boolean> isValidationForRefCreationComplete(
Artifact baseArtifact, Artifact parentArtifact, GitType gitType, RefType refType) {
if (RefType.BRANCH.equals(refType)) {
return Mono.just(TRUE);
}

return getStatus(baseArtifact, parentArtifact, false, true, gitType).map(gitStatusDTO -> {
if (!Boolean.TRUE.equals(gitStatusDTO.getIsClean())) {
return FALSE;
}

return TRUE;
});
}
Comment on lines +603 to +616
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding validation for tag name format

The reference creation validation method should also validate the tag name format when RefType is TAG.

 protected Mono<Boolean> isValidationForRefCreationComplete(
         Artifact baseArtifact, Artifact parentArtifact, GitType gitType, RefType refType) {
     if (RefType.BRANCH.equals(refType)) {
         return Mono.just(TRUE);
     }
+    if (RefType.TAG.equals(refType)) {
+        String tagName = parentArtifact.getGitArtifactMetadata().getRefName();
+        if (!isValidTagName(tagName)) {
+            return Mono.just(FALSE);
+        }
+    }
     return getStatus(baseArtifact, parentArtifact, false, true, gitType).map(gitStatusDTO -> {

Committable suggestion skipped: line range outside the PR's diff.


@Override
public Mono<? extends Artifact> deleteGitReference(
String baseArtifactId, GitRefDTO gitRefDTO, ArtifactType artifactType, GitType gitType) {
Expand Down Expand Up @@ -650,7 +685,9 @@ protected Mono<? extends Artifact> deleteGitReference(

return gitHandlingService
.deleteGitReference(jsonTransformationDTO)
.doFinally(signalType -> gitRedisUtils.releaseFileLock(baseArtifactId, TRUE))
.flatMap(isReferenceDeleted -> gitRedisUtils
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same error handling question for all

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No release in chain

.releaseFileLock(baseArtifactId, TRUE)
.thenReturn(isReferenceDeleted))
.flatMap(isReferenceDeleted -> {
if (FALSE.equals(isReferenceDeleted)) {
return Mono.error(new AppsmithException(
Expand Down Expand Up @@ -1384,10 +1421,16 @@ protected Mono<GitStatusDTO> getStatus(
fetchRemoteMono = Mono.just("ignored");
}

return Mono.zip(prepareForStatus, fetchRemoteMono)
.then(gitHandlingService.getStatus(jsonTransformationDTO));
return Mono.zip(prepareForStatus, fetchRemoteMono).flatMap(tuple2 -> {
return gitHandlingService
.getStatus(jsonTransformationDTO)
.flatMap(gitStatusDTO -> {
return gitRedisUtils
.releaseFileLock(baseArtifactId, isFileLock)
.thenReturn(gitStatusDTO);
});
});
})
.doFinally(signalType -> gitRedisUtils.releaseFileLock(baseArtifactId, isFileLock))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No release in chain

.onErrorResume(throwable -> {
/*
in case of any error, the global exception handler will release the lock
Expand Down Expand Up @@ -1640,11 +1683,12 @@ public Mono<? extends Artifact> discardChanges(
new AppsmithException(AppsmithError.INVALID_GIT_CONFIGURATION, GIT_CONFIG_ERROR));
}

return Mono.just(branchedArtifact)
.doFinally(signalType -> gitRedisUtils.acquireGitLock(
return gitRedisUtils
.acquireGitLock(
branchedGitData.getDefaultArtifactId(),
GitConstants.GitCommandConstants.DISCARD,
TRUE));
TRUE)
.thenReturn(branchedArtifact);
})
.flatMap(branchedArtifact -> {
GitArtifactMetadata branchedGitData = branchedArtifact.getGitArtifactMetadata();
Expand Down Expand Up @@ -1677,10 +1721,13 @@ public Mono<? extends Artifact> discardChanges(
// 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)))
.flatMap(branchedArtifact -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No release in chain

return gitRedisUtils
.releaseFileLock(
branchedArtifact.getGitArtifactMetadata().getDefaultArtifactId(), TRUE)
.then(gitAnalyticsUtils.addAnalyticsForGitOperation(
AnalyticsEvents.GIT_DISCARD_CHANGES, branchedArtifact, null));
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding retry mechanism for lock release

Lock release is critical for preventing deadlocks. Consider adding a retry mechanism with exponential backoff.

 .flatMap(branchedArtifact -> {
     return gitRedisUtils
             .releaseFileLock(
                     branchedArtifact.getGitArtifactMetadata().getDefaultArtifactId(), TRUE)
+            .retryWhen(Retry.backoff(3, Duration.ofMillis(100))
+                .filter(throwable -> throwable instanceof LockAcquisitionException))
             .then(gitAnalyticsUtils.addAnalyticsForGitOperation(
                     AnalyticsEvents.GIT_DISCARD_CHANGES, branchedArtifact, null));
 })

Committable suggestion skipped: line range outside the PR's diff.

.name(GitSpan.OPS_DISCARD_CHANGES)
.tap(Micrometer.observation(observationRegistry));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.appsmith.server.git.central;

import com.appsmith.external.dtos.GitRefDTO;
import com.appsmith.external.dtos.GitStatusDTO;
import com.appsmith.external.git.constants.ce.RefType;
import com.appsmith.git.dto.CommitDTO;
Expand Down Expand Up @@ -76,7 +77,9 @@ Mono<? extends ArtifactExchangeJson> recreateArtifactJsonFromLastCommit(

Mono<GitStatusDTO> getStatus(ArtifactJsonTransformationDTO jsonTransformationDTO);

Mono<String> createGitReference(ArtifactJsonTransformationDTO artifactJsonTransformationDTO);
Mono<String> createGitReference(ArtifactJsonTransformationDTO artifactJsonTransformationDTO, GitRefDTO gitRefDTO);

Mono<Boolean> deleteGitReference(ArtifactJsonTransformationDTO jsonTransformationDTO);

Mono<Boolean> checkoutArtifact(ArtifactJsonTransformationDTO jsonTransformationDTO);
}
Loading
Loading