Search in sources :

Example 6 with ProjectType

use of org.finos.legend.sdlc.domain.model.project.ProjectType in project legend-sdlc by finos.

the class ProjectStructure method updateProjectConfiguration.

private static Revision updateProjectConfiguration(ProjectConfigurationUpdateBuilder updateBuilder, boolean requireRevisionId) {
    ProjectFileAccessProvider projectFileAccessProvider = CachingProjectFileAccessProvider.wrap(updateBuilder.getProjectFileAccessProvider());
    ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = updateBuilder.getWorkspaceAccessType();
    WorkspaceType workspaceType = updateBuilder.getWorkspaceType();
    if (updateBuilder.hasGroupId() && !isValidGroupId(updateBuilder.getGroupId())) {
        throw new LegendSDLCServerException("Invalid groupId: " + updateBuilder.getGroupId(), Status.BAD_REQUEST);
    }
    if (updateBuilder.hasArtifactId() && !isValidArtifactId(updateBuilder.getArtifactId())) {
        throw new LegendSDLCServerException("Invalid artifactId: " + updateBuilder.getArtifactId(), Status.BAD_REQUEST);
    }
    ProjectType projectType = updateBuilder.getProjectType();
    String projectId = updateBuilder.getProjectId();
    String workspaceId = updateBuilder.getWorkspaceId();
    String revisionId = updateBuilder.getRevisionId();
    // if revisionId not specified, get the current revision
    if (revisionId == null) {
        Revision currentRevision = projectFileAccessProvider.getRevisionAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType).getCurrentRevision();
        if (currentRevision != null) {
            revisionId = currentRevision.getId();
        } else if (requireRevisionId) {
            StringBuilder builder = new StringBuilder("Could not find current revision for ");
            if (workspaceId != null) {
                builder.append(workspaceType.getLabel() + " " + workspaceAccessType.getLabel()).append(" ").append(workspaceId).append("in ");
            }
            builder.append("project ").append(projectId).append(": it may be corrupt");
            throw new LegendSDLCServerException(builder.toString());
        }
    }
    // find out what we need to update for project structure
    FileAccessContext fileAccessContext = CachingFileAccessContext.wrap(projectFileAccessProvider.getFileAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType, revisionId));
    ProjectFile configFile = getProjectConfigurationFile(fileAccessContext);
    ProjectConfiguration currentConfig = (configFile == null) ? getDefaultProjectConfiguration(projectId, projectType) : readProjectConfiguration(configFile);
    if (projectType != currentConfig.getProjectType()) {
        throw new LegendSDLCServerException("Project type mismatch for project " + projectId + ": got " + projectType + ", found " + currentConfig.getProjectType(), Status.BAD_REQUEST);
    }
    boolean updateProjectStructureVersion = updateBuilder.hasProjectStructureVersion() && (updateBuilder.getProjectStructureVersion() != currentConfig.getProjectStructureVersion().getVersion());
    boolean updateProjectStructureExtensionVersion = updateBuilder.hasProjectStructureExtensionVersion() && !updateBuilder.getProjectStructureExtensionVersion().equals(currentConfig.getProjectStructureVersion().getExtensionVersion());
    boolean updateGroupId = updateBuilder.hasGroupId() && !updateBuilder.getGroupId().equals(currentConfig.getGroupId());
    boolean updateArtifactId = updateBuilder.hasArtifactId() && !updateBuilder.getArtifactId().equals(currentConfig.getArtifactId());
    // find out which dependencies we need to update
    boolean updateProjectDependencies = false;
    Set<ProjectDependency> projectDependencies = Sets.mutable.withAll(currentConfig.getProjectDependencies());
    Set<ProjectDependency> toUpdateProjectDependencies = projectDependencies.stream().filter(dep -> ProjectDependency.isLegacyProjectDependency(dep) && !(updateBuilder.hasProjectDependenciesToRemove() && updateBuilder.getProjectDependenciesToRemove().contains(dep))).collect(Collectors.toSet());
    if (toUpdateProjectDependencies.size() > 0) {
        updateProjectDependencies = true;
        updateOrAddDependencies(toUpdateProjectDependencies, projectFileAccessProvider, projectDependencies, true);
    }
    if (updateBuilder.hasProjectDependenciesToRemove()) {
        updateProjectDependencies |= projectDependencies.removeAll(updateBuilder.getProjectDependenciesToRemove());
    }
    // add new dependencies to the list of dependencies while also validate that there are no unknown/non-prod dependencies
    if (updateBuilder.hasProjectDependenciesToAdd()) {
        updateProjectDependencies = true;
        updateOrAddDependencies(updateBuilder.getProjectDependenciesToAdd(), projectFileAccessProvider, projectDependencies, false);
    }
    // validate if there are any conflicts between the dependencies
    if (updateProjectDependencies) {
        validateDependencyConflicts(projectDependencies, ProjectDependency::getProjectId, (id, deps) -> {
            if ((deps.size() <= 1) || deps.stream().allMatch(dep -> getProjectStructure(projectFileAccessProvider.getFileAccessContext(dep.getProjectId(), dep.getVersionId())).isSupportedArtifactType(ArtifactType.versioned_entities))) {
                return null;
            }
            List<ProjectDependency> supported = Lists.mutable.empty();
            List<ProjectDependency> unsupported = Lists.mutable.empty();
            deps.forEach(dep -> (getProjectStructure(projectFileAccessProvider.getFileAccessContext(dep.getProjectId(), dep.getVersionId())).isSupportedArtifactType(ArtifactType.versioned_entities) ? supported : unsupported).add(dep));
            StringBuilder message = new StringBuilder();
            unsupported.forEach(dep -> dep.appendVersionIdString((message.length() == 0) ? message : message.append(", ")));
            message.append((unsupported.size() == 1) ? " does" : " do").append(" not support multi-version dependency");
            if (!supported.isEmpty()) {
                int startLen = message.length();
                supported.forEach(dep -> dep.appendVersionIdString(message.append((message.length() == startLen) ? "; " : ", ")));
                message.append((supported.size() == 1) ? " does" : " do");
            }
            return message.toString();
        }, "projects");
    }
    // check if we need to update any metamodel dependencies
    boolean updateMetamodelDependencies = false;
    Set<MetamodelDependency> metamodelDependencies = Sets.mutable.withAll(currentConfig.getMetamodelDependencies());
    if (updateBuilder.hasMetamodelDependenciesToRemove()) {
        updateMetamodelDependencies |= metamodelDependencies.removeAll(updateBuilder.getMetamodelDependenciesToRemove());
    }
    // add new metamodel dependencies to the list of metamodel dependencies while also validate that there are no unknown metamodel dependencies
    if (updateBuilder.hasMetamodelDependenciesToAdd()) {
        List<MetamodelDependency> unknownDependencies = Lists.mutable.empty();
        for (MetamodelDependency metamodelDependency : updateBuilder.getMetamodelDependenciesToAdd()) {
            if (metamodelDependencies.add(metamodelDependency)) {
                updateMetamodelDependencies = true;
                if (!isKnownMetamodel(metamodelDependency)) {
                    unknownDependencies.add(metamodelDependency);
                }
            }
        }
        if (!unknownDependencies.isEmpty()) {
            StringBuilder builder = new StringBuilder("There were issues with one or more added metamodel dependencies");
            builder.append("; unknown ").append((unknownDependencies.size() == 1) ? "dependency" : "dependencies").append(": ");
            unknownDependencies.sort(Comparator.naturalOrder());
            unknownDependencies.forEach(d -> d.appendDependencyString((d == unknownDependencies.get(0)) ? builder : builder.append(", ")));
            throw new LegendSDLCServerException(builder.toString(), Status.BAD_REQUEST);
        }
    }
    // validate that there are no conflicts between the metamodel dependencies
    if (updateMetamodelDependencies) {
        validateDependencyConflicts(metamodelDependencies, MetamodelDependency::getMetamodel, (id, deps) -> (deps.size() > 1) ? deps.stream().collect(StringBuilder::new, (builder, dep) -> dep.appendVersionIdString(builder.append((builder.length() == 0) ? "multiple versions not allowed: " : ", ")), StringBuilder::append).toString() : null, "metamodels");
    }
    boolean updateGeneration = false;
    Map<String, ArtifactGeneration> generationsByName = currentConfig.getArtifactGenerations().stream().collect(Collectors.toMap(ArtifactGeneration::getName, Function.identity()));
    if (updateBuilder.hasArtifactGenerationsToRemove()) {
        updateGeneration = generationsByName.keySet().stream().anyMatch(updateBuilder.getArtifactGenerationToRemove()::contains);
        updateBuilder.getArtifactGenerationToRemove().forEach(generationsByName::remove);
    }
    if (updateBuilder.hasArtifactGenerationsToAdd()) {
        validateArtifactGenerations(generationsByName, updateBuilder.getArtifactGenerationToAdd());
        updateGeneration = updateBuilder.getArtifactGenerationToAdd().stream().noneMatch(generationsByName.values()::contains);
        updateBuilder.getArtifactGenerationToAdd().forEach(art -> generationsByName.put(art.getName(), art));
    }
    // abort if there is no change to make
    if (!updateProjectStructureVersion && !updateProjectStructureExtensionVersion && !updateGroupId && !updateArtifactId && !updateProjectDependencies && !updateMetamodelDependencies && !updateGeneration) {
        return null;
    }
    // Collect operations
    List<ProjectFileOperation> operations = Lists.mutable.empty();
    // New configuration
    SimpleProjectConfiguration newConfig = new SimpleProjectConfiguration(currentConfig);
    if (updateProjectStructureVersion) {
        if (updateBuilder.hasProjectStructureExtensionVersion()) {
            newConfig.setProjectStructureVersion(updateBuilder.getProjectStructureVersion(), updateBuilder.getProjectStructureExtensionVersion());
        } else if (updateBuilder.hasProjectStructureExtensionProvider()) {
            newConfig.setProjectStructureVersion(updateBuilder.getProjectStructureVersion(), updateBuilder.getProjectStructureExtensionProvider().getLatestVersionForProjectStructureVersion(updateBuilder.getProjectStructureVersion()));
        } else {
            newConfig.setProjectStructureVersion(updateBuilder.getProjectStructureVersion(), null);
        }
    } else if (updateProjectStructureExtensionVersion) {
        newConfig.setProjectStructureVersion(currentConfig.getProjectStructureVersion().getVersion(), updateBuilder.getProjectStructureExtensionVersion());
    }
    if (updateGroupId) {
        newConfig.setGroupId(updateBuilder.getGroupId());
    }
    if (updateArtifactId) {
        newConfig.setArtifactId(updateBuilder.getArtifactId());
    }
    if (updateProjectDependencies) {
        List<ProjectDependency> projectDependencyList = Lists.mutable.withAll(projectDependencies);
        projectDependencyList.sort(Comparator.naturalOrder());
        newConfig.setProjectDependencies(projectDependencyList);
    }
    if (updateMetamodelDependencies) {
        List<MetamodelDependency> metamodelDependencyList = Lists.mutable.withAll(metamodelDependencies);
        metamodelDependencyList.sort(Comparator.naturalOrder());
        newConfig.setMetamodelDependencies(metamodelDependencyList);
    }
    if (updateGeneration) {
        List<ArtifactGeneration> artifactGenerationsList = Lists.mutable.withAll(generationsByName.values());
        artifactGenerationsList.sort(Comparator.comparing(ArtifactGeneration::getName));
        newConfig.setArtifactGeneration(artifactGenerationsList);
    }
    // prevent downgrading project
    if (newConfig.getProjectStructureVersion().compareTo(currentConfig.getProjectStructureVersion()) < 0) {
        throw new LegendSDLCServerException("Cannot change project " + projectId + " from project structure version " + currentConfig.getProjectStructureVersion().toVersionString() + " to version " + newConfig.getProjectStructureVersion().toVersionString(), Status.BAD_REQUEST);
    }
    String serializedNewConfig = serializeProjectConfiguration(newConfig);
    operations.add((configFile == null) ? ProjectFileOperation.addFile(PROJECT_CONFIG_PATH, serializedNewConfig) : ProjectFileOperation.modifyFile(PROJECT_CONFIG_PATH, serializedNewConfig));
    ProjectStructure currentProjectStructure = getProjectStructure(currentConfig, updateBuilder.getProjectStructurePlatformExtensions());
    ProjectStructure newProjectStructure = getProjectStructure(newConfig, updateBuilder.getProjectStructurePlatformExtensions());
    // Move or re-serialize entities if necessary
    List<EntitySourceDirectory> currentEntityDirectories = currentProjectStructure.getEntitySourceDirectories();
    List<EntitySourceDirectory> newEntityDirectories = newProjectStructure.getEntitySourceDirectories();
    if (!currentEntityDirectories.equals(newEntityDirectories)) {
        currentEntityDirectories.forEach(currentSourceDirectory -> fileAccessContext.getFilesInDirectory(currentSourceDirectory.getDirectory()).forEach(file -> {
            String currentPath = file.getPath();
            if (currentSourceDirectory.isPossiblyEntityFilePath(currentPath)) {
                byte[] currentBytes = file.getContentAsBytes();
                Entity entity;
                try {
                    entity = currentSourceDirectory.deserialize(currentBytes);
                } catch (Exception e) {
                    StringBuilder builder = new StringBuilder("Error deserializing entity from file \"").append(currentPath).append('"');
                    StringTools.appendThrowableMessageIfPresent(builder, e);
                    throw new LegendSDLCServerException(builder.toString(), e);
                }
                EntitySourceDirectory newSourceDirectory = Iterate.detectWith(newEntityDirectories, EntitySourceDirectory::canSerialize, entity);
                if (newSourceDirectory == null) {
                    throw new LegendSDLCServerException("Could not find a new source directory for entity " + entity.getPath() + ", currently in " + currentPath);
                }
                if (!currentSourceDirectory.equals(newSourceDirectory)) {
                    String newPath = newSourceDirectory.entityPathToFilePath(entity.getPath());
                    byte[] newBytes = newSourceDirectory.serializeToBytes(entity);
                    if (!newPath.equals(currentPath)) {
                        operations.add(ProjectFileOperation.moveFile(currentPath, newPath, newBytes));
                    } else if (!Arrays.equals(currentBytes, newBytes)) {
                        operations.add(ProjectFileOperation.modifyFile(currentPath, newBytes));
                    }
                }
            }
        }));
    }
    // Collect any further update operations
    newProjectStructure.collectUpdateProjectConfigurationOperations(currentProjectStructure, fileAccessContext, projectFileAccessProvider::getFileAccessContext, operations::add);
    // Collect update operations from any project structure extension
    if (updateBuilder.hasProjectStructureExtensionProvider() && (newConfig.getProjectStructureVersion().getExtensionVersion() != null)) {
        ProjectStructureExtension projectStructureExtension = updateBuilder.getProjectStructureExtensionProvider().getProjectStructureExtension(newConfig.getProjectStructureVersion().getVersion(), newConfig.getProjectStructureVersion().getExtensionVersion());
        projectStructureExtension.collectUpdateProjectConfigurationOperations(currentConfig, newConfig, fileAccessContext, operations::add);
    }
    // Submit changes
    return projectFileAccessProvider.getFileModificationContext(projectId, workspaceId, workspaceType, workspaceAccessType, revisionId).submit(updateBuilder.getMessage(), operations);
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) Arrays(java.util.Arrays) ArtifactType(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactType) SortedSet(java.util.SortedSet) BiFunction(java.util.function.BiFunction) MutableList(org.eclipse.collections.api.list.MutableList) StringTools(org.finos.legend.sdlc.server.tools.StringTools) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Map(java.util.Map) EnumSet(java.util.EnumSet) Dependency(org.finos.legend.sdlc.domain.model.project.configuration.Dependency) ArtifactTypeGenerationConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactTypeGenerationConfiguration) VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) MetamodelDependency(org.finos.legend.sdlc.domain.model.project.configuration.MetamodelDependency) Collection(java.util.Collection) Set(java.util.Set) StreamWriteFeature(com.fasterxml.jackson.core.StreamWriteFeature) Reader(java.io.Reader) Iterate(org.eclipse.collections.impl.utility.Iterate) StreamReadFeature(com.fasterxml.jackson.core.StreamReadFeature) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) Collectors(java.util.stream.Collectors) ListAdapter(org.eclipse.collections.impl.list.mutable.ListAdapter) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) ArtifactGeneration(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactGeneration) SortedMap(java.util.SortedMap) ProjectStructureExtension(org.finos.legend.sdlc.server.project.extension.ProjectStructureExtension) Lists(org.eclipse.collections.api.factory.Lists) Function(java.util.function.Function) TreeSet(java.util.TreeSet) FileAccessContext(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider.FileAccessContext) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) EntitySerializer(org.finos.legend.sdlc.serialization.EntitySerializer) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Status(javax.ws.rs.core.Response.Status) Sets(org.eclipse.collections.api.factory.Sets) IOException(java.io.IOException) EntitySerializers(org.finos.legend.sdlc.serialization.EntitySerializers) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) ProjectStructureVersion(org.finos.legend.sdlc.domain.model.project.configuration.ProjectStructureVersion) Consumer(java.util.function.Consumer) ProjectFile(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider.ProjectFile) ProjectDependency(org.finos.legend.sdlc.domain.model.project.configuration.ProjectDependency) TreeMap(java.util.TreeMap) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) ArtifactGeneration(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactGeneration) ProjectStructureExtension(org.finos.legend.sdlc.server.project.extension.ProjectStructureExtension) ProjectFile(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider.ProjectFile) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) FileAccessContext(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider.FileAccessContext) ProjectDependency(org.finos.legend.sdlc.domain.model.project.configuration.ProjectDependency) MetamodelDependency(org.finos.legend.sdlc.domain.model.project.configuration.MetamodelDependency) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) IOException(java.io.IOException) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)

Example 7 with ProjectType

use of org.finos.legend.sdlc.domain.model.project.ProjectType in project legend-sdlc by finos.

the class GitLabProjectApi method getProjects.

@Override
public List<Project> getProjects(boolean user, String search, Iterable<String> tags, Iterable<ProjectType> types, Integer limit) {
    try {
        Set<ProjectType> typesSet;
        if (types == null) {
            typesSet = Collections.emptySet();
        } else if (types instanceof Set) {
            typesSet = (Set<ProjectType>) types;
        } else {
            typesSet = EnumSet.noneOf(ProjectType.class);
            types.forEach(typesSet::add);
        }
        Iterable<GitLabMode> modes = typesSet.isEmpty() ? getValidGitLabModes() : typesSet.stream().map(GitLabProjectApi::getGitLabModeFromProjectType).filter(this::isValidGitLabMode).collect(Collectors.toList());
        List<Project> projects = Lists.mutable.empty();
        Set<String> tagSet = (tags == null) ? Collections.emptySet() : toLegendSDLCTagSet(tags);
        for (GitLabMode mode : modes) {
            Pager<org.gitlab4j.api.models.Project> pager = withRetries(() -> getGitLabApi(mode).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()) {
                stream = stream.filter(p -> p.getTagList().stream().anyMatch(tagSet::contains));
            }
            if (limit != null) {
                // NOTE: this check implies that the mode that is scanned first could take all the slots within the
                // limit. This limitation hopefully will be removed when we remove support for prototype (UAT) mode.
                stream = stream.limit(limit - projects.size());
            }
            stream.map(p -> fromGitLabProject(p, mode)).forEach(projects::add);
            if (limit != null && projects.size() >= limit) {
                // If the number of projects found already exceed the limit, skip the check for the other modes
                break;
            }
        }
        // ensure the list of returned projects cannot exceed the limit (if specified) for whatever reasons
        if (limit != null && projects.size() > limit) {
            return projects.subList(0, limit);
        }
        return projects;
    } catch (Exception e) {
        throw buildException(e, () -> {
            StringBuilder message = new StringBuilder("Failed to find ");
            if (user) {
                message.append("user ");
            }
            message.append("projects");
            List<String> tagList = (tags == null) ? Collections.emptyList() : StreamSupport.stream(tags.spliterator(), false).collect(Collectors.toList());
            if ((search != null) || !tagList.isEmpty()) {
                message.append(" (");
                if (search != null) {
                    message.append("search=\"").append(search).append("\"");
                    if (!tagList.isEmpty()) {
                        message.append(", ");
                    }
                }
                if (!tagList.isEmpty()) {
                    tagList.sort(Comparator.naturalOrder());
                    message.append("tags=[").append(String.join(", ", tagList)).append("]");
                }
                message.append(')');
            }
            return message.toString();
        });
    }
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) ProjectStructureConfiguration(org.finos.legend.sdlc.server.project.config.ProjectStructureConfiguration) Branch(org.gitlab4j.api.models.Branch) GitLabConfiguration(org.finos.legend.sdlc.server.gitlab.GitLabConfiguration) ProjectAuthorizationAction(org.finos.legend.sdlc.domain.model.project.accessRole.ProjectAuthorizationAction) LoggerFactory(org.slf4j.LoggerFactory) AccessLevel(org.gitlab4j.api.models.AccessLevel) GitLabApiTools(org.finos.legend.sdlc.server.gitlab.tools.GitLabApiTools) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) MergeRequest(org.gitlab4j.api.models.MergeRequest) BackgroundTaskProcessor(org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor) EnumSet(java.util.EnumSet) Visibility(org.gitlab4j.api.models.Visibility) RepositoryApi(org.gitlab4j.api.RepositoryApi) Pager(org.gitlab4j.api.Pager) Set(java.util.Set) Iterate(org.eclipse.collections.impl.utility.Iterate) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ProjectConfigurationUpdateBuilder(org.finos.legend.sdlc.server.project.ProjectConfigurationUpdateBuilder) List(java.util.List) Stream(java.util.stream.Stream) ProjectStructurePlatformExtensions(org.finos.legend.sdlc.server.project.ProjectStructurePlatformExtensions) Pattern(java.util.regex.Pattern) GitLabApi(org.gitlab4j.api.GitLabApi) ProjectApi(org.finos.legend.sdlc.server.domain.api.project.ProjectApi) Permissions(org.gitlab4j.api.models.Permissions) Lists(org.eclipse.collections.api.factory.Lists) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) ProjectStructureExtensionProvider(org.finos.legend.sdlc.server.project.extension.ProjectStructureExtensionProvider) ProjectCreationConfiguration(org.finos.legend.sdlc.server.project.config.ProjectCreationConfiguration) AccessRole(org.finos.legend.sdlc.domain.model.project.accessRole.AccessRole) Inject(javax.inject.Inject) StreamSupport(java.util.stream.StreamSupport) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) Status(javax.ws.rs.core.Response.Status) Sets(org.eclipse.collections.api.factory.Sets) Logger(org.slf4j.Logger) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) ProtectedTag(org.gitlab4j.api.models.ProtectedTag) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) ListIterate(org.eclipse.collections.impl.utility.ListIterate) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) ProjectStructureVersion(org.finos.legend.sdlc.domain.model.project.configuration.ProjectStructureVersion) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Project(org.finos.legend.sdlc.domain.model.project.Project) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Comparator(java.util.Comparator) ProjectAccess(org.gitlab4j.api.models.ProjectAccess) Collections(java.util.Collections) EnumSet(java.util.EnumSet) Set(java.util.Set) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Project(org.finos.legend.sdlc.domain.model.project.Project) ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) List(java.util.List) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 8 with ProjectType

use of org.finos.legend.sdlc.domain.model.project.ProjectType in project legend-sdlc by finos.

the class GitLabApiWithFileAccess method getProjectConfiguration.

protected ProjectConfiguration getProjectConfiguration(String projectId, VersionId versionId) {
    ProjectConfiguration config = ProjectStructure.getProjectConfiguration(projectId, versionId, getProjectFileAccessProvider());
    if (config == null) {
        ProjectType projectType = getProjectTypeFromMode(GitLabProjectId.getGitLabMode(projectId));
        config = ProjectStructure.getDefaultProjectConfiguration(projectId, projectType);
    }
    return config;
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)

Example 9 with ProjectType

use of org.finos.legend.sdlc.domain.model.project.ProjectType in project legend-sdlc by finos.

the class TestProjectStructure method testUpdateGroupAndArtifactIds.

protected void testUpdateGroupAndArtifactIds(ProjectType projectType) {
    buildProjectStructure(projectType);
    ProjectStructure projectStructure = ProjectStructure.getProjectStructure(PROJECT_ID, null, null, this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
    List<Entity> testEntities = getTestEntities();
    List<ProjectFileOperation> addEntityOperations = ListIterate.collectWith(testEntities, this::generateAddOperationForEntity, projectStructure);
    Revision entitiesRevision = this.fileAccessProvider.getProjectFileModificationContext(PROJECT_ID).submit("Add entities", addEntityOperations);
    ProjectConfiguration beforeProjectConfig = ProjectStructure.getProjectConfiguration(PROJECT_ID, null, null, this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
    Assert.assertEquals(PROJECT_ID, beforeProjectConfig.getProjectId());
    Assert.assertEquals(GROUP_ID, beforeProjectConfig.getGroupId());
    Assert.assertEquals(ARTIFACT_ID, beforeProjectConfig.getArtifactId());
    Assert.assertEquals(this.projectStructureVersion, beforeProjectConfig.getProjectStructureVersion().getVersion());
    Assert.assertEquals(this.projectStructureExtensionVersion, beforeProjectConfig.getProjectStructureVersion().getExtensionVersion());
    Assert.assertEquals(Collections.emptyList(), beforeProjectConfig.getMetamodelDependencies());
    Assert.assertEquals(Collections.emptyList(), beforeProjectConfig.getProjectDependencies());
    assertStateValid(PROJECT_ID, null, null);
    String workspaceId = "UpdateGroupAndArtifactIds";
    this.fileAccessProvider.createWorkspace(PROJECT_ID, workspaceId);
    ProjectConfiguration beforeWorkspaceConfig = ProjectStructure.getProjectConfiguration(PROJECT_ID, null, null, this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
    Assert.assertEquals(PROJECT_ID, beforeWorkspaceConfig.getProjectId());
    Assert.assertEquals(GROUP_ID, beforeWorkspaceConfig.getGroupId());
    Assert.assertEquals(ARTIFACT_ID, beforeWorkspaceConfig.getArtifactId());
    Assert.assertEquals(this.projectStructureVersion, beforeWorkspaceConfig.getProjectStructureVersion().getVersion());
    Assert.assertEquals(this.projectStructureExtensionVersion, beforeWorkspaceConfig.getProjectStructureVersion().getExtensionVersion());
    Assert.assertEquals(Collections.emptyList(), beforeWorkspaceConfig.getMetamodelDependencies());
    Assert.assertEquals(Collections.emptyList(), beforeWorkspaceConfig.getProjectDependencies());
    assertStateValid(PROJECT_ID, workspaceId, null);
    Revision configUpdateRevision = ProjectConfigurationUpdateBuilder.newBuilder(this.fileAccessProvider, projectType, PROJECT_ID).withWorkspace(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withRevisionId(entitiesRevision.getId()).withMessage("Update group and artifact ids").withGroupId(GROUP_ID_2).withArtifactId(ARTIFACT_ID_2).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).updateProjectConfiguration();
    ProjectConfiguration afterWorkspaceConfig = ProjectStructure.getProjectConfiguration(PROJECT_ID, workspaceId, null, this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
    Assert.assertEquals(PROJECT_ID, afterWorkspaceConfig.getProjectId());
    Assert.assertEquals(GROUP_ID_2, afterWorkspaceConfig.getGroupId());
    Assert.assertEquals(ARTIFACT_ID_2, afterWorkspaceConfig.getArtifactId());
    Assert.assertEquals(this.projectStructureVersion, afterWorkspaceConfig.getProjectStructureVersion().getVersion());
    Assert.assertEquals(this.projectStructureExtensionVersion, afterWorkspaceConfig.getProjectStructureVersion().getExtensionVersion());
    Assert.assertEquals(Collections.emptyList(), afterWorkspaceConfig.getMetamodelDependencies());
    Assert.assertEquals(Collections.emptyList(), afterWorkspaceConfig.getProjectDependencies());
    assertStateValid(PROJECT_ID, workspaceId, null);
    this.fileAccessProvider.commitWorkspace(PROJECT_ID, workspaceId);
    ProjectConfiguration afterProjectConfig = ProjectStructure.getProjectConfiguration(PROJECT_ID, null, null, this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
    Assert.assertEquals(PROJECT_ID, afterProjectConfig.getProjectId());
    Assert.assertEquals(GROUP_ID_2, afterProjectConfig.getGroupId());
    Assert.assertEquals(ARTIFACT_ID_2, afterProjectConfig.getArtifactId());
    Assert.assertEquals(this.projectStructureVersion, afterProjectConfig.getProjectStructureVersion().getVersion());
    Assert.assertEquals(this.projectStructureExtensionVersion, afterProjectConfig.getProjectStructureVersion().getExtensionVersion());
    Assert.assertEquals(Collections.emptyList(), afterProjectConfig.getMetamodelDependencies());
    Assert.assertEquals(Collections.emptyList(), afterProjectConfig.getProjectDependencies());
    assertStateValid(PROJECT_ID, null, null);
    ProjectConfiguration projectEntitiesRevisionConfig = ProjectStructure.getProjectConfiguration(PROJECT_ID, null, entitiesRevision.getId(), this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
    Assert.assertEquals(PROJECT_ID, projectEntitiesRevisionConfig.getProjectId());
    Assert.assertEquals(GROUP_ID, projectEntitiesRevisionConfig.getGroupId());
    Assert.assertEquals(ARTIFACT_ID, projectEntitiesRevisionConfig.getArtifactId());
    Assert.assertEquals(this.projectStructureVersion, projectEntitiesRevisionConfig.getProjectStructureVersion().getVersion());
    Assert.assertEquals(this.projectStructureExtensionVersion, projectEntitiesRevisionConfig.getProjectStructureVersion().getExtensionVersion());
    Assert.assertEquals(Collections.emptyList(), projectEntitiesRevisionConfig.getMetamodelDependencies());
    Assert.assertEquals(Collections.emptyList(), projectEntitiesRevisionConfig.getProjectDependencies());
    ProjectConfiguration projectConfigUpdateRevisionConfig = ProjectStructure.getProjectConfiguration(PROJECT_ID, null, configUpdateRevision.getId(), this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
    Assert.assertEquals(PROJECT_ID, projectConfigUpdateRevisionConfig.getProjectId());
    Assert.assertEquals(GROUP_ID_2, projectConfigUpdateRevisionConfig.getGroupId());
    Assert.assertEquals(ARTIFACT_ID_2, projectConfigUpdateRevisionConfig.getArtifactId());
    Assert.assertEquals(this.projectStructureVersion, projectConfigUpdateRevisionConfig.getProjectStructureVersion().getVersion());
    Assert.assertEquals(this.projectStructureExtensionVersion, projectConfigUpdateRevisionConfig.getProjectStructureVersion().getExtensionVersion());
    Assert.assertEquals(Collections.emptyList(), projectConfigUpdateRevisionConfig.getMetamodelDependencies());
    Assert.assertEquals(Collections.emptyList(), projectConfigUpdateRevisionConfig.getProjectDependencies());
    assertEntitiesEquivalent(testEntities, getActualEntities(PROJECT_ID));
    Map<String, String> actualFiles = this.fileAccessProvider.getFileAccessContext(PROJECT_ID, null, null, null, configUpdateRevision.getId()).getFiles().collect(Collectors.toMap(ProjectFileAccessProvider.ProjectFile::getPath, ProjectFileAccessProvider.ProjectFile::getContentAsString));
    List<String> unExpectedFiles = actualFiles.keySet().stream().filter(filePath -> !filePath.equals("/pom.xml") && filePath.endsWith("/pom.xml") && !filePath.startsWith("/" + ARTIFACT_ID_2)).collect(Collectors.toList());
    Assert.assertTrue("non expected files " + Arrays.toString(unExpectedFiles.toArray()), unExpectedFiles.isEmpty());
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) Arrays(java.util.Arrays) ArtifactType(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactType) Lists(org.eclipse.collections.api.factory.Lists) ProjectStructureExtensionProvider(org.finos.legend.sdlc.server.project.extension.ProjectStructureExtensionProvider) IntSets(org.eclipse.collections.impl.factory.primitive.IntSets) Function(java.util.function.Function) Maps(org.eclipse.collections.api.factory.Maps) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) TestTools(org.finos.legend.sdlc.domain.model.TestTools) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Sets(org.eclipse.collections.api.factory.Sets) Before(org.junit.Before) ArtifactTypeGenerationConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactTypeGenerationConfiguration) VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) MetamodelDependency(org.finos.legend.sdlc.domain.model.project.configuration.MetamodelDependency) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) StreamWriteFeature(com.fasterxml.jackson.core.StreamWriteFeature) Iterate(org.eclipse.collections.impl.utility.Iterate) ListIterate(org.eclipse.collections.impl.utility.ListIterate) Instant(java.time.Instant) StreamReadFeature(com.fasterxml.jackson.core.StreamReadFeature) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) Collectors(java.util.stream.Collectors) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) ProjectDependency(org.finos.legend.sdlc.domain.model.project.configuration.ProjectDependency) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration) StringJoiner(java.util.StringJoiner) Optional(java.util.Optional) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) ImmutableIntSet(org.eclipse.collections.api.set.primitive.ImmutableIntSet) Assert(org.junit.Assert) Comparator(java.util.Comparator) ArtifactGeneration(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactGeneration) Collections(java.util.Collections) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)

Example 10 with ProjectType

use of org.finos.legend.sdlc.domain.model.project.ProjectType in project legend-sdlc by finos.

the class TestProjectStructureStaticMethods method testGetDefaultProjectConfiguration.

@Test
public void testGetDefaultProjectConfiguration() {
    String projectId = "pid";
    for (ProjectType projectType : ProjectType.values()) {
        ProjectConfiguration config = ProjectStructure.getDefaultProjectConfiguration(projectId, projectType);
        Assert.assertNotNull(config);
        Assert.assertEquals(0, config.getProjectStructureVersion().getVersion());
        Assert.assertNull(config.getProjectStructureVersion().getExtensionVersion());
        Assert.assertEquals(projectId, config.getProjectId());
        Assert.assertSame(projectType, config.getProjectType());
    }
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration) Test(org.junit.Test)

Aggregations

ProjectType (org.finos.legend.sdlc.domain.model.project.ProjectType)20 Project (org.finos.legend.sdlc.domain.model.project.Project)14 Entity (org.finos.legend.sdlc.domain.model.entity.Entity)10 Workspace (org.finos.legend.sdlc.domain.model.project.workspace.Workspace)10 List (java.util.List)7 ProjectConfiguration (org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)7 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)7 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)5 RepositoryApi (org.gitlab4j.api.RepositoryApi)5 MergeRequest (org.gitlab4j.api.models.MergeRequest)5 Collections (java.util.Collections)3 Comparator (java.util.Comparator)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Lists (org.eclipse.collections.api.factory.Lists)3 Sets (org.eclipse.collections.api.factory.Sets)3 Iterate (org.eclipse.collections.impl.utility.Iterate)3 WorkspaceType (org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType)3 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)3 StreamReadFeature (com.fasterxml.jackson.core.StreamReadFeature)2