Search in sources :

Example 1 with RepositoryApi

use of org.gitlab4j.api.RepositoryApi 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 RepositoryApi

use of org.gitlab4j.api.RepositoryApi 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 RepositoryApi

use of org.gitlab4j.api.RepositoryApi 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 RepositoryApi

use of org.gitlab4j.api.RepositoryApi in project legend-sdlc by finos.

the class GitLabReviewApi method getMergeRequestTargetRevision.

private String getMergeRequestTargetRevision(GitLabProjectId projectId, GitLabApi gitLabApi, MergeRequest mergeRequest) {
    RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
    Branch targetBranch;
    try {
        targetBranch = withRetries(() -> repositoryApi.getBranch(projectId.getGitLabId(), mergeRequest.getTargetBranch()));
    } catch (Exception e) {
        LOGGER.error("Error getting target branch head for merge request {} in project {} (target branch: {})", mergeRequest.getIid(), projectId, mergeRequest.getTargetBranch(), e);
        StringBuilder builder = new StringBuilder("Error getting target revision for review ").append(mergeRequest.getIid()).append(" for project ").append(projectId);
        StringTools.appendThrowableMessageIfPresent(builder, e);
        throw new LegendSDLCServerException(builder.toString(), e);
    }
    Commit targetHead = targetBranch.getCommit();
    if ((targetHead == null) || (targetHead.getId() == null)) {
        LOGGER.error("Error getting target branch head for merge request {} in project {} (target branch: {}): {}", mergeRequest.getIid(), projectId, mergeRequest.getTargetBranch(), targetHead);
        throw new LegendSDLCServerException("Error getting target revision for review " + mergeRequest.getIid() + " for project");
    }
    return targetHead.getId();
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Commit(org.gitlab4j.api.models.Commit) Branch(org.gitlab4j.api.models.Branch) RepositoryApi(org.gitlab4j.api.RepositoryApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 5 with RepositoryApi

use of org.gitlab4j.api.RepositoryApi in project legend-sdlc by finos.

the class GitLabWorkspaceApi method attemptToRebaseWorkspaceUsingTemporaryBranch.

/**
 * This method attempts to rebase the workspace branch on top of master by using a temp branch. Detailed procedure outlined below:
 * 1. Create a new merge request (MR) that merges temp branch into master branch so that we can use gitlab rebase functionality
 * 2. Call rebase.
 * 3. Continuously check the rebase status of the merge request:
 * - If failed -> return `false`
 * - If succeeded, proceed
 * 4. Re-create workspace branch on top of the rebased temp branch.
 * 5. Cleanup: remove the temp branch and the MR
 * 6. Return `true`
 *
 * @return a boolean flag indicating if the attempted rebase succeeded.
 */
private boolean attemptToRebaseWorkspaceUsingTemporaryBranch(String projectId, String workspaceId, WorkspaceType workspaceType, String tempBranchName, String masterRevisionId) {
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
    RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
    // Create merge request to rebase
    MergeRequestApi mergeRequestApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi();
    String title = "Update workspace " + workspaceId;
    String message = "Update workspace " + workspaceId + " up to revision " + masterRevisionId;
    MergeRequest mergeRequest;
    try {
        mergeRequest = mergeRequestApi.createMergeRequest(gitLabProjectId.getGitLabId(), tempBranchName, MASTER_BRANCH, title, message, null, null, null, null, false, false);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create merge request in project " + projectId, () -> "Unknown branch in project " + projectId + ": " + tempBranchName, () -> "Error creating merge request in project " + projectId);
    }
    // Attempt to rebase the merge request
    try {
        mergeRequestApi.rebaseMergeRequest(gitLabProjectId.getGitLabId(), mergeRequest.getIid());
        // Check rebase status
        // This only throws when we have 403, so we need to keep polling till we know the result
        // See https://docs.gitlab.com/ee/api/merge_requests.html#rebase-a-merge-request
        CallUntil<MergeRequest, GitLabApiException> rebaseStatusCallUntil = CallUntil.callUntil(() -> withRetries(() -> mergeRequestApi.getRebaseStatus(gitLabProjectId.getGitLabId(), mergeRequest.getIid())), mr -> !mr.getRebaseInProgress(), 600, 1000L);
        if (!rebaseStatusCallUntil.succeeded()) {
            LOGGER.warn("Timeout waiting for merge request " + mergeRequest.getIid() + " in project " + projectId + " to finish rebasing");
            return false;
        }
        // Check if there is merge conflict
        if (rebaseStatusCallUntil.getResult().getMergeError() != null) {
            return false;
        } else // if there are no merge conflicts, proceed with the update
        {
            // Create backup branch
            Branch backupBranch;
            ProjectFileAccessProvider.WorkspaceAccessType backupWorkspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.BACKUP;
            ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
            try {
                backupBranch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceAccessType), getWorkspaceBranchName(workspaceId, workspaceType, workspaceAccessType), 30, 1_000);
            } catch (Exception e) {
                throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create " + workspaceType.getLabel() + " " + backupWorkspaceAccessType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating " + workspaceType.getLabel() + " " + backupWorkspaceAccessType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
            }
            if (backupBranch == null) {
                throw new LegendSDLCServerException("Failed to create " + workspaceType.getLabel() + " " + backupWorkspaceAccessType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " from " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
            }
            // Delete original branch
            boolean originalBranchDeleted;
            try {
                originalBranchDeleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, workspaceAccessType), 20, 1_000);
            } catch (Exception e) {
                throw buildException(e, () -> "Error while attempting to update the workspace " + workspaceId + " in project " + projectId + ": user " + getCurrentUser() + " is not allowed to delete workspace", () -> "Error while attempting to update the workspace " + workspaceId + " in project " + projectId + ": unknown workspace or project", () -> "Error while attempting to update the workspace " + workspaceId + " in project " + projectId + ": error deleting workspace");
            }
            if (!originalBranchDeleted) {
                throw new LegendSDLCServerException("Failed to delete " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
            }
            // Create new workspace branch off the temp branch head
            Branch newWorkspaceBranch;
            try {
                newWorkspaceBranch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, workspaceAccessType), tempBranchName, 30, 1_000);
            } catch (Exception e) {
                throw buildException(e, () -> "Error while attempting to update the workspace " + workspaceId + " in project " + projectId + ": user " + getCurrentUser() + " is not allowed to create workspace", () -> "Error while attempting to update the workspace " + workspaceId + " in project " + projectId + ": unknown project: " + projectId, () -> "Error while attempting to update the workspace " + workspaceId + " in project " + projectId + ": error creating workspace");
            }
            if (newWorkspaceBranch == null) {
                throw new LegendSDLCServerException("Failed to create " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " from temporary workspace " + tempBranchName + " in project " + projectId);
            }
            // Delete backup branch
            try {
                boolean deleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceAccessType), 20, 1_000);
                if (!deleted) {
                    LOGGER.error("Failed to delete {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceAccessType.getLabel() + " " + workspaceAccessType.getLabel(), workspaceId, projectId);
                }
            } catch (Exception e) {
                // unfortunate, but this should not throw error
                LOGGER.error("Error deleting {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceAccessType.getLabel() + " " + workspaceAccessType.getLabel(), workspaceId, projectId);
            }
        }
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to rebase merge request " + mergeRequest.getIid() + " in project " + projectId, () -> "Unknown merge request ( " + mergeRequest.getIid() + " ) or project ( " + projectId + " )", () -> "Error rebasing merge request " + mergeRequest.getIid() + " in project " + projectId);
    } finally {
        // Try to close merge request
        try {
            mergeRequestApi.updateMergeRequest(gitLabProjectId.getGitLabId(), mergeRequest.getIid(), null, title, null, null, StateEvent.CLOSE, null, null, null, null, null, null);
        } catch (Exception closeEx) {
            // if we fail, log the error but we don't throw it
            LOGGER.error("Could not close merge request {} for project {}: {}", mergeRequest.getIid(), projectId, mergeRequest.getWebUrl(), closeEx);
        }
        // Delete temporary branch in the background
        submitBackgroundRetryableTask(() -> waitForPipelinesDeleteBranchAndVerify(gitLabApi, gitLabProjectId, tempBranchName), 5000L, "delete " + tempBranchName);
    }
    return true;
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) GitLabApiException(org.gitlab4j.api.GitLabApiException) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) 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) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) RepositoryApi(org.gitlab4j.api.RepositoryApi)

Aggregations

RepositoryApi (org.gitlab4j.api.RepositoryApi)24 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)22 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)19 GitLabApiException (org.gitlab4j.api.GitLabApiException)17 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)15 Branch (org.gitlab4j.api.models.Branch)12 MergeRequest (org.gitlab4j.api.models.MergeRequest)8 Project (org.finos.legend.sdlc.domain.model.project.Project)5 ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)5 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)5 List (java.util.List)4 Entity (org.finos.legend.sdlc.domain.model.entity.Entity)4 ProjectType (org.finos.legend.sdlc.domain.model.project.ProjectType)4 Workspace (org.finos.legend.sdlc.domain.model.project.workspace.Workspace)4 Review (org.finos.legend.sdlc.domain.model.review.Review)4 CommitsApi (org.gitlab4j.api.CommitsApi)4 GitLabApi (org.gitlab4j.api.GitLabApi)4 Commit (org.gitlab4j.api.models.Commit)4 DiffRef (org.gitlab4j.api.models.DiffRef)3 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)2