Search in sources :

Example 1 with ProjectStructure

use of org.finos.legend.sdlc.server.project.ProjectStructure in project legend-sdlc by finos.

the class MavenProjectStructure method projectDependencyToMavenDependencies.

public static Stream<Dependency> projectDependencyToMavenDependencies(ProjectDependency projectDependency, BiFunction<String, VersionId, ProjectFileAccessProvider.FileAccessContext> versionFileAccessContextProvider, Collection<? extends ArtifactType> artifactTypes, boolean setVersion) {
    String versionString = setVersion ? projectDependency.getVersionId().toVersionIdString() : null;
    if (!ProjectDependency.isLegacyProjectDependency(projectDependency)) {
        Pair<String, String> mavenCoordinates = getGroupAndArtifactIdFromProjectDependency(projectDependency);
        Collection<? extends ArtifactType> resolvedArtifactTypes = ((artifactTypes == null) || artifactTypes.isEmpty()) ? DEFAULT_ARTIFACT_TYPES.castToCollection() : artifactTypes;
        return resolvedArtifactTypes.stream().map(artifactType -> newMavenDependency(mavenCoordinates.getOne(), mavenCoordinates.getTwo() + "-" + artifactType.name().replace('_', '-').toLowerCase(), versionString));
    }
    ProjectStructure versionStructure = getProjectStructureForProjectDependency(projectDependency, versionFileAccessContextProvider);
    ProjectConfiguration versionConfig = versionStructure.getProjectConfiguration();
    String groupId = versionConfig.getGroupId();
    Stream<String> stream = ((artifactTypes == null) || artifactTypes.isEmpty()) ? versionStructure.getAllArtifactIds() : versionStructure.getArtifactIds(artifactTypes);
    return stream.map(aid -> newMavenDependency(groupId, aid, versionString));
}
Also used : ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)

Example 2 with ProjectStructure

use of org.finos.legend.sdlc.server.project.ProjectStructure in project legend-sdlc by finos.

the class GitLabProjectApi method importProject.

@Override
public ImportReport importProject(String id, ProjectType type, String groupId, String artifactId) {
    LegendSDLCServerException.validateNonNull(id, "id may not be null");
    LegendSDLCServerException.validateNonNull(type, "type may not be null");
    LegendSDLCServerException.validate(groupId, ProjectStructure::isValidGroupId, g -> "Invalid groupId: " + g);
    LegendSDLCServerException.validate(artifactId, ProjectStructure::isValidArtifactId, a -> "Invalid artifactId: " + a);
    // Get project id
    GitLabProjectId projectId;
    if (id.chars().allMatch(Character::isDigit)) {
        projectId = GitLabProjectId.newProjectId(getGitLabModeFromProjectType(type), Integer.parseInt(id));
    } else {
        projectId = parseProjectId(id);
        if (projectId.getGitLabMode() != getGitLabModeFromProjectType(type)) {
            throw new LegendSDLCServerException("Invalid project id \"" + id + "\" for project type " + type, Status.BAD_REQUEST);
        }
    }
    // Find project
    GitLabApi gitLabApi = getGitLabApi(projectId.getGitLabMode());
    org.gitlab4j.api.ProjectApi gitLabProjectApi = gitLabApi.getProjectApi();
    org.gitlab4j.api.models.Project currentProject;
    try {
        currentProject = withRetries(() -> gitLabProjectApi.getProject(projectId.getGitLabId()));
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to access project " + id + " of type " + type);
    }
    // Create a workspace for project configuration
    RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
    String workspaceId = "ProjectConfiguration_" + getRandomIdString();
    Branch workspaceBranch;
    try {
        workspaceBranch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, projectId.getGitLabId(), getWorkspaceBranchName(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE), MASTER_BRANCH, 30, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create a workspace for initial configuration of project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to create workspace for initial configuration of project " + id + " of type " + type);
    }
    if (workspaceBranch == null) {
        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(), null, null, projectFileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
        ProjectConfigurationUpdateBuilder builder = ProjectConfigurationUpdateBuilder.newBuilder(projectFileAccessProvider, type, projectId.toString()).withWorkspace(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withGroupId(groupId).withArtifactId(artifactId).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions);
        int defaultProjectStructureVersion = getDefaultProjectStructureVersion();
        if (currentConfig == null) {
            // No current project structure: build a new one
            configRevision = builder.withProjectStructureVersion(defaultProjectStructureVersion).withMessage("Build project structure").buildProjectStructure();
        } else {
            // Existing project structure: update
            if (currentConfig.getProjectType() != type) {
                throw new LegendSDLCServerException("Mismatch between requested project type (" + type + ") and found project type (" + currentConfig.getProjectType() + ")", Status.BAD_REQUEST);
            }
            ProjectStructureVersion currentVersion = currentConfig.getProjectStructureVersion();
            if ((currentVersion == null) || (currentVersion.getVersion() < defaultProjectStructureVersion)) {
                builder.withProjectStructureVersion(defaultProjectStructureVersion).withProjectStructureExtensionVersion(null);
            }
            configRevision = builder.withMessage("Update project structure").updateProjectConfiguration();
        }
    } catch (Exception e) {
        // Try to delete the branch in case of exception
        deleteWorkspace(projectId, repositoryApi, workspaceId);
        throw e;
    }
    // Submit workspace changes, if any, for review
    String reviewId;
    if (configRevision == null) {
        // No changes: nothing to submit
        reviewId = null;
        // Try to delete the branch
        deleteWorkspace(projectId, repositoryApi, workspaceId);
    } else {
        MergeRequest mergeRequest;
        try {
            mergeRequest = gitLabApi.getMergeRequestApi().createMergeRequest(projectId.getGitLabId(), getWorkspaceBranchName(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE), MASTER_BRANCH, "Project structure", "Set up project structure", null, null, null, null, true, false);
        } catch (Exception e) {
            // Try to delete the branch in case of exception
            deleteWorkspace(projectId, repositoryApi, workspaceId);
            throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to submit project configuration changes create a workspace for initial configuration of project " + id + " of type " + type, () -> "Could not find workspace " + workspaceId + " project " + id + " of type " + type, () -> "Failed to create a review for configuration of project " + id + " of type " + type);
        }
        reviewId = toStringIfNotNull(mergeRequest.getIid());
    }
    // Add tags
    Project finalProject;
    List<String> currentTags = currentProject.getTagList();
    if ((currentTags != null) && currentTags.stream().anyMatch(this::isLegendSDLCProjectTag)) {
        // already has the necessary tag
        finalProject = fromGitLabProject(currentProject, projectId.getGitLabMode());
    } else {
        List<String> updatedTags = Lists.mutable.ofInitialCapacity((currentTags == null) ? 1 : (currentTags.size() + 1));
        if (currentTags != null) {
            updatedTags.addAll(currentTags);
        }
        updatedTags.add(getLegendSDLCProjectTag());
        org.gitlab4j.api.models.Project updatedProject;
        try {
            updatedProject = gitLabProjectApi.updateProject(new org.gitlab4j.api.models.Project().withId(currentProject.getId()).withTagList(updatedTags));
        } catch (Exception e) {
            // Try to delete the branch in case of exception
            deleteWorkspace(projectId, repositoryApi, workspaceId);
            throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to import project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to import project " + id + " of type " + type);
        }
        finalProject = fromGitLabProject(updatedProject, projectId.getGitLabMode());
    }
    return new ImportReport() {

        @Override
        public Project getProject() {
            return finalProject;
        }

        @Override
        public String getReviewId() {
            return reviewId;
        }
    };
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) ProjectStructureVersion(org.finos.legend.sdlc.domain.model.project.configuration.ProjectStructureVersion) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Branch(org.gitlab4j.api.models.Branch) RepositoryApi(org.gitlab4j.api.RepositoryApi) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) Project(org.finos.legend.sdlc.domain.model.project.Project) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) ProjectConfigurationUpdateBuilder(org.finos.legend.sdlc.server.project.ProjectConfigurationUpdateBuilder) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)

Example 3 with ProjectStructure

use of org.finos.legend.sdlc.server.project.ProjectStructure in project legend-sdlc by finos.

the class GitLabRevisionApi method getProjectPackageRevisionContext.

@Override
public RevisionAccessContext getProjectPackageRevisionContext(String projectId, String packagePath) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(packagePath, "packagePath may not be null");
    if (!isValidPackagePath(packagePath)) {
        throw new LegendSDLCServerException("Invalid package path: " + packagePath, Status.BAD_REQUEST);
    }
    ProjectStructure projectStructure = getProjectStructure(projectId, null, null, null, null);
    MutableList<String> directories = Iterate.collectWith(projectStructure.getEntitySourceDirectories(), ProjectStructure.EntitySourceDirectory::packagePathToFilePath, packagePath, Lists.mutable.empty());
    MutableList<String> canonicalizedAndReducedDirectories = ProjectPaths.canonicalizeAndReduceDirectories(directories);
    return new ProjectFileRevisionAccessContextWrapper(getProjectFileAccessProvider().getRevisionAccessContext(projectId, null, null, null, canonicalizedAndReducedDirectories), new PackageablePathExceptionProcessor(packagePath, canonicalizedAndReducedDirectories));
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure)

Example 4 with ProjectStructure

use of org.finos.legend.sdlc.server.project.ProjectStructure in project legend-sdlc by finos.

the class GitLabRevisionApi method getWorkspacePackageRevisionContextByWorkspaceAccessType.

private RevisionAccessContext getWorkspacePackageRevisionContextByWorkspaceAccessType(String projectId, String workspaceId, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType, String packagePath) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceType, "workspaceType may not be null");
    LegendSDLCServerException.validateNonNull(workspaceAccessType, "workspaceAccessType may not be null");
    LegendSDLCServerException.validateNonNull(packagePath, "packagePath may not be null");
    if (!isValidPackagePath(packagePath)) {
        throw new LegendSDLCServerException("Invalid package path: " + packagePath, Status.BAD_REQUEST);
    }
    ProjectStructure projectStructure = getProjectStructure(projectId, workspaceId, null, workspaceType, workspaceAccessType);
    MutableList<String> directories = Iterate.collectWith(projectStructure.getEntitySourceDirectories(), ProjectStructure.EntitySourceDirectory::packagePathToFilePath, packagePath, Lists.mutable.empty());
    MutableList<String> canonicalizedAndReducedDirectories = ProjectPaths.canonicalizeAndReduceDirectories(directories);
    return new ProjectFileRevisionAccessContextWrapper(getProjectFileAccessProvider().getRevisionAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType, canonicalizedAndReducedDirectories), new PackageablePathExceptionProcessor(packagePath, canonicalizedAndReducedDirectories));
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure)

Example 5 with ProjectStructure

use of org.finos.legend.sdlc.server.project.ProjectStructure in project legend-sdlc by finos.

the class GitLabRevisionApi method getWorkspaceEntityRevisionContextByWorkspaceAccessType.

private RevisionAccessContext getWorkspaceEntityRevisionContextByWorkspaceAccessType(String projectId, String workspaceId, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType, String entityPath) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceType, "workspaceType may not be null");
    LegendSDLCServerException.validateNonNull(workspaceAccessType, "workspaceAccessType may not be null");
    LegendSDLCServerException.validateNonNull(entityPath, "entityPath may not be null");
    if (!isValidEntityPath(entityPath)) {
        throw new LegendSDLCServerException("Invalid entity path: " + entityPath, Status.BAD_REQUEST);
    }
    ProjectFileAccessProvider fileAccessProvider = getProjectFileAccessProvider();
    ProjectFileAccessProvider.FileAccessContext fileAccessContext = fileAccessProvider.getFileAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType, null);
    ProjectStructure projectStructure = ProjectStructure.getProjectStructure(fileAccessContext);
    String filePath = projectStructure.findEntityFile(entityPath, fileAccessContext);
    if (filePath == null) {
        throw new LegendSDLCServerException("Cannot find entity \"" + entityPath + "\" in " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId, Status.NOT_FOUND);
    }
    String canonicalizedFilePath = ProjectPaths.canonicalizeFile(filePath);
    return new ProjectFileRevisionAccessContextWrapper(fileAccessProvider.getRevisionAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType, canonicalizedFilePath), new PackageablePathExceptionProcessor(entityPath, canonicalizedFilePath));
}
Also used : ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure)

Aggregations

ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)14 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)10 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)7 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)5 RepositoryApi (org.gitlab4j.api.RepositoryApi)5 MergeRequest (org.gitlab4j.api.models.MergeRequest)3 Project (org.finos.legend.sdlc.domain.model.project.Project)2 ProjectConfiguration (org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)2 GitLabApi (org.gitlab4j.api.GitLabApi)2 DiffRef (org.gitlab4j.api.models.DiffRef)2 Objects (java.util.Objects)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Comparison (org.finos.legend.sdlc.domain.model.comparison.Comparison)1 EntityDiff (org.finos.legend.sdlc.domain.model.comparison.EntityDiff)1 EntityChangeType (org.finos.legend.sdlc.domain.model.entity.change.EntityChangeType)1 ProjectStructureVersion (org.finos.legend.sdlc.domain.model.project.configuration.ProjectStructureVersion)1 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)1 GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)1 ProjectConfigurationUpdateBuilder (org.finos.legend.sdlc.server.project.ProjectConfigurationUpdateBuilder)1 ProjectFileOperation (org.finos.legend.sdlc.server.project.ProjectFileOperation)1