Search in sources :

Example 31 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabConflictResolutionApi method discardConflictResolution.

@Override
public void discardConflictResolution(String projectId, String workspaceId, WorkspaceType workspaceType) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    RepositoryApi repositoryApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi();
    boolean conflictResolutionBranchDeleted;
    ProjectFileAccessProvider.WorkspaceAccessType conflictResolutionWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.CONFLICT_RESOLUTION;
    try {
        conflictResolutionBranchDeleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType), 20, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to delete " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " (" + workspaceId + ") or project (" + projectId + ")", () -> "Error deleting " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    if (!conflictResolutionBranchDeleted) {
        throw new LegendSDLCServerException("Failed to delete " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
}
Also used : ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) RepositoryApi(org.gitlab4j.api.RepositoryApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 32 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabConflictResolutionApi method discardChangesConflictResolution.

/**
 * This method will discard all changes in conflict resolution. Assume we have workspace branch `w1`, this method will:
 * 1. Remove conflict resolution branch of `w1`
 * 2. Create backup branch for `w1`
 * 3. Remove `w1`
 * 4. Create `w1` from project head
 * 5. Remove backup branch for `w1`
 */
@Override
public void discardChangesConflictResolution(String projectId, String workspaceId, WorkspaceType workspaceType) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    RepositoryApi repositoryApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi();
    ProjectFileAccessProvider.WorkspaceAccessType conflictResolutionWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.CONFLICT_RESOLUTION;
    // Verify conflict resolution is happening
    try {
        withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType)));
    } catch (Exception e) {
        if (GitLabApiTools.isNotFoundGitLabApiException(e)) {
            LOGGER.error("Conflict resolution is not happening on {} {} in project {}, so discard changes is not actionable", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId);
        }
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " (" + workspaceId + ") or project (" + projectId + "). " + "This implies that conflict resolution is not taking place, hence discard changes is not actionable", () -> "Error getting " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    // Delete backup branch if already exists
    boolean backupWorkspaceDeleted;
    ProjectFileAccessProvider.WorkspaceAccessType backupWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.BACKUP;
    try {
        backupWorkspaceDeleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceType), 20, 1_000);
    } catch (Exception e) {
        // If we fail to delete the residual backup workspace, we cannot proceed anyway, so we will throw the error here
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to delete " + workspaceType.getLabel() + " " + backupWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error deleting " + workspaceType.getLabel() + " " + backupWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    if (!backupWorkspaceDeleted) {
        throw new LegendSDLCServerException("Failed to delete " + workspaceType.getLabel() + " " + backupWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    // Create backup branch
    Branch workspaceBranch;
    ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
    try {
        workspaceBranch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceType), getWorkspaceBranchName(workspaceId, workspaceType, workspaceAccessType), 30, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create " + workspaceType.getLabel() + " " + backupWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating " + workspaceType.getLabel() + " " + backupWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    if (workspaceBranch == null) {
        throw new LegendSDLCServerException("Failed to create " + workspaceType.getLabel() + " " + backupWorkspaceType.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, () -> "User " + getCurrentUser() + " is not allowed to delete " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " (" + workspaceId + ") or project (" + projectId + ")", () -> "Error deleting " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    if (!originalBranchDeleted) {
        throw new LegendSDLCServerException("Failed to delete " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    // Create new workspace branch off the project HEAD
    Branch newWorkspaceBranch;
    try {
        newWorkspaceBranch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, workspaceAccessType), MASTER_BRANCH, 30, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    if (newWorkspaceBranch == null) {
        throw new LegendSDLCServerException("Failed to create " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    // Delete conflict resolution branch
    boolean conflictResolutionWorkspaceDeleted;
    try {
        conflictResolutionWorkspaceDeleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType), 20, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to delete " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " (" + workspaceId + ") or project (" + projectId + ")", () -> "Error deleting " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    if (!conflictResolutionWorkspaceDeleted) {
        throw new LegendSDLCServerException("Failed to delete " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    // Delete backup branch
    try {
        boolean deleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceType), 20, 1_000);
        if (!deleted) {
            LOGGER.error("Failed to delete {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceType.getLabel(), workspaceId, projectId);
        }
    } catch (Exception e) {
        // unfortunate, but this should not throw error
        LOGGER.error("Error deleting {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceType.getLabel(), workspaceId, projectId, e);
    }
}
Also used : ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) 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 33 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabEntityApi method validateEntityChanges.

private static void validateEntityChanges(List<? extends EntityChange> entityChanges) {
    StringBuilder builder = new StringBuilder();
    List<String> errorMessages = Lists.mutable.ofInitialCapacity(4);
    int i = 0;
    for (EntityChange change : entityChanges) {
        collectErrorsForEntityChange(change, errorMessages);
        if (!errorMessages.isEmpty()) {
            if (builder.length() == 0) {
                builder.append("There are entity change errors:");
            }
            builder.append("\n\tEntity change #").append(i + 1).append(" (").append(change).append("):");
            errorMessages.forEach(m -> builder.append("\n\t\t").append(m));
            errorMessages.clear();
        }
        i++;
    }
    if (builder.length() > 0) {
        throw new LegendSDLCServerException(builder.toString(), Status.BAD_REQUEST);
    }
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) EntityChange(org.finos.legend.sdlc.domain.model.entity.change.EntityChange)

Example 34 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabEntityApi method getReviewFromEntityAccessContext.

public EntityAccessContext getReviewFromEntityAccessContext(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 new GitLabEntityAccessContext() {

            @Override
            protected ProjectFileAccessProvider.FileAccessContext getFileAccessContext(ProjectFileAccessProvider projectFileAccessProvider) {
                return projectFileAccessProvider.getFileAccessContext(projectId, null, null, null, revisionId);
            }

            @Override
            protected String getInfoForException() {
                return "review " + reviewId + " of project " + projectId;
            }
        };
    } else {
        throw new LegendSDLCServerException("Unable to get [from] revision info in project " + projectId + " for review " + reviewId);
    }
}
Also used : 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) DiffRef(org.gitlab4j.api.models.DiffRef)

Example 35 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabEntityApi method getReviewToEntityAccessContext.

public EntityAccessContext getReviewToEntityAccessContext(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.getHeadSha();
        return new GitLabEntityAccessContext() {

            @Override
            protected ProjectFileAccessProvider.FileAccessContext getFileAccessContext(ProjectFileAccessProvider projectFileAccessProvider) {
                return projectFileAccessProvider.getFileAccessContext(projectId, null, null, null, revisionId);
            }

            @Override
            protected String getInfoForException() {
                return "review " + reviewId + " of project " + projectId;
            }
        };
    } else {
        throw new LegendSDLCServerException("Unable to get [to] revision info in project " + projectId + " for review " + reviewId);
    }
}
Also used : 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) DiffRef(org.gitlab4j.api.models.DiffRef)

Aggregations

LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)64 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)34 GitLabApiException (org.gitlab4j.api.GitLabApiException)23 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)20 RepositoryApi (org.gitlab4j.api.RepositoryApi)17 MergeRequest (org.gitlab4j.api.models.MergeRequest)16 GitLabApi (org.gitlab4j.api.GitLabApi)14 Branch (org.gitlab4j.api.models.Branch)11 ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)10 ProjectConfiguration (org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)7 GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)7 DiffRef (org.gitlab4j.api.models.DiffRef)7 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)6 VersionId (org.finos.legend.sdlc.domain.model.version.VersionId)6 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)6 List (java.util.List)5 CommitsApi (org.gitlab4j.api.CommitsApi)5 Commit (org.gitlab4j.api.models.Commit)5 Comparator (java.util.Comparator)4 Pattern (java.util.regex.Pattern)4