Skip to content

Commit

Permalink
fixup! fix: editor update flow
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <[email protected]>
  • Loading branch information
olexii4 committed Feb 5, 2025
1 parent 53628ab commit 9a617d5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import devfileApi from '@/services/devfileApi';
import { EDITOR_DEVFILE_API_QUERY } from '@/store/DevfileRegistries/const';

const getVSCodeDevWorkspaceTemplate = (cpuLimit = '1500m'): devfileApi.DevWorkspaceTemplate => {
return {
Expand All @@ -19,8 +20,7 @@ const getVSCodeDevWorkspaceTemplate = (cpuLimit = '1500m'): devfileApi.DevWorksp
metadata: {
annotations: {
'che.eclipse.org/components-update-policy': 'managed',
'che.eclipse.org/plugin-registry-url':
'http://127.0.0.1:8080/dashboard/api/editors/devfile?che-editor=che-incubator/che-code/latest',
'che.eclipse.org/plugin-registry-url': `${EDITOR_DEVFILE_API_QUERY}che-incubator/che-code/latest`,
},
creationTimestamp: new Date('2024-05-30T12:51:45Z'),
generation: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
DevWorkspaceClient,
REGISTRY_URL,
} from '@/services/workspace-client/devworkspace/devWorkspaceClient';
import { EDITOR_DEVFILE_API_QUERY } from '@/store/DevfileRegistries/const';

describe('DevWorkspace client editor update', () => {
const namespace = 'admin-che';
Expand Down Expand Up @@ -257,8 +258,7 @@ describe('DevWorkspace client editor update', () => {
path: '/metadata/annotations',
value: {
[COMPONENT_UPDATE_POLICY]: 'managed',
[REGISTRY_URL]:
'http://127.0.0.1:8080/dashboard/api/editors/devfile?che-editor=che-incubator/che-code/latest',
[REGISTRY_URL]: `${EDITOR_DEVFILE_API_QUERY}che-incubator/che-code/latest`,
},
},
]);
Expand Down Expand Up @@ -305,8 +305,7 @@ describe('DevWorkspace client editor update', () => {
path: '/metadata/annotations',
value: {
[COMPONENT_UPDATE_POLICY]: 'managed',
[REGISTRY_URL]:
'http://127.0.0.1:8080/dashboard/api/editors/devfile?che-editor=che-incubator/custom-editor/latest',
[REGISTRY_URL]: `${EDITOR_DEVFILE_API_QUERY}che-incubator/custom-editor/latest`,
},
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
devWorkspaceVersion,
} from '@/services/workspace-client/devworkspace/converters';
import { DevWorkspaceDefaultPluginsHandler } from '@/services/workspace-client/devworkspace/DevWorkspaceDefaultPluginsHandler';
import { EDITOR_DEVFILE_API_QUERY } from '@/store/DevfileRegistries/const';
import { WorkspacesDefaultPlugins } from '@/store/Plugins/devWorkspacePlugins';

export const COMPONENT_UPDATE_POLICY = 'che.eclipse.org/components-update-policy';
Expand Down Expand Up @@ -668,18 +669,16 @@ export class DevWorkspaceClient {
title: string;
},
): Promise<api.IPatch[]> {
const managedTemplate = await DwtApi.getTemplateByName(namespace, editorName);

const patch: api.IPatch[] = [];

const managedTemplate = await DwtApi.getTemplateByName(namespace, editorName);
const editorIdOrPath = managedTemplate.metadata?.annotations?.[REGISTRY_URL];
const updatePolicy = managedTemplate.metadata?.annotations?.[COMPONENT_UPDATE_POLICY];

if (!editorIdOrPath || updatePolicy !== 'managed') {
console.log('Template is not managed');
return patch;
}
// ------------------------------------

if (/^(https?:\/\/)/.test(editorIdOrPath)) {
// Define a regular expression pattern to match URLs containing 'plugin-registry/v3/plugins'
// and ending with 'devfile.yaml'. The part between 'v3/plugins/' and '/devfile.yaml' is captured.
Expand All @@ -689,7 +688,7 @@ export class DevWorkspaceClient {
if (match) {
const annotations = {
[COMPONENT_UPDATE_POLICY]: 'managed',
[REGISTRY_URL]: `http://127.0.0.1:8080/dashboard/api/editors/devfile?che-editor=${match[1]}`,
[REGISTRY_URL]: `${EDITOR_DEVFILE_API_QUERY}${match[1]}`,
};
// Create a patch to update the annotations by replacing plugin registry URL with the editor reference
patch.push({
Expand All @@ -701,7 +700,7 @@ export class DevWorkspaceClient {
} else {
const annotations = {
[COMPONENT_UPDATE_POLICY]: 'managed',
[REGISTRY_URL]: `http://127.0.0.1:8080/dashboard/api/editors/devfile?che-editor=${editorIdOrPath}`,
[REGISTRY_URL]: `${EDITOR_DEVFILE_API_QUERY}${editorIdOrPath}`,
};
patch.push({
op: 'replace',
Expand All @@ -710,49 +709,48 @@ export class DevWorkspaceClient {
});
}

let plugin: devfileApi.Devfile | undefined = undefined;
let editor: devfileApi.Devfile | undefined = undefined;
// Found the target editors
if (editorIdOrPath.match(/^http:\/\/127.0.0.1:8080\/dashboard\/api\/editors\/devfile/)) {
const searchParams = new URLSearchParams(editorIdOrPath.split('?')[1]);
const editorReference = searchParams.get('che-editor');
if (editorIdOrPath.startsWith(EDITOR_DEVFILE_API_QUERY)) {
const editorReference = editorIdOrPath.replace(EDITOR_DEVFILE_API_QUERY, '');

const _plugin = editors.find(editor => {
const _editor = editors.find(e => {
return (
editor.metadata?.attributes?.publisher +
e.metadata?.attributes?.publisher +
'/' +
editor.metadata?.name +
e.metadata?.name +
'/' +
editor.metadata?.attributes?.version ===
e.metadata?.attributes?.version ===
editorReference
);
});
if (_plugin !== undefined) {
plugin = cloneDeep(_plugin);
if (_editor !== undefined) {
editor = cloneDeep(_editor);
}
} else {
const pluginContent = await fetchData<string | devfileApi.Devfile>(editorIdOrPath);
if (typeof pluginContent === 'string') {
plugin = load(pluginContent) as devfileApi.Devfile;
} else if (typeof pluginContent === 'object') {
plugin = pluginContent;
const editorContent = await fetchData<string | devfileApi.Devfile>(editorIdOrPath);
if (typeof editorContent === 'string') {
editor = load(editorContent) as devfileApi.Devfile;
} else if (typeof editorContent === 'object') {
editor = editorContent;
}
}

if (plugin === undefined) {
console.warn('Failed to get plugin');
if (editor === undefined) {
console.warn('Failed to get editor');
return patch;
}

const spec: Partial<V1alpha2DevWorkspaceTemplateSpec> = {};
for (const key in plugin) {
for (const key in editor) {
if (key !== 'schemaVersion' && key !== 'metadata') {
if (key === 'components') {
plugin.components?.forEach(component => {
editor.components?.forEach(component => {
if (component.container && !component.container.sourceMapping) {
component.container.sourceMapping = '/projects';
}
});
spec.components = plugin.components;
spec.components = editor.components;
this.addEnvVarsToContainers(
spec.components,
pluginRegistryUrl,
Expand All @@ -761,7 +759,7 @@ export class DevWorkspaceClient {
clusterConsole,
);
} else {
spec[key] = plugin[key];
spec[key] = editor[key];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { dump } from 'js-yaml';
import devfileApi from '@/services/devfileApi';
import { RootState } from '@/store';
import { actionCreators } from '@/store/DevfileRegistries/actions';
import { EDITOR_DEVFILE_API_QUERY } from '@/store/DevfileRegistries/const';
import { getEditor } from '@/store/DevfileRegistries/getEditor';
import { State } from '@/store/DevfileRegistries/reducer';

Expand Down Expand Up @@ -140,8 +141,7 @@ describe('getEditor', () => {

expect(result).toEqual({
content: 'dumped devfile content',
editorYamlUrl:
'http://127.0.0.1:8080/dashboard/api/editors/devfile?che-editor=che-incubator/che-idea/next',
editorYamlUrl: `${EDITOR_DEVFILE_API_QUERY}che-incubator/che-idea/next`,
});
expect(dispatch).not.toHaveBeenCalled();
});
Expand Down
14 changes: 14 additions & 0 deletions packages/dashboard-frontend/src/store/DevfileRegistries/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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
*/

export const EDITOR_DEVFILE_API_QUERY =
'http://127.0.0.1:8080/dashboard/api/editors/devfile?che-editor=';
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { dump } from 'js-yaml';
import devfileApi from '@/services/devfileApi';
import { RootState } from '@/store';
import { actionCreators } from '@/store/DevfileRegistries/actions';
import { EDITOR_DEVFILE_API_QUERY } from '@/store/DevfileRegistries/const';

export async function getEditor(
editorIdOrPath: string,
Expand Down Expand Up @@ -61,7 +62,7 @@ export async function getEditor(
if (editor) {
return {
content: dump(editor),
editorYamlUrl: `http://127.0.0.1:8080/dashboard/api/editors/devfile?che-editor=${editorId}`,
editorYamlUrl: `${EDITOR_DEVFILE_API_QUERY}${editorId}`,
};
} else {
throw new Error(`Failed to fetch editor yaml by id: ${editorIdOrPath}.`);
Expand Down

0 comments on commit 9a617d5

Please sign in to comment.