use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class TestProjectStructure method testCantAddInvalidProjectArtifactGenerations.
protected void testCantAddInvalidProjectArtifactGenerations(ProjectType projectType) {
ProjectStructure projectStructure = buildProjectStructure(projectType);
List<Entity> testEntities = getTestEntities();
List<ProjectFileOperation> addEntityOperations = ListIterate.collectWith(testEntities, this::generateAddOperationForEntity, projectStructure);
this.fileAccessProvider.getProjectFileModificationContext(PROJECT_ID).submit("Add entities", addEntityOperations);
ProjectConfiguration beforeProjectConfig = ProjectStructure.getProjectConfiguration(PROJECT_ID, null, null, this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
Assert.assertEquals(PROJECT_ID, beforeProjectConfig.getProjectId());
Assert.assertEquals(GROUP_ID, beforeProjectConfig.getGroupId());
Assert.assertEquals(ARTIFACT_ID, beforeProjectConfig.getArtifactId());
Assert.assertEquals(this.projectStructureVersion, beforeProjectConfig.getProjectStructureVersion().getVersion());
Assert.assertEquals(this.projectStructureExtensionVersion, beforeProjectConfig.getProjectStructureVersion().getExtensionVersion());
Assert.assertTrue(beforeProjectConfig.getArtifactGenerations().isEmpty());
assertStateValid(PROJECT_ID, null, null);
String addGenerationsWorkspaceId = "AddInvalidGeneration";
this.fileAccessProvider.createWorkspace(PROJECT_ID, addGenerationsWorkspaceId);
ProjectConfiguration beforeWorkspaceConfig = ProjectStructure.getProjectConfiguration(PROJECT_ID, null, null, this.fileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
Assert.assertEquals(PROJECT_ID, beforeWorkspaceConfig.getProjectId());
Assert.assertEquals(GROUP_ID, beforeWorkspaceConfig.getGroupId());
Assert.assertEquals(ARTIFACT_ID, beforeWorkspaceConfig.getArtifactId());
Assert.assertEquals(this.projectStructureVersion, beforeWorkspaceConfig.getProjectStructureVersion().getVersion());
Assert.assertEquals(this.projectStructureExtensionVersion, beforeWorkspaceConfig.getProjectStructureVersion().getExtensionVersion());
Assert.assertTrue(beforeWorkspaceConfig.getMetamodelDependencies().isEmpty());
Assert.assertTrue(beforeWorkspaceConfig.getProjectDependencies().isEmpty());
Assert.assertTrue(beforeWorkspaceConfig.getArtifactGenerations().isEmpty());
assertStateValid(PROJECT_ID, addGenerationsWorkspaceId, null);
try {
ProjectConfigurationUpdateBuilder.newBuilder(this.fileAccessProvider, projectType, PROJECT_ID).withWorkspace(addGenerationsWorkspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withMessage("Add invalid type generations").withArtifactGenerationToAdd(new SimpleArtifactGeneration().withType(ArtifactType.entities).withName("invalid")).withArtifactGenerationToAdd(new SimpleArtifactGeneration().withType(ArtifactType.versioned_entities).withName("invalid2")).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).updateProjectConfiguration();
Assert.fail();
} catch (LegendSDLCServerException e) {
Assert.assertEquals("There were issues with one or more added artifact generations: generation types Entity, Service Execution, Versioned Entity are not allowed", e.getMessage());
}
if (!projectStructure.isSupportedArtifactType(ArtifactType.avro)) {
return;
}
Map<String, Object> params = Collections.singletonMap("one", "one");
try {
ProjectConfigurationUpdateBuilder.newBuilder(this.fileAccessProvider, projectType, PROJECT_ID).withWorkspace(addGenerationsWorkspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withMessage("Add duplicate generations").withArtifactGenerationsToAdd(Arrays.asList(new SimpleArtifactGeneration().withType(ArtifactType.avro).withName("dup"), new SimpleArtifactGeneration().withType(ArtifactType.avro).withName("dup").withParameters(params))).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).updateProjectConfiguration();
Assert.fail();
} catch (LegendSDLCServerException e) {
Assert.assertEquals("There were issues with one or more added artifact generations: generations to add contain duplicates", e.getMessage());
}
if (!projectStructure.isSupportedArtifactType(ArtifactType.java)) {
return;
}
try {
ProjectConfigurationUpdateBuilder.newBuilder(this.fileAccessProvider, projectType, PROJECT_ID).withWorkspace(addGenerationsWorkspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withMessage("Add duplicate names generations").withArtifactGenerationToAdd(new SimpleArtifactGeneration().withType(ArtifactType.avro).withName("same")).withArtifactGenerationToAdd(new SimpleArtifactGeneration().withType(ArtifactType.java).withName("same")).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).updateProjectConfiguration();
Assert.fail();
} catch (LegendSDLCServerException e) {
Assert.assertEquals("There were issues with one or more added artifact generations: generations to add contain duplicates", e.getMessage());
}
try {
ProjectConfigurationUpdateBuilder.newBuilder(this.fileAccessProvider, projectType, PROJECT_ID).withWorkspace(addGenerationsWorkspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withMessage("Add a names generations").withArtifactGenerationToAdd(new SimpleArtifactGeneration().withType(ArtifactType.avro).withName("dup")).withArtifactGenerationToAdd(new SimpleArtifactGeneration().withType(ArtifactType.avro).withName("dup")).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).updateProjectConfiguration();
Assert.fail();
} catch (LegendSDLCServerException e) {
Assert.assertEquals("There were issues with one or more added artifact generations: generations to add contain duplicates", e.getMessage());
}
ProjectConfigurationUpdateBuilder.newBuilder(this.fileAccessProvider, projectType, PROJECT_ID).withWorkspace(addGenerationsWorkspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withMessage("Add a names generations").withArtifactGenerationToAdd(new SimpleArtifactGeneration().withType(ArtifactType.avro).withName("dup")).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).updateProjectConfiguration();
try {
ProjectConfigurationUpdateBuilder.newBuilder(this.fileAccessProvider, projectType, PROJECT_ID).withWorkspace(addGenerationsWorkspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withMessage("Add a duplicate name").withArtifactGenerationToAdd(new SimpleArtifactGeneration().withType(ArtifactType.avro).withName("dup").withParameters(params)).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).updateProjectConfiguration();
Assert.fail();
} catch (LegendSDLCServerException e) {
Assert.assertEquals("There were issues with one or more added artifact generations: trying to add duplicate artifact generations", e.getMessage());
}
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabUserContext method getGitLabAPI.
public GitLabApi getGitLabAPI(GitLabMode mode, boolean redirectAllowed) {
GitLabApi api = this.apiCache.get(mode);
if (api == null) {
if (!isValidMode(mode)) {
throw new LegendSDLCServerException("GitLab mode " + mode + " is not supported", Status.BAD_REQUEST);
}
GitLabSession gitLabSession = getGitLabSession();
synchronized (this.apiCache) {
api = this.apiCache.get(mode);
if (api == null) {
GitLabModeInfo modeInfo = gitLabSession.getModeInfo(mode);
GitLabToken token = gitLabSession.getGitLabToken(mode);
if (token == null) {
try {
token = authorizerManager.authorize(session, modeInfo);
} catch (GitLabOAuthAuthenticator.UserInputRequiredException e) {
URI redirectURI = GitLabOAuthAuthenticator.buildAppAuthorizationURI(e.getModeInfo(), this.httpRequest);
throw new LegendSDLCServerException(redirectURI.toString(), Status.FOUND);
} catch (GitLabAuthFailureException e) {
throw new LegendSDLCServerException(e.getMessage(), Status.FORBIDDEN, e);
} catch (GitLabAuthException e) {
throw new LegendSDLCServerException(e.getMessage(), Status.INTERNAL_SERVER_ERROR, e);
}
if (token != null) {
gitLabSession.putGitLabToken(mode, token);
LegendSDLCWebFilter.setSessionCookie(this.httpResponse, gitLabSession);
} else if (redirectAllowed) {
URI redirectURI = GitLabOAuthAuthenticator.buildAppAuthorizationURI(modeInfo, this.httpRequest);
throw new LegendSDLCServerException(redirectURI.toString(), Status.FOUND);
} else {
throw new LegendSDLCServerException("{\"message\":\"Authorization required\",\"auth_uri\":\"/auth/authorize\"}", Status.FORBIDDEN);
}
}
api = new GitLabApi(ApiVersion.V4, modeInfo.getServerInfo().getGitLabURLString(), token.getTokenType(), token.getToken());
this.apiCache.put(mode, api);
}
}
}
return api;
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class ProjectStructure method updateOrAddDependencies.
private static void updateOrAddDependencies(Set<ProjectDependency> toUpdateProjectDependencies, ProjectFileAccessProvider projectFileAccessProvider, Set<ProjectDependency> projectDependencies, boolean purgeOldDependency) {
List<ProjectDependency> unknownDependencies = Lists.mutable.empty();
List<ProjectDependency> nonProdDependencies = Lists.mutable.empty();
SortedMap<ProjectDependency, Exception> accessExceptions = new TreeMap<>();
for (ProjectDependency projectDependency : toUpdateProjectDependencies) {
if (ProjectDependency.isLegacyProjectDependency(projectDependency)) {
try {
ProjectConfiguration dependencyConfig = getProjectConfiguration(projectFileAccessProvider.getFileAccessContext(projectDependency.getProjectId(), projectDependency.getVersionId()));
if (dependencyConfig == null || dependencyConfig.getArtifactId() == null || dependencyConfig.getGroupId() == null) {
unknownDependencies.add(projectDependency);
} else if (dependencyConfig.getProjectType() != ProjectType.PRODUCTION) {
nonProdDependencies.add(projectDependency);
} else {
if (purgeOldDependency) {
projectDependencies.remove(projectDependency);
}
projectDependencies.add(ProjectDependency.newProjectDependency(dependencyConfig.getGroupId() + ":" + dependencyConfig.getArtifactId(), projectDependency.getVersionId()));
}
} catch (Exception e) {
accessExceptions.put(projectDependency, e);
}
} else {
projectDependencies.add(ProjectDependency.newProjectDependency(projectDependency.getProjectId(), projectDependency.getVersionId()));
}
}
if (!unknownDependencies.isEmpty() || !nonProdDependencies.isEmpty() || !accessExceptions.isEmpty()) {
StringBuilder builder = new StringBuilder("There were issues with one or more added project dependencies");
if (!unknownDependencies.isEmpty()) {
builder.append("; unknown ").append((unknownDependencies.size() == 1) ? "dependency" : "dependencies").append(": ");
unknownDependencies.sort(Comparator.naturalOrder());
unknownDependencies.forEach(d -> d.appendDependencyString((d == unknownDependencies.get(0)) ? builder : builder.append(", ")));
}
if (!nonProdDependencies.isEmpty()) {
builder.append("; non-production ").append((unknownDependencies.size() == 1) ? "dependency" : "dependencies").append(": ");
nonProdDependencies.sort(Comparator.naturalOrder());
nonProdDependencies.forEach(d -> d.appendDependencyString((d == nonProdDependencies.get(0)) ? builder : builder.append(", ")));
}
if (!accessExceptions.isEmpty()) {
builder.append("; access ").append((accessExceptions.size() == 1) ? "exception" : "exceptions").append(": ");
ProjectDependency first = accessExceptions.firstKey();
accessExceptions.forEach((d, e) -> d.appendDependencyString((d == first) ? builder : builder.append(", ")).append(" (").append(e.getMessage()).append(')'));
}
throw new LegendSDLCServerException(builder.toString(), Status.BAD_REQUEST);
}
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabProjectConfigurationApi method getReviewToProjectConfiguration.
@Override
public ProjectConfiguration getReviewToProjectConfiguration(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.getHeadSha();
return getProjectRevisionProjectConfiguration(projectId, revisionId);
} else {
throw new LegendSDLCServerException("Unable to get [to] 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 GitLabReviewApi method commitReview.
@Override
public Review commitReview(String projectId, String reviewId, String message) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
LegendSDLCServerException.validateNonNull(message, "message may not be null");
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
MergeRequestApi mergeRequestApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi();
// Find the merge request
MergeRequest mergeRequest = getReviewMergeRequest(mergeRequestApi, gitLabProjectId, reviewId);
// Validate that the merge request is ready to be merged
// Check that the state is open
validateMergeRequestReviewState(mergeRequest, ReviewState.OPEN);
// Check that there are no approvals still required
Integer approvalsLeft = mergeRequest.getApprovalsLeft();
if ((approvalsLeft != null) && (approvalsLeft > 0)) {
throw new LegendSDLCServerException("Review " + reviewId + " in project " + projectId + " still requires " + approvalsLeft + " approvals", Status.CONFLICT);
}
// Accept
try {
return fromGitLabMergeRequest(projectId, mergeRequestApi.acceptMergeRequest(gitLabProjectId.getGitLabId(), mergeRequest.getIid(), message, true, null, null));
} catch (GitLabApiException e) {
switch(e.getHttpStatus()) {
// Status 401 (Unauthorized) can indicate either that the user is not properly authenticated or that the user is not allowed to merge the request.
case 401:
case 403:
{
// This shouldn't happen, but just in case ...
throw new LegendSDLCServerException("User " + getCurrentUser() + " is not allowed to commit changes from review " + reviewId + " in project " + projectId, Status.FORBIDDEN, e);
}
case 404:
{
// This shouldn't happen, as we already verified the review exists
throw new LegendSDLCServerException("Unknown review in project " + projectId + ": " + reviewId, Status.NOT_FOUND, e);
}
case 405:
{
// Status 405 (Method Not Allowed) indicates the merge request could not be accepted because it's not in an appropriate state (i.e., work in progress, closed, pipeline pending completion, or failed while requiring success)
throw new LegendSDLCServerException("Review " + reviewId + " in project " + projectId + " is not in a committable state; for more details, see: " + mergeRequest.getWebUrl(), Status.CONFLICT, e);
}
case 406:
{
// Status 406 (Not Acceptable) indicates the merge could not occur because of a conflict
throw new LegendSDLCServerException("Could not commit review " + reviewId + " in project " + projectId + " because of a conflict; for more details, see: " + mergeRequest.getWebUrl(), Status.CONFLICT, e);
}
default:
{
StringBuilder builder = new StringBuilder("Error committing changes from review ").append(reviewId).append(" to project ").append(projectId);
StringTools.appendThrowableMessageIfPresent(builder, e);
LOGGER.warn("Unexpected response status committing changes from review {} to project {}; status {}; message: {}", reviewId, projectId, e.getHttpStatus(), e.getMessage());
throw new LegendSDLCServerException(builder.toString(), e);
}
}
} catch (LegendSDLCServerException e) {
throw e;
} catch (Exception e) {
StringBuilder builder = new StringBuilder("Error committing changes from review ").append(reviewId).append(" to project ").append(projectId);
StringTools.appendThrowableMessageIfPresent(builder, e);
throw new LegendSDLCServerException(builder.toString(), e);
}
}
Aggregations