Search in sources :

Example 1 with VersionId

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

the class ProjectDependency method parseProjectDependency.

public static ProjectDependency parseProjectDependency(String string, int start, int end, char delimiter) {
    if (string == null) {
        throw new IllegalArgumentException("Invalid project dependency string: null");
    }
    int delimiterIndex = string.lastIndexOf(delimiter, end - 1);
    if ((delimiterIndex == -1) || (delimiterIndex < start)) {
        throw new IllegalArgumentException(new StringBuilder("Invalid project dependency string: \"").append(string, start, end).append('"').toString());
    }
    String projectId = string.substring(start, delimiterIndex);
    VersionId versionId;
    try {
        versionId = VersionId.parseVersionId(string, delimiterIndex + 1, end);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(new StringBuilder("Invalid project dependency string: \"").append(string, start, end).append('"').toString(), e);
    }
    return newProjectDependency(projectId, versionId);
}
Also used : VersionId(org.finos.legend.sdlc.domain.model.version.VersionId)

Example 2 with VersionId

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

the class MultiModuleMavenProjectStructure method createMavenOtherModuleModel.

protected Model createMavenOtherModuleModel(String otherModuleName, BiFunction<String, VersionId, ProjectFileAccessProvider.FileAccessContext> versionFileAccessContextProvider) {
    Model mavenModel = createMavenModuleModel(otherModuleName);
    ArtifactType typeForConfig = otherModules.get(otherModuleName);
    Map<ModuleConfigType, Method> otherModuleConfigMethods = this.moduleConfigMethods.get(getModuleConfigName(typeForConfig));
    if (otherModuleConfigMethods != null) {
        // Properties
        Method propertiesMethod = otherModuleConfigMethods.get(ModuleConfigType.PROPERTIES);
        if (propertiesMethod != null) {
            try {
                Properties properties = new SortedPropertiesForSerialization();
                invokeConfigMethod(propertiesMethod, (BiConsumer<String, String>) properties::setProperty);
                if (!properties.isEmpty()) {
                    mavenModel.setProperties(properties);
                }
            } catch (LegendSDLCServerException e) {
                throw e;
            } catch (Exception e) {
                throw new LegendSDLCServerException("Error adding maven properties for module " + otherModuleName, e);
            }
        }
        // Module dependencies
        Method moduleDependenciesMethod = otherModuleConfigMethods.get(ModuleConfigType.MODULE_DEPENDENCIES);
        if (moduleDependenciesMethod != null) {
            try {
                invokeConfigMethod(moduleDependenciesMethod, (Consumer<String>) m -> mavenModel.addDependency(getModuleWithNoVersionDependency(m)));
            } catch (LegendSDLCServerException e) {
                throw e;
            } catch (Exception e) {
                throw new LegendSDLCServerException("Error adding maven module dependencies for module " + otherModuleName, e);
            }
        }
        // Dependencies
        Method dependenciesMethod = otherModuleConfigMethods.get(ModuleConfigType.DEPENDENCIES);
        if (dependenciesMethod != null) {
            try {
                invokeConfigMethod(dependenciesMethod, versionFileAccessContextProvider, (Consumer<Dependency>) mavenModel::addDependency);
            } catch (LegendSDLCServerException e) {
                throw e;
            } catch (Exception e) {
                throw new LegendSDLCServerException("Error adding maven dependencies for module " + otherModuleName, e);
            }
        }
        // Plugins
        Method pluginsMethod = otherModuleConfigMethods.get(ModuleConfigType.PLUGINS);
        if (pluginsMethod != null) {
            try {
                Build build = new Build();
                invokeConfigMethod(pluginsMethod, otherModuleName, versionFileAccessContextProvider, (Consumer<Plugin>) build::addPlugin);
                if (!build.getPlugins().isEmpty()) {
                    mavenModel.setBuild(build);
                }
            } catch (LegendSDLCServerException e) {
                throw e;
            } catch (Exception e) {
                throw new LegendSDLCServerException("Error adding maven plugins for module " + otherModuleName, e);
            }
        }
    }
    return mavenModel;
}
Also used : ProjectFileOperation(org.finos.legend.sdlc.server.project.ProjectFileOperation) ArtifactType(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactType) BiFunction(java.util.function.BiFunction) Dependency(org.apache.maven.model.Dependency) Lists(org.eclipse.collections.api.factory.Lists) Build(org.apache.maven.model.Build) Parent(org.apache.maven.model.Parent) Retention(java.lang.annotation.Retention) Maps(org.eclipse.collections.api.factory.Maps) EntitySerializer(org.finos.legend.sdlc.serialization.EntitySerializer) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Method(java.lang.reflect.Method) Sets(org.eclipse.collections.api.factory.Sets) VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) Properties(java.util.Properties) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) EnumMap(java.util.EnumMap) Set(java.util.Set) Target(java.lang.annotation.Target) ListIterate(org.eclipse.collections.impl.utility.ListIterate) ElementType(java.lang.annotation.ElementType) EntitySerializers(org.finos.legend.sdlc.serialization.EntitySerializers) InvocationTargetException(java.lang.reflect.InvocationTargetException) Consumer(java.util.function.Consumer) Plugin(org.apache.maven.model.Plugin) List(java.util.List) Stream(java.util.stream.Stream) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) ProjectDependency(org.finos.legend.sdlc.domain.model.project.configuration.ProjectDependency) TreeMap(java.util.TreeMap) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration) ProjectStructurePlatformExtensions(org.finos.legend.sdlc.server.project.ProjectStructurePlatformExtensions) ProjectPaths(org.finos.legend.sdlc.server.project.ProjectPaths) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) ArtifactGeneration(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactGeneration) SortedMap(java.util.SortedMap) Model(org.apache.maven.model.Model) RetentionPolicy(java.lang.annotation.RetentionPolicy) Method(java.lang.reflect.Method) Dependency(org.apache.maven.model.Dependency) ProjectDependency(org.finos.legend.sdlc.domain.model.project.configuration.ProjectDependency) Properties(java.util.Properties) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ArtifactType(org.finos.legend.sdlc.domain.model.project.configuration.ArtifactType) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) Plugin(org.apache.maven.model.Plugin)

Example 3 with VersionId

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

the class GitLabVersionApi method newVersion.

@Override
public Version newVersion(String projectId, NewVersionType type, String revisionId, String notes) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(type, "type may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    switch(getProjectTypeFromMode(gitLabProjectId.getGitLabMode())) {
        case PROTOTYPE:
            {
                throw new LegendSDLCServerException("Cannot create versions for prototype projects", Status.BAD_REQUEST);
            }
        case PRODUCTION:
            {
                Version latestVersion = getLatestVersion(gitLabProjectId);
                VersionId latestVersionId = (latestVersion == null) ? NULL_VERSION : latestVersion.getId();
                VersionId nextVersionId;
                switch(type) {
                    case MAJOR:
                        {
                            nextVersionId = latestVersionId.nextMajorVersion();
                            break;
                        }
                    case MINOR:
                        {
                            nextVersionId = latestVersionId.nextMinorVersion();
                            break;
                        }
                    case PATCH:
                        {
                            nextVersionId = latestVersionId.nextPatchVersion();
                            break;
                        }
                    default:
                        {
                            throw new LegendSDLCServerException("Unknown new version type: " + type, Status.BAD_REQUEST);
                        }
                }
                return newVersion(gitLabProjectId, revisionId, nextVersionId, notes);
            }
        default:
            {
                throw new LegendSDLCServerException("Unknown project: " + projectId, Status.BAD_REQUEST);
            }
    }
}
Also used : VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Version(org.finos.legend.sdlc.domain.model.version.Version) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId)

Example 4 with VersionId

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

the class GitLabVersionApi method newVersion.

private Version newVersion(GitLabProjectId projectId, String revisionId, VersionId versionId, String notes) {
    String tagName = buildVersionTagName(versionId);
    String message = "Release tag for version " + versionId.toVersionIdString();
    try {
        GitLabApi gitLabApi = getGitLabApi(projectId.getGitLabMode());
        CommitsApi commitsApi = gitLabApi.getCommitsApi();
        Commit referenceCommit;
        if (revisionId == null) {
            referenceCommit = commitsApi.getCommit(projectId.getGitLabId(), MASTER_BRANCH);
            if (referenceCommit == null) {
                throw new LegendSDLCServerException("Cannot create version " + versionId.toVersionIdString() + " of project " + projectId + ": cannot find current revision (project may be corrupt)", Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            try {
                referenceCommit = commitsApi.getCommit(projectId.getGitLabId(), revisionId);
            } catch (GitLabApiException e) {
                if (GitLabApiTools.isNotFoundGitLabApiException(e)) {
                    throw new LegendSDLCServerException("Revision " + revisionId + " is unknown in project " + projectId, Status.BAD_REQUEST);
                }
                throw e;
            }
            Pager<CommitRef> referenceCommitBranchPager = withRetries(() -> commitsApi.getCommitRefs(projectId.getGitLabId(), referenceCommit.getId(), RefType.BRANCH, ITEMS_PER_PAGE));
            Stream<CommitRef> referenceCommitBranches = PagerTools.stream(referenceCommitBranchPager);
            if (referenceCommitBranches.noneMatch(ref -> MASTER_BRANCH.equals(ref.getName()))) {
                throw new LegendSDLCServerException("Revision " + revisionId + " is unknown in project " + projectId, Status.BAD_REQUEST);
            }
        }
        String referenceRevisionId = referenceCommit.getId();
        Pager<CommitRef> referenceCommitTagPager = withRetries(() -> commitsApi.getCommitRefs(projectId.getGitLabId(), referenceRevisionId, RefType.TAG, ITEMS_PER_PAGE));
        List<CommitRef> referenceCommitTags = PagerTools.stream(referenceCommitTagPager).collect(Collectors.toList());
        if (referenceCommitTags.stream().map(CommitRef::getName).anyMatch(GitLabVersionApi::isVersionTagName)) {
            StringBuilder builder = new StringBuilder("Revision ").append(referenceRevisionId).append(" has already been released in ");
            List<VersionId> revisionVersionIds = referenceCommitTags.stream().map(CommitRef::getName).filter(GitLabVersionApi::isVersionTagName).map(GitLabVersionApi::parseVersionTagName).collect(Collectors.toList());
            if (revisionVersionIds.size() == 1) {
                builder.append("version ");
                revisionVersionIds.get(0).appendVersionIdString(builder);
            } else {
                builder.append("versions ");
                revisionVersionIds.sort(Comparator.naturalOrder());
                boolean first = true;
                for (VersionId revisionVersionId : revisionVersionIds) {
                    if (first) {
                        first = false;
                    } else {
                        builder.append(", ");
                    }
                    revisionVersionId.appendVersionIdString(builder);
                }
            }
            throw new LegendSDLCServerException(builder.toString());
        }
        Tag tag = getGitLabApi(projectId.getGitLabMode()).getTagsApi().createTag(projectId.getGitLabId(), tagName, referenceRevisionId, message, notes);
        return fromGitLabTag(projectId.toString(), tag);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create version " + versionId.toVersionIdString() + " of project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating version " + versionId.toVersionIdString() + " of project " + projectId);
    }
}
Also used : VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) GitLabApi(org.gitlab4j.api.GitLabApi) GitLabApiException(org.gitlab4j.api.GitLabApiException) CommitRef(org.gitlab4j.api.models.CommitRef) CommitsApi(org.gitlab4j.api.CommitsApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Commit(org.gitlab4j.api.models.Commit) Tag(org.gitlab4j.api.models.Tag)

Example 5 with VersionId

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

the class DepotProjectVersion method parseDepotProjectVersion.

public static DepotProjectVersion parseDepotProjectVersion(String depotProjectVersion) {
    if (depotProjectVersion == null) {
        return null;
    }
    int delimiterIndex = depotProjectVersion.lastIndexOf(DELIMITER);
    if (delimiterIndex == -1) {
        throw new IllegalArgumentException("Invalid project string: " + depotProjectVersion);
    }
    String projectId = depotProjectVersion.substring(0, delimiterIndex);
    VersionId versionId;
    try {
        versionId = VersionId.parseVersionId(depotProjectVersion, delimiterIndex + 1, depotProjectVersion.length());
    } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException("Invalid project string: " + depotProjectVersion, ex);
    }
    return newDepotProjectVersion(projectId, versionId);
}
Also used : VersionId(org.finos.legend.sdlc.domain.model.version.VersionId)

Aggregations

VersionId (org.finos.legend.sdlc.domain.model.version.VersionId)11 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)4 List (java.util.List)2 Set (java.util.Set)2 Lists (org.eclipse.collections.api.factory.Lists)2 Sets (org.eclipse.collections.api.factory.Sets)2 Entity (org.finos.legend.sdlc.domain.model.entity.Entity)2 Version (org.finos.legend.sdlc.domain.model.version.Version)2 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)2 ElementType (java.lang.annotation.ElementType)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Target (java.lang.annotation.Target)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumMap (java.util.EnumMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1