Search in sources :

Example 21 with Revision

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

the class TestWorkspaceEntitiesResource method testGetAndUpdateUserWorkspaceEntity.

@Test
public void testGetAndUpdateUserWorkspaceEntity() throws HttpResponseException {
    String projectId = "A";
    String workspaceOneId = "entityw3";
    String entityOneName = "testentityone";
    String entityPackageName = "testpkg";
    this.backend.project(projectId).addEntities(workspaceOneId, InMemoryEntity.newEntity(entityOneName, entityPackageName));
    Response responseOne = this.clientFor("/api/projects/A/workspaces/entityw3").request().get();
    if (responseOne.getStatus() != 200) {
        throw new HttpResponseException(responseOne.getStatus(), "Error during getting user workspace with status: " + responseOne.getStatus() + ", entity: " + responseOne.readEntity(String.class));
    }
    Workspace workspace = responseOne.readEntity(new GenericType<Workspace>() {
    });
    Assert.assertNotNull(workspace);
    Assert.assertEquals(workspaceOneId, workspace.getWorkspaceId());
    Assert.assertEquals(projectId, workspace.getProjectId());
    Response responseTwo = this.clientFor("/api/projects/A/workspaces/entityw3/entities/testpkg::testentityone").request().get();
    if (responseTwo.getStatus() != 200) {
        throw new HttpResponseException(responseTwo.getStatus(), "Error during getting entity in user workspace with status: " + responseTwo.getStatus() + ", entity: " + responseTwo.readEntity(String.class));
    }
    Entity entity = responseTwo.readEntity(new GenericType<Entity>() {
    });
    Assert.assertNotNull(entity);
    Assert.assertEquals(entityPackageName + "::" + entityOneName, entity.getPath());
    Assert.assertEquals(entityPackageName, entity.getContent().get("package"));
    Assert.assertEquals(entityOneName, entity.getContent().get("name"));
    UpdateEntitiesCommand updateEntitiesCommand = new UpdateEntitiesCommand();
    updateEntitiesCommand.setReplace(true);
    Response responseThree = this.clientFor("/api/projects/A/workspaces/entityw3/entities").request().post(javax.ws.rs.client.Entity.entity(updateEntitiesCommand, MediaType.APPLICATION_JSON));
    if (responseThree.getStatus() != 200) {
        throw new HttpResponseException(responseThree.getStatus(), "Error during updating entity in user workspace with status: " + responseThree.getStatus() + ", entity: " + responseThree.readEntity(String.class));
    }
    Revision revision = responseThree.readEntity(new GenericType<Revision>() {
    });
    Assert.assertNotNull(revision);
    Assert.assertNotNull(revision.getId());
}
Also used : Response(javax.ws.rs.core.Response) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) InMemoryEntity(org.finos.legend.sdlc.server.inmemory.domain.api.InMemoryEntity) UpdateEntitiesCommand(org.finos.legend.sdlc.server.application.entity.UpdateEntitiesCommand) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) HttpResponseException(org.apache.http.client.HttpResponseException) Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace) Test(org.junit.Test)

Example 22 with Revision

use of org.finos.legend.sdlc.domain.model.revision.Revision 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 23 with Revision

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

the class BaseGitLabApi method resolveRevisionId.

protected static String resolveRevisionId(String revisionId, ProjectFileAccessProvider.RevisionAccessContext revisionAccessContext) {
    Revision revision;
    if (revisionId == null) {
        throw new IllegalArgumentException("Resolving revision alias does not work with null revisionId, null handling must be done before using this method");
    }
    RevisionAlias revisionAlias = getRevisionAlias(revisionId);
    switch(revisionAlias) {
        case BASE:
            {
                revision = revisionAccessContext.getBaseRevision();
                return revision == null ? null : revision.getId();
            }
        case HEAD:
            {
                revision = revisionAccessContext.getCurrentRevision();
                return revision == null ? null : revision.getId();
            }
        case REVISION_ID:
            {
                return revisionId;
            }
        default:
            {
                throw new IllegalArgumentException("Unknown revision alias type " + revisionAlias);
            }
    }
}
Also used : Revision(org.finos.legend.sdlc.domain.model.revision.Revision) RevisionAlias(org.finos.legend.sdlc.domain.model.revision.RevisionAlias)

Example 24 with Revision

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

the class GitLabApiWithFileAccess method fromGitLabCommit.

protected static Revision fromGitLabCommit(Commit commit) {
    if (commit == null) {
        return null;
    }
    String id = commit.getId();
    String authorName = commit.getAuthorName();
    Instant authoredTimestamp = toInstantIfNotNull(commit.getAuthoredDate());
    String committerName = commit.getCommitterName();
    Instant committedTimestamp = toInstantIfNotNull(commit.getCommittedDate());
    String message = commit.getMessage();
    return new Revision() {

        @Override
        public String getId() {
            return id;
        }

        @Override
        public String getAuthorName() {
            return authorName;
        }

        @Override
        public Instant getAuthoredTimestamp() {
            return authoredTimestamp;
        }

        @Override
        public String getCommitterName() {
            return committerName;
        }

        @Override
        public Instant getCommittedTimestamp() {
            return committedTimestamp;
        }

        @Override
        public String getMessage() {
            return message;
        }
    };
}
Also used : Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Instant(java.time.Instant)

Example 25 with Revision

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

the class GitLabEntityApi method updateEntities.

private Revision updateEntities(String projectId, String workspaceId, Iterable<? extends Entity> newEntities, boolean replace, String message, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType) {
    Map<String, Entity> newEntityDefinitions = Maps.mutable.empty();
    List<String> errorMessages = Lists.mutable.empty();
    for (Entity entity : newEntities) {
        if (entity == null) {
            errorMessages.add("Invalid entity: null");
            continue;
        }
        String path = entity.getPath();
        if (!isValidEntityPath(path)) {
            errorMessages.add("Invalid entity path: \"" + path + "\"");
            continue;
        }
        String classifierPath = entity.getClassifierPath();
        if (!isValidClassifierPath(classifierPath)) {
            errorMessages.add("Entity: " + path + "; error: invalid classifier path \"" + classifierPath + "\"");
        }
        Map<String, ?> content = entity.getContent();
        if (content == null) {
            errorMessages.add("Entity: " + path + "; error: missing content");
        } else if (path != null) {
            Object pkg = content.get("package");
            Object name = content.get("name");
            if (!(pkg instanceof String) || !(name instanceof String) || !path.equals(pkg + "::" + name)) {
                StringBuilder builder = new StringBuilder("Entity: ").append(path).append("; mismatch between entity path and package (");
                if (pkg instanceof String) {
                    builder.append('"').append(pkg).append('"');
                } else {
                    builder.append(pkg);
                }
                builder.append(") and name (");
                if (name instanceof String) {
                    builder.append('"').append(name).append('"');
                } else {
                    builder.append(name);
                }
                builder.append(") properties");
                errorMessages.add(builder.toString());
            }
        }
        Entity oldDefinition = newEntityDefinitions.put(path, entity);
        if (oldDefinition != null) {
            errorMessages.add("Entity: " + path + "; error: multiple definitions");
        }
    }
    if (!errorMessages.isEmpty()) {
        throw new LegendSDLCServerException((errorMessages.size() == 1) ? errorMessages.get(0) : "There are errors with entity definitions:\n\t" + String.join("\n\t", errorMessages), Status.BAD_REQUEST);
    }
    Revision currentWorkspaceRevision = getProjectFileAccessProvider().getRevisionAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType).getCurrentRevision();
    if (currentWorkspaceRevision == null) {
        throw new LegendSDLCServerException("Could not find current revision for " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId + ": " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " may be corrupt");
    }
    String revisionId = currentWorkspaceRevision.getId();
    LOGGER.debug("Using revision {} for reference in entity update in {} {} in project {}", revisionId, workspaceType.getLabel() + " " + workspaceAccessType.getLabel(), workspaceId, projectId);
    List<EntityChange> entityChanges = Lists.mutable.ofInitialCapacity(newEntityDefinitions.size());
    if (newEntityDefinitions.isEmpty()) {
        if (replace) {
            try (Stream<EntityProjectFile> stream = getEntityProjectFiles(projectId, workspaceId, revisionId, workspaceType, workspaceAccessType)) {
                stream.map(EntityProjectFile::getEntityPath).map(EntityChange::newDeleteEntity).forEach(entityChanges::add);
            }
        }
    } else {
        try (Stream<EntityProjectFile> stream = getEntityProjectFiles(projectId, workspaceId, revisionId, workspaceType, workspaceAccessType)) {
            stream.forEach(epf -> {
                String path = epf.getEntityPath();
                Entity newDefinition = newEntityDefinitions.remove(path);
                if (newDefinition == null) {
                    if (replace) {
                        entityChanges.add(EntityChange.newDeleteEntity(path));
                    }
                } else {
                    Entity entity = epf.getEntity();
                    String newClassifierPath = newDefinition.getClassifierPath();
                    Map<String, ?> newContent = newDefinition.getContent();
                    if (!newClassifierPath.equals(entity.getClassifierPath()) || !newContent.equals(entity.getContent())) {
                        entityChanges.add(EntityChange.newModifyEntity(path, newClassifierPath, newContent));
                    }
                }
            });
        }
        newEntityDefinitions.values().forEach(definition -> entityChanges.add(EntityChange.newCreateEntity(definition.getPath(), definition.getClassifierPath(), definition.getContent())));
    }
    return performChanges(projectId, workspaceId, revisionId, message, entityChanges, workspaceType, workspaceAccessType);
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) EntityChange(org.finos.legend.sdlc.domain.model.entity.change.EntityChange) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Revision(org.finos.legend.sdlc.domain.model.revision.Revision)

Aggregations

Revision (org.finos.legend.sdlc.domain.model.revision.Revision)26 Entity (org.finos.legend.sdlc.domain.model.entity.Entity)15 ProjectConfiguration (org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)10 Workspace (org.finos.legend.sdlc.domain.model.project.workspace.Workspace)9 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)7 Project (org.finos.legend.sdlc.domain.model.project.Project)6 ProjectType (org.finos.legend.sdlc.domain.model.project.ProjectType)6 Instant (java.time.Instant)5 List (java.util.List)5 Test (org.junit.Test)5 Response (javax.ws.rs.core.Response)4 HttpResponseException (org.apache.http.client.HttpResponseException)4 Collections (java.util.Collections)3 Comparator (java.util.Comparator)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 Lists (org.eclipse.collections.api.factory.Lists)3 Iterate (org.eclipse.collections.impl.utility.Iterate)3 ArtifactGeneration (org.finos.legend.sdlc.domain.model.project.configuration.ArtifactGeneration)3 MetamodelDependency (org.finos.legend.sdlc.domain.model.project.configuration.MetamodelDependency)3