Skip to content

Commit

Permalink
Add a new workspace creation URL parameter (#1037)
Browse files Browse the repository at this point in the history
* feat: Add a new workspace creation URL parameter named editor-image

Signed-off-by: Oleksii Orel <[email protected]>
  • Loading branch information
olexii4 authored Jan 18, 2024
1 parent 277067c commit 203577f
Show file tree
Hide file tree
Showing 17 changed files with 424 additions and 19 deletions.
1 change: 1 addition & 0 deletions .deps/EXCLUDED/prod.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ This file lists dependencies that do not need CQs or auto-detection does not wor
| Packages | Resolved CQs |
| --- | --- |
| `@fastify/[email protected]` | [clearlydefined](https://clearlydefined.io/definitions/npm/npmjs/@fastify/cors/8.4.1) |
| `@fastify/reply-from@^9.6.0` | [clearlydefined](https://clearlydefined.io/definitions/npm/npmjs/@fastify/reply-from/9.6.0) |
| `@patternfly/[email protected]` | [clearlydefined](https://clearlydefined.io/definitions/npm/npmjs/@patternfly/react-core/4.278.0) |
| `@patternfly/[email protected]` | [clearlydefined](https://clearlydefined.io/definitions/npm/npmjs/@patternfly/react-table/4.113.6) |
| `@patternfly/[email protected]` | [clearlydefined](https://clearlydefined.io/definitions/npm/npmjs/@patternfly/react-icons/4.93.7) |
Expand Down
4 changes: 2 additions & 2 deletions .deps/prod.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
| [`@fastify/[email protected]`](git+https://github.com/fastify/fast-json-stringify-compiler.git) | MIT | clearlydefined |
| [`@fastify/[email protected]`](git+https://github.com/fastify/fastify-http-proxy.git) | MIT | clearlydefined |
| [`@fastify/[email protected]`](git+https://github.com/fastify/fastify-oauth2.git) | MIT | clearlydefined |
| [`@fastify/reply-from@9.4.0`](git+https://github.com/fastify/fastify-reply-from.git) | MIT | clearlydefined |
| [`@fastify/reply-from@9.6.0`](git+https://github.com/fastify/fastify-reply-from.git) | MIT | clearlydefined |
| [`@fastify/[email protected]`](git+https://github.com/fastify/send.git) | MIT | clearlydefined |
| [`@fastify/[email protected]`](https://github.com/fastify/fastify-static.git) | MIT | clearlydefined |
| [`@fastify/[email protected]`](git+https://github.com/fastify/fastify-swagger-ui.git) | MIT | clearlydefined |
Expand Down Expand Up @@ -170,7 +170,7 @@
| [`[email protected]`](https://github.com/react-dropzone/file-selector.git) | MIT | [CQ22350](https://dev.eclipse.org/ipzilla/show_bug.cgi?id=22350) |
| [`[email protected]`](git+https://github.com/delvedor/find-my-way.git) | MIT | clearlydefined |
| [`[email protected]`](git+https://github.com/focus-trap/focus-trap.git) | MIT | clearlydefined |
| [`[email protected].3`]([email protected]:follow-redirects/follow-redirects.git) | MIT | #10782 |
| [`[email protected].4`]([email protected]:follow-redirects/follow-redirects.git) | MIT | #10782 |
| [`[email protected]`](https://github.com/mikeal/forever-agent) | Apache-2.0 | clearlydefined |
| [`[email protected]`](git://github.com/form-data/form-data.git) | MIT | clearlydefined |
| [`[email protected]`](https://github.com/jshttp/forwarded.git) | MIT | clearlydefined |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class CreatingStepApplyResources extends ProgressStep<Props, State> {
return false;
}

await this.props.createWorkspaceFromResources(...resources, cheEditor);
await this.props.createWorkspaceFromResources(...resources, factoryParams, cheEditor);

// wait for the workspace creation to complete
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ describe('test buildFactoryLoaderPath()', () => {
);
});

test('editor-image parameter', () => {
const result = buildFactoryLoaderPath(
'[email protected]:eclipse-che/che-dashboard.git?editor-image=quay.io/mloriedo/che-code:copilot-builtin',
);
expect(result).toEqual(
'/f?editor-image=quay.io%2Fmloriedo%2Fche-code%3Acopilot-builtin&url=git%40github.com%3Aeclipse-che%2Fche-dashboard.git',
);
});

test('devfilePath parameter', () => {
const result = buildFactoryLoaderPath(
'[email protected]:eclipse-che/che-dashboard.git?devfilePath=devfilev2.yaml',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const POLICIES_CREATE_ATTR = 'policies.create';
export const STORAGE_TYPE_ATTR = 'storageType';
export const REMOTES_ATTR = 'remotes';
export const IMAGE_ATTR = 'image';
export const EDITOR_IMAGE_ATTR = 'editor-image';
export const USE_DEFAULT_DEVFILE = 'useDefaultDevfile';
export const DEBUG_WORKSPACE_START = 'debugWorkspaceStart';
export const PROPAGATE_FACTORY_ATTRS = [
Expand All @@ -32,6 +33,7 @@ export const PROPAGATE_FACTORY_ATTRS = [
STORAGE_TYPE_ATTR,
REMOTES_ATTR,
IMAGE_ATTR,
EDITOR_IMAGE_ATTR,
];
export const OVERRIDE_ATTR_PREFIX = 'override.';
export const DEFAULT_POLICIES_CREATE = 'peruser';
Expand All @@ -46,6 +48,7 @@ export type FactoryParams = {
errorCode: ErrorCode | undefined;
storageType: che.WorkspaceStorageType | undefined;
cheEditor: string | undefined;
editorImage: string | undefined;
remotes: string | undefined;
image: string | undefined;
useDefaultDevfile: boolean;
Expand All @@ -59,6 +62,7 @@ export type ErrorCode = 'invalid_request' | 'access_denied';
export function buildFactoryParams(searchParams: URLSearchParams): FactoryParams {
return {
cheEditor: getEditorId(searchParams),
editorImage: getEditorImage(searchParams),
errorCode: getErrorCode(searchParams),
factoryId: buildFactoryId(searchParams),
factoryUrl: getFactoryUrl(searchParams),
Expand Down Expand Up @@ -108,6 +112,10 @@ function getEditorId(searchParams: URLSearchParams): string | undefined {
return searchParams.get(EDITOR_ATTR) || undefined;
}

function getEditorImage(searchParams: URLSearchParams): string | undefined {
return searchParams.get(EDITOR_IMAGE_ATTR) || undefined;
}

function getErrorCode(searchParams: URLSearchParams): ErrorCode | undefined {
return (searchParams.get(ERROR_CODE_ATTR) as ErrorCode) || undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,11 @@ describe('DevWorkspace store, actions', () => {
mockOnStart.mockResolvedValueOnce(undefined);

await store.dispatch(
testStore.actionCreators.createWorkspaceFromResources(devWorkspace, devWorkspaceTemplate),
testStore.actionCreators.createWorkspaceFromResources(
devWorkspace,
devWorkspaceTemplate,
{},
),
);

const actions = store.getActions();
Expand Down Expand Up @@ -844,7 +848,11 @@ describe('DevWorkspace store, actions', () => {

try {
await store.dispatch(
testStore.actionCreators.createWorkspaceFromResources(devWorkspace, devWorkspaceTemplate),
testStore.actionCreators.createWorkspaceFromResources(
devWorkspace,
devWorkspaceTemplate,
{},
),
);
} catch (e) {
// no-op
Expand Down Expand Up @@ -892,7 +900,11 @@ describe('DevWorkspace store, actions', () => {

it('should provide default editor id when creating a new workspace from resources', async () => {
await store.dispatch(
testStore.actionCreators.createWorkspaceFromResources(devWorkspace, devWorkspaceTemplate),
testStore.actionCreators.createWorkspaceFromResources(
devWorkspace,
devWorkspaceTemplate,
{},
),
);

expect(mockCreateDevWorkspace).toHaveBeenCalledWith(
Expand All @@ -907,6 +919,7 @@ describe('DevWorkspace store, actions', () => {
testStore.actionCreators.createWorkspaceFromResources(
devWorkspace,
devWorkspaceTemplate,
{},
'editorid',
),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright (c) 2018-2024 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 common from '@eclipse-che/common';
import fs from 'fs';
import { dump, load } from 'js-yaml';

import devfileApi from '@/services/devfileApi';
import { DevWorkspaceBuilder } from '@/store/__mocks__/devWorkspaceBuilder';
import {
getEditorImage,
updateDevWorkspaceTemplate,
updateEditorDevfile,
} from '@/store/Workspaces/devWorkspaces/editorImage';

describe('Update editor image', () => {
describe('devfile source annotation', () => {
it('should return undefined without devfile-source', () => {
const devWorkspace = new DevWorkspaceBuilder()
.withTemplateAttributes({
'dw.metadata.annotations': {},
})
.build();

const editorImageParam = getEditorImage(devWorkspace);

expect(editorImageParam).toBeUndefined();
});

it('should return undefined without factory params', () => {
const devWorkspace = new DevWorkspaceBuilder()
.withTemplateAttributes({
'dw.metadata.annotations': {
'che.eclipse.org/devfile-source': dump({
factory: {},
}),
},
})
.build();

const editorImageParam = getEditorImage(devWorkspace);

expect(editorImageParam).toBeUndefined();
});

it('should return the editor image param', () => {
const devWorkspace = new DevWorkspaceBuilder()
.withTemplateAttributes({
'dw.metadata.annotations': {
'che.eclipse.org/devfile-source': dump({
factory: {
params:
'editor-image=test-images/che-code:tag&url=https://github.com/eclipse-che/che-dashboard',
},
}),
},
})
.build();

const editorImageParam = getEditorImage(devWorkspace);

expect(editorImageParam).toStrictEqual('test-images/che-code:tag');
});
});

describe('editor devfile', () => {
it('should throw an error if editorContent is not defined', () => {
const customEditorImage = 'test-images/che-code:tag';

const editorContent = '';

let errorMessage: string | undefined;
try {
updateEditorDevfile(editorContent, customEditorImage);
} catch (err) {
errorMessage = common.helpers.errors.getMessage(err);
}

expect(errorMessage).toEqual('Editor content is empty.');
});

it('should throw an error if editor components are not defined', () => {
const customEditorImage = 'test-images/che-code:tag';

const editorContent = fs.readFileSync(
__dirname + '/fixtures/test-devfile-without-components.yaml',
'utf-8',
);

let errorMessage: string | undefined;
try {
updateEditorDevfile(editorContent, customEditorImage);
} catch (err) {
errorMessage = common.helpers.errors.getMessage(err);
}

expect(errorMessage).toEqual(
'Failed to update editor image. Editor components is not defined.',
);
});

it('should update the target image', () => {
const customEditorImage = 'test-images/che-code:tag';

const editorContent = fs.readFileSync(
__dirname + '/fixtures/test-editor-devfile.yaml',
'utf-8',
);

const customEditorContent = updateEditorDevfile(editorContent, customEditorImage);

const output = fs.readFileSync(
__dirname + '/fixtures/test-editor-devfile-with-custom-image.yaml',
'utf-8',
);

expect(customEditorContent).toStrictEqual(output);
});
});

describe('devWorkspace template', () => {
it('should throw an error if editor components are not defined', () => {
const customEditorImage = 'test-images/che-code:tag';

const devWorkspaceTemplate = load(
fs.readFileSync(
__dirname + '/fixtures/test-devworkspace-template-without-components.yaml',
'utf-8',
),
) as devfileApi.DevWorkspaceTemplate;

let errorMessage: string | undefined;
try {
updateDevWorkspaceTemplate(devWorkspaceTemplate, customEditorImage);
} catch (err) {
errorMessage = common.helpers.errors.getMessage(err);
}

expect(errorMessage).toEqual(
'Failed to update editor image. Editor components is not defined.',
);
});

it('should update the target image', () => {
const customEditorImage = 'test-images/che-code:tag';

const devWorkspaceTemplate = load(
fs.readFileSync(__dirname + '/fixtures/test-devworkspace-template.yaml', 'utf-8'),
) as devfileApi.DevWorkspaceTemplate;

const customEditorContent = updateDevWorkspaceTemplate(
devWorkspaceTemplate,
customEditorImage,
);

const output = load(
fs.readFileSync(
__dirname + '/fixtures/test-devworkspace-template-with-custom-image.yaml',
'utf-8',
),
);

expect(customEditorContent).toStrictEqual(output);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schemaVersion: 2.2.0
metadata:
name: che-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: workspace.devfile.io/v1alpha2
kind: DevWorkspaceTemplate
metadata:
annotations:
che.eclipse.org/components-update-policy: manual
name: che-code
spec:
components:
- attributes:
controller.devfile.io/container-contribution: true
container:
image: test-images/che-code:tag
name: che-code-runtime-description
- name: checode
volume: {}
- container:
image: quay.io/che-incubator/che-code:next
name: che-code-injector
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: workspace.devfile.io/v1alpha2
kind: DevWorkspaceTemplate
metadata:
annotations:
che.eclipse.org/components-update-policy: managed
name: che-code
spec: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: workspace.devfile.io/v1alpha2
kind: DevWorkspaceTemplate
metadata:
annotations:
che.eclipse.org/components-update-policy: managed
name: che-code
spec:
components:
- attributes:
controller.devfile.io/container-contribution: true
container:
image: quay.io/devfile/universal-developer-image:next
name: che-code-runtime-description
- name: checode
volume: {}
- container:
image: quay.io/che-incubator/che-code:next
name: che-code-injector
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schemaVersion: 2.2.0
metadata:
name: che-code
components:
- name: che-code-runtime-description
container:
image: test-images/che-code:tag
attributes:
controller.devfile.io/container-contribution: true
- name: checode
volume: {}
- name: che-code-injector
container:
image: quay.io/che-incubator/che-code:next
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schemaVersion: 2.2.0
metadata:
name: che-code
components:
- name: che-code-runtime-description
container:
image: quay.io/devfile/universal-developer-image:next
attributes:
controller.devfile.io/container-contribution: true
- name: checode
volume: {}
- name: che-code-injector
container:
image: quay.io/che-incubator/che-code:next
Loading

0 comments on commit 203577f

Please sign in to comment.