@finos/legend-studio
Version:
240 lines • 10.2 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { observable, action, makeAutoObservable, flowResult } from 'mobx';
import { LEGEND_STUDIO_APP_EVENT } from '../LegendStudioAppEvent.js';
import { assertErrorThrown, LogEvent, ActionState, } from '@finos/legend-shared';
import { generateSetupRoute } from '../LegendStudioRouter.js';
import { WorkspaceType, ImportReport, Project, Review, Workspace, WorkspaceAccessType, } from '@finos/legend-server-sdlc';
const buildProjectOption = (project) => ({
label: project.name,
value: project.projectId,
});
const buildWorkspaceOption = (workspace) => ({
label: workspace.workspaceId,
value: workspace,
});
export class WorkspaceSetupStore {
applicationStore;
sdlcServerClient;
currentProjectId;
currentWorkspaceIdentifier;
projects;
workspacesByProject = new Map();
loadWorkspacesState = ActionState.create();
createWorkspaceState = ActionState.create();
createOrImportProjectState = ActionState.create();
loadProjectsState = ActionState.create();
showCreateProjectModal = false;
showCreateWorkspaceModal = false;
importProjectSuccessReport;
constructor(applicationStore, sdlcServerClient) {
makeAutoObservable(this, {
applicationStore: false,
sdlcServerClient: false,
setShowCreateProjectModal: action,
setShowCreateWorkspaceModal: action,
setCurrentProjectId: action,
setCurrentWorkspaceIdentifier: action,
setImportProjectSuccessReport: action,
});
this.applicationStore = applicationStore;
this.sdlcServerClient = sdlcServerClient;
}
get currentProject() {
return this.projects && this.currentProjectId
? this.projects.get(this.currentProjectId)
: undefined;
}
get currentProjectWorkspaces() {
return this.currentProjectId
? this.workspacesByProject.get(this.currentProjectId)
: undefined;
}
get currentWorkspace() {
return this.currentProjectWorkspaces && this.currentWorkspaceCompositeId
? this.currentProjectWorkspaces.get(this.currentWorkspaceCompositeId)
: undefined;
}
get currentWorkspaceCompositeId() {
return this.currentWorkspaceIdentifier
? this.buildWorkspaceCompositeId(this.currentWorkspaceIdentifier)
: undefined;
}
init(workspaceId, groupWorkspaceId) {
if (workspaceId) {
this.setCurrentWorkspaceIdentifier({
workspaceId,
workspaceType: WorkspaceType.USER,
});
}
else if (groupWorkspaceId) {
this.setCurrentWorkspaceIdentifier({
workspaceId: groupWorkspaceId,
workspaceType: WorkspaceType.GROUP,
});
}
else {
this.setCurrentWorkspaceIdentifier(undefined);
}
}
setShowCreateProjectModal(val) {
this.showCreateProjectModal = val;
}
setShowCreateWorkspaceModal(val) {
this.showCreateWorkspaceModal = val;
}
setCurrentProjectId(id) {
this.currentProjectId = id;
}
setCurrentWorkspaceIdentifier(val) {
this.currentWorkspaceIdentifier = val;
}
setImportProjectSuccessReport(importProjectSuccessReport) {
this.importProjectSuccessReport = importProjectSuccessReport;
}
*fetchProjects() {
this.loadProjectsState.inProgress();
try {
const projects = (yield this.sdlcServerClient.getProjects(undefined, undefined, undefined, undefined)).map((v) => Project.serialization.fromJson(v));
const projectIndex = observable(new Map());
projects.forEach((project) => projectIndex.set(project.projectId, project));
this.projects = projectIndex;
this.loadProjectsState.pass();
}
catch (error) {
assertErrorThrown(error);
this.applicationStore.log.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.WORKSPACE_SETUP_FAILURE), error);
this.applicationStore.notifyError(error);
this.loadProjectsState.fail();
}
}
*createProject(name, description, groupId, artifactId, tags = []) {
this.createOrImportProjectState.inProgress();
try {
const createdProject = Project.serialization.fromJson((yield this.sdlcServerClient.createProject({
name,
description,
groupId,
artifactId,
tags,
})));
this.applicationStore.notifySuccess(`Project '${name}' is succesfully created`);
yield flowResult(this.fetchProjects());
this.projects?.set(createdProject.projectId, createdProject);
this.applicationStore.navigator.goTo(generateSetupRoute(createdProject.projectId));
this.setShowCreateProjectModal(false);
}
catch (error) {
assertErrorThrown(error);
this.applicationStore.notifyError(error);
}
finally {
this.createOrImportProjectState.reset();
}
}
*importProject(id, groupId, artifactId) {
this.createOrImportProjectState.inProgress();
try {
const report = ImportReport.serialization.fromJson((yield this.sdlcServerClient.importProject({
id,
groupId,
artifactId,
})));
const importReview = Review.serialization.fromJson((yield this.sdlcServerClient.getReview(report.project.projectId, report.reviewId)));
this.setImportProjectSuccessReport({
projectName: report.project.name,
projectId: report.project.projectId,
reviewUrl: importReview.webURL,
});
yield flowResult(this.fetchProjects());
this.projects?.set(report.project.projectId, report.project);
this.setCurrentProjectId(report.project.projectId);
}
catch (error) {
assertErrorThrown(error);
this.applicationStore.notifyError(error);
}
finally {
this.createOrImportProjectState.reset();
}
}
get projectOptions() {
return this.projects
? Array.from(this.projects.values()).map(buildProjectOption)
: [];
}
get currentProjectWorkspaceOptions() {
return this.currentProjectWorkspaces
? Array.from(this.currentProjectWorkspaces.values()).map(buildWorkspaceOption)
: [];
}
*fetchWorkspaces(projectId) {
this.loadWorkspacesState.inProgress();
try {
const workspacesInConflictResolutionIds = (yield this.sdlcServerClient.getWorkspacesInConflictResolutionMode(projectId)).map((workspace) => workspace.workspaceId);
const workspaceIndex = observable(new Map());
(yield this.sdlcServerClient.getWorkspaces(projectId))
.map((v) => Workspace.serialization.fromJson(v))
.forEach((workspace) => {
// NOTE we don't handle workspaces that only have conflict resolution but no standard workspace
// since that indicates bad state of the SDLC server
if (workspacesInConflictResolutionIds.includes(workspace.workspaceId)) {
workspace.accessType = WorkspaceAccessType.CONFLICT_RESOLUTION;
}
workspaceIndex.set(this.buildWorkspaceCompositeId(workspace), workspace);
});
this.workspacesByProject.set(projectId, workspaceIndex);
}
catch (error) {
assertErrorThrown(error);
// TODO handle error when fetching workspaces for an individual project
this.applicationStore.log.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.WORKSPACE_SETUP_FAILURE), error);
}
finally {
this.loadWorkspacesState.reset();
}
}
buildWorkspaceCompositeId(workspace) {
return `${workspace.workspaceType}/${workspace.workspaceId}`;
}
*createWorkspace(projectId, workspaceId, workspaceType) {
this.createWorkspaceState.inProgress();
try {
const workspace = Workspace.serialization.fromJson((yield this.sdlcServerClient.createWorkspace(projectId, workspaceId, workspaceType)));
const existingWorkspaceForProject = this.workspacesByProject.get(projectId);
if (existingWorkspaceForProject) {
existingWorkspaceForProject.set(this.buildWorkspaceCompositeId(workspace), workspace);
}
else {
const workspaceIndex = observable(new Map());
workspaceIndex.set(this.buildWorkspaceCompositeId(workspace), workspace);
this.workspacesByProject.set(projectId, workspaceIndex);
}
this.applicationStore.notifySuccess(`Workspace '${workspace.workspaceId}' is succesfully created`);
this.setCurrentProjectId(projectId);
this.setCurrentWorkspaceIdentifier(workspace);
this.setShowCreateWorkspaceModal(false);
this.createWorkspaceState.pass();
}
catch (error) {
assertErrorThrown(error);
this.applicationStore.log.error(LogEvent.create(LEGEND_STUDIO_APP_EVENT.WORKSPACE_SETUP_FAILURE), error);
this.applicationStore.notifyError(error);
this.createWorkspaceState.fail();
}
}
}
//# sourceMappingURL=WorkspaceSetupStore.js.map