Search in sources :

Example 1 with Version

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

the class GitLabVersionApi method getVersions.

private Stream<Version> getVersions(GitLabProjectId projectId, Integer minMajorVersion, Integer maxMajorVersion, Integer minMinorVersion, Integer maxMinorVersion, Integer minPatchVersion, Integer maxPatchVersion) {
    switch(getProjectTypeFromMode(projectId.getGitLabMode())) {
        case PROTOTYPE:
            {
                return Stream.empty();
            }
        case PRODUCTION:
            {
                try {
                    Stream<Version> stream = PagerTools.stream(getGitLabApi(projectId.getGitLabMode()).getTagsApi().getTags(projectId.getGitLabId(), ITEMS_PER_PAGE)).filter(GitLabVersionApi::isVersionTag).map(tag -> fromGitLabTag(projectId.toString(), tag));
                    // major version constraint
                    if ((minMajorVersion != null) && (maxMajorVersion != null)) {
                        int minMajorVersionInt = minMajorVersion;
                        int maxMajorVersionInt = maxMajorVersion;
                        if (minMajorVersionInt == maxMajorVersionInt) {
                            stream = stream.filter(v -> v.getId().getMajorVersion() == minMajorVersionInt);
                        } else {
                            stream = stream.filter(v -> {
                                int majorVersion = v.getId().getMajorVersion();
                                return (minMajorVersionInt <= majorVersion) && (majorVersion <= maxMajorVersionInt);
                            });
                        }
                    } else if (minMajorVersion != null) {
                        int minMajorVersionInt = minMajorVersion;
                        stream = stream.filter(v -> v.getId().getMajorVersion() >= minMajorVersionInt);
                    } else if (maxMajorVersion != null) {
                        int maxMajorVersionInt = maxMajorVersion;
                        stream = stream.filter(v -> v.getId().getMajorVersion() <= maxMajorVersionInt);
                    }
                    // minor version constraint
                    if ((minMinorVersion != null) && (maxMinorVersion != null)) {
                        int minMinorVersionInt = minMinorVersion;
                        int maxMinorVersionInt = maxMinorVersion;
                        if (minMinorVersionInt == maxMinorVersionInt) {
                            stream = stream.filter(v -> v.getId().getMinorVersion() == minMinorVersionInt);
                        } else {
                            stream = stream.filter(v -> {
                                int minorVersion = v.getId().getMinorVersion();
                                return (minMinorVersionInt <= minorVersion) && (minorVersion <= maxMinorVersionInt);
                            });
                        }
                    } else if (minMinorVersion != null) {
                        int minMinorVersionInt = minMinorVersion;
                        stream = stream.filter(v -> v.getId().getMinorVersion() >= minMinorVersionInt);
                    } else if (maxMinorVersion != null) {
                        int maxMinorVersionInt = maxMinorVersion;
                        stream = stream.filter(v -> v.getId().getMinorVersion() <= maxMinorVersionInt);
                    }
                    // patch version constraint
                    if ((minPatchVersion != null) && (maxPatchVersion != null)) {
                        int minPatchVersionInt = minPatchVersion;
                        int maxPatchVersionInt = maxPatchVersion;
                        if (minPatchVersionInt == maxPatchVersionInt) {
                            stream = stream.filter(v -> v.getId().getPatchVersion() == minPatchVersionInt);
                        } else {
                            stream = stream.filter(v -> {
                                int patchVersion = v.getId().getPatchVersion();
                                return (minPatchVersionInt <= patchVersion) && (patchVersion <= maxPatchVersionInt);
                            });
                        }
                    } else if (minPatchVersion != null) {
                        int minPatchVersionInt = minPatchVersion;
                        stream = stream.filter(v -> v.getId().getPatchVersion() >= minPatchVersionInt);
                    } else if (maxPatchVersion != null) {
                        int maxPatchVersionInt = maxPatchVersion;
                        stream = stream.filter(v -> v.getId().getPatchVersion() <= maxPatchVersionInt);
                    }
                    return stream;
                } catch (Exception e) {
                    throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get versions for project " + projectId, () -> "Unknown project: " + projectId, () -> "Error getting versions for project " + projectId);
                }
            }
        default:
            {
                throw new LegendSDLCServerException("Unknown project: " + projectId, Status.BAD_REQUEST);
            }
    }
}
Also used : CommitsApi(org.gitlab4j.api.CommitsApi) NewVersionType(org.finos.legend.sdlc.server.domain.api.version.NewVersionType) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) CommitRef(org.gitlab4j.api.models.CommitRef) Inject(javax.inject.Inject) GitLabApiTools(org.finos.legend.sdlc.server.gitlab.tools.GitLabApiTools) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Tag(org.gitlab4j.api.models.Tag) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) BackgroundTaskProcessor(org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor) VersionApi(org.finos.legend.sdlc.server.domain.api.version.VersionApi) Status(javax.ws.rs.core.Response.Status) Commit(org.gitlab4j.api.models.Commit) VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) Pager(org.gitlab4j.api.Pager) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Version(org.finos.legend.sdlc.domain.model.version.Version) RefType(org.gitlab4j.api.models.CommitRef.RefType) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) List(java.util.List) Stream(java.util.stream.Stream) GitLabApiException(org.gitlab4j.api.GitLabApiException) Comparator(java.util.Comparator) Collections(java.util.Collections) GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Stream(java.util.stream.Stream) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 2 with Version

use of org.finos.legend.sdlc.domain.model.version.Version 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 3 with Version

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

the class GitLabRevisionApi method getRevisionStatus.

@Override
public RevisionStatus getRevisionStatus(String projectId, String revisionId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(revisionId, "revisionId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    try {
        GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
        CommitsApi commitsApi = gitLabApi.getCommitsApi();
        Revision revision = getProjectRevisionContext(projectId).getRevision(revisionId);
        Pager<CommitRef> commitRefPager = withRetries(() -> commitsApi.getCommitRefs(gitLabProjectId.getGitLabId(), revision.getId(), RefType.ALL, ITEMS_PER_PAGE));
        List<CommitRef> commitRefs = PagerTools.stream(commitRefPager).collect(Collectors.toList());
        boolean isCommitted = commitRefs.stream().anyMatch(cr -> MASTER_BRANCH.equals(cr.getName()));
        List<Version> versions;
        List<String> versionTagNames = commitRefs.stream().filter(cr -> (RefType.TAG == cr.getType()) && isVersionTagName(cr.getName())).map(CommitRef::getName).collect(Collectors.toList());
        if (versionTagNames.isEmpty()) {
            versions = Collections.emptyList();
        } else {
            TagsApi tagsApi = gitLabApi.getTagsApi();
            versions = Lists.mutable.ofInitialCapacity(versionTagNames.size());
            for (String tagName : versionTagNames) {
                Tag tag = withRetries(() -> tagsApi.getTag(gitLabProjectId.getGitLabId(), tagName));
                versions.add(fromGitLabTag(projectId, tag));
            }
            versions.sort(Comparator.comparing(Version::getId));
        }
        List<Workspace> workspaces;
        if (isCommitted) {
            workspaces = Collections.emptyList();
        } else {
            // Note that here we will not account for conflict resolution or backup branch because in the model those are not real workspaces.
            workspaces = commitRefs.stream().filter(cr -> (RefType.BRANCH == cr.getType()) && isWorkspaceBranchName(cr.getName(), ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE)).map(cr -> fromWorkspaceBranchName(projectId, cr.getName(), WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE)).collect(Collectors.toList());
        }
        return new RevisionStatus() {

            @Override
            public Revision getRevision() {
                return revision;
            }

            @Override
            public boolean isCommitted() {
                return isCommitted;
            }

            @Override
            public List<Workspace> getWorkspaces() {
                return workspaces;
            }

            @Override
            public List<Version> getVersions() {
                return versions;
            }
        };
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access the status for revision " + revisionId + " of project " + projectId, () -> "Unknown: revision " + revisionId + " of project " + projectId, () -> "Error getting the status for revision " + revisionId + " of project " + projectId);
    }
}
Also used : Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace) CommitsApi(org.gitlab4j.api.CommitsApi) Lists(org.eclipse.collections.api.factory.Lists) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) TagsApi(org.gitlab4j.api.TagsApi) MutableList(org.eclipse.collections.api.list.MutableList) CommitRef(org.gitlab4j.api.models.CommitRef) Function(java.util.function.Function) Inject(javax.inject.Inject) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) Tag(org.gitlab4j.api.models.Tag) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) BackgroundTaskProcessor(org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor) Status(javax.ws.rs.core.Response.Status) RevisionStatus(org.finos.legend.sdlc.domain.model.revision.RevisionStatus) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) Predicate(java.util.function.Predicate) Pager(org.gitlab4j.api.Pager) Iterate(org.eclipse.collections.impl.utility.Iterate) Instant(java.time.Instant) RevisionApi(org.finos.legend.sdlc.server.domain.api.revision.RevisionApi) Collectors(java.util.stream.Collectors) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) Version(org.finos.legend.sdlc.domain.model.version.Version) RefType(org.gitlab4j.api.models.CommitRef.RefType) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) List(java.util.List) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) RevisionAccessContext(org.finos.legend.sdlc.server.domain.api.revision.RevisionAccessContext) ProjectPaths(org.finos.legend.sdlc.server.project.ProjectPaths) ListIterable(org.eclipse.collections.api.list.ListIterable) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) Collections(java.util.Collections) GitLabApi(org.gitlab4j.api.GitLabApi) GitLabApi(org.gitlab4j.api.GitLabApi) CommitRef(org.gitlab4j.api.models.CommitRef) CommitsApi(org.gitlab4j.api.CommitsApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) RevisionStatus(org.finos.legend.sdlc.domain.model.revision.RevisionStatus) TagsApi(org.gitlab4j.api.TagsApi) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Version(org.finos.legend.sdlc.domain.model.version.Version) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Tag(org.gitlab4j.api.models.Tag) Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace)

Example 4 with Version

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

the class GitLabApiWithFileAccess method fromGitLabTag.

protected static Version fromGitLabTag(String projectId, Tag tag) {
    if (tag == null) {
        return null;
    }
    VersionId versionId = parseVersionTagName(tag.getName());
    String revisionId = tag.getCommit().getId();
    String notes = applyIfNotNull(Release::getDescription, tag.getRelease());
    return new Version() {

        @Override
        public VersionId getId() {
            return versionId;
        }

        @Override
        public String getProjectId() {
            return projectId;
        }

        @Override
        public String getRevisionId() {
            return revisionId;
        }

        @Override
        public String getNotes() {
            return notes;
        }
    };
}
Also used : VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) Version(org.finos.legend.sdlc.domain.model.version.Version) Release(org.gitlab4j.api.models.Release)

Aggregations

Version (org.finos.legend.sdlc.domain.model.version.Version)4 VersionId (org.finos.legend.sdlc.domain.model.version.VersionId)3 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)3 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)3 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Status (javax.ws.rs.core.Response.Status)2 GitLabUserContext (org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext)2 PagerTools (org.finos.legend.sdlc.server.gitlab.tools.PagerTools)2 BackgroundTaskProcessor (org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor)2 CommitsApi (org.gitlab4j.api.CommitsApi)2 GitLabApi (org.gitlab4j.api.GitLabApi)2 Pager (org.gitlab4j.api.Pager)2 CommitRef (org.gitlab4j.api.models.CommitRef)2 RefType (org.gitlab4j.api.models.CommitRef.RefType)2 Tag (org.gitlab4j.api.models.Tag)2 Instant (java.time.Instant)1