Search in sources :

Example 1 with User

use of org.finos.legend.sdlc.domain.model.user.User in project legend-sdlc by finos.

the class GitLabUserApi method getUserById.

@Override
public User getUserById(String userId) {
    LegendSDLCServerException.validateNonNull(userId, "userId cannot be null");
    Exception exception = null;
    for (GitLabMode mode : getValidGitLabModes()) {
        User user;
        try {
            user = fromGitLabAbstractUser(getGitLabApi(mode).getUserApi().getUser(userId));
        } catch (Exception e) {
            exception = e;
            user = null;
        }
        if (user != null) {
            return user;
        }
    }
    if (exception != null) {
        throw buildException(exception, () -> "User " + getCurrentUser() + " is not allowed to get user " + userId, () -> "Unknown user: " + userId, () -> "Error getting user " + userId);
    }
    throw new LegendSDLCServerException("Unknown user: " + userId, Status.NOT_FOUND);
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) User(org.finos.legend.sdlc.domain.model.user.User) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 2 with User

use of org.finos.legend.sdlc.domain.model.user.User in project legend-sdlc by finos.

the class GitLabUserApi method getCurrentUserInfo.

@Override
public User getCurrentUserInfo() {
    List<Exception> exceptions = Lists.mutable.empty();
    for (GitLabMode mode : getValidGitLabModes()) {
        try {
            User user = fromGitLabAbstractUser(getGitLabApi(mode).getUserApi().getCurrentUser());
            if (user != null) {
                return user;
            }
        } catch (Exception e) {
            exceptions.add(e);
        }
    }
    if (exceptions.isEmpty()) {
        throw new LegendSDLCServerException("Could not get current user information");
    }
    Exception e = exceptions.get(0);
    for (Exception other : exceptions) {
        if (other != e) {
            e.addSuppressed(other);
        }
    }
    throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get current user information", null, () -> "Error getting current user information");
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) User(org.finos.legend.sdlc.domain.model.user.User) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 3 with User

use of org.finos.legend.sdlc.domain.model.user.User 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

User (org.finos.legend.sdlc.domain.model.user.User)3 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)3 GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)3 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