Search in sources :

Example 16 with RepositoryApi

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

the class GitLabReviewApi method getMergeRequestBaseRevision.

private String getMergeRequestBaseRevision(GitLabProjectId projectId, GitLabApi gitLabApi, MergeRequest mergeRequest) {
    DiffRef diffRef = mergeRequest.getDiffRefs();
    if (diffRef != null) {
        String baseRevisionId = diffRef.getBaseSha();
        if (baseRevisionId != null) {
            return baseRevisionId;
        }
    }
    RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
    String sourceBranchName = mergeRequest.getSourceBranch();
    String targetBranchName = mergeRequest.getTargetBranch();
    Commit mergeBase;
    try {
        mergeBase = withRetries(() -> repositoryApi.getMergeBase(projectId.getGitLabId(), Arrays.asList(sourceBranchName, targetBranchName)));
    } catch (Exception e) {
        LOGGER.error("Error getting merge base for merge request {} in project {} (source branch: {}, target branch: {})", mergeRequest.getIid(), projectId, sourceBranchName, targetBranchName);
        StringBuilder builder = new StringBuilder("Error getting base revision for review ").append(mergeRequest.getIid()).append(" for project ").append(projectId);
        StringTools.appendThrowableMessageIfPresent(builder, e);
        throw new LegendSDLCServerException(builder.toString(), e);
    }
    if ((mergeBase == null) || (mergeBase.getId() == null)) {
        LOGGER.error("Error getting merge base for merge request {} in project {} (source branch: {}, target branch: {}): {}", mergeRequest.getIid(), projectId, sourceBranchName, targetBranchName, mergeBase);
        throw new LegendSDLCServerException("Error getting base revision for review " + mergeRequest.getIid() + " for project " + projectId);
    }
    return mergeBase.getId();
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Commit(org.gitlab4j.api.models.Commit) RepositoryApi(org.gitlab4j.api.RepositoryApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException) DiffRef(org.gitlab4j.api.models.DiffRef)

Example 17 with RepositoryApi

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

the class GitLabWorkspaceApi method isWorkspaceOutdatedByAccessType.

private boolean isWorkspaceOutdatedByAccessType(String projectId, String workspaceId, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceType, "workspaceType may not be null");
    LegendSDLCServerException.validateNonNull(workspaceAccessType, "workspaceAccessType may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    String workspaceBranchName = getBranchName(workspaceId, workspaceType, workspaceAccessType);
    GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
    RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
    // Get the workspace branch
    Branch workspaceBranch;
    try {
        workspaceBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), workspaceBranchName));
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " in project " + projectId + ": " + workspaceId, () -> "Error accessing " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    String workspaceRevisionId = workspaceBranch.getCommit().getId();
    // Get HEAD of master
    Branch masterBranch;
    try {
        masterBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), MASTER_BRANCH));
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access the latest revision in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error accessing latest revision for project " + projectId);
    }
    String masterRevisionId = masterBranch.getCommit().getId();
    CommitsApi commitsApi = gitLabApi.getCommitsApi();
    // Check if the workspace does not have the latest revision of the project, i.e. it is outdated
    try {
        if (masterRevisionId.equals(workspaceRevisionId)) {
            return false;
        }
        Pager<CommitRef> masterHeadCommitRefsPager = withRetries(() -> commitsApi.getCommitRefs(gitLabProjectId.getGitLabId(), masterRevisionId, RefType.BRANCH, ITEMS_PER_PAGE));
        Stream<CommitRef> masterHeadCommitRefs = PagerTools.stream(masterHeadCommitRefsPager);
        // This will check if the branch contains the master HEAD commit by looking up the list of references the commit is pushed to
        return masterHeadCommitRefs.noneMatch(cr -> workspaceBranchName.equals(cr.getName()));
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to check if " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId + " is outdated", () -> "Unknown revision (" + masterRevisionId + "), or " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " (" + workspaceId + ") or project (" + projectId + ")", () -> "Error checking if " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " of project " + projectId + " is outdated");
    }
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Branch(org.gitlab4j.api.models.Branch) RepositoryApi(org.gitlab4j.api.RepositoryApi) CommitRef(org.gitlab4j.api.models.CommitRef) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException) CommitsApi(org.gitlab4j.api.CommitsApi)

Example 18 with RepositoryApi

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

the class GitLabWorkspaceApi method newWorkspace.

/**
 * When we create a new workspace, we also should clean left-over backup and conflict resolution workspaces with the same name
 */
@Override
public Workspace newWorkspace(String projectId, String workspaceId, WorkspaceType workspaceType) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    validateWorkspaceId(workspaceId);
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    RepositoryApi repositoryApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi();
    // Delete backup workspace with the same name if exists
    Branch backupBranch = null;
    ProjectFileAccessProvider.WorkspaceAccessType backupWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.BACKUP;
    try {
        backupBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceType)));
    } catch (Exception e) {
        if (!GitLabApiTools.isNotFoundGitLabApiException(e)) {
            LOGGER.error("Error accessing {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceType.getLabel(), workspaceId, projectId, e);
        }
    }
    if (backupBranch != null) {
        LOGGER.debug("Cleaning up left-over {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceType.getLabel(), workspaceId, projectId);
        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);
        }
    }
    // Delete workspace with conflict resolution with the same name if exists
    Branch conflictResolutionBranch = null;
    ProjectFileAccessProvider.WorkspaceAccessType conflictResolutionWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.CONFLICT_RESOLUTION;
    try {
        conflictResolutionBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType)));
    } catch (Exception e) {
        if (!GitLabApiTools.isNotFoundGitLabApiException(e)) {
            LOGGER.error("Error accessing {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId, e);
        }
    }
    if (conflictResolutionBranch != null) {
        LOGGER.debug("Cleaning up left-over {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId);
        try {
            boolean deleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType), 20, 1_000);
            if (!deleted) {
                LOGGER.error("Failed to delete {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId);
            }
        } catch (Exception e) {
            // unfortunate, but this should not throw error
            LOGGER.error("Error deleting {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId, e);
        }
    }
    // Create new workspace
    Branch branch;
    ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
    try {
        branch = 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 workspace " + workspaceId + " in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    if (branch == null) {
        throw new LegendSDLCServerException("Failed to create " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    return workspaceBranchToWorkspace(projectId, branch, workspaceType, workspaceAccessType);
}
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 19 with RepositoryApi

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

the class GitLabWorkspaceApi method attemptToSquashAndRebaseWorkspace.

/**
 * This method is called when we failed to rebase the workspace branch on top of master branch. This implies that either
 * the whole change is causing a merge conflict or one of the intermediate commits have conflicts with the change in master branch
 * <p>
 * To handle the latter case, this method will squash all commits on the workspace branch and attempt rebase again.
 * If this fails, it implies that rebase fails because of merge conflicts, which means we have to enter conflict resolution route.
 * <p>
 * NOTE: based on the nature of this method, we have an optimization here where we check for the number of commits on the current
 * branch, if it is less than 2 (not counting the base), it means no squashing is needed and we can just immediately tell that there
 * is merge conflict and the workspace update should enter conflict resolution route
 * <p>
 * So following is the summary of this method:
 * 1. Create a temp branch to do the squashing on that branch
 * 3. Attempt to rebase the temp branch on master:
 * - If succeeded: re-create current workspace branch on top of the temp branch
 * - If failed -> implies conflict resolution is needed
 *
 * @return a workspace update report that might have status as UPDATED or CONFLICT.
 */
private WorkspaceUpdateReport attemptToSquashAndRebaseWorkspace(String projectId, String workspaceId, WorkspaceType workspaceType, String masterRevisionId, String currentWorkspaceRevisionId, String workspaceCreationRevisionId) {
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    RepositoryApi repositoryApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi();
    // Create temp branch for rebasing
    String tempBranchName = newUserTemporaryBranchName();
    Branch tempBranch;
    try {
        tempBranch = GitLabApiTools.createBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), tempBranchName, workspaceCreationRevisionId, 30, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create temporary workspace " + tempBranchName + " in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating temporary workspace " + tempBranchName + " in project " + projectId);
    }
    if (tempBranch == null) {
        throw new LegendSDLCServerException("Failed to create temporary workspace " + tempBranchName + " in project " + projectId + " from revision " + workspaceCreationRevisionId);
    }
    CompareResults comparisonResult;
    try {
        comparisonResult = withRetries(() -> repositoryApi.compare(gitLabProjectId.getGitLabId(), workspaceCreationRevisionId, currentWorkspaceRevisionId, true));
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get comparison information from revision " + workspaceCreationRevisionId + "  to revision " + currentWorkspaceRevisionId + " on project" + projectId, () -> "Could not find revisions " + workspaceCreationRevisionId + " ," + currentWorkspaceRevisionId + " on project" + projectId, () -> "Failed to fetch comparison information from revision " + workspaceCreationRevisionId + "  to revision " + currentWorkspaceRevisionId + " on project" + projectId);
    }
    // Create a new commit on temp branch that squashes all changes on the concerned workspace branch
    CommitsApi commitsApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getCommitsApi();
    ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
    ProjectFileAccessProvider.FileAccessContext workspaceFileAccessContext = getProjectFileAccessProvider().getWorkspaceFileAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType);
    Commit squashedCommit;
    try {
        List<CommitAction> commitActions = Lists.mutable.empty();
        comparisonResult.getDiffs().forEach(diff -> {
            if (diff.getDeletedFile()) {
                // DELETE
                commitActions.add(new CommitAction().withAction(CommitAction.Action.DELETE).withFilePath(diff.getOldPath()));
            } else if (diff.getRenamedFile()) {
                // MOVE
                // 
                // tl;dr;
                // split this into a delete and a create to make sure the moved entity has the content of the entity
                // in workspace HEAD revision
                // 
                // Since we use comparison API to compute the diff, Git has a smart algorithm to calculate file move
                // as it is a heuristics such that if file content is only slightly different, Git can conclude that the
                // diff was of type RENAME.
                // 
                // The problem with this is when we compare the workspace BASE revision and workspace HEAD revision
                // Assume that we actually renamed an entity, we also want to update the entity path, not just its location
                // the location part is correctly identified by Git, and the change in content is captured as a diff string
                // in comparison result (which shows the change in the path)
                // but when we create CommitAction, there is no way for us to apply this patch on top of the entity content
                // so if we just create action of type MOVE for CommitAction, we will have the content of the old entity
                // which has the wrong path, and thus this entity if continue to exist in the workspace will throw off
                // our path and entity location validation check
                commitActions.add(new CommitAction().withAction(CommitAction.Action.DELETE).withFilePath(diff.getOldPath()));
                commitActions.add(new CommitAction().withAction(CommitAction.Action.CREATE).withFilePath(diff.getNewPath()).withEncoding(Constants.Encoding.BASE64).withContent(encodeBase64(workspaceFileAccessContext.getFile(diff.getNewPath()).getContentAsBytes())));
            } else if (diff.getNewFile()) {
                // CREATE
                commitActions.add(new CommitAction().withAction(CommitAction.Action.CREATE).withFilePath(diff.getNewPath()).withEncoding(Constants.Encoding.BASE64).withContent(encodeBase64(workspaceFileAccessContext.getFile(diff.getNewPath()).getContentAsBytes())));
            } else {
                // UPDATE
                commitActions.add(new CommitAction().withAction(CommitAction.Action.UPDATE).withFilePath(diff.getOldPath()).withEncoding(Constants.Encoding.BASE64).withContent(encodeBase64(workspaceFileAccessContext.getFile(diff.getOldPath()).getContentAsBytes())));
            }
        });
        squashedCommit = commitsApi.createCommit(gitLabProjectId.getGitLabId(), tempBranchName, "aggregated changes for workspace " + workspaceId, null, null, getCurrentUser(), commitActions);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create commit on temporary workspace " + tempBranchName + " of project " + projectId, () -> "Unknown project: " + projectId + " or temporary workspace " + tempBranchName, () -> "Failed to create commit in temporary workspace " + tempBranchName + " of project " + projectId);
    }
    // Attempt to rebase the temporary branch on top of master
    boolean attemptRebaseResult = this.attemptToRebaseWorkspaceUsingTemporaryBranch(projectId, workspaceId, workspaceType, tempBranchName, masterRevisionId);
    // If rebasing failed, this implies there are conflicts, otherwise, the workspace should be updated
    if (!attemptRebaseResult) {
        return createWorkspaceUpdateReport(WorkspaceUpdateReportStatus.CONFLICT, null, null);
    }
    return createWorkspaceUpdateReport(WorkspaceUpdateReportStatus.UPDATED, masterRevisionId, squashedCommit.getId());
}
Also used : CompareResults(org.gitlab4j.api.models.CompareResults) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException) CommitsApi(org.gitlab4j.api.CommitsApi) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Commit(org.gitlab4j.api.models.Commit) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Branch(org.gitlab4j.api.models.Branch) RepositoryApi(org.gitlab4j.api.RepositoryApi) CommitAction(org.gitlab4j.api.models.CommitAction)

Example 20 with RepositoryApi

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

the class GitLabBackupApi method discardBackupWorkspace.

@Override
public void discardBackupWorkspace(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 backupWorkspaceDeleted;
    ProjectFileAccessProvider.WorkspaceAccessType backupWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.BACKUP;
    try {
        backupWorkspaceDeleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceType), 20, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to delete " + workspaceType.getLabel() + " " + backupWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown " + workspaceType.getLabel() + " " + backupWorkspaceType.getLabel() + " (" + workspaceId + ") or 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);
    }
}
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)

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