Search in sources :

Example 1 with CommitsApi

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

the class GitLabVersionApi method newVersion.

private Version newVersion(GitLabProjectId projectId, String revisionId, VersionId versionId, String notes) {
    String tagName = buildVersionTagName(versionId);
    String message = "Release tag for version " + versionId.toVersionIdString();
    try {
        GitLabApi gitLabApi = getGitLabApi(projectId.getGitLabMode());
        CommitsApi commitsApi = gitLabApi.getCommitsApi();
        Commit referenceCommit;
        if (revisionId == null) {
            referenceCommit = commitsApi.getCommit(projectId.getGitLabId(), MASTER_BRANCH);
            if (referenceCommit == null) {
                throw new LegendSDLCServerException("Cannot create version " + versionId.toVersionIdString() + " of project " + projectId + ": cannot find current revision (project may be corrupt)", Status.INTERNAL_SERVER_ERROR);
            }
        } else {
            try {
                referenceCommit = commitsApi.getCommit(projectId.getGitLabId(), revisionId);
            } catch (GitLabApiException e) {
                if (GitLabApiTools.isNotFoundGitLabApiException(e)) {
                    throw new LegendSDLCServerException("Revision " + revisionId + " is unknown in project " + projectId, Status.BAD_REQUEST);
                }
                throw e;
            }
            Pager<CommitRef> referenceCommitBranchPager = withRetries(() -> commitsApi.getCommitRefs(projectId.getGitLabId(), referenceCommit.getId(), RefType.BRANCH, ITEMS_PER_PAGE));
            Stream<CommitRef> referenceCommitBranches = PagerTools.stream(referenceCommitBranchPager);
            if (referenceCommitBranches.noneMatch(ref -> MASTER_BRANCH.equals(ref.getName()))) {
                throw new LegendSDLCServerException("Revision " + revisionId + " is unknown in project " + projectId, Status.BAD_REQUEST);
            }
        }
        String referenceRevisionId = referenceCommit.getId();
        Pager<CommitRef> referenceCommitTagPager = withRetries(() -> commitsApi.getCommitRefs(projectId.getGitLabId(), referenceRevisionId, RefType.TAG, ITEMS_PER_PAGE));
        List<CommitRef> referenceCommitTags = PagerTools.stream(referenceCommitTagPager).collect(Collectors.toList());
        if (referenceCommitTags.stream().map(CommitRef::getName).anyMatch(GitLabVersionApi::isVersionTagName)) {
            StringBuilder builder = new StringBuilder("Revision ").append(referenceRevisionId).append(" has already been released in ");
            List<VersionId> revisionVersionIds = referenceCommitTags.stream().map(CommitRef::getName).filter(GitLabVersionApi::isVersionTagName).map(GitLabVersionApi::parseVersionTagName).collect(Collectors.toList());
            if (revisionVersionIds.size() == 1) {
                builder.append("version ");
                revisionVersionIds.get(0).appendVersionIdString(builder);
            } else {
                builder.append("versions ");
                revisionVersionIds.sort(Comparator.naturalOrder());
                boolean first = true;
                for (VersionId revisionVersionId : revisionVersionIds) {
                    if (first) {
                        first = false;
                    } else {
                        builder.append(", ");
                    }
                    revisionVersionId.appendVersionIdString(builder);
                }
            }
            throw new LegendSDLCServerException(builder.toString());
        }
        Tag tag = getGitLabApi(projectId.getGitLabMode()).getTagsApi().createTag(projectId.getGitLabId(), tagName, referenceRevisionId, message, notes);
        return fromGitLabTag(projectId.toString(), tag);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create version " + versionId.toVersionIdString() + " of project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating version " + versionId.toVersionIdString() + " of project " + projectId);
    }
}
Also used : VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) GitLabApi(org.gitlab4j.api.GitLabApi) GitLabApiException(org.gitlab4j.api.GitLabApiException) CommitRef(org.gitlab4j.api.models.CommitRef) CommitsApi(org.gitlab4j.api.CommitsApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Commit(org.gitlab4j.api.models.Commit) Tag(org.gitlab4j.api.models.Tag)

Example 2 with CommitsApi

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

the class GitLabWorkspaceApi method createConflictResolution.

/**
 * This method will mark create a conflict resolution branch from a given workspace branch. Assume we have workspace branch `w1`, this method will:
 * 1. Create resolution branch from `master` branch (check if that's the latest)
 * 2. Get all the changes of workspace branch `w1`
 * 3. Copy and replace those changes to resolution branch `w1` and create a new commit out of that.
 */
private WorkspaceUpdateReport createConflictResolution(String projectId, String workspaceId, WorkspaceType workspaceType, String masterRevisionId) {
    // Check if conflict resolution is happening, if it is, it means conflict resolution branch already existed, so we will
    // scrap that branch and create a new one.
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    RepositoryApi repositoryApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi();
    Branch previousConflictResolutionBranch = null;
    ProjectFileAccessProvider.WorkspaceAccessType conflictResolutionWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.CONFLICT_RESOLUTION;
    try {
        previousConflictResolutionBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType)));
    } catch (Exception e) {
        if (!GitLabApiTools.isNotFoundGitLabApiException(e)) {
            LOGGER.error("Error updating {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId);
        }
    }
    // Delete conflict resolution workspace
    if (previousConflictResolutionBranch != null) {
        LOGGER.debug("Conflict resolution already happened in workspace {} in project {}, but we will recreate this conflict resolution workspace to make sure it's up to date", workspaceId, projectId);
        boolean conflictResolutionBranchDeleted;
        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);
        }
    }
    // Create conflict resolution workspace
    Branch conflictResolutionBranch;
    try {
        conflictResolutionBranch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType), 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 workspace " + workspaceId + " in project " + projectId);
    }
    if (conflictResolutionBranch == null) {
        throw new LegendSDLCServerException("Failed to create " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    // Get the changes of the current workspace
    String currentWorkspaceRevisionId = this.revisionApi.getWorkspaceRevisionContext(projectId, workspaceId, workspaceType).getCurrentRevision().getId();
    String workspaceCreationRevisionId;
    try {
        workspaceCreationRevisionId = withRetries(() -> repositoryApi.getMergeBase(gitLabProjectId.getGitLabId(), Arrays.asList(MASTER_BRANCH, currentWorkspaceRevisionId)).getId());
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get merged base revision for revisions " + MASTER_BRANCH + ", " + currentWorkspaceRevisionId + " from project " + projectId, () -> "Could not find revisions " + MASTER_BRANCH + ", " + currentWorkspaceRevisionId + " from project " + projectId, () -> "Failed to fetch merged base information for revisions " + MASTER_BRANCH + ", " + currentWorkspaceRevisionId + " from project " + projectId);
    }
    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);
    }
    List<Diff> diffs = comparisonResult.getDiffs();
    // Create a new commit on conflict resolution branch
    CommitsApi commitsApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getCommitsApi();
    ProjectFileAccessProvider.FileAccessContext projectFileAccessContext = getProjectFileAccessProvider().getProjectFileAccessContext(projectId);
    ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
    ProjectFileAccessProvider.FileAccessContext workspaceFileAccessContext = getProjectFileAccessProvider().getWorkspaceFileAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType);
    try {
        List<CommitAction> commitActions = Lists.mutable.empty();
        // NOTE: we are bringing the diffs from the workspace and applying those diffs to the project HEAD. Now, the project
        // HEAD could potentially differ greatly from the workspace base revision. This means that when we create the commit
        // action from the diff, we must be careful about the action type.
        // Take for example DELETE: if according to the diff, we delete file A, but at project HEAD, A is already deleted
        // in one of the commits between workspace base and project HEAD, such DELETE commit action will fail, this should then be
        // a NO_OP. Then again, we will have to be careful when CREATE, MOVE, and UPDATE.
        diffs.forEach(diff -> {
            if (diff.getDeletedFile()) {
                // Ensure the file to delete exists at project HEAD
                ProjectFileAccessProvider.ProjectFile fileToDelete = projectFileAccessContext.getFile(diff.getOldPath());
                if (fileToDelete != null) {
                    commitActions.add(new CommitAction().withAction(CommitAction.Action.DELETE).withFilePath(diff.getOldPath()));
                }
            } else if (diff.getRenamedFile()) {
                // split a MOVE into a DELETE followed by an CREATE/UPDATE to handle cases when
                // file to be moved is already deleted at project HEAD
                // and file to be moved to is already created at project HEAD
                ProjectFileAccessProvider.ProjectFile fileToDelete = projectFileAccessContext.getFile(diff.getOldPath());
                ProjectFileAccessProvider.ProjectFile fileToReplace = projectFileAccessContext.getFile(diff.getNewPath());
                if (fileToDelete != null) {
                    commitActions.add(new CommitAction().withAction(CommitAction.Action.DELETE).withFilePath(diff.getOldPath()));
                }
                commitActions.add(new CommitAction().withAction(fileToReplace == null ? CommitAction.Action.CREATE : CommitAction.Action.UPDATE).withFilePath(diff.getNewPath()).withEncoding(Constants.Encoding.BASE64).withContent(encodeBase64(workspaceFileAccessContext.getFile(diff.getNewPath()).getContentAsBytes())));
            } else if (diff.getNewFile()) {
                // If the file to be created already exists at project HEAD, change this to an UPDATE
                ProjectFileAccessProvider.ProjectFile fileToCreate = projectFileAccessContext.getFile(diff.getOldPath());
                commitActions.add(new CommitAction().withAction(fileToCreate == null ? CommitAction.Action.CREATE : CommitAction.Action.UPDATE).withFilePath(diff.getNewPath()).withEncoding(Constants.Encoding.BASE64).withContent(encodeBase64(workspaceFileAccessContext.getFile(diff.getNewPath()).getContentAsBytes())));
            } else {
                // File was updated
                // If the file to be updated is deleted at project HEAD, change this to a CREATE
                ProjectFileAccessProvider.ProjectFile fileToUpdate = projectFileAccessContext.getFile(diff.getOldPath());
                commitActions.add(new CommitAction().withAction(fileToUpdate == null ? CommitAction.Action.CREATE : CommitAction.Action.UPDATE).withFilePath(diff.getOldPath()).withEncoding(Constants.Encoding.BASE64).withContent(encodeBase64(workspaceFileAccessContext.getFile(diff.getOldPath()).getContentAsBytes())));
            }
        });
        commitsApi.createCommit(gitLabProjectId.getGitLabId(), this.getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType), "aggregated changes for conflict resolution", null, null, getCurrentUser(), commitActions);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create commit on " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + workspaceId + " of project " + projectId, () -> "Unknown project: " + projectId + " or " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + " " + workspaceId, () -> "Failed to create commit in " + workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel() + workspaceId + " of project" + projectId);
    }
    return createWorkspaceUpdateReport(WorkspaceUpdateReportStatus.CONFLICT, masterRevisionId, conflictResolutionBranch.getCommit().getId());
}
Also used : CompareResults(org.gitlab4j.api.models.CompareResults) Diff(org.gitlab4j.api.models.Diff) 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) 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 3 with CommitsApi

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

the class GitLabWorkspaceApi method updateWorkspace.

/**
 * There are 4 possible outcome for this method:
 * 1. NO_OP: If the workspace is already branching from the HEAD of master, nothing is needed.
 * 2. UPDATED: If the workspace is not branching from the HEAD of master, and we successfully rebase the branch to master HEAD.
 * 3. CONFLICT: If the workspace is not branching from the HEAD of master, and we failed to rebase the branch to master HEAD due to merge conflicts.
 * 4. ERROR
 * <p>
 * The procedure goes like the followings:
 * 1. Check if the current workspace is already up to date:
 * - If yes, return NO_OP
 * - If no, proceed
 * 2. Create a temp branch to attempt to rebase:
 * - If rebase succeeded, return UPDATED
 * - If rebase failed, further check if we need to enter conflict resolution mode.
 * This check makes sure the conflict that causes rebase to fail does not come from intermediate
 * commits by squashing these commits and attempt to do another rebase there. If this still fails
 * it means the workspace in overall truly has merge conflicts while updating, so entering conflict resolution mode
 */
@Override
public WorkspaceUpdateReport updateWorkspace(String projectId, String workspaceId, WorkspaceType workspaceType) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    LOGGER.info("Updating workspace {} in project {} to latest revision", workspaceId, projectId);
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
    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 currentWorkspaceRevisionId = workspaceBranch.getCommit().getId();
    LOGGER.info("Found latest revision of {} {} in project {}: {}", workspaceType.getLabel() + " " + workspaceAccessType.getLabel(), workspaceId, projectId, currentWorkspaceRevisionId);
    // Determine the revision to update to
    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();
    LOGGER.info("Found latest revision of project {}: {}", projectId, masterRevisionId);
    CommitsApi commitsApi = gitLabApi.getCommitsApi();
    // Check if the workspace already has the latest revision
    try {
        boolean isAlreadyLatest = false;
        // This will check if the branch contains the master HEAD commit by looking up the list of references the commit is pushed to
        if (masterRevisionId.equals(currentWorkspaceRevisionId)) {
            isAlreadyLatest = true;
        } else {
            Pager<CommitRef> masterHeadCommitRefPager = withRetries(() -> commitsApi.getCommitRefs(gitLabProjectId.getGitLabId(), masterRevisionId, RefType.BRANCH, ITEMS_PER_PAGE));
            Stream<CommitRef> masterHeadCommitRefs = PagerTools.stream(masterHeadCommitRefPager);
            if (masterHeadCommitRefs.anyMatch(cr -> workspaceBranchName.equals(cr.getName()))) {
                isAlreadyLatest = true;
            }
        }
        if (isAlreadyLatest) {
            // revision is already in the workspace, no update necessary, hence NO_OP
            LOGGER.info("Workspace {} in project {} already has revision {}, no update necessary", workspaceId, projectId, masterRevisionId);
            return createWorkspaceUpdateReport(WorkspaceUpdateReportStatus.NO_OP, masterRevisionId, currentWorkspaceRevisionId);
        }
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access revision " + masterRevisionId + " in project " + projectId, () -> "Unknown revision in project " + projectId + ": " + masterRevisionId, () -> "Error accessing revision " + masterRevisionId + " of project " + projectId);
    }
    // Temp branch for checking for merge conflicts
    String tempBranchName = newUserTemporaryBranchName();
    Branch tempBranch;
    try {
        tempBranch = GitLabApiTools.createBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), tempBranchName, currentWorkspaceRevisionId, 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 " + currentWorkspaceRevisionId);
    }
    // Attempt to rebase the temporary branch on top of master
    boolean rebaseSucceeded = this.attemptToRebaseWorkspaceUsingTemporaryBranch(projectId, workspaceId, workspaceType, tempBranchName, masterRevisionId);
    // 2. There are merge conflicts, so we enter conflict resolution route
    if (!rebaseSucceeded) {
        String workspaceCreationRevisionId;
        try {
            workspaceCreationRevisionId = withRetries(() -> repositoryApi.getMergeBase(gitLabProjectId.getGitLabId(), Arrays.asList(MASTER_BRANCH, currentWorkspaceRevisionId)).getId());
        } catch (Exception e) {
            throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get merged base revision for workspace " + workspaceId + " from project " + projectId, () -> "Could not find revision " + currentWorkspaceRevisionId + " from project " + projectId, () -> "Failed to fetch merged base revision for workspace " + workspaceId + " from project " + projectId);
        }
        // Small optimization step to make sure we need squashing.
        // If there are less than 2 commits (not including the base commit), there is no point in squashing
        List<Revision> latestTwoRevisionsOnWorkspaceBranch = this.revisionApi.getWorkspaceRevisionContext(projectId, workspaceId, workspaceType).getRevisions(null, null, null, 2);
        Set<String> latestTwoRevisionOnWorkspaceBranchIds = latestTwoRevisionsOnWorkspaceBranch.stream().map(Revision::getId).collect(Collectors.toSet());
        if (latestTwoRevisionOnWorkspaceBranchIds.contains(workspaceCreationRevisionId)) {
            LOGGER.debug("Failed to rebase branch {}, but the branch does not have enough commits to perform squashing. Proceeding to conflict resolution...", workspaceBranchName);
            return this.createConflictResolution(projectId, workspaceId, workspaceType, masterRevisionId);
        } else {
            LOGGER.debug("Failed to rebase branch {}. Performing squashing commits and re-attempting rebase...", workspaceBranchName);
        }
        WorkspaceUpdateReport rebaseUpdateAttemptReport = this.attemptToSquashAndRebaseWorkspace(projectId, workspaceId, workspaceType, masterRevisionId, currentWorkspaceRevisionId, workspaceCreationRevisionId);
        return WorkspaceUpdateReportStatus.UPDATED.equals(rebaseUpdateAttemptReport.getStatus()) ? rebaseUpdateAttemptReport : this.createConflictResolution(projectId, workspaceId, workspaceType, masterRevisionId);
    }
    String updatedCurrentWorkspaceRevisionId = this.revisionApi.getWorkspaceRevisionContext(projectId, workspaceId, workspaceType).getCurrentRevision().getId();
    return createWorkspaceUpdateReport(WorkspaceUpdateReportStatus.UPDATED, masterRevisionId, updatedCurrentWorkspaceRevisionId);
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) CommitRef(org.gitlab4j.api.models.CommitRef) 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) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Branch(org.gitlab4j.api.models.Branch) RepositoryApi(org.gitlab4j.api.RepositoryApi)

Example 4 with CommitsApi

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

the class GitLabReviewApi method getReviews.

@Override
public List<Review> getReviews(String projectId, ReviewState state, Iterable<String> revisionIds, Instant since, Instant until, Integer limit) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    Set<String> revisionIdSet;
    if (revisionIds == null) {
        revisionIdSet = Collections.emptySet();
    } else if (revisionIds instanceof Set) {
        revisionIdSet = (Set<String>) revisionIds;
    } else {
        revisionIdSet = Sets.mutable.withAll(revisionIds);
    }
    Stream<MergeRequest> mergeRequestStream;
    try {
        GitLabProjectId gitLabProjectId = parseProjectId(projectId);
        if (!revisionIdSet.isEmpty()) {
            // TODO: we might want to do this differently since the number of revision IDs can be huge
            // we can have a threshold for which we change our strategy to  to make a single call for
            // merge requests by the other criteria and then filter by revisionIds.
            MutableIntSet mergeRequestIds = IntSets.mutable.empty();
            CommitsApi commitsApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getCommitsApi();
            // Combine all MRs associated with each revision
            mergeRequestStream = revisionIdSet.stream().flatMap(revisionId -> {
                try {
                    return PagerTools.stream(withRetries(() -> commitsApi.getMergeRequests(gitLabProjectId.getGitLabId(), revisionId, ITEMS_PER_PAGE)));
                } catch (Exception e) {
                    throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get reviews associated with revision " + revisionId + " for project " + projectId, () -> "Unknown revision (" + revisionId + ") or project (" + projectId + ")", () -> "Error getting reviews associated with revision " + revisionId + " for project " + projectId);
                }
            }).filter(// remove duplicates
            mr -> (mr.getIid() != null) && mergeRequestIds.add(mr.getIid()));
            MergeRequestState mergeRequestState = getMergeRequestState(state);
            if (mergeRequestState != MergeRequestState.ALL) {
                String mergeRequestStateString = mergeRequestState.toString();
                mergeRequestStream = mergeRequestStream.filter(mr -> mergeRequestStateString.equalsIgnoreCase(mr.getState()));
            }
        } else {
            // if no revision ID is specified we will use the default merge request API from Gitlab to take advantage of the filter
            MergeRequestFilter mergeRequestFilter = withMergeRequestFilters(new MergeRequestFilter(), state, since, until).withProjectId(gitLabProjectId.getGitLabId());
            mergeRequestStream = PagerTools.stream(withRetries(() -> getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi().getMergeRequests(mergeRequestFilter, ITEMS_PER_PAGE)));
        }
        Stream<Review> stream = mergeRequestStream.filter(BaseGitLabApi::isReviewMergeRequest).map(mr -> fromGitLabMergeRequest(projectId, mr));
        return addReviewFilters(stream, state, since, until, limit).collect(Collectors.toList());
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get reviews for project " + projectId + ((state == null) ? "" : (" with state " + state)), () -> "Unknown project (" + projectId + ")", () -> "Error getting reviews for project " + projectId + ((state == null) ? "" : (" with state " + state)));
    }
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) CommitsApi(org.gitlab4j.api.CommitsApi) Arrays(java.util.Arrays) Branch(org.gitlab4j.api.models.Branch) MergeRequestScope(org.gitlab4j.api.Constants.MergeRequestScope) Date(java.util.Date) AbstractUser(org.gitlab4j.api.models.AbstractUser) LoggerFactory(org.slf4j.LoggerFactory) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) StateEvent(org.gitlab4j.api.Constants.StateEvent) ReviewApi(org.finos.legend.sdlc.server.domain.api.review.ReviewApi) IntSets(org.eclipse.collections.impl.factory.primitive.IntSets) StringTools(org.finos.legend.sdlc.server.tools.StringTools) MergeRequestState(org.gitlab4j.api.Constants.MergeRequestState) Function(java.util.function.Function) Inject(javax.inject.Inject) ReviewState(org.finos.legend.sdlc.domain.model.review.ReviewState) MutableIntSet(org.eclipse.collections.api.set.primitive.MutableIntSet) MergeRequestParams(org.gitlab4j.api.models.MergeRequestParams) Review(org.finos.legend.sdlc.domain.model.review.Review) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) DiffRef(org.gitlab4j.api.models.DiffRef) Status(javax.ws.rs.core.Response.Status) Commit(org.gitlab4j.api.models.Commit) EnumSet(java.util.EnumSet) Sets(org.eclipse.collections.api.factory.Sets) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) RepositoryApi(org.gitlab4j.api.RepositoryApi) Logger(org.slf4j.Logger) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) Predicate(java.util.function.Predicate) Set(java.util.Set) MergeRequestFilter(org.gitlab4j.api.models.MergeRequestFilter) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) CallUntil(org.finos.legend.sdlc.server.tools.CallUntil) List(java.util.List) Stream(java.util.stream.Stream) User(org.finos.legend.sdlc.domain.model.user.User) GitLabApiException(org.gitlab4j.api.GitLabApiException) Collections(java.util.Collections) GitLabApi(org.gitlab4j.api.GitLabApi) MutableIntSet(org.eclipse.collections.api.set.primitive.MutableIntSet) EnumSet(java.util.EnumSet) Set(java.util.Set) Review(org.finos.legend.sdlc.domain.model.review.Review) MergeRequestState(org.gitlab4j.api.Constants.MergeRequestState) CommitsApi(org.gitlab4j.api.CommitsApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException) MutableIntSet(org.eclipse.collections.api.set.primitive.MutableIntSet) MergeRequest(org.gitlab4j.api.models.MergeRequest) MergeRequestFilter(org.gitlab4j.api.models.MergeRequestFilter) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId)

Example 5 with CommitsApi

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

the class GitLabRevisionApi method getRevisionStatus.

@Override
public RevisionStatus getRevisionStatus(String projectId, String revisionId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(revisionId, "revisionId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    try {
        GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
        CommitsApi commitsApi = gitLabApi.getCommitsApi();
        Revision revision = getProjectRevisionContext(projectId).getRevision(revisionId);
        Pager<CommitRef> commitRefPager = withRetries(() -> commitsApi.getCommitRefs(gitLabProjectId.getGitLabId(), revision.getId(), RefType.ALL, ITEMS_PER_PAGE));
        List<CommitRef> commitRefs = PagerTools.stream(commitRefPager).collect(Collectors.toList());
        boolean isCommitted = commitRefs.stream().anyMatch(cr -> MASTER_BRANCH.equals(cr.getName()));
        List<Version> versions;
        List<String> versionTagNames = commitRefs.stream().filter(cr -> (RefType.TAG == cr.getType()) && isVersionTagName(cr.getName())).map(CommitRef::getName).collect(Collectors.toList());
        if (versionTagNames.isEmpty()) {
            versions = Collections.emptyList();
        } else {
            TagsApi tagsApi = gitLabApi.getTagsApi();
            versions = Lists.mutable.ofInitialCapacity(versionTagNames.size());
            for (String tagName : versionTagNames) {
                Tag tag = withRetries(() -> tagsApi.getTag(gitLabProjectId.getGitLabId(), tagName));
                versions.add(fromGitLabTag(projectId, tag));
            }
            versions.sort(Comparator.comparing(Version::getId));
        }
        List<Workspace> workspaces;
        if (isCommitted) {
            workspaces = Collections.emptyList();
        } else {
            // Note that here we will not account for conflict resolution or backup branch because in the model those are not real workspaces.
            workspaces = commitRefs.stream().filter(cr -> (RefType.BRANCH == cr.getType()) && isWorkspaceBranchName(cr.getName(), ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE)).map(cr -> fromWorkspaceBranchName(projectId, cr.getName(), WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE)).collect(Collectors.toList());
        }
        return new RevisionStatus() {

            @Override
            public Revision getRevision() {
                return revision;
            }

            @Override
            public boolean isCommitted() {
                return isCommitted;
            }

            @Override
            public List<Workspace> getWorkspaces() {
                return workspaces;
            }

            @Override
            public List<Version> getVersions() {
                return versions;
            }
        };
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access the status for revision " + revisionId + " of project " + projectId, () -> "Unknown: revision " + revisionId + " of project " + projectId, () -> "Error getting the status for revision " + revisionId + " of project " + projectId);
    }
}
Also used : Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace) CommitsApi(org.gitlab4j.api.CommitsApi) Lists(org.eclipse.collections.api.factory.Lists) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) TagsApi(org.gitlab4j.api.TagsApi) MutableList(org.eclipse.collections.api.list.MutableList) CommitRef(org.gitlab4j.api.models.CommitRef) Function(java.util.function.Function) Inject(javax.inject.Inject) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) Tag(org.gitlab4j.api.models.Tag) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) BackgroundTaskProcessor(org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor) Status(javax.ws.rs.core.Response.Status) RevisionStatus(org.finos.legend.sdlc.domain.model.revision.RevisionStatus) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) Predicate(java.util.function.Predicate) Pager(org.gitlab4j.api.Pager) Iterate(org.eclipse.collections.impl.utility.Iterate) Instant(java.time.Instant) RevisionApi(org.finos.legend.sdlc.server.domain.api.revision.RevisionApi) Collectors(java.util.stream.Collectors) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) Version(org.finos.legend.sdlc.domain.model.version.Version) RefType(org.gitlab4j.api.models.CommitRef.RefType) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) List(java.util.List) LazyIterate(org.eclipse.collections.impl.utility.LazyIterate) RevisionAccessContext(org.finos.legend.sdlc.server.domain.api.revision.RevisionAccessContext) ProjectPaths(org.finos.legend.sdlc.server.project.ProjectPaths) ListIterable(org.eclipse.collections.api.list.ListIterable) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) Collections(java.util.Collections) GitLabApi(org.gitlab4j.api.GitLabApi) GitLabApi(org.gitlab4j.api.GitLabApi) CommitRef(org.gitlab4j.api.models.CommitRef) CommitsApi(org.gitlab4j.api.CommitsApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) RevisionStatus(org.finos.legend.sdlc.domain.model.revision.RevisionStatus) TagsApi(org.gitlab4j.api.TagsApi) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Version(org.finos.legend.sdlc.domain.model.version.Version) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Tag(org.gitlab4j.api.models.Tag) Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace)

Aggregations

CommitsApi (org.gitlab4j.api.CommitsApi)8 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)7 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)6 GitLabApiException (org.gitlab4j.api.GitLabApiException)6 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)5 GitLabApi (org.gitlab4j.api.GitLabApi)5 RepositoryApi (org.gitlab4j.api.RepositoryApi)5 Branch (org.gitlab4j.api.models.Branch)5 Commit (org.gitlab4j.api.models.Commit)4 CommitRef (org.gitlab4j.api.models.CommitRef)4 Instant (java.time.Instant)2 Collections (java.util.Collections)2 List (java.util.List)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Status (javax.ws.rs.core.Response.Status)2 WorkspaceType (org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType)2 GitLabUserContext (org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext)2