Search in sources :

Example 6 with GitLabProjectId

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

the class GitLabReviewApi method rejectReview.

@Override
public Review rejectReview(String projectId, String reviewId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    MergeRequestApi mergeRequestApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi();
    MergeRequest mergeRequest = getReviewMergeRequest(mergeRequestApi, gitLabProjectId, reviewId);
    validateMergeRequestReviewState(mergeRequest, ReviewState.OPEN);
    try {
        MergeRequest rejectMergeRequest = updateMergeRequestState(mergeRequestApi, gitLabProjectId, mergeRequest, StateEvent.CLOSE);
        return fromGitLabMergeRequest(projectId, rejectMergeRequest);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to reject review " + reviewId + " in project " + projectId, () -> "Unknown review in project " + projectId + ": " + reviewId, () -> "Error rejecting review " + reviewId + " in project " + projectId);
    }
}
Also used : MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 7 with GitLabProjectId

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

the class GitLabReviewApi method getReviewUpdateStatus.

@Override
public ReviewUpdateStatus getReviewUpdateStatus(String projectId, String reviewId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
    MergeRequest mergeRequest = getReviewMergeRequest(gitLabApi.getMergeRequestApi(), gitLabProjectId, reviewId);
    if (!(isOpen(mergeRequest) || isLocked(mergeRequest))) {
        throw new LegendSDLCServerException("Cannot get update status for review " + mergeRequest.getIid() + " in project " + projectId + ": state is " + getReviewState(mergeRequest), Status.CONFLICT);
    }
    return getReviewUpdateStatus(gitLabProjectId, gitLabApi, mergeRequest);
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId)

Example 8 with GitLabProjectId

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

the class GitLabReviewApi method approveReview.

@Override
public Review approveReview(String projectId, String reviewId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    MergeRequestApi mergeRequestApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi();
    MergeRequest mergeRequest = getReviewMergeRequest(mergeRequestApi, gitLabProjectId, reviewId);
    try {
        MergeRequest approvalMergeRequest = mergeRequestApi.approveMergeRequest(gitLabProjectId.getGitLabId(), mergeRequest.getIid(), mergeRequest.getSha());
        // The MergeRequest that comes back from the approveMergeRequest call is not adequate for
        // creating a Review, as most relevant properties are null. The only useful thing we get
        // from it is the last update time.
        mergeRequest.setUpdatedAt(approvalMergeRequest.getUpdatedAt());
        return fromGitLabMergeRequest(projectId, mergeRequest);
    } catch (GitLabApiException e) {
        switch(e.getHttpStatus()) {
            // Status 401 (Unauthorized) can indicate either that the user is not properly authenticated or that the user is not a valid approver for the merge request.
            case 401:
            case 403:
                {
                    throw new LegendSDLCServerException("User " + getCurrentUser() + " is not allowed to approve review " + reviewId + " in project " + projectId, Status.FORBIDDEN, e);
                }
            case 404:
                {
                    throw new LegendSDLCServerException("Unknown review in project " + projectId + ": " + reviewId, Status.NOT_FOUND, e);
                }
            default:
                {
                    StringBuilder builder = new StringBuilder("Error approving review ").append(reviewId).append(" in project ").append(projectId);
                    String eMessage = e.getMessage();
                    if (eMessage != null) {
                        builder.append(": ").append(eMessage);
                    }
                    throw new LegendSDLCServerException(builder.toString(), e);
                }
        }
    } catch (LegendSDLCServerException e) {
        throw e;
    } catch (Exception e) {
        StringBuilder builder = new StringBuilder("Error approving review ").append(reviewId).append(" in project ").append(projectId);
        String eMessage = e.getMessage();
        if (eMessage != null) {
            builder.append(": ").append(eMessage);
        }
        throw new LegendSDLCServerException(builder.toString(), e);
    }
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) GitLabApiException(org.gitlab4j.api.GitLabApiException) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 9 with GitLabProjectId

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

the class GitLabReviewApi method updateReview.

@Override
public ReviewUpdateStatus updateReview(String projectId, String reviewId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
    MergeRequestApi mergeRequestApi = gitLabApi.getMergeRequestApi();
    // Check the current status of the review
    MergeRequest initialMergeRequest = getReviewMergeRequest(mergeRequestApi, gitLabProjectId, reviewId);
    if (!isOpen(initialMergeRequest)) {
        throw new LegendSDLCServerException("Only open reviews can be updated: state of review " + initialMergeRequest.getIid() + " in project " + projectId + " is " + getReviewState(initialMergeRequest), Status.CONFLICT);
    }
    ReviewUpdateStatus updateStatus = getReviewUpdateStatus(gitLabProjectId, gitLabApi, initialMergeRequest);
    if (updateStatus.isUpdateInProgress() || ((updateStatus.getBaseRevisionId() != null) && updateStatus.getBaseRevisionId().equals(updateStatus.getTargetRevisionId()))) {
        // Update in progress or already up to date: no need to update
        return updateStatus;
    }
    // Start update attempt
    MergeRequest rebaseMergeRequest;
    try {
        CallUntil<MergeRequest, GitLabApiException> callUntil = CallUntil.callUntil(() -> withRetries(() -> mergeRequestApi.rebaseMergeRequest(gitLabProjectId.getGitLabId(), initialMergeRequest.getIid())), MergeRequest::getRebaseInProgress, 3, 500L);
        if (!callUntil.succeeded()) {
            throw new LegendSDLCServerException("Failed to start update for review " + reviewId + " in project " + projectId);
        }
        rebaseMergeRequest = callUntil.getResult();
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to update review " + reviewId + " in project " + projectId, () -> "Unknown review in project " + projectId + ": " + reviewId, () -> "Error updating review " + reviewId + " in project " + projectId);
    }
    return getReviewUpdateStatus(gitLabProjectId, gitLabApi, rebaseMergeRequest);
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) GitLabApiException(org.gitlab4j.api.GitLabApiException) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 10 with GitLabProjectId

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

the class GitLabVersionApi method getVersions.

private Stream<Version> getVersions(GitLabProjectId projectId, Integer minMajorVersion, Integer maxMajorVersion, Integer minMinorVersion, Integer maxMinorVersion, Integer minPatchVersion, Integer maxPatchVersion) {
    switch(getProjectTypeFromMode(projectId.getGitLabMode())) {
        case PROTOTYPE:
            {
                return Stream.empty();
            }
        case PRODUCTION:
            {
                try {
                    Stream<Version> stream = PagerTools.stream(getGitLabApi(projectId.getGitLabMode()).getTagsApi().getTags(projectId.getGitLabId(), ITEMS_PER_PAGE)).filter(GitLabVersionApi::isVersionTag).map(tag -> fromGitLabTag(projectId.toString(), tag));
                    // major version constraint
                    if ((minMajorVersion != null) && (maxMajorVersion != null)) {
                        int minMajorVersionInt = minMajorVersion;
                        int maxMajorVersionInt = maxMajorVersion;
                        if (minMajorVersionInt == maxMajorVersionInt) {
                            stream = stream.filter(v -> v.getId().getMajorVersion() == minMajorVersionInt);
                        } else {
                            stream = stream.filter(v -> {
                                int majorVersion = v.getId().getMajorVersion();
                                return (minMajorVersionInt <= majorVersion) && (majorVersion <= maxMajorVersionInt);
                            });
                        }
                    } else if (minMajorVersion != null) {
                        int minMajorVersionInt = minMajorVersion;
                        stream = stream.filter(v -> v.getId().getMajorVersion() >= minMajorVersionInt);
                    } else if (maxMajorVersion != null) {
                        int maxMajorVersionInt = maxMajorVersion;
                        stream = stream.filter(v -> v.getId().getMajorVersion() <= maxMajorVersionInt);
                    }
                    // minor version constraint
                    if ((minMinorVersion != null) && (maxMinorVersion != null)) {
                        int minMinorVersionInt = minMinorVersion;
                        int maxMinorVersionInt = maxMinorVersion;
                        if (minMinorVersionInt == maxMinorVersionInt) {
                            stream = stream.filter(v -> v.getId().getMinorVersion() == minMinorVersionInt);
                        } else {
                            stream = stream.filter(v -> {
                                int minorVersion = v.getId().getMinorVersion();
                                return (minMinorVersionInt <= minorVersion) && (minorVersion <= maxMinorVersionInt);
                            });
                        }
                    } else if (minMinorVersion != null) {
                        int minMinorVersionInt = minMinorVersion;
                        stream = stream.filter(v -> v.getId().getMinorVersion() >= minMinorVersionInt);
                    } else if (maxMinorVersion != null) {
                        int maxMinorVersionInt = maxMinorVersion;
                        stream = stream.filter(v -> v.getId().getMinorVersion() <= maxMinorVersionInt);
                    }
                    // patch version constraint
                    if ((minPatchVersion != null) && (maxPatchVersion != null)) {
                        int minPatchVersionInt = minPatchVersion;
                        int maxPatchVersionInt = maxPatchVersion;
                        if (minPatchVersionInt == maxPatchVersionInt) {
                            stream = stream.filter(v -> v.getId().getPatchVersion() == minPatchVersionInt);
                        } else {
                            stream = stream.filter(v -> {
                                int patchVersion = v.getId().getPatchVersion();
                                return (minPatchVersionInt <= patchVersion) && (patchVersion <= maxPatchVersionInt);
                            });
                        }
                    } else if (minPatchVersion != null) {
                        int minPatchVersionInt = minPatchVersion;
                        stream = stream.filter(v -> v.getId().getPatchVersion() >= minPatchVersionInt);
                    } else if (maxPatchVersion != null) {
                        int maxPatchVersionInt = maxPatchVersion;
                        stream = stream.filter(v -> v.getId().getPatchVersion() <= maxPatchVersionInt);
                    }
                    return stream;
                } catch (Exception e) {
                    throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get versions for project " + projectId, () -> "Unknown project: " + projectId, () -> "Error getting versions for project " + projectId);
                }
            }
        default:
            {
                throw new LegendSDLCServerException("Unknown project: " + projectId, Status.BAD_REQUEST);
            }
    }
}
Also used : CommitsApi(org.gitlab4j.api.CommitsApi) NewVersionType(org.finos.legend.sdlc.server.domain.api.version.NewVersionType) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) CommitRef(org.gitlab4j.api.models.CommitRef) Inject(javax.inject.Inject) GitLabApiTools(org.finos.legend.sdlc.server.gitlab.tools.GitLabApiTools) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Tag(org.gitlab4j.api.models.Tag) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) BackgroundTaskProcessor(org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor) VersionApi(org.finos.legend.sdlc.server.domain.api.version.VersionApi) Status(javax.ws.rs.core.Response.Status) Commit(org.gitlab4j.api.models.Commit) VersionId(org.finos.legend.sdlc.domain.model.version.VersionId) Pager(org.gitlab4j.api.Pager) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) 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) Stream(java.util.stream.Stream) GitLabApiException(org.gitlab4j.api.GitLabApiException) Comparator(java.util.Comparator) Collections(java.util.Collections) GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Stream(java.util.stream.Stream) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Aggregations

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