Search in sources :

Example 1 with MergeRequestFilter

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

the class GitLabReviewApi method getReviews.

@Override
public List<Review> getReviews(Set<ProjectType> projectTypes, boolean assignedToMe, boolean authoredByMe, List<String> labels, ReviewState state, Instant since, Instant until, Integer limit) {
    if (assignedToMe && authoredByMe) {
        throw new LegendSDLCServerException("assignedToMe and authoredByMe may not both be true", Status.BAD_REQUEST);
    }
    Set<GitLabMode> modes = EnumSet.noneOf(GitLabMode.class);
    if (projectTypes == null || projectTypes.isEmpty()) {
        getValidGitLabModes().forEach(modes::add);
    } else {
        projectTypes.forEach(a -> modes.add(getGitLabModeFromProjectType(a)));
    }
    MergeRequestFilter mergeRequestFilter = withMergeRequestLabels(withMergeRequestFilters(new MergeRequestFilter(), state, since, until).withScope(assignedToMe ? MergeRequestScope.ASSIGNED_TO_ME : (authoredByMe ? MergeRequestScope.CREATED_BY_ME : MergeRequestScope.ALL)), labels);
    return addReviewFilters(modes.stream().flatMap(mode -> getReviewStream(mode, mergeRequestFilter)), state, since, until, limit).collect(Collectors.toList());
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequestFilter(org.gitlab4j.api.models.MergeRequestFilter) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 2 with MergeRequestFilter

use of org.gitlab4j.api.models.MergeRequestFilter 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)

Aggregations

LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)2 GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)2 Instant (java.time.Instant)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Date (java.util.Date)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Inject (javax.inject.Inject)1 Status (javax.ws.rs.core.Response.Status)1 Sets (org.eclipse.collections.api.factory.Sets)1 MutableIntSet (org.eclipse.collections.api.set.primitive.MutableIntSet)1 IntSets (org.eclipse.collections.impl.factory.primitive.IntSets)1 ProjectType (org.finos.legend.sdlc.domain.model.project.ProjectType)1 WorkspaceType (org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType)1