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));
}
}
}
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();
});
}
}
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());
}
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);
}
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");
}
Aggregations