Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

feat(workspace-plugin): Update to new devfile 2.0 API #1021

Merged
merged 1 commit into from
Mar 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
13 changes: 13 additions & 0 deletions plugins/workspace-plugin/__mocks__/@eclipse-che/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**********************************************************************
* Copyright (c) 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
***********************************************************************/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chePlugin: any = {};
module.exports = chePlugin;
20 changes: 10 additions & 10 deletions plugins/workspace-plugin/__mocks__/@theia/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* Copyright (c) 2020 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials are made
/**********************************************************************
* Copyright (c) 2020-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/
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

/**
* Mock of @theia/plugin module
* @author Valerii Svydenko
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const theiaPlugin: any = {};
theiaPlugin.window = {};
module.exports = theiaPlugin;
32 changes: 7 additions & 25 deletions plugins/workspace-plugin/src/ephemeral-workspace-checker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2018-2020 Red Hat, Inc.
* 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
Expand All @@ -11,21 +11,18 @@
import * as che from '@eclipse-che/plugin';
import * as theia from '@theia/plugin';

import { che as cheApi } from '@eclipse-che/api';

/**
* Make checks on workspace ephemeral configuration and shows dedicated information to user.
*/
export class EphemeralWorkspaceChecker {
constructor() {}

public check(): void {
che.workspace.getCurrentWorkspace().then((workspace: cheApi.workspace.Workspace) => {
const isEphemeralWorkspace = this.isEphemeralWorkspace(workspace);
if (isEphemeralWorkspace) {
this.displayEphemeralWarning();
}
});
public async check(): Promise<void> {
const devfile = await che.devfile.get();
const isEphemeralWorkspace = devfile.metadata?.attributes && devfile.metadata.attributes.persistVolumes === 'false';
if (isEphemeralWorkspace) {
this.displayEphemeralWarning();
}
}

/**
Expand All @@ -39,19 +36,4 @@ export class EphemeralWorkspaceChecker {
item.color = '#fcc13d';
item.show();
}

/**
* Returns, whether provided workspace is ephmeral or not.
*/
private isEphemeralWorkspace(workspace: cheApi.workspace.Workspace): boolean {
if (workspace.devfile) {
const workspaceAttributes = workspace.devfile.attributes;
if (workspaceAttributes) {
return workspaceAttributes.persistVolumes === 'false';
}
return false;
} else {
return workspace.config!.attributes!.persistVolumes === 'false' || false;
}
}
}
8 changes: 4 additions & 4 deletions plugins/workspace-plugin/src/git.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Copyright (c) 2019-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -45,16 +45,16 @@ export async function getRemoteURL(remote: string, projectPath: string): Promise
export async function sparseCheckout(
projectPath: string,
repositoryUri: string,
sparseCheckoutDirectory: string,
sparseCheckoutDirectories: string[],
commitReference: string
): Promise<void> {
await initRepository(projectPath);
// Enable sparse checkout feature
await setConfig(projectPath, 'core.sparsecheckout', 'true');
// Write sparse checkout directory
const gitInfoFolderPath = path.join(projectPath, '.git/info/');
fs.ensureDirSync(gitInfoFolderPath);
fs.writeFileSync(path.join(gitInfoFolderPath, 'sparse-checkout'), sparseCheckoutDirectory);
await fs.ensureDir(gitInfoFolderPath);
await fs.writeFile(path.join(gitInfoFolderPath, 'sparse-checkout'), sparseCheckoutDirectories.join('\n'), 'utf-8');
// Add remote, pull changes and create the selected directory content
await execGit(projectPath, 'remote', 'add', '-f', 'origin', repositoryUri);
await execGit(projectPath, 'pull', 'origin', commitReference);
Expand Down
119 changes: 30 additions & 89 deletions plugins/workspace-plugin/src/projects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (c) 2019-2020 Red Hat, Inc.
* Copyright (c) 2019-2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -8,7 +8,7 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import { che as cheApi } from '@eclipse-che/api';
import * as che from '@eclipse-che/plugin';

// devfile projects handling

Expand All @@ -22,11 +22,11 @@ import { che as cheApi } from '@eclipse-che/api';
* @param projectGitRemoteBranch git branch of the project
*/
export function updateOrCreateGitProjectInDevfile(
projects: cheApi.workspace.devfile.Project[],
projects: che.devfile.DevfileProject[] | undefined,
projectPath: string | undefined,
projectGitLocation: string,
projectGitRemoteBranch: string
): cheApi.workspace.devfile.Project[] {
): che.devfile.DevfileProject[] {
if (!projects) {
projects = [];
}
Expand All @@ -42,29 +42,39 @@ export function updateOrCreateGitProjectInDevfile(
// create a new one
projects.push({
name: projectName ? projectName : 'new-project',
source: {
location: projectGitLocation,
type: 'git',
branch: projectGitRemoteBranch,
git: {
remotes: {
origin: projectGitLocation,
},
checkoutFrom: {
revision: projectGitRemoteBranch,
},
},
clonePath: projectPath,
});
return projects;
}

filteredProject.forEach(project => {
if (!project.source) {
project.source = {
location: projectGitLocation,
type: 'git',
branch: projectGitRemoteBranch,
if (!project.git) {
project.git = {
remotes: {
origin: projectGitLocation,
},
checkoutFrom: {
revision: projectGitRemoteBranch,
},
};
}
const defaultRemote = project.git.checkoutFrom?.remote || Object.keys(project.git.remotes)[0];
project.git.remotes[defaultRemote] = projectGitLocation;
if (!project.git.checkoutFrom) {
project.git.checkoutFrom = {
revision: projectGitRemoteBranch,
};
} else {
project.git.checkoutFrom.revision = projectGitRemoteBranch;
}
project.source.location = projectGitLocation;
project.source.branch = projectGitRemoteBranch;
delete project.source.startPoint;
delete project.source.tag;
delete project.source.commitId;
});

return projects;
Expand All @@ -78,9 +88,9 @@ export function updateOrCreateGitProjectInDevfile(
* @param projectPath relative path of the project to delete according to projets root directory
*/
export function deleteProjectFromDevfile(
projects: cheApi.workspace.devfile.Project[],
projects: che.devfile.DevfileProject[] | undefined,
projectPath: string
): cheApi.workspace.devfile.Project[] {
): che.devfile.DevfileProject[] {
if (!projects) {
projects = [];
}
Expand All @@ -96,72 +106,3 @@ export function deleteProjectFromDevfile(

return projects;
}

// workspace config projects handling

export function updateOrCreateGitProjectInWorkspaceConfig(
projects: cheApi.workspace.ProjectConfig[],
projectPath: string,
projectGitLocation: string,
projectGitRemoteBranch: string
): cheApi.workspace.ProjectConfig[] {
const filteredProject = projects.filter(project => project.path === projectPath);
if (filteredProject.length === 0) {
const projectName = projectPath.split('/').pop();

// create a new one
projects.push({
name: projectName ? projectName : 'new-project',
attributes: {},
source: {
location: projectGitLocation,
type: 'git',
parameters: {
branch: projectGitRemoteBranch,
},
},
path: projectPath,
description: '',
mixins: [],
});
return projects;
}

filteredProject.forEach(project => {
if (!project.source) {
project.source = {
type: 'git',
location: projectGitLocation,
parameters: {
branch: projectGitRemoteBranch,
},
};
}
project.source.location = projectGitLocation;
if (!project.source.parameters) {
project.source.parameters = {};
}
project.source.parameters['branch'] = projectGitRemoteBranch;
delete project.source.parameters['startPoint'];
delete project.source.parameters['tag'];
delete project.source.parameters['commitId'];
});

return projects;
}

export function deleteProjectFromWorkspaceConfig(
projects: cheApi.workspace.ProjectConfig[],
projectPath: string
): cheApi.workspace.ProjectConfig[] {
for (let i = 0; i < projects.length; i++) {
const project = projects[i];
const currentProjectPath = project.path ? project.path : project.name;
if (currentProjectPath === projectPath) {
projects.splice(i, 1);
break;
}
}

return projects;
}
Loading