Search in sources :

Example 1 with GitLabMode

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

the class TestGitLabProjectId method testGetProjectIdString.

@Test
public void testGetProjectIdString() {
    for (GitLabMode mode : GitLabMode.values()) {
        for (int i = 0; i < 1024; i++) {
            Project project = new Project().withId(i);
            Assert.assertEquals(mode.name() + "-" + i, GitLabProjectId.getProjectIdString(mode, project));
        }
    }
}
Also used : Project(org.gitlab4j.api.models.Project) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) Test(org.junit.Test)

Example 2 with GitLabMode

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

the class GitLabProjectApi method getProjects.

@Override
public List<Project> getProjects(boolean user, String search, Iterable<String> tags, Iterable<ProjectType> types, Integer limit) {
    try {
        Set<ProjectType> typesSet;
        if (types == null) {
            typesSet = Collections.emptySet();
        } else if (types instanceof Set) {
            typesSet = (Set<ProjectType>) types;
        } else {
            typesSet = EnumSet.noneOf(ProjectType.class);
            types.forEach(typesSet::add);
        }
        Iterable<GitLabMode> modes = typesSet.isEmpty() ? getValidGitLabModes() : typesSet.stream().map(GitLabProjectApi::getGitLabModeFromProjectType).filter(this::isValidGitLabMode).collect(Collectors.toList());
        List<Project> projects = Lists.mutable.empty();
        Set<String> tagSet = (tags == null) ? Collections.emptySet() : toLegendSDLCTagSet(tags);
        for (GitLabMode mode : modes) {
            Pager<org.gitlab4j.api.models.Project> pager = withRetries(() -> getGitLabApi(mode).getProjectApi().getProjects(null, null, null, null, search, true, null, user, null, null, ITEMS_PER_PAGE));
            Stream<org.gitlab4j.api.models.Project> stream = PagerTools.stream(pager).filter(this::isLegendSDLCProject);
            if (!tagSet.isEmpty()) {
                stream = stream.filter(p -> p.getTagList().stream().anyMatch(tagSet::contains));
            }
            if (limit != null) {
                // NOTE: this check implies that the mode that is scanned first could take all the slots within the
                // limit. This limitation hopefully will be removed when we remove support for prototype (UAT) mode.
                stream = stream.limit(limit - projects.size());
            }
            stream.map(p -> fromGitLabProject(p, mode)).forEach(projects::add);
            if (limit != null && projects.size() >= limit) {
                // If the number of projects found already exceed the limit, skip the check for the other modes
                break;
            }
        }
        // ensure the list of returned projects cannot exceed the limit (if specified) for whatever reasons
        if (limit != null && projects.size() > limit) {
            return projects.subList(0, limit);
        }
        return projects;
    } catch (Exception e) {
        throw buildException(e, () -> {
            StringBuilder message = new StringBuilder("Failed to find ");
            if (user) {
                message.append("user ");
            }
            message.append("projects");
            List<String> tagList = (tags == null) ? Collections.emptyList() : StreamSupport.stream(tags.spliterator(), false).collect(Collectors.toList());
            if ((search != null) || !tagList.isEmpty()) {
                message.append(" (");
                if (search != null) {
                    message.append("search=\"").append(search).append("\"");
                    if (!tagList.isEmpty()) {
                        message.append(", ");
                    }
                }
                if (!tagList.isEmpty()) {
                    tagList.sort(Comparator.naturalOrder());
                    message.append("tags=[").append(String.join(", ", tagList)).append("]");
                }
                message.append(')');
            }
            return message.toString();
        });
    }
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) ProjectStructureConfiguration(org.finos.legend.sdlc.server.project.config.ProjectStructureConfiguration) Branch(org.gitlab4j.api.models.Branch) GitLabConfiguration(org.finos.legend.sdlc.server.gitlab.GitLabConfiguration) ProjectAuthorizationAction(org.finos.legend.sdlc.domain.model.project.accessRole.ProjectAuthorizationAction) LoggerFactory(org.slf4j.LoggerFactory) AccessLevel(org.gitlab4j.api.models.AccessLevel) GitLabApiTools(org.finos.legend.sdlc.server.gitlab.tools.GitLabApiTools) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) MergeRequest(org.gitlab4j.api.models.MergeRequest) BackgroundTaskProcessor(org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor) EnumSet(java.util.EnumSet) Visibility(org.gitlab4j.api.models.Visibility) RepositoryApi(org.gitlab4j.api.RepositoryApi) Pager(org.gitlab4j.api.Pager) Set(java.util.Set) Iterate(org.eclipse.collections.impl.utility.Iterate) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ProjectConfigurationUpdateBuilder(org.finos.legend.sdlc.server.project.ProjectConfigurationUpdateBuilder) List(java.util.List) Stream(java.util.stream.Stream) ProjectStructurePlatformExtensions(org.finos.legend.sdlc.server.project.ProjectStructurePlatformExtensions) Pattern(java.util.regex.Pattern) GitLabApi(org.gitlab4j.api.GitLabApi) ProjectApi(org.finos.legend.sdlc.server.domain.api.project.ProjectApi) Permissions(org.gitlab4j.api.models.Permissions) Lists(org.eclipse.collections.api.factory.Lists) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) ProjectStructureExtensionProvider(org.finos.legend.sdlc.server.project.extension.ProjectStructureExtensionProvider) ProjectCreationConfiguration(org.finos.legend.sdlc.server.project.config.ProjectCreationConfiguration) AccessRole(org.finos.legend.sdlc.domain.model.project.accessRole.AccessRole) Inject(javax.inject.Inject) StreamSupport(java.util.stream.StreamSupport) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) Status(javax.ws.rs.core.Response.Status) Sets(org.eclipse.collections.api.factory.Sets) Logger(org.slf4j.Logger) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) ProtectedTag(org.gitlab4j.api.models.ProtectedTag) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) ListIterate(org.eclipse.collections.impl.utility.ListIterate) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) ProjectStructureVersion(org.finos.legend.sdlc.domain.model.project.configuration.ProjectStructureVersion) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Project(org.finos.legend.sdlc.domain.model.project.Project) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Comparator(java.util.Comparator) ProjectAccess(org.gitlab4j.api.models.ProjectAccess) Collections(java.util.Collections) EnumSet(java.util.EnumSet) Set(java.util.Set) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Project(org.finos.legend.sdlc.domain.model.project.Project) ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) List(java.util.List) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 3 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode 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 4 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode 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 5 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode 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)

Aggregations

GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)15 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)7 Test (org.junit.Test)4 GitLabApi (org.gitlab4j.api.GitLabApi)3 Project (org.finos.legend.sdlc.domain.model.project.Project)2 User (org.finos.legend.sdlc.domain.model.user.User)2 GitLabUserContext (org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext)2 GitLabModeInfo (org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo)2 ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)2 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 Inject (javax.inject.Inject)1