Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -522,6 +522,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 +534,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,8 +565,12 @@ 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));
});
Expand All @@ -572,6 +588,21 @@ public Mono<? extends Artifact> createReference(
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
Copy Markdown
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
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,7 @@ 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.appsmith.external.constants.AnalyticsEvents;
import com.appsmith.external.dtos.GitBranchDTO;
import com.appsmith.external.dtos.GitRefDTO;
import com.appsmith.external.dtos.GitStatusDTO;
import com.appsmith.external.git.constants.GitConstants;
import com.appsmith.external.git.constants.GitSpan;
Expand Down Expand Up @@ -662,7 +663,7 @@ public Mono<GitStatusDTO> getStatus(ArtifactJsonTransformationDTO jsonTransforma
}

@Override
public Mono<String> createGitReference(ArtifactJsonTransformationDTO jsonTransformationDTO) {
public Mono<String> createGitReference(ArtifactJsonTransformationDTO jsonTransformationDTO, GitRefDTO gitRefDTO) {
GitArtifactHelper<?> gitArtifactHelper =
gitArtifactHelperResolver.getArtifactHelper(jsonTransformationDTO.getArtifactType());

Expand All @@ -671,7 +672,7 @@ public Mono<String> createGitReference(ArtifactJsonTransformationDTO jsonTransfo
jsonTransformationDTO.getBaseArtifactId(),
jsonTransformationDTO.getRepoName());

return fsGitHandler.createAndCheckoutToBranch(repoSuffix, jsonTransformationDTO.getRefName());
return fsGitHandler.createAndCheckoutReference(repoSuffix, gitRefDTO);
}

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

import com.appsmith.external.git.constants.ce.RefType;
import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.domains.Artifact;
import com.appsmith.server.dtos.ArtifactExchangeJson;
Expand Down Expand Up @@ -68,4 +69,6 @@ public interface GitArtifactHelperCE<T extends Artifact> {
T getNewArtifact(String workspaceId, String repoName);

Mono<T> publishArtifactPostCommit(Artifact committedArtifact);

Mono<T> publishArtifactPostRefCreation(Artifact artifact, RefType refType, Boolean isPublishedManually);
}