use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabProjectApi method importProject.
@Override
public ImportReport importProject(String id, ProjectType type, String groupId, String artifactId) {
LegendSDLCServerException.validateNonNull(id, "id may not be null");
LegendSDLCServerException.validateNonNull(type, "type may not be null");
LegendSDLCServerException.validate(groupId, ProjectStructure::isValidGroupId, g -> "Invalid groupId: " + g);
LegendSDLCServerException.validate(artifactId, ProjectStructure::isValidArtifactId, a -> "Invalid artifactId: " + a);
// Get project id
GitLabProjectId projectId;
if (id.chars().allMatch(Character::isDigit)) {
projectId = GitLabProjectId.newProjectId(getGitLabModeFromProjectType(type), Integer.parseInt(id));
} else {
projectId = parseProjectId(id);
if (projectId.getGitLabMode() != getGitLabModeFromProjectType(type)) {
throw new LegendSDLCServerException("Invalid project id \"" + id + "\" for project type " + type, Status.BAD_REQUEST);
}
}
// Find project
GitLabApi gitLabApi = getGitLabApi(projectId.getGitLabMode());
org.gitlab4j.api.ProjectApi gitLabProjectApi = gitLabApi.getProjectApi();
org.gitlab4j.api.models.Project currentProject;
try {
currentProject = withRetries(() -> gitLabProjectApi.getProject(projectId.getGitLabId()));
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to access project " + id + " of type " + type);
}
// Create a workspace for project configuration
RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
String workspaceId = "ProjectConfiguration_" + getRandomIdString();
Branch workspaceBranch;
try {
workspaceBranch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, projectId.getGitLabId(), getWorkspaceBranchName(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE), MASTER_BRANCH, 30, 1_000);
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create a workspace for initial configuration of project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to create workspace for initial configuration of project " + id + " of type " + type);
}
if (workspaceBranch == null) {
throw new LegendSDLCServerException("Failed to create workspace " + workspaceId + " in project " + projectId);
}
// Configure project in workspace
ProjectFileAccessProvider projectFileAccessProvider = getProjectFileAccessProvider();
Revision configRevision;
try {
ProjectConfiguration currentConfig = ProjectStructure.getProjectConfiguration(projectId.toString(), null, null, projectFileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
ProjectConfigurationUpdateBuilder builder = ProjectConfigurationUpdateBuilder.newBuilder(projectFileAccessProvider, type, projectId.toString()).withWorkspace(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withGroupId(groupId).withArtifactId(artifactId).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions);
int defaultProjectStructureVersion = getDefaultProjectStructureVersion();
if (currentConfig == null) {
// No current project structure: build a new one
configRevision = builder.withProjectStructureVersion(defaultProjectStructureVersion).withMessage("Build project structure").buildProjectStructure();
} else {
// Existing project structure: update
if (currentConfig.getProjectType() != type) {
throw new LegendSDLCServerException("Mismatch between requested project type (" + type + ") and found project type (" + currentConfig.getProjectType() + ")", Status.BAD_REQUEST);
}
ProjectStructureVersion currentVersion = currentConfig.getProjectStructureVersion();
if ((currentVersion == null) || (currentVersion.getVersion() < defaultProjectStructureVersion)) {
builder.withProjectStructureVersion(defaultProjectStructureVersion).withProjectStructureExtensionVersion(null);
}
configRevision = builder.withMessage("Update project structure").updateProjectConfiguration();
}
} catch (Exception e) {
// Try to delete the branch in case of exception
deleteWorkspace(projectId, repositoryApi, workspaceId);
throw e;
}
// Submit workspace changes, if any, for review
String reviewId;
if (configRevision == null) {
// No changes: nothing to submit
reviewId = null;
// Try to delete the branch
deleteWorkspace(projectId, repositoryApi, workspaceId);
} else {
MergeRequest mergeRequest;
try {
mergeRequest = gitLabApi.getMergeRequestApi().createMergeRequest(projectId.getGitLabId(), getWorkspaceBranchName(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE), MASTER_BRANCH, "Project structure", "Set up project structure", null, null, null, null, true, false);
} catch (Exception e) {
// Try to delete the branch in case of exception
deleteWorkspace(projectId, repositoryApi, workspaceId);
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to submit project configuration changes create a workspace for initial configuration of project " + id + " of type " + type, () -> "Could not find workspace " + workspaceId + " project " + id + " of type " + type, () -> "Failed to create a review for configuration of project " + id + " of type " + type);
}
reviewId = toStringIfNotNull(mergeRequest.getIid());
}
// Add tags
Project finalProject;
List<String> currentTags = currentProject.getTagList();
if ((currentTags != null) && currentTags.stream().anyMatch(this::isLegendSDLCProjectTag)) {
// already has the necessary tag
finalProject = fromGitLabProject(currentProject, projectId.getGitLabMode());
} else {
List<String> updatedTags = Lists.mutable.ofInitialCapacity((currentTags == null) ? 1 : (currentTags.size() + 1));
if (currentTags != null) {
updatedTags.addAll(currentTags);
}
updatedTags.add(getLegendSDLCProjectTag());
org.gitlab4j.api.models.Project updatedProject;
try {
updatedProject = gitLabProjectApi.updateProject(new org.gitlab4j.api.models.Project().withId(currentProject.getId()).withTagList(updatedTags));
} catch (Exception e) {
// Try to delete the branch in case of exception
deleteWorkspace(projectId, repositoryApi, workspaceId);
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to import project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to import project " + id + " of type " + type);
}
finalProject = fromGitLabProject(updatedProject, projectId.getGitLabMode());
}
return new ImportReport() {
@Override
public Project getProject() {
return finalProject;
}
@Override
public String getReviewId() {
return reviewId;
}
};
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabProjectApi method checkUserAuthorizationActions.
@Override
public Set<ProjectAuthorizationAction> checkUserAuthorizationActions(String id, Set<ProjectAuthorizationAction> actions) {
try {
GitLabProjectId projectId = parseProjectId(id);
org.gitlab4j.api.models.Project gitLabProject = withRetries(() -> getGitLabApi(projectId.getGitLabMode()).getProjectApi().getProject(projectId.getGitLabId()));
if (!isLegendSDLCProject(gitLabProject)) {
throw new LegendSDLCServerException("Failed to get project " + id);
}
AccessLevel userLevel = getUserAccess(gitLabProject);
if (userLevel == null) {
return Collections.emptySet();
}
return actions.stream().filter(Objects::nonNull).filter(a -> checkUserAction(projectId, a, userLevel)).collect(Collectors.toSet());
} catch (Exception e) {
throw buildException(e, () -> "Failed to get project " + id);
}
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabProjectConfigurationApi method getReviewFromProjectConfiguration.
@Override
public ProjectConfiguration getReviewFromProjectConfiguration(String projectId, String reviewId) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
MergeRequest mergeRequest = getReviewMergeRequest(getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi(), gitLabProjectId, reviewId);
validateMergeRequestForComparison(mergeRequest);
DiffRef diffRef = mergeRequest.getDiffRefs();
if (diffRef != null && diffRef.getStartSha() != null && diffRef.getHeadSha() != null) {
String revisionId = diffRef.getStartSha();
return getProjectRevisionProjectConfiguration(projectId, revisionId);
} else {
throw new LegendSDLCServerException("Unable to get [from] revision info in project " + projectId + " for review " + reviewId);
}
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabProjectConfigurationApi method updateProjectConfigurationByWorkspaceAccessType.
private Revision updateProjectConfigurationByWorkspaceAccessType(String projectId, String workspaceId, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType, String message, Integer projectStructureVersion, Integer projectStructureExtensionVersion, String groupId, String artifactId, Iterable<? extends ProjectDependency> projectDependenciesToAdd, Iterable<? extends ProjectDependency> projectDependenciesToRemove, List<? extends ArtifactGeneration> artifactGenerationsToAdd, List<String> artifactGenerationsToRemove) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
LegendSDLCServerException.validateNonNull(workspaceType, "workspaceType may not be null");
LegendSDLCServerException.validateNonNull(workspaceAccessType, "workspaceAccessType may not be null");
LegendSDLCServerException.validateNonNull(message, "message may not be null");
if ((groupId != null) && !ProjectStructure.isValidGroupId(groupId)) {
throw new LegendSDLCServerException("Invalid groupId: " + groupId, Status.BAD_REQUEST);
}
if ((artifactId != null) && !ProjectStructure.isValidArtifactId(artifactId)) {
throw new LegendSDLCServerException("Invalid artifactId: " + artifactId, Status.BAD_REQUEST);
}
if ((projectStructureVersion != null) && (this.projectStructureConfig != null) && this.projectStructureConfig.getDemisedVersions().contains(projectStructureVersion)) {
throw new LegendSDLCServerException("Project structure version " + projectStructureVersion + " is demised", Status.BAD_REQUEST);
}
try {
ProjectFileAccessProvider fileAccessProvider = getProjectFileAccessProvider();
Revision currentRevision = fileAccessProvider.getRevisionAccessContext(projectId, workspaceId, workspaceType, workspaceAccessType).getCurrentRevision();
if (currentRevision == null) {
throw new LegendSDLCServerException("Could not find current revision for " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId + ": " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " may be corrupt");
}
return ProjectConfigurationUpdateBuilder.newBuilder(fileAccessProvider, getProjectTypeFromMode(GitLabProjectId.getGitLabMode(projectId)), projectId).withWorkspace(workspaceId, workspaceType, workspaceAccessType).withRevisionId(currentRevision.getId()).withMessage(message).withProjectStructureVersion(projectStructureVersion).withProjectStructureExtensionVersion(projectStructureExtensionVersion).withGroupId(groupId).withArtifactId(artifactId).withProjectDependenciesToAdd(projectDependenciesToAdd).withProjectDependenciesToRemove(projectDependenciesToRemove).withArtifactGenerationsToAdd(artifactGenerationsToAdd).withArtifactGenerationsToRemove(artifactGenerationsToRemove).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).updateProjectConfiguration();
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to update project configuration for project " + projectId + " in " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId, () -> "Unknown project (" + projectId + ") or " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " (" + workspaceId + ")", () -> "Failed to update project configuration for project " + projectId + " in " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId);
}
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException 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());
}
Aggregations