Skip to content

Commit

Permalink
feat: revisit create policies for DevWorkspace mode
Browse files Browse the repository at this point in the history
Try to open again a workspace by default
Creates a new workspace if specify ?new at the end of short URL

Note: it applies only for DevWorkspaces
fix eclipse-che/che#20867
  • Loading branch information
benoitf committed Dec 6, 2021
1 parent d60de97 commit 294b87f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ const WS_ATTRIBUTES_TO_SAVE: string[] = [
'che-editor',
];

const DEFAULT_CREATE_POLICY = 'perclick';

export type CreatePolicy = 'perclick' | 'peruser';

enum ErrorCodes {
Expand Down Expand Up @@ -102,16 +100,21 @@ export class FactoryLoaderContainer extends React.PureComponent<Props, State> {

const { search } = this.props.history.location;
const cheDevworkspaceEnabled = isDevworkspacesEnabled(this.props.workspacesSettings);

const createPolicy = this.getDefaultCreatePolicy();
this.state = {
currentStep: LoadFactorySteps.INITIALIZING,
hasError: false,
createPolicy: DEFAULT_CREATE_POLICY,
createPolicy,
search,
cheDevworkspaceEnabled,
};
}

private getDefaultCreatePolicy(): CreatePolicy {
const devWorkspaceMode = isDevworkspacesEnabled(this.props.workspacesSettings);
return devWorkspaceMode ? 'peruser' : 'perclick';
}

private resetOverrideParams(): void {
this.overrideDevfileObject = {};
}
Expand Down Expand Up @@ -220,7 +223,7 @@ export class FactoryLoaderContainer extends React.PureComponent<Props, State> {
}

private getCreatePolicy(attrs: { [key: string]: string }): CreatePolicy | undefined {
const policy = attrs['policies.create'] || DEFAULT_CREATE_POLICY;
const policy = attrs['policies.create'] || this.getDefaultCreatePolicy();
if (this.isCreatePolicy(policy)) {
return policy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,35 @@ describe('Factory Loader container', () => {
jest.useRealTimers();
});

describe('default policy', () => {
const component = FactoryLoaderContainer.WrappedComponent;
const emptyProps = {
history: {
location: 'https://foo.location',
},
} as any;

it('default policy in in Che Server mode is perclick', () => {
const instance = new component({
...emptyProps,
workspacesSettings: {
'che.devworkspaces.enabled': 'false',
},
});
expect(instance.state.createPolicy).toBe('perclick');
});

it('default policy in in DevWorkspaces mode is peruser', () => {
const instance = new component({
...emptyProps,
workspacesSettings: {
'che.devworkspaces.enabled': 'true',
},
});
expect(instance.state.createPolicy).toBe('peruser');
});
});

describe('converting devfiles', () => {
it('should NOT convert devfile v1 in Che Server mode', async () => {
const location = 'http://test-location';
Expand Down Expand Up @@ -667,6 +696,34 @@ describe('Factory Loader container', () => {
});

describe('Use a devfile V2', () => {
it('should resolve the factory with default policy and open existing workspace', async () => {
const location = 'http://test-location?new';
const name = 'test-name';
const devWorkspace = new DevWorkspaceBuilder()
.withId('id-wksp-test')
.withName(name)
.withNamespace('test')
.build();
renderComponentV2(location, devWorkspace);

const elementCurrentStep = screen.getByTestId('factory-loader-current-step');
expect(LoadFactorySteps[elementCurrentStep.innerHTML]).toEqual(
LoadFactorySteps[LoadFactorySteps.LOOKING_FOR_DEVFILE],
);

jest.runOnlyPendingTimers();
await waitFor(() =>
expect(requestFactoryResolverMock).toHaveBeenCalledWith(location.split('&')[0]),
);
expect(LoadFactorySteps[elementCurrentStep.innerHTML]).toEqual(
LoadFactorySteps[LoadFactorySteps.APPLYING_DEVFILE],
);

jest.runOnlyPendingTimers();

await waitFor(() => expect(createWorkspaceFromDevfileMock).not.toBeCalled());
});

it('should resolve the factory with create policie "peruser" and open existing workspace', async () => {
const location = 'http://test-location&policies.create=peruser';
const name = 'test-name';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 { buildFactoryLoaderLocation } from '../location';

describe('Location test', () => {
test('new policy', () => {
const result = buildFactoryLoaderLocation(
'https://github.com/che-samples/java-spring-petclinic/tree/devfilev2?new=',
);
expect(result.search).toBe(
'?url=https%3A%2F%2Fgithub.com%2Fche-samples%2Fjava-spring-petclinic%2Ftree%2Fdevfilev2&policies.create=perclick',
);
});

test('che-editor parameter', () => {
const result = buildFactoryLoaderLocation(
'https://github.com/che-samples/java-spring-petclinic/tree/devfilev2?che-editor=che-incubator/checode/insiders',
);
expect(result.search).toBe(
'?url=https%3A%2F%2Fgithub.com%2Fche-samples%2Fjava-spring-petclinic%2Ftree%2Fdevfilev2&che-editor=che-incubator/checode/insiders',
);
});

test('devfilePath parameter', () => {
const result = buildFactoryLoaderLocation(
'https://github.com/che-samples/java-spring-petclinic/tree/devfilev2?devfilePath=devfilev2.yaml',
);
expect(result.search).toBe(
'?url=https%3A%2F%2Fgithub.com%2Fche-samples%2Fjava-spring-petclinic%2Ftree%2Fdevfilev2&override.devfileFilename=devfilev2.yaml',
);
});
});
10 changes: 10 additions & 0 deletions packages/dashboard-frontend/src/services/helpers/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export function buildFactoryLoaderLocation(url?: string): Location {
devfilePath = extractUrlParam(fullUrl, 'df');
}

// creation policy
const newWorkspace = extractUrlParam(fullUrl, 'new');
const encodedUrl = encodeURIComponent(fullUrl.toString());

// if editor specified, add it as a new parameter
Expand All @@ -62,6 +64,9 @@ export function buildFactoryLoaderLocation(url?: string): Location {
if (devfilePath) {
pathAndQuery = `${pathAndQuery}&override.devfileFilename=${devfilePath}`;
}
if (newWorkspace) {
pathAndQuery = `${pathAndQuery}&policies.create=perclick`;
}
}
return _buildLocationObject(pathAndQuery);
}
Expand All @@ -72,6 +77,11 @@ function extractUrlParam(fullUrl: URL, paramName: string): string | undefined {
let value;
if (param && typeof param === 'string') {
value = param.slice();
} else if (!param) {
// boolean parameter
if (fullUrl.searchParams.has(paramName)) {
value = true;
}
}
fullUrl.searchParams.delete(paramName);
return value;
Expand Down

0 comments on commit 294b87f

Please sign in to comment.