Skip to content

Commit

Permalink
Studio Sandbox Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mrudula-gs committed Nov 21, 2023
1 parent 467c620 commit 886bbda
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import org.finos.legend.sdlc.domain.model.project.accessRole.AccessRole;
import org.finos.legend.sdlc.domain.model.project.accessRole.AuthorizableProjectAction;
import org.finos.legend.sdlc.domain.model.project.accessRole.UserPermission;
import org.finos.legend.sdlc.domain.model.revision.Revision;
import org.finos.legend.sdlc.server.api.entity.FileSystemApiWithFileAccess;
import org.finos.legend.sdlc.server.domain.api.project.ProjectApi;
import org.finos.legend.sdlc.server.domain.api.project.ProjectConfigurationUpdater;
import org.finos.legend.sdlc.server.domain.api.workspace.WorkspaceSpecification;
import org.finos.legend.sdlc.server.error.LegendSDLCServerException;
import org.finos.legend.sdlc.server.exception.FSException;
import org.finos.legend.sdlc.server.gitlab.GitLabProjectId;
import org.finos.legend.sdlc.server.project.ProjectStructure;
import org.finos.legend.sdlc.server.project.ProjectStructurePlatformExtensions;
import org.finos.legend.sdlc.server.project.config.ProjectCreationConfiguration;
Expand Down Expand Up @@ -227,6 +230,12 @@ public void deleteProject(String id)
throw FSException.unavailableFeature();
}

@Override
public Revision configureProjectInWorkspace(GitLabProjectId projectId, ProjectType type, String groupId, String artifactId, WorkspaceSpecification workspaceSpec)
{
throw FSException.unavailableFeature();
}

@Override
public void changeProjectName(String id, String newName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import org.finos.legend.sdlc.domain.model.project.accessRole.AccessRole;
import org.finos.legend.sdlc.domain.model.project.accessRole.AuthorizableProjectAction;
import org.finos.legend.sdlc.domain.model.project.accessRole.UserPermission;
import org.finos.legend.sdlc.domain.model.revision.Revision;
import org.finos.legend.sdlc.server.domain.api.workspace.WorkspaceSpecification;
import org.finos.legend.sdlc.server.gitlab.GitLabProjectId;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -46,6 +49,8 @@ default List<Project> getProjects(boolean user, String search, Iterable<String>

void deleteProject(String id);

Revision configureProjectInWorkspace(GitLabProjectId projectId, ProjectType type, String groupId, String artifactId, WorkspaceSpecification workspaceSpec);

void changeProjectName(String id, String newName);

void changeProjectDescription(String id, String newDescription);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Project getProject(String id)
}

@Override
public List<Project> getProjects(boolean user, String search, Iterable<String> tags, Integer limit)
public List<Project> getProjects(boolean user, String searchString, Iterable<String> tags, Integer limit)
{
boolean limited = false;
if (limit != null)
Expand All @@ -124,9 +124,10 @@ public List<Project> getProjects(boolean user, String search, Iterable<String> t
}
limited = true;
}
Set<String> tagSet = (tags == null) ? Collections.emptySet() : toLegendSDLCTagSet(tags);
String search = (tagSet.contains("legend_sandbox") && searchString == null) ? getCurrentUser() : searchString;
try
{
Set<String> tagSet = (tags == null) ? Collections.emptySet() : toLegendSDLCTagSet(tags);
Pager<org.gitlab4j.api.models.Project> pager = withRetries(() -> getGitLabApi().getProjectApi().getProjects(null, null, null, null, search, true, null, user, null, null, ITEMS_PER_PAGE));
Stream<org.gitlab4j.api.models.Project> stream = PagerTools.stream(pager).filter(this::isLegendSDLCProject);
if (!tagSet.isEmpty())
Expand Down Expand Up @@ -394,51 +395,7 @@ public ImportReport importProject(String id, ProjectType type, String groupId, S
throw new LegendSDLCServerException("Failed to create workspace " + workspaceId + " in project " + projectId);
}

// Configure project in workspace
ProjectFileAccessProvider projectFileAccessProvider = getProjectFileAccessProvider();
Revision configRevision;
try
{
ProjectConfiguration currentConfig = ProjectStructure.getProjectConfiguration(projectId.toString(), SourceSpecification.projectSourceSpecification(), null, projectFileAccessProvider);
ProjectConfigurationUpdater configUpdater = ProjectConfigurationUpdater.newUpdater()
.withProjectType(type)
.withGroupId(groupId)
.withArtifactId(artifactId);
ProjectStructure.UpdateBuilder builder = ProjectStructure.newUpdateBuilder(projectFileAccessProvider, projectId.toString(), configUpdater)
.withWorkspace(workspaceSpec)
.withProjectStructureExtensionProvider(this.projectStructureExtensionProvider)
.withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions);
int defaultProjectStructureVersion = getDefaultProjectStructureVersion();
if (currentConfig == null)
{
// No current project structure: build a new one
configUpdater.setProjectStructureVersion(defaultProjectStructureVersion);
if (this.projectStructureExtensionProvider != null && type != ProjectType.EMBEDDED)
{
configUpdater.setProjectStructureExtensionVersion(this.projectStructureExtensionProvider.getLatestVersionForProjectStructureVersion(defaultProjectStructureVersion));
}
configRevision = builder.withMessage("Build project structure").build();
}
else
{
ProjectStructureVersion currentVersion = currentConfig.getProjectStructureVersion();
if ((currentVersion == null) || (currentVersion.getVersion() < defaultProjectStructureVersion))
{
configUpdater.setProjectStructureVersion(defaultProjectStructureVersion);
if (this.projectStructureExtensionProvider != null && (type != null ? type : currentConfig.getProjectType()) != ProjectType.EMBEDDED)
{
configUpdater.setProjectStructureExtensionVersion(this.projectStructureExtensionProvider.getLatestVersionForProjectStructureVersion(defaultProjectStructureVersion));
}
}
configRevision = builder.withMessage("Update project structure").update();
}
}
catch (Exception e)
{
// Try to delete the branch in case of exception
deleteWorkspace(projectId, repositoryApi, workspaceSpec);
throw e;
}
Revision configRevision = configureProjectInWorkspace(projectId, type, groupId, artifactId, workspaceSpec);

// Submit workspace changes, if any, for review
String reviewId;
Expand Down Expand Up @@ -517,6 +474,57 @@ public String getReviewId()
};
}

public Revision configureProjectInWorkspace(GitLabProjectId projectId, ProjectType type, String groupId, String artifactId, WorkspaceSpecification workspaceSpec)
{
ProjectFileAccessProvider projectFileAccessProvider = getProjectFileAccessProvider();
Revision configRevision;
try
{
ProjectConfiguration currentConfig = ProjectStructure.getProjectConfiguration(projectId.toString(), SourceSpecification.projectSourceSpecification(), null, projectFileAccessProvider);
ProjectConfigurationUpdater configUpdater = ProjectConfigurationUpdater.newUpdater()
.withProjectType(type)
.withGroupId(groupId)
.withArtifactId(artifactId);
ProjectStructure.UpdateBuilder builder = ProjectStructure.newUpdateBuilder(projectFileAccessProvider, projectId.toString(), configUpdater)
.withWorkspace(workspaceSpec)
.withProjectStructureExtensionProvider(this.projectStructureExtensionProvider)
.withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions);
int defaultProjectStructureVersion = getDefaultProjectStructureVersion();
if (currentConfig == null)
{
// No current project structure: build a new one
configUpdater.setProjectStructureVersion(defaultProjectStructureVersion);
if (this.projectStructureExtensionProvider != null && type != ProjectType.EMBEDDED)
{
configUpdater.setProjectStructureExtensionVersion(this.projectStructureExtensionProvider.getLatestVersionForProjectStructureVersion(defaultProjectStructureVersion));
}
configRevision = builder.withMessage("Build project structure").build();
}
else
{
ProjectStructureVersion currentVersion = currentConfig.getProjectStructureVersion();
if ((currentVersion == null) || (currentVersion.getVersion() < defaultProjectStructureVersion))
{
configUpdater.setProjectStructureVersion(defaultProjectStructureVersion);
if (this.projectStructureExtensionProvider != null && (type != null ? type : currentConfig.getProjectType()) != ProjectType.EMBEDDED)
{
configUpdater.setProjectStructureExtensionVersion(this.projectStructureExtensionProvider.getLatestVersionForProjectStructureVersion(defaultProjectStructureVersion));
}
}
configRevision = builder.withMessage("Update project structure").update();
}
return configRevision;
}
catch (Exception e)
{
// Try to delete the branch in case of exception
GitLabApi gitLabApi = getGitLabApi();
RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
deleteWorkspace(projectId, repositoryApi, workspaceSpec);
throw e;
}
}

private void deleteWorkspace(GitLabProjectId projectId, RepositoryApi repositoryApi, WorkspaceSpecification workspaceSpec)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package org.finos.legend.sdlc.server.gitlab.api;

import org.eclipse.collections.api.factory.Lists;
import org.finos.legend.sdlc.domain.model.project.ProjectType;
import org.finos.legend.sdlc.domain.model.project.workspace.Workspace;
import org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType;
import org.finos.legend.sdlc.domain.model.revision.Revision;
import org.finos.legend.sdlc.server.domain.api.project.ProjectApi;
import org.finos.legend.sdlc.server.domain.api.revision.RevisionApi;
import org.finos.legend.sdlc.server.domain.api.workspace.PatchWorkspaceSource;
import org.finos.legend.sdlc.server.domain.api.workspace.WorkspaceApi;
Expand Down Expand Up @@ -69,12 +71,17 @@ public class GitLabWorkspaceApi extends GitLabApiWithFileAccess implements Works
{
private static final Logger LOGGER = LoggerFactory.getLogger(GitLabWorkspaceApi.class);

private final ProjectApi projectApi;
private final RevisionApi revisionApi;
private final String sandboxArtifactId = "my-prototype";
private final String sandboxGroupId = "com.gs.alloy.sandbox";


@Inject
public GitLabWorkspaceApi(GitLabConfiguration gitLabConfiguration, GitLabUserContext userContext, RevisionApi revisionApi, BackgroundTaskProcessor backgroundTaskProcessor)
public GitLabWorkspaceApi(GitLabConfiguration gitLabConfiguration, GitLabUserContext userContext, ProjectApi projectApi, RevisionApi revisionApi, BackgroundTaskProcessor backgroundTaskProcessor)
{
super(gitLabConfiguration, userContext, backgroundTaskProcessor);
this.projectApi = projectApi;
this.revisionApi = revisionApi;
}

Expand Down Expand Up @@ -393,9 +400,11 @@ protected void accept(PatchWorkspaceSource source)
String workspaceBranchName = getWorkspaceBranchName(workspaceSpecification);
String sourceBranchName = getSourceBranch(gitLabProjectId, workspaceSpecification);
Branch branch;
List<String> tags;
try
{
branch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), workspaceBranchName, sourceBranchName, 30, 1_000);
tags = getGitLabApi().getProjectApi().getProject(gitLabProjectId.getGitLabId()).getTagList();
}
catch (Exception e)
{
Expand All @@ -408,6 +417,10 @@ protected void accept(PatchWorkspaceSource source)
{
throw new LegendSDLCServerException("Failed to create " + getReferenceInfo(projectId, workspaceSpecification));
}
if (tags.contains("legend_sandbox") && tags.contains("legend"))
{
this.projectApi.configureProjectInWorkspace(gitLabProjectId, ProjectType.MANAGED, sandboxGroupId, sandboxArtifactId, workspaceSpecification);
}
return fromWorkspaceBranchName(projectId, branch.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static void setUpComparisonApi()

GitLabProjectApi gitLabProjectApi = new GitLabProjectApi(gitLabConfig, gitLabOwnerUserContext, projectStructureConfig, null, backgroundTaskProcessor, null);
GitLabRevisionApi gitLabRevisionApi = new GitLabRevisionApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabRevisionApi, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabProjectApi, gitLabRevisionApi, backgroundTaskProcessor);
GitLabEntityApi gitLabEntityApi = new GitLabEntityApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabComparisonApi gitLabComparisonApi = new GitLabComparisonApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabPatchApi gitLabPatchApi = new GitLabPatchApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static void setUpEntityApi()

GitLabProjectApi gitLabProjectApi = new GitLabProjectApi(gitLabConfig, gitLabOwnerUserContext, projectStructureConfig, null, backgroundTaskProcessor, null);
GitLabRevisionApi gitLabRevisionApi = new GitLabRevisionApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabRevisionApi, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabProjectApi, gitLabRevisionApi, backgroundTaskProcessor);
GitLabEntityApi gitLabEntityApi = new GitLabEntityApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabReviewApi gitLabCommitterReviewApi = new GitLabReviewApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabReviewApi gitLabApproverReviewApi = new GitLabReviewApi(gitLabConfig, gitLabOwnerUserContext, backgroundTaskProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static void setUpProjectConfigurationApi()

GitLabProjectApi gitLabProjectApi = new GitLabProjectApi(gitLabConfig, gitLabOwnerUserContext, projectStructureConfig, null, backgroundTaskProcessor, null);
GitLabRevisionApi gitLabRevisionApi = new GitLabRevisionApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabRevisionApi, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabProjectApi, gitLabRevisionApi, backgroundTaskProcessor);
GitLabProjectConfigurationApi gitLabProjectConfigurationApi = new GitLabProjectConfigurationApi(gitLabConfig, gitLabMemberUserContext, projectStructureConfig.getProjectStructureExtensionProvider(), backgroundTaskProcessor, null);
GitLabPatchApi gitLabPatchApi = new GitLabPatchApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabVersionApi gitLabVersionApi = new GitLabVersionApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static void setUpRevisionApi()

GitLabProjectApi gitLabProjectApi = new GitLabProjectApi(gitLabConfig, gitLabOwnerUserContext, projectStructureConfig, null, backgroundTaskProcessor, null);
GitLabRevisionApi gitLabRevisionApi = new GitLabRevisionApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabRevisionApi, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabProjectApi, gitLabRevisionApi, backgroundTaskProcessor);
GitLabEntityApi gitLabEntityApi = new GitLabEntityApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabPatchApi gitLabPatchApi = new GitLabPatchApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabVersionApi gitLabVersionApi = new GitLabVersionApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static void setUpWorkspaceApi()

GitLabProjectApi gitLabProjectApi = new GitLabProjectApi(gitLabConfig, gitLabOwnerUserContext, projectStructureConfig, null, backgroundTaskProcessor, null);
GitLabRevisionApi gitLabRevisionApi = new GitLabRevisionApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabRevisionApi, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabProjectApi, gitLabRevisionApi, backgroundTaskProcessor);
GitLabEntityApi gitLabEntityApi = new GitLabEntityApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabReviewApi gitLabCommitterReviewApi = new GitLabReviewApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabReviewApi gitLabApproverReviewApi = new GitLabReviewApi(gitLabConfig, gitLabOwnerUserContext, backgroundTaskProcessor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static void setUpComparisonApi()

GitLabProjectApi gitLabProjectApi = new GitLabProjectApi(gitLabConfig, gitLabOwnerUserContext, projectStructureConfig, null, backgroundTaskProcessor, null);
GitLabRevisionApi gitLabRevisionApi = new GitLabRevisionApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabRevisionApi, backgroundTaskProcessor);
GitLabWorkspaceApi gitLabWorkspaceApi = new GitLabWorkspaceApi(gitLabConfig, gitLabMemberUserContext, gitLabProjectApi, gitLabRevisionApi, backgroundTaskProcessor);
GitLabEntityApi gitLabEntityApi = new GitLabEntityApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabComparisonApi gitLabComparisonApi = new GitLabComparisonApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
GitLabPatchApi gitLabPatchApi = new GitLabPatchApi(gitLabConfig, gitLabMemberUserContext, backgroundTaskProcessor);
Expand Down
Loading

0 comments on commit 886bbda

Please sign in to comment.