Search in sources :

Example 1 with GitLabProjectId

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

the class GitLabEntityApiTestResource method runEntitiesInNormalUserWorkspaceWorkflowTest.

public void runEntitiesInNormalUserWorkspaceWorkflowTest() throws GitLabApiException {
    String projectName = "CommitFlowTestProject";
    String description = "A test project.";
    ProjectType projectType = ProjectType.PRODUCTION;
    String groupId = "org.finos.sdlc.test";
    String artifactId = "entitytestproj";
    List<String> tags = Lists.mutable.with("doe", "moffitt", AbstractGitLabServerApiTest.INTEGRATION_TEST_PROJECT_TAG);
    String workspaceName = "entitytestworkspace";
    Project createdProject = gitLabProjectApi.createProject(projectName, description, projectType, groupId, artifactId, tags);
    String projectId = createdProject.getProjectId();
    Workspace createdWorkspace = gitLabWorkspaceApi.newUserWorkspace(projectId, workspaceName);
    String workspaceId = createdWorkspace.getWorkspaceId();
    List<Entity> initialWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    List<Entity> initialProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertEquals(Collections.emptyList(), initialWorkspaceEntities);
    Assert.assertEquals(Collections.emptyList(), initialProjectEntities);
    String entityPath = "test::entity";
    String classifierPath = "meta::test::mathematicsDepartment";
    Map<String, String> entityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-113", "abstract-algebra", "math-185", "complex-analysis");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPath, classifierPath, entityContentMap, "initial entity");
    List<Entity> modifiedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    List<Entity> modifiedProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertNotNull(modifiedWorkspaceEntities);
    Assert.assertEquals(Collections.emptyList(), modifiedProjectEntities);
    Assert.assertEquals(1, modifiedWorkspaceEntities.size());
    Entity initalEntity = modifiedWorkspaceEntities.get(0);
    Assert.assertEquals(initalEntity.getPath(), entityPath);
    Assert.assertEquals(initalEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(initalEntity.getContent(), entityContentMap);
    Map<String, String> newEntityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-128", "numerical-analysis", "math-110", "linear-algebra");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).updateEntity(entityPath, classifierPath, newEntityContentMap, "update entity");
    List<Entity> updatedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(updatedWorkspaceEntities);
    Assert.assertEquals(1, updatedWorkspaceEntities.size());
    Entity updatedEntity = updatedWorkspaceEntities.get(0);
    Assert.assertEquals(updatedEntity.getPath(), entityPath);
    Assert.assertEquals(updatedEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(updatedEntity.getContent(), newEntityContentMap);
    String entityPathTwo = "testtwo::entitytwo";
    String classifierPathTwo = "meta::test::csDepartment";
    Map<String, String> newEntityContentMapTwo = Maps.mutable.with("package", "testtwo", "name", "entitytwo", "cs-194", "computational-imaging", "cs-189", "machine-learning");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPathTwo, classifierPathTwo, newEntityContentMapTwo, "second entity");
    List<Entity> postAddWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(postAddWorkspaceEntities);
    Assert.assertEquals(2, postAddWorkspaceEntities.size());
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).deleteEntity(entityPath, classifierPath);
    List<Entity> postDeleteWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(postDeleteWorkspaceEntities);
    Assert.assertEquals(1, postDeleteWorkspaceEntities.size());
    Entity remainedEntity = postDeleteWorkspaceEntities.get(0);
    Assert.assertEquals(remainedEntity.getPath(), entityPathTwo);
    Assert.assertEquals(remainedEntity.getClassifierPath(), classifierPathTwo);
    Assert.assertEquals(remainedEntity.getContent(), newEntityContentMapTwo);
    List<String> paths = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntityPaths(null, null, null);
    Assert.assertNotNull(paths);
    Assert.assertEquals(1, paths.size());
    Assert.assertEquals(entityPathTwo, paths.get(0));
    List<String> labels = Collections.singletonList("default");
    Review testReview = gitLabCommitterReviewApi.createReview(projectId, workspaceId, WorkspaceType.USER, "Add Courses.", "add two courses", labels);
    String reviewId = testReview.getId();
    Review approvedReview = gitLabApproverReviewApi.approveReview(projectId, reviewId);
    Assert.assertNotNull(approvedReview);
    Assert.assertEquals(reviewId, approvedReview.getId());
    Assert.assertEquals(ReviewState.OPEN, approvedReview.getState());
    Assert.assertEquals(labels, approvedReview.getLabels());
    GitLabProjectId sdlcGitLabProjectId = GitLabProjectId.parseProjectId(projectId);
    MergeRequestApi mergeRequestApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getMergeRequestApi();
    int parsedMergeRequestId = Integer.parseInt(reviewId);
    int gitlabProjectId = sdlcGitLabProjectId.getGitLabId();
    String requiredStatus = "can_be_merged";
    CallUntil<MergeRequest, GitLabApiException> callUntil = CallUntil.callUntil(() -> mergeRequestApi.getMergeRequest(gitlabProjectId, parsedMergeRequestId), mr -> requiredStatus.equals(mr.getMergeStatus()), 20, 1000);
    if (!callUntil.succeeded()) {
        throw new RuntimeException("Merge request " + approvedReview.getId() + " still does not have status \"" + requiredStatus + "\" after " + callUntil.getTryCount() + " tries");
    }
    LOGGER.info("Waited {} times for merge to have status \"{}\"", callUntil.getTryCount(), requiredStatus);
    gitLabCommitterReviewApi.commitReview(projectId, reviewId, "add two math courses");
    String requiredMergedStatus = "merged";
    CallUntil<MergeRequest, GitLabApiException> callUntilMerged = CallUntil.callUntil(() -> mergeRequestApi.getMergeRequest(gitlabProjectId, parsedMergeRequestId), mr -> requiredMergedStatus.equals(mr.getState()), 10, 500);
    if (!callUntilMerged.succeeded()) {
        throw new RuntimeException("Merge request " + reviewId + " still does not have state \"" + requiredMergedStatus + "\" after " + callUntilMerged.getTryCount() + " tries");
    }
    LOGGER.info("Waited {} times for merge request to have state \"{}\"", callUntilMerged.getTryCount(), requiredMergedStatus);
    RepositoryApi repositoryApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getRepositoryApi();
    CallUntil<List<Branch>, GitLabApiException> callUntilBranchDeleted = CallUntil.callUntil(() -> repositoryApi.getBranches(sdlcGitLabProjectId.getGitLabId()), GitLabEntityApiTestResource::hasOnlyMasterBranch, 15, 1000);
    if (!callUntilBranchDeleted.succeeded()) {
        // Warn instead of throwing exception since we cannot manage time expectation on GitLab to reflect branch deletion.
        LOGGER.warn("Branch is still not deleted post merge after {} tries", callUntilBranchDeleted.getTryCount());
    }
    LOGGER.info("Waited {} times for branch to be deleted post merge", callUntilBranchDeleted.getTryCount());
    List<Entity> postCommitProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertNotNull(postCommitProjectEntities);
    Assert.assertEquals(1, postCommitProjectEntities.size());
    Entity projectEntity = postCommitProjectEntities.get(0);
    Assert.assertEquals(projectEntity.getPath(), entityPathTwo);
    Assert.assertEquals(projectEntity.getClassifierPath(), classifierPathTwo);
    Assert.assertEquals(projectEntity.getContent(), newEntityContentMapTwo);
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) GitLabApiException(org.gitlab4j.api.GitLabApiException) Review(org.finos.legend.sdlc.domain.model.review.Review) Project(org.finos.legend.sdlc.domain.model.project.Project) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) RepositoryApi(org.gitlab4j.api.RepositoryApi) List(java.util.List) Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace)

Example 2 with GitLabProjectId

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

the class GitLabWorkspaceApiTestResource method runUpdateUserWorkspaceWithRebaseNoConflictTest.

public void runUpdateUserWorkspaceWithRebaseNoConflictTest() throws GitLabApiException {
    // Create new workspace from previous HEAD
    String projectName = "WorkspaceTestProjectTwo";
    String description = "A test project.";
    ProjectType projectType = ProjectType.PRODUCTION;
    String groupId = "org.finos.sdlc.test";
    String artifactId = "testworkprojtwo";
    List<String> tags = Lists.mutable.with("doe", "moffitt", AbstractGitLabServerApiTest.INTEGRATION_TEST_PROJECT_TAG);
    String workspaceName = "workspaceone";
    Project createdProject = gitLabProjectApi.createProject(projectName, description, projectType, groupId, artifactId, tags);
    String projectId = createdProject.getProjectId();
    Workspace createdWorkspace = gitLabWorkspaceApi.newUserWorkspace(projectId, workspaceName);
    String workspaceId = createdWorkspace.getWorkspaceId();
    List<Entity> initialWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    List<Entity> initialProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertEquals(Collections.emptyList(), initialWorkspaceEntities);
    Assert.assertEquals(Collections.emptyList(), initialProjectEntities);
    // Create another workspace, commit, review, merge to move project HEAD forward -- use workspace two
    String workspaceTwoName = "workspacetwo";
    Workspace createdWorkspaceTwo = gitLabWorkspaceApi.newUserWorkspace(projectId, workspaceTwoName);
    String workspaceTwoId = createdWorkspaceTwo.getWorkspaceId();
    List<Entity> initialWorkspaceTwoEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceTwoId).getEntities(null, null, null);
    Assert.assertEquals(Collections.emptyList(), initialWorkspaceTwoEntities);
    String entityPath = "test::entity";
    String classifierPath = "meta::test::mathematicsDepartment";
    Map<String, String> entityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-113", "abstract-algebra", "math-185", "complex-analysis");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceTwoId).createEntity(entityPath, classifierPath, entityContentMap, "initial entity");
    List<Entity> modifiedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceTwoId).getEntities(null, null, null);
    Assert.assertNotNull(modifiedWorkspaceEntities);
    Assert.assertEquals(1, modifiedWorkspaceEntities.size());
    Entity initalEntity = modifiedWorkspaceEntities.get(0);
    Assert.assertEquals(initalEntity.getPath(), entityPath);
    Assert.assertEquals(initalEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(initalEntity.getContent(), entityContentMap);
    List<String> labels = Collections.singletonList("default");
    Review testReview = gitLabCommitterReviewApi.createReview(projectId, workspaceTwoId, WorkspaceType.USER, "Add Courses.", "add two math courses", labels);
    String reviewId = testReview.getId();
    Review approvedReview = gitLabApproverReviewApi.approveReview(projectId, reviewId);
    Assert.assertNotNull(approvedReview);
    Assert.assertEquals(reviewId, approvedReview.getId());
    Assert.assertEquals(ReviewState.OPEN, approvedReview.getState());
    GitLabProjectId sdlcGitLabProjectId = GitLabProjectId.parseProjectId(projectId);
    MergeRequestApi mergeRequestApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getMergeRequestApi();
    Integer parsedMergeRequestId = Integer.parseInt(reviewId);
    Integer gitlabProjectId = sdlcGitLabProjectId.getGitLabId();
    String requiredStatus = "can_be_merged";
    CallUntil<MergeRequest, GitLabApiException> callUntil = CallUntil.callUntil(() -> mergeRequestApi.getMergeRequest(gitlabProjectId, parsedMergeRequestId), mr -> requiredStatus.equals(mr.getMergeStatus()), 20, 1000);
    if (!callUntil.succeeded()) {
        throw new RuntimeException("Merge request " + approvedReview.getId() + " still does not have status \"" + requiredStatus + "\" after " + callUntil.getTryCount() + " tries");
    }
    LOGGER.info("Waited {} times for merge to have status \"{}\"", callUntil.getTryCount(), requiredStatus);
    gitLabCommitterReviewApi.commitReview(projectId, reviewId, "add two math courses");
    String requiredMergedStatus = "merged";
    CallUntil<MergeRequest, GitLabApiException> callUntilMerged = CallUntil.callUntil(() -> mergeRequestApi.getMergeRequest(gitlabProjectId, parsedMergeRequestId), mr -> requiredMergedStatus.equals(mr.getState()), 10, 500);
    if (!callUntilMerged.succeeded()) {
        throw new RuntimeException("Merge request " + reviewId + " still does not have state \"" + requiredMergedStatus + "\" after " + callUntilMerged.getTryCount() + " tries");
    }
    LOGGER.info("Waited {} times for merge request to have state \"{}\"", callUntilMerged.getTryCount(), requiredMergedStatus);
    RepositoryApi repositoryApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getRepositoryApi();
    CallUntil<List<Branch>, GitLabApiException> callUntilBranchDeleted = CallUntil.callUntil(() -> repositoryApi.getBranches(sdlcGitLabProjectId.getGitLabId()), branches -> GitLabApiTestSetupUtil.hasOnlyBranchesWithNames(branches, Lists.mutable.of(workspaceName, "master")), 15, 1000);
    if (!callUntilBranchDeleted.succeeded()) {
        // Warn instead of throwing exception since we cannot manage time expectation on GitLab to reflect branch deletion.
        LOGGER.warn("Branch {} is still not deleted post merge after {} tries", workspaceTwoName, callUntilBranchDeleted.getTryCount());
    }
    LOGGER.info("Waited {} times for branch {} to be deleted post merge", callUntilBranchDeleted.getTryCount(), workspaceTwoName);
    List<Entity> postCommitProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertNotNull(postCommitProjectEntities);
    Assert.assertEquals(1, postCommitProjectEntities.size());
    Entity projectEntity = postCommitProjectEntities.get(0);
    Assert.assertEquals(projectEntity.getPath(), entityPath);
    Assert.assertEquals(projectEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(projectEntity.getContent(), entityContentMap);
    // Create changes and make change in workspace branch -- use workspace
    Map<String, String> currentEntityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-113", "abstract-algebra", "math-185", "complex-analysis");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPath, classifierPath, currentEntityContentMap, "initial entity");
    List<Entity> modifiedWorkspaceEntitiesNew = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(modifiedWorkspaceEntitiesNew);
    Assert.assertEquals(1, modifiedWorkspaceEntities.size());
    Entity initalEntityNew = modifiedWorkspaceEntitiesNew.get(0);
    Assert.assertEquals(initalEntityNew.getPath(), entityPath);
    Assert.assertEquals(initalEntityNew.getClassifierPath(), classifierPath);
    Assert.assertEquals(initalEntityNew.getContent(), currentEntityContentMap);
    // Update workspace branch and trigger rebase
    gitLabWorkspaceApi.updateUserWorkspace(projectId, workspaceId);
    List<Entity> updatedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(updatedWorkspaceEntities);
    Assert.assertEquals(1, updatedWorkspaceEntities.size());
    Entity updatedEntity = updatedWorkspaceEntities.get(0);
    Assert.assertEquals(updatedEntity.getPath(), entityPath);
    Assert.assertEquals(updatedEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(updatedEntity.getContent(), currentEntityContentMap);
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) GitLabApiException(org.gitlab4j.api.GitLabApiException) Review(org.finos.legend.sdlc.domain.model.review.Review) Project(org.finos.legend.sdlc.domain.model.project.Project) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) RepositoryApi(org.gitlab4j.api.RepositoryApi) List(java.util.List) Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace)

Example 3 with GitLabProjectId

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

the class GitLabProjectApi method importProject.

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

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

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

Example 4 with GitLabProjectId

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

the class GitLabProjectApi method checkUserAuthorizationActions.

@Override
public Set<ProjectAuthorizationAction> checkUserAuthorizationActions(String id, Set<ProjectAuthorizationAction> actions) {
    try {
        GitLabProjectId projectId = parseProjectId(id);
        org.gitlab4j.api.models.Project gitLabProject = withRetries(() -> getGitLabApi(projectId.getGitLabMode()).getProjectApi().getProject(projectId.getGitLabId()));
        if (!isLegendSDLCProject(gitLabProject)) {
            throw new LegendSDLCServerException("Failed to get project " + id);
        }
        AccessLevel userLevel = getUserAccess(gitLabProject);
        if (userLevel == null) {
            return Collections.emptySet();
        }
        return actions.stream().filter(Objects::nonNull).filter(a -> checkUserAction(projectId, a, userLevel)).collect(Collectors.toSet());
    } catch (Exception e) {
        throw buildException(e, () -> "Failed to get project " + id);
    }
}
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) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Objects(java.util.Objects) AccessLevel(org.gitlab4j.api.models.AccessLevel) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException)

Example 5 with GitLabProjectId

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

the class GitLabProjectConfigurationApi method getReviewFromProjectConfiguration.

@Override
public ProjectConfiguration getReviewFromProjectConfiguration(String projectId, String reviewId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    MergeRequest mergeRequest = getReviewMergeRequest(getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi(), gitLabProjectId, reviewId);
    validateMergeRequestForComparison(mergeRequest);
    DiffRef diffRef = mergeRequest.getDiffRefs();
    if (diffRef != null && diffRef.getStartSha() != null && diffRef.getHeadSha() != null) {
        String revisionId = diffRef.getStartSha();
        return getProjectRevisionProjectConfiguration(projectId, revisionId);
    } else {
        throw new LegendSDLCServerException("Unable to get [from] revision info in project " + projectId + " for review " + reviewId);
    }
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) DiffRef(org.gitlab4j.api.models.DiffRef)

Aggregations

GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)61 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)56 GitLabApiException (org.gitlab4j.api.GitLabApiException)31 MergeRequest (org.gitlab4j.api.models.MergeRequest)29 RepositoryApi (org.gitlab4j.api.RepositoryApi)27 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)25 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)17 Branch (org.gitlab4j.api.models.Branch)17 GitLabApi (org.gitlab4j.api.GitLabApi)14 List (java.util.List)12 Project (org.finos.legend.sdlc.domain.model.project.Project)11 CommitsApi (org.gitlab4j.api.CommitsApi)9 ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)8 Collectors (java.util.stream.Collectors)7 Inject (javax.inject.Inject)7 Status (javax.ws.rs.core.Response.Status)7 ProjectType (org.finos.legend.sdlc.domain.model.project.ProjectType)7 Workspace (org.finos.legend.sdlc.domain.model.project.workspace.Workspace)7 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)7 GitLabUserContext (org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext)7