Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the ability to edit devworkspaces #421

Merged
merged 2 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,8 @@ export class EditorTab extends React.PureComponent<Props, State> {
}

private async onSave(): Promise<void> {
if (
isCheWorkspace(this.props.workspace.ref) ||
this.props.workspace.status !== DevWorkspaceStatus.RUNNING.toUpperCase()
) {
this.saveDevfile();
if (!this.props.workspace.isRunning || isCheWorkspace(this.props.workspace.ref)) {
await this.saveDevfile();
} else {
this.setState({
showDevfileV2ConfirmationModal: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2018-2021 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/

import { JSONSchema7 } from 'json-schema';

export default async function getDevfileSchema(
apiCallback: (schemaVersion: string) => Promise<JSONSchema7>,
schemaVersion: string,
): Promise<JSONSchema7> {
const schema = await apiCallback(schemaVersion);

if (
typeof schema?.properties?.schemaVersion === 'object' &&
schema.properties.schemaVersion.const === undefined
) {
schema.properties.schemaVersion.const = schemaVersion;
}

return schema;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { container } from '../../inversify.config';
import { CheWorkspaceClient } from '../../services/workspace-client/cheworkspace/cheWorkspaceClient';
import { selectPlugins } from '../Plugins/chePlugins/selectors';
import { isDevworkspacesEnabled } from '../../services/helpers/devworkspace';
import getDevfileSchema from './getDevfileSchema';

const WorkspaceClient = container.get(CheWorkspaceClient);

Expand Down Expand Up @@ -221,11 +222,14 @@ export const actionCreators: ActionCreators = {
);
const parsedSchemaV1 = JSON.parse(patchedJSONString);

const schemav200 = await WorkspaceClient.restApiClient.getDevfileSchema('2.0.0');
const schemav210 = await WorkspaceClient.restApiClient.getDevfileSchema('2.1.0');
const schemav220alpha = await WorkspaceClient.restApiClient.getDevfileSchema('2.2.0');
const apiCallback = WorkspaceClient.restApiClient.getDevfileSchema;

const schemav200 = await getDevfileSchema(apiCallback, '2.0.0');
const schemav210 = await getDevfileSchema(apiCallback, '2.1.0');
const schemav220 = await getDevfileSchema(apiCallback, '2.2.0');

schema = {
oneOf: [parsedSchemaV1, schemav200, schemav210, schemav220alpha],
oneOf: [parsedSchemaV1, schemav200, schemav210, schemav220],
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,10 @@ export const actionCreators: ActionCreators = {
): AppThunk<KnownAction, Promise<void>> =>
async (dispatch, getState): Promise<void> => {
dispatch({ type: 'REQUEST_DEVWORKSPACE' });
const state = getState();
try {
await devWorkspaceClient.updateDebugMode(workspace, debugWorkspace);
let updatedWorkspace: devfileApi.DevWorkspace;
if (workspace.metadata.annotations?.[DEVWORKSPACE_NEXT_START_ANNOTATION]) {
// If the workspace has DEVWORKSPACE_NEXT_START_ANNOTATION then update the devworkspace with the DEVWORKSPACE_NEXT_START_ANNOTATION annotation value and then start the devworkspace
const plugins = selectDwEditorsPluginsList(state.dwPlugins.defaultEditorName)(state).map(
entry => entry.devfile,
);

const storedDevWorkspace = JSON.parse(
workspace.metadata.annotations[DEVWORKSPACE_NEXT_START_ANNOTATION],
) as unknown;
Expand All @@ -274,7 +268,7 @@ export const actionCreators: ActionCreators = {
delete workspace.metadata.annotations[DEVWORKSPACE_NEXT_START_ANNOTATION];
workspace.spec.template = storedDevWorkspace.spec.template;
workspace.spec.started = true;
updatedWorkspace = await devWorkspaceClient.update(workspace, plugins);
updatedWorkspace = await devWorkspaceClient.update(workspace);
} else {
updatedWorkspace = await devWorkspaceClient.changeWorkspaceStatus(workspace, true);
}
Expand Down Expand Up @@ -345,7 +339,7 @@ export const actionCreators: ActionCreators = {
(workspace: devfileApi.DevWorkspace): AppThunk<KnownAction, Promise<void>> =>
async (dispatch): Promise<void> => {
try {
devWorkspaceClient.changeWorkspaceStatus(workspace, false);
await devWorkspaceClient.changeWorkspaceStatus(workspace, false);
dispatch({
type: 'DELETE_DEVWORKSPACE_LOGS',
workspaceId: WorkspaceAdapter.getId(workspace),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2018-2021 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/

import getDevfileSchema from '../../DevfileRegistries/getDevfileSchema';
import { JSONSchema7 } from 'json-schema';
import { cloneDeep } from 'lodash';

describe('Get devfile schema', () => {
it('Should return unmodified schema if the schemaVersion property includes a constant', async () => {
const schemaWithVersionConst = {
description: 'Devfile describes the structure...',
type: 'object',
title: 'Devfile schema',
properties: {
schemaVersion: {
description: 'Devfile schema version',
type: 'string',
const: '2.0.0',
},
components: {
type: 'array',
},
},
} as JSONSchema7;

const targetScheme = await getDevfileSchema(version => {
const cloneObj = cloneDeep(schemaWithVersionConst);
return Promise.resolve(cloneObj);
}, '2.0.0');

expect(targetScheme).toEqual(schemaWithVersionConst);
});

it('Should return modified schema if the schemaVersion property does not include a constant', async () => {
const schemaWithoutVersionConst = {
description: 'Devfile describes the structure...',
type: 'object',
title: 'Devfile schema',
properties: {
schemaVersion: {
description: 'Devfile schema version',
type: 'string',
},
components: {
type: 'array',
},
},
} as JSONSchema7;

const targetScheme = await getDevfileSchema(version => {
const cloneObj = cloneDeep(schemaWithoutVersionConst);
return Promise.resolve(cloneObj);
}, '2.0.0');

expect(targetScheme).not.toEqual(schemaWithoutVersionConst);

expect(targetScheme).toEqual({
description: 'Devfile describes the structure...',
type: 'object',
title: 'Devfile schema',
properties: {
schemaVersion: {
description: 'Devfile schema version',
type: 'string',
const: '2.0.0',
},
components: {
type: 'array',
},
},
});
});
});