diff --git a/extensions/eclipse-che-theia-plugin-ext/src/browser/che-workspace-main.ts b/extensions/eclipse-che-theia-plugin-ext/src/browser/che-workspace-main.ts index 5089fa609..46ddd8ed6 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/browser/che-workspace-main.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/browser/che-workspace-main.ts @@ -41,6 +41,10 @@ export class CheWorkspaceMainImpl implements CheWorkspaceMain { ); } + $getCurrentNamespace(): Promise { + return this.workspaceService.getCurrentNamespace(); + } + async $getById(workspaceId: string): Promise { return this.workspaceService.getWorkspaceById(workspaceId).then( workspace => workspace, diff --git a/extensions/eclipse-che-theia-plugin-ext/src/common/che-protocol.ts b/extensions/eclipse-che-theia-plugin-ext/src/common/che-protocol.ts index a488134bd..4ccbb6cfb 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/common/che-protocol.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/common/che-protocol.ts @@ -35,6 +35,7 @@ export interface CheWorkspace { export interface CheWorkspaceMain { $getCurrentWorkspace(): Promise; + $getCurrentNamespace(): Promise; // getAll(): Promise; // getAllByNamespace(namespace: string): Promise; $getById(workspaceId: string): Promise; diff --git a/extensions/eclipse-che-theia-plugin-ext/src/plugin/che-api.ts b/extensions/eclipse-che-theia-plugin-ext/src/plugin/che-api.ts index 1eb2598e4..a38caa9a8 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/plugin/che-api.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/plugin/che-api.ts @@ -66,6 +66,9 @@ export function createAPIFactory(rpc: RPCProtocol): CheApiFactory { getCurrentWorkspace(): Promise { return cheWorkspaceImpl.getCurrentWorkspace(); }, + getCurrentNamespace(): Promise { + return cheWorkspaceImpl.getCurrentNamespace(); + }, getAll(): Promise { return cheWorkspaceImpl.getAll(); }, diff --git a/extensions/eclipse-che-theia-plugin-ext/src/plugin/che-workspace.ts b/extensions/eclipse-che-theia-plugin-ext/src/plugin/che-workspace.ts index 3ef3d68fa..1112ee80d 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/plugin/che-workspace.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/plugin/che-workspace.ts @@ -64,6 +64,10 @@ export class CheWorkspaceImpl implements CheWorkspace { throw new Error('Method not implemented.'); } + getCurrentNamespace(): Promise { + return this.workspaceMain.$getCurrentNamespace(); + } + getAll(): Promise { throw new Error('Method not implemented.'); } diff --git a/extensions/eclipse-che-theia-plugin/src/che-proposed.d.ts b/extensions/eclipse-che-theia-plugin/src/che-proposed.d.ts index 69e63ab1e..2ea248f11 100644 --- a/extensions/eclipse-che-theia-plugin/src/che-proposed.d.ts +++ b/extensions/eclipse-che-theia-plugin/src/che-proposed.d.ts @@ -29,6 +29,7 @@ declare module '@eclipse-che/plugin' { export namespace workspace { export function getCurrentWorkspace(): Promise; + export function getCurrentNamespace(): Promise; export function getAll(): Promise; export function getAllByNamespace(namespace: string): Promise; export function getById(workspaceId: string): Promise; diff --git a/extensions/eclipse-che-theia-remote-api/src/common/workspace-service.ts b/extensions/eclipse-che-theia-remote-api/src/common/workspace-service.ts index 1194d2f4f..ab33bc936 100644 --- a/extensions/eclipse-che-theia-remote-api/src/common/workspace-service.ts +++ b/extensions/eclipse-che-theia-remote-api/src/common/workspace-service.ts @@ -55,6 +55,7 @@ export interface WorkspaceSettings { export const WorkspaceService = Symbol('WorkspaceService'); export interface WorkspaceService { + getCurrentNamespace(): Promise; getCurrentWorkspaceId(): Promise; currentWorkspace(): Promise; getWorkspaceById(workspaceId: string): Promise; diff --git a/extensions/eclipse-che-theia-remote-impl-che-server/src/node/che-server-workspace-service-impl.ts b/extensions/eclipse-che-theia-remote-impl-che-server/src/node/che-server-workspace-service-impl.ts index 51e2108c7..97bbcc98c 100644 --- a/extensions/eclipse-che-theia-remote-impl-che-server/src/node/che-server-workspace-service-impl.ts +++ b/extensions/eclipse-che-theia-remote-impl-che-server/src/node/che-server-workspace-service-impl.ts @@ -23,6 +23,8 @@ export class CheServerWorkspaceServiceImpl implements WorkspaceService { @inject(CheServerRemoteApiImpl) private cheServerRemoteApiImpl: CheServerRemoteApiImpl; + private INFRASTRUCTURE_NAMESPACE = 'infrastructureNamespace'; + /** * Workspace client based variables. * @@ -52,6 +54,11 @@ export class CheServerWorkspaceServiceImpl implements WorkspaceService { return this.workspaceId; } + public async getCurrentNamespace(): Promise { + const workspace = await this.currentWorkspace(); + return workspace.attributes?.[this.INFRASTRUCTURE_NAMESPACE] || workspace.namespace || ''; + } + public async currentWorkspace(): Promise { return this.cheServerRemoteApiImpl.getAPI().getById(this.workspaceId); } diff --git a/plugins/resource-monitor-plugin/__mocks__/@eclipse-che/plugin.ts b/plugins/resource-monitor-plugin/__mocks__/@eclipse-che/plugin.ts index 44521d620..9dfa9beb4 100644 --- a/plugins/resource-monitor-plugin/__mocks__/@eclipse-che/plugin.ts +++ b/plugins/resource-monitor-plugin/__mocks__/@eclipse-che/plugin.ts @@ -16,7 +16,6 @@ * @author Valerii Svydenko */ const che: any = {}; -che.devfile = {}; -che.devfile.metadata = {}; +che.workspace = {}; che.k8s = {}; module.exports = che; diff --git a/plugins/resource-monitor-plugin/src/plugin.ts b/plugins/resource-monitor-plugin/src/plugin.ts index 09bc3091b..b13ba53c2 100644 --- a/plugins/resource-monitor-plugin/src/plugin.ts +++ b/plugins/resource-monitor-plugin/src/plugin.ts @@ -27,7 +27,5 @@ export async function start(context: theia.PluginContext): Promise { } export async function getNamespace(): Promise { - // get namespace from devfile service - const devfile = await che.devfile.get(); - return devfile.metadata?.attributes ? devfile.metadata.attributes.infrastructureNamespace : ''; + return await che.workspace.getCurrentNamespace(); } diff --git a/plugins/resource-monitor-plugin/tests/plugin.spec.ts b/plugins/resource-monitor-plugin/tests/plugin.spec.ts index 834de87e1..0534b8c21 100644 --- a/plugins/resource-monitor-plugin/tests/plugin.spec.ts +++ b/plugins/resource-monitor-plugin/tests/plugin.spec.ts @@ -69,20 +69,14 @@ const context: theia.PluginContext = { describe('Test Plugin', () => { jest.mock('../src/inversify-binding'); - const devfileMock = jest.fn(); + const getCurrentNamespace = jest.fn(); let oldBindings: any; let initBindings: jest.Mock; beforeEach(() => { // Prepare Namespace - che.devfile.get = devfileMock; - const attributes = { infrastructureNamespace: 'che-namespace' }; - const devfile = { - metadata: { - attributes, - }, - }; - devfileMock.mockReturnValue(devfile); + che.workspace.getCurrentNamespace = getCurrentNamespace; + getCurrentNamespace.mockReturnValue('che-namespace'); oldBindings = InversifyBinding.prototype.initBindings; initBindings = jest.fn(); InversifyBinding.prototype.initBindings = initBindings; @@ -107,13 +101,5 @@ describe('Test Plugin', () => { const namespace = await plugin.getNamespace(); expect(namespace).toBe('che-namespace'); }); - test('read che namespace from workspace service if no infrastructureNamespace attribute in devile metadata', async () => { - const devfile = { - metadata: {}, - }; - devfileMock.mockReturnValue(devfile); - const namespace = await plugin.getNamespace(); - expect(namespace).toBe(''); - }); }); });