use of org.gitlab4j.api.Pager 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.gitlab4j.api.Pager in project legend-sdlc by finos.
the class GitLabWorkspaceApi method getWorkspacesByAccessType.
private List<Workspace> getWorkspacesByAccessType(String projectId, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(projectId, "workspaceAccessType may not be null");
try {
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
Pager<Branch> pager = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi().getBranches(gitLabProjectId.getGitLabId(), ITEMS_PER_PAGE);
return PagerTools.stream(pager).filter(branch -> (branch != null) && isUserOrGroupWorkspaceBranchName(branch.getName(), workspaceType, workspaceAccessType)).map(branch -> workspaceBranchToWorkspace(projectId, branch, workspaceType, workspaceAccessType)).collect(Collectors.toList());
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get " + workspaceType.getLabel() + " " + workspaceAccessType.getLabelPlural() + " for project " + projectId, () -> "Unknown project: " + projectId, () -> "Error getting " + workspaceType.getLabel() + " " + workspaceAccessType.getLabelPlural() + " for project " + projectId);
}
}
use of org.gitlab4j.api.Pager in project legend-sdlc by finos.
the class GitLabWorkspaceApi method getAllWorkspacesByAccessType.
private List<Workspace> getAllWorkspacesByAccessType(String projectId, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(workspaceType, "workspaceType may not be null");
LegendSDLCServerException.validateNonNull(workspaceAccessType, "workspaceAccessType may not be null");
try {
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
Pager<Branch> pager = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi().getBranches(gitLabProjectId.getGitLabId(), ITEMS_PER_PAGE);
return PagerTools.stream(pager).filter(branch -> (branch != null) && isWorkspaceBranchName(branch.getName(), workspaceType, workspaceAccessType)).map(branch -> workspaceBranchToWorkspace(projectId, branch, workspaceType, workspaceAccessType)).collect(Collectors.toList());
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get " + workspaceType.getLabel() + " " + workspaceAccessType.getLabelPlural() + " for project " + projectId, () -> "Unknown project: " + projectId, () -> "Error getting " + workspaceType.getLabel() + " " + workspaceAccessType.getLabelPlural() + " for project " + projectId);
}
}
use of org.gitlab4j.api.Pager in project legend-sdlc by finos.
the class GitLabRevisionApi method getRevisionStatus.
@Override
public RevisionStatus getRevisionStatus(String projectId, String revisionId) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(revisionId, "revisionId may not be null");
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
try {
GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
CommitsApi commitsApi = gitLabApi.getCommitsApi();
Revision revision = getProjectRevisionContext(projectId).getRevision(revisionId);
Pager<CommitRef> commitRefPager = withRetries(() -> commitsApi.getCommitRefs(gitLabProjectId.getGitLabId(), revision.getId(), RefType.ALL, ITEMS_PER_PAGE));
List<CommitRef> commitRefs = PagerTools.stream(commitRefPager).collect(Collectors.toList());
boolean isCommitted = commitRefs.stream().anyMatch(cr -> MASTER_BRANCH.equals(cr.getName()));
List<Version> versions;
List<String> versionTagNames = commitRefs.stream().filter(cr -> (RefType.TAG == cr.getType()) && isVersionTagName(cr.getName())).map(CommitRef::getName).collect(Collectors.toList());
if (versionTagNames.isEmpty()) {
versions = Collections.emptyList();
} else {
TagsApi tagsApi = gitLabApi.getTagsApi();
versions = Lists.mutable.ofInitialCapacity(versionTagNames.size());
for (String tagName : versionTagNames) {
Tag tag = withRetries(() -> tagsApi.getTag(gitLabProjectId.getGitLabId(), tagName));
versions.add(fromGitLabTag(projectId, tag));
}
versions.sort(Comparator.comparing(Version::getId));
}
List<Workspace> workspaces;
if (isCommitted) {
workspaces = Collections.emptyList();
} else {
// Note that here we will not account for conflict resolution or backup branch because in the model those are not real workspaces.
workspaces = commitRefs.stream().filter(cr -> (RefType.BRANCH == cr.getType()) && isWorkspaceBranchName(cr.getName(), ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE)).map(cr -> fromWorkspaceBranchName(projectId, cr.getName(), WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE)).collect(Collectors.toList());
}
return new RevisionStatus() {
@Override
public Revision getRevision() {
return revision;
}
@Override
public boolean isCommitted() {
return isCommitted;
}
@Override
public List<Workspace> getWorkspaces() {
return workspaces;
}
@Override
public List<Version> getVersions() {
return versions;
}
};
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access the status for revision " + revisionId + " of project " + projectId, () -> "Unknown: revision " + revisionId + " of project " + projectId, () -> "Error getting the status for revision " + revisionId + " of project " + projectId);
}
}
Aggregations