-
Notifications
You must be signed in to change notification settings - Fork 4.7k
chore: reference lifecycle #38174
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
Merged
Merged
chore: reference lifecycle #38174
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9e090e7
added status changes
sondermanish 7d556ed
added spotless
sondermanish e74219c
added checkout branch
sondermanish 21933b8
reverted config file commit
sondermanish 4cd584f
reverted config changes
sondermanish a7b8606
reverted config changes
sondermanish 9f2f3bc
reverted config changes
sondermanish 592833e
changes to EOF for commit file
sondermanish f5dcc3f
added changes
sondermanish 73a48ae
maded EOF change to config file
sondermanish 376d3ba
Merge branch 'chore/status' into chore/references
sondermanish 804ed27
added changes for checkout
sondermanish fe1648a
merge with release
sondermanish 5cf5026
merge with release
sondermanish 03a3843
added changes
sondermanish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package com.appsmith.server.git.central; | ||
|
|
||
| import com.appsmith.external.constants.AnalyticsEvents; | ||
| import com.appsmith.external.dtos.GitStatusDTO; | ||
| import com.appsmith.external.git.constants.GitConstants; | ||
| import com.appsmith.external.git.constants.GitSpan; | ||
| import com.appsmith.external.models.Datasource; | ||
|
|
@@ -67,6 +68,7 @@ | |
| import static com.appsmith.external.git.constants.ce.GitConstantsCE.GIT_CONFIG_ERROR; | ||
| import static com.appsmith.external.git.constants.ce.GitConstantsCE.GIT_PROFILE_ERROR; | ||
| import static com.appsmith.external.git.constants.ce.GitSpanCE.OPS_COMMIT; | ||
| import static com.appsmith.external.git.constants.ce.GitSpanCE.OPS_STATUS; | ||
| import static com.appsmith.external.helpers.AppsmithBeanUtils.copyNestedNonNullProperties; | ||
| import static com.appsmith.server.constants.FieldName.BRANCH_NAME; | ||
| import static com.appsmith.server.constants.FieldName.DEFAULT; | ||
|
|
@@ -102,6 +104,9 @@ public class CentralGitServiceCEImpl implements CentralGitServiceCE { | |
| private final GitRedisUtils gitRedisUtils; | ||
| private final ObservationRegistry observationRegistry; | ||
|
|
||
| protected static final String ORIGIN = "origin/"; | ||
| protected static final String REMOTE_NAME_REPLACEMENT = ""; | ||
|
|
||
| protected Mono<Boolean> isRepositoryLimitReachedForWorkspace(String workspaceId, Boolean isRepositoryPrivate) { | ||
| if (!isRepositoryPrivate) { | ||
| return Mono.just(FALSE); | ||
|
|
@@ -331,6 +336,107 @@ private boolean checkIsDatasourceNameConflict( | |
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<? extends Artifact> checkoutReference( | ||
| String referenceArtifactId, | ||
| String referenceToBeCheckedOut, | ||
| boolean addFileLock, | ||
| ArtifactType artifactType, | ||
| GitType gitType, | ||
| RefType refType) { | ||
|
|
||
| if (!hasText(referenceToBeCheckedOut)) { | ||
| return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.REF_NAME)); | ||
| } | ||
|
|
||
| Mono<Tuple2<? extends Artifact, ? extends Artifact>> baseAndBranchedArtifactMono = | ||
| getBaseAndBranchedArtifacts(referenceArtifactId, artifactType); | ||
|
|
||
| return baseAndBranchedArtifactMono.flatMap(artifactTuples -> { | ||
| Artifact sourceArtifact = artifactTuples.getT1(); | ||
| return checkoutReference(sourceArtifact, referenceToBeCheckedOut, addFileLock, gitType, refType); | ||
| }); | ||
| } | ||
|
|
||
| protected Mono<? extends Artifact> checkoutReference( | ||
| Artifact baseArtifact, | ||
| String referenceToBeCheckedOut, | ||
| boolean addFileLock, | ||
| GitType gitType, | ||
| RefType refType) { | ||
|
|
||
| if (!hasText(referenceToBeCheckedOut)) { | ||
| return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.REF_NAME)); | ||
| } | ||
|
|
||
| GitArtifactMetadata baseGitMetadata = baseArtifact.getGitArtifactMetadata(); | ||
|
|
||
| if (isBaseGitMetadataInvalid(baseGitMetadata, gitType)) { | ||
| return Mono.error(new AppsmithException(AppsmithError.INVALID_GIT_SSH_CONFIGURATION)); | ||
| } | ||
|
|
||
| String baseArtifactId = baseGitMetadata.getDefaultArtifactId(); | ||
| final String finalRefName = referenceToBeCheckedOut.replaceFirst(ORIGIN, REMOTE_NAME_REPLACEMENT); | ||
|
|
||
| GitArtifactHelper<?> gitArtifactHelper = | ||
| gitArtifactHelperResolver.getArtifactHelper(baseArtifact.getArtifactType()); | ||
| GitHandlingService gitHandlingService = gitHandlingServiceResolver.getGitHandlingService(gitType); | ||
|
|
||
| Mono<Boolean> acquireFileLock = gitRedisUtils.acquireGitLock( | ||
| baseArtifactId, GitConstants.GitCommandConstants.CHECKOUT_BRANCH, addFileLock); | ||
|
|
||
| Mono<? extends Artifact> checkedOutArtifactMono; | ||
| // If the user is trying to check out remote reference, create a new reference if it does not exist already | ||
|
|
||
| ArtifactJsonTransformationDTO jsonTransformationDTO = new ArtifactJsonTransformationDTO(); | ||
| jsonTransformationDTO.setWorkspaceId(baseArtifact.getWorkspaceId()); | ||
| jsonTransformationDTO.setBaseArtifactId(baseGitMetadata.getDefaultArtifactId()); | ||
| jsonTransformationDTO.setRefType(refType); | ||
| jsonTransformationDTO.setArtifactType(baseArtifact.getArtifactType()); | ||
| jsonTransformationDTO.setRepoName(baseGitMetadata.getRepoName()); | ||
|
|
||
| if (referenceToBeCheckedOut.startsWith(ORIGIN)) { | ||
|
|
||
| checkedOutArtifactMono = gitHandlingService | ||
| .listReferences(jsonTransformationDTO, refType) | ||
| .flatMap(gitRefs -> { | ||
| long branchMatchCount = gitRefs.stream() | ||
| .filter(gitRef -> gitRef.equals(finalRefName)) | ||
| .count(); | ||
|
|
||
| if (branchMatchCount == 0) { | ||
| return checkoutRemoteBranch(baseArtifact, finalRefName); | ||
| } | ||
|
|
||
| return Mono.error(new AppsmithException( | ||
| AppsmithError.GIT_ACTION_FAILED, | ||
| "checkout", | ||
| referenceToBeCheckedOut + " already exists in local - " + finalRefName)); | ||
| }); | ||
| } else { | ||
| // TODO refactor method to account for RefName as well | ||
| checkedOutArtifactMono = Mono.defer(() -> gitArtifactHelper | ||
| .getArtifactByBaseIdAndBranchName( | ||
| baseArtifactId, finalRefName, gitArtifactHelper.getArtifactReadPermission()) | ||
| .flatMap(artifact -> gitAnalyticsUtils.addAnalyticsForGitOperation( | ||
| AnalyticsEvents.GIT_CHECKOUT_BRANCH, | ||
| artifact, | ||
| artifact.getGitArtifactMetadata().getIsRepoPrivate()))); | ||
| } | ||
|
|
||
| return checkedOutArtifactMono | ||
| .doFinally(signalType -> gitRedisUtils.releaseFileLock(baseArtifactId, addFileLock)) | ||
| .tag(GitConstants.GitMetricConstants.CHECKOUT_REMOTE, FALSE.toString()) | ||
| .name(GitSpan.OPS_CHECKOUT_BRANCH) | ||
| .tap(Micrometer.observation(observationRegistry)) | ||
| .onErrorResume(Mono::error); | ||
| } | ||
|
|
||
| // TODO @Manish: add checkout Remote Branch | ||
| protected Mono<? extends Artifact> checkoutRemoteBranch(Artifact baseArtifact, String finalRefName) { | ||
| return null; | ||
| } | ||
|
Comment on lines
+439
to
+442
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implement the checkoutRemoteBranch method. The method is currently returning null which could lead to NullPointerException when checking out remote branches. Would you like me to help implement this method or create a GitHub issue to track this task? |
||
|
|
||
| /** | ||
| * Connect the artifact from Appsmith to a git repo | ||
| * This is the prerequisite step needed to perform all the git operation for an artifact | ||
|
|
@@ -929,6 +1035,137 @@ private boolean isBaseGitMetadataInvalid(GitArtifactMetadata gitArtifactMetadata | |
| .isGitAuthInvalid(gitArtifactMetadata.getGitAuth()); | ||
| } | ||
|
|
||
| private Mono<GitStatusDTO> getStatusAfterComparingWithRemote( | ||
| String baseArtifactId, boolean isFileLock, ArtifactType artifactType, GitType gitType) { | ||
| return getStatus(baseArtifactId, isFileLock, true, artifactType, gitType); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<GitStatusDTO> getStatus( | ||
| String branchedArtifactId, boolean compareRemote, ArtifactType artifactType, GitType gitType) { | ||
| return getStatus(branchedArtifactId, true, compareRemote, artifactType, gitType); | ||
| } | ||
|
|
||
| /** | ||
| * Get the status of the artifact for given branched id | ||
| * | ||
| * @param branchedArtifactId branched id of the artifact | ||
| * @param isFileLock if the locking is required, since the status API is used in the other flows of git | ||
| * Only for the direct hits from the client the locking will be added | ||
| * @param artifactType Type of artifact in context | ||
| * @param gitType Type of the service | ||
| * @return Map of json file names which are added, modified, conflicting, removed and the working tree if this is clean | ||
| */ | ||
| private Mono<GitStatusDTO> getStatus( | ||
| String branchedArtifactId, | ||
| boolean isFileLock, | ||
| boolean compareRemote, | ||
| ArtifactType artifactType, | ||
| GitType gitType) { | ||
|
|
||
| Mono<Tuple2<? extends Artifact, ? extends Artifact>> baseAndBranchedArtifacts = | ||
| getBaseAndBranchedArtifacts(branchedArtifactId, artifactType); | ||
|
|
||
| return baseAndBranchedArtifacts.flatMap(artifactTuple -> { | ||
| Artifact baseArtifact = artifactTuple.getT1(); | ||
| Artifact branchedArtifact = artifactTuple.getT2(); | ||
| return getStatus(baseArtifact, branchedArtifact, isFileLock, compareRemote, gitType); | ||
| }); | ||
| } | ||
|
|
||
| protected Mono<GitStatusDTO> getStatus( | ||
| Artifact baseArtifact, | ||
| Artifact branchedArtifact, | ||
| boolean isFileLock, | ||
| boolean compareRemote, | ||
| GitType gitType) { | ||
|
|
||
| ArtifactType artifactType = baseArtifact.getArtifactType(); | ||
| GitArtifactHelper<?> gitArtifactHelper = gitArtifactHelperResolver.getArtifactHelper(artifactType); | ||
| GitHandlingService gitHandlingService = gitHandlingServiceResolver.getGitHandlingService(gitType); | ||
|
|
||
| GitArtifactMetadata baseGitMetadata = baseArtifact.getGitArtifactMetadata(); | ||
| final String baseArtifactId = baseGitMetadata.getDefaultArtifactId(); | ||
|
|
||
| GitArtifactMetadata branchedGitMetadata = branchedArtifact.getGitArtifactMetadata(); | ||
| branchedGitMetadata.setGitAuth(baseGitMetadata.getGitAuth()); | ||
|
|
||
| final String finalBranchName = branchedGitMetadata.getBranchName(); | ||
|
|
||
| if (!StringUtils.hasText(finalBranchName)) { | ||
| return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.BRANCH_NAME)); | ||
| } | ||
|
|
||
| Mono<? extends ArtifactExchangeJson> exportedArtifactJsonMono = | ||
| exportService.exportByArtifactId(branchedArtifact.getId(), VERSION_CONTROL, artifactType); | ||
|
|
||
| Mono<GitStatusDTO> statusMono = exportedArtifactJsonMono | ||
| .flatMap(artifactExchangeJson -> { | ||
| return gitRedisUtils | ||
| .acquireGitLock(baseArtifactId, GitConstants.GitCommandConstants.STATUS, isFileLock) | ||
| .thenReturn(artifactExchangeJson); | ||
| }) | ||
| .flatMap(artifactExchangeJson -> { | ||
| ArtifactJsonTransformationDTO jsonTransformationDTO = new ArtifactJsonTransformationDTO(); | ||
| jsonTransformationDTO.setRefType(RefType.BRANCH); | ||
| jsonTransformationDTO.setWorkspaceId(baseArtifact.getWorkspaceId()); | ||
| jsonTransformationDTO.setBaseArtifactId(baseArtifact.getId()); | ||
| jsonTransformationDTO.setRepoName( | ||
| branchedArtifact.getGitArtifactMetadata().getRepoName()); | ||
| jsonTransformationDTO.setArtifactType(artifactExchangeJson.getArtifactJsonType()); | ||
| jsonTransformationDTO.setRefName(finalBranchName); | ||
|
|
||
| Mono<Boolean> prepareForStatus = | ||
| gitHandlingService.prepareChangesToBeCommitted(jsonTransformationDTO, artifactExchangeJson); | ||
| Mono<String> fetchRemoteMono; | ||
|
|
||
| if (compareRemote) { | ||
| fetchRemoteMono = Mono.defer( | ||
| () -> fetchRemoteChanges(baseArtifact, branchedArtifact, FALSE, gitType, RefType.BRANCH) | ||
| .onErrorResume(error -> Mono.error(new AppsmithException( | ||
| AppsmithError.GIT_GENERIC_ERROR, error.getMessage())))); | ||
| } else { | ||
| fetchRemoteMono = Mono.just("ignored"); | ||
| } | ||
|
|
||
| return Mono.zip(prepareForStatus, fetchRemoteMono) | ||
| .then(gitHandlingService.getStatus(jsonTransformationDTO)); | ||
| }) | ||
| .doFinally(signalType -> gitRedisUtils.releaseFileLock(baseArtifactId, isFileLock)) | ||
| .onErrorResume(throwable -> { | ||
| /* | ||
| in case of any error, the global exception handler will release the lock | ||
| hence we don't need to do that manually | ||
| */ | ||
| log.error( | ||
| "Error to get status for application: {}, branch: {}", | ||
| baseArtifactId, | ||
| finalBranchName, | ||
| throwable); | ||
| return Mono.error(new AppsmithException(AppsmithError.GIT_GENERIC_ERROR, throwable.getMessage())); | ||
| }); | ||
|
|
||
| return Mono.zip(statusMono, sessionUserService.getCurrentUser()) | ||
| .elapsed() | ||
| .flatMap(objects -> { | ||
| Long elapsedTime = objects.getT1(); | ||
| GitStatusDTO gitStatusDTO = objects.getT2().getT1(); | ||
| User currentUser = objects.getT2().getT2(); | ||
| String flowName; | ||
| if (compareRemote) { | ||
| flowName = AnalyticsEvents.GIT_STATUS.getEventName(); | ||
| } else { | ||
| flowName = AnalyticsEvents.GIT_STATUS_WITHOUT_FETCH.getEventName(); | ||
| } | ||
|
|
||
| return gitAnalyticsUtils | ||
| .sendUnitExecutionTimeAnalyticsEvent(flowName, elapsedTime, currentUser, branchedArtifact) | ||
| .thenReturn(gitStatusDTO); | ||
| }) | ||
| .name(OPS_STATUS) | ||
| .tap(Micrometer.observation(observationRegistry)); | ||
| } | ||
|
|
||
| public Mono<String> fetchRemoteChanges( | ||
| Artifact baseArtifact, Artifact refArtifact, boolean isFileLock, GitType gitType, RefType refType) { | ||
|
|
||
|
|
@@ -967,7 +1204,9 @@ public Mono<String> fetchRemoteChanges( | |
| .then(Mono.defer(() -> | ||
| gitHandlingService.fetchRemoteChanges(jsonTransformationDTO, baseArtifactGitData.getGitAuth()))) | ||
| .flatMap(fetchedRemoteStatusString -> { | ||
| return gitRedisUtils.releaseFileLock(baseArtifactId).thenReturn(fetchedRemoteStatusString); | ||
| return gitRedisUtils | ||
| .releaseFileLock(baseArtifactId, isFileLock) | ||
| .thenReturn(fetchedRemoteStatusString); | ||
| }) | ||
| .onErrorResume(throwable -> { | ||
| /* | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, should we not be able to make a DTO out of these params?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will you be replacing usage in a separate PR?