diff --git a/plugins/factory-plugin/src/projects.ts b/plugins/factory-plugin/src/projects.ts index a17dddf03..e5ea41b12 100644 --- a/plugins/factory-plugin/src/projects.ts +++ b/plugins/factory-plugin/src/projects.ts @@ -27,41 +27,43 @@ export function updateOrCreateGitProjectInDevfile( projectGitLocation: string, projectGitRemoteBranch: string ): cheApi.workspace.devfile.Project[] { - if (!projects) { projects = []; } - for (const project of projects) { - const currentProjectPath = project.clonePath ? project.clonePath : project.name; - if (currentProjectPath === projectPath) { - project.source.type = 'git'; - project.source.location = projectGitLocation; - project.source.branch = projectGitRemoteBranch; - - return projects; + const filteredProject = projects.filter(project => (project.clonePath ? project.clonePath : project.name) === projectPath); + if (filteredProject.length === 0) { + const projectName = projectPath.split('/').pop(); + if (projectPath === projectName) { + projectPath = undefined; } + // create a new one + projects.push({ + name: projectName ? projectName : 'new-project', + source: { + location: projectGitLocation, + type: 'git', + branch: projectGitRemoteBranch + }, + clonePath: projectPath + }); + return projects; } - // project not found in the list of existed, create a new one - const projectPathSegments = projectPath.split('/'); - const isCustomPath = projectPathSegments.filter(segment => segment !== '').length !== 1; - const projectName = projectPathSegments.pop(); - - const newProject: cheApi.workspace.devfile.Project = { - 'name': projectName ? projectName : 'new-project', - 'source': { - 'type': 'git', - 'location': projectGitLocation, - 'branch': projectGitRemoteBranch + filteredProject.forEach(project => { + if (!project.source) { + project.source = { + location: projectGitLocation, + type: 'git', + branch: projectGitRemoteBranch + }; } - }; - - if (isCustomPath) { - newProject.clonePath = projectPath; - } - - projects.push(newProject); + project.source.location = projectGitLocation; + project.source.branch = projectGitRemoteBranch; + delete project.source.startPoint; + delete project.source.tag; + delete project.source.commitId; + }); return projects; } @@ -77,9 +79,8 @@ export function deleteProjectFromDevfile( projects: cheApi.workspace.devfile.Project[], projectPath: string ): cheApi.workspace.devfile.Project[] { - - if (!projects || projects.length === 0) { - return []; + if (!projects) { + projects = []; } for (let i = 0; i < projects.length; i++) { @@ -153,7 +154,8 @@ export function deleteProjectFromWorkspaceConfig( ): cheApi.workspace.ProjectConfig[] { for (let i = 0; i < projects.length; i++) { const project = projects[i]; - if (project.path === projectPath) { + const currentProjectPath = project.path ? project.path : project.name; + if (currentProjectPath === projectPath) { projects.splice(i, 1); break; } diff --git a/plugins/factory-plugin/tests/projects.spec.ts b/plugins/factory-plugin/tests/projects.spec.ts index 30a1e8be8..2f4652002 100644 --- a/plugins/factory-plugin/tests/projects.spec.ts +++ b/plugins/factory-plugin/tests/projects.spec.ts @@ -12,264 +12,229 @@ import { che as cheApi } from '@eclipse-che/api'; import * as projecthelper from '../src/projects'; describe('Devfile: Projects:', () => { - const CHE_REPOSITORY = 'https://github.com/eclipse/che.git'; - const CHE_THEIA_REPOSITORY = 'https://github.com/eclipse/che-theia.git'; - const BRANCH1 = 'che-13112'; - const BRANCH2 = 'issue-12321'; - const CUSTOM_PROJECT_PATH = 'theia/packages/che-theia'; - - test('Should be able to create project if no projects defined', () => { - const projects: cheApi.workspace.devfile.Project[] = []; - - projecthelper.updateOrCreateGitProjectInDevfile( - projects, - 'che', - CHE_REPOSITORY, - BRANCH1 - ); - - expect(projects.length).toBe(1); - expect(projects[0].name).toBe('che'); - expect(projects[0].source.location).toBe(CHE_REPOSITORY); - expect(projects[0].source.branch).toBe(BRANCH1); - }); - - test('Should be able to add project into existing projects list', () => { - const projects: cheApi.workspace.devfile.Project[] = [ - { - name: 'che', - source: { - type: 'git', - location: CHE_REPOSITORY, - branch: BRANCH1 - } - } - ]; - - projecthelper.updateOrCreateGitProjectInDevfile( - projects, - 'che-theia', - CHE_THEIA_REPOSITORY, - BRANCH2 - ); - - expect(projects.length).toBe(2); - - expect(projects[0].name).toBe('che'); - expect(projects[0].source.location).toBe(CHE_REPOSITORY); - expect(projects[0].source.branch).toBe(BRANCH1); - - expect(projects[1].name).toBe('che-theia'); - expect(projects[1].source.location).toBe(CHE_THEIA_REPOSITORY); - expect(projects[1].source.branch).toBe(BRANCH2); - }); - - test('Should be able to delete existing project', () => { - const projects: cheApi.workspace.devfile.Project[] = [ - { - name: 'che', - source: { - type: 'git', - location: CHE_REPOSITORY, - branch: BRANCH1 - } - }, - { - name: 'che-theia', - source: { - type: 'git', - location: CHE_THEIA_REPOSITORY, - branch: BRANCH2 + describe('Testing basic functionality:', () => { + + test('Should be able to create project if no projects defined', () => { + const projects: cheApi.workspace.devfile.Project[] = []; + + projecthelper.updateOrCreateGitProjectInDevfile( + projects, + 'che', + 'https://github.com/eclipse/che.git', + 'che-13112' + ); + + expect(projects.length).toBe(1); + expect(projects[0].name).toBe('che'); + expect(projects[0].clonePath).toBe(undefined); + expect(projects[0].source.location).toBe('https://github.com/eclipse/che.git'); + expect(projects[0].source.branch).toBe('che-13112'); + }); + + test('Should be able to add project into existing projects list', () => { + const projects: cheApi.workspace.devfile.Project[] = [ + { + name: 'che', + source: { + type: 'git', + location: 'https://github.com/eclipse/che.git', + branch: 'che-13112' + } } - } - ]; - - projecthelper.deleteProjectFromDevfile( - projects, - 'che-theia' - ); - - expect(projects.length).toBe(1); - - expect(projects[0].name).toBe('che'); - expect(projects[0].source.location).toBe(CHE_REPOSITORY); - expect(projects[0].source.branch).toBe(BRANCH1); - }); - - test('Should be able to add project with custom location', () => { - const projects: cheApi.workspace.devfile.Project[] = [ - { - name: 'che', - source: { - type: 'git', - location: CHE_REPOSITORY, - branch: BRANCH1 + ]; + + projecthelper.updateOrCreateGitProjectInDevfile( + projects, + 'che-theia', + 'https://github.com/eclipse/che-theia.git', + 'issue-12321' + ); + + expect(projects.length).toBe(2); + expect(projects[0].name).toBe('che'); + expect(projects[0].clonePath).toBe(undefined); + expect(projects[0].source.location).toBe('https://github.com/eclipse/che.git'); + expect(projects[0].source.branch).toBe('che-13112'); + expect(projects[1].name).toBe('che-theia'); + expect(projects[1].clonePath).toBe(undefined); + expect(projects[1].source.location).toBe('https://github.com/eclipse/che-theia.git'); + expect(projects[1].source.branch).toBe('issue-12321'); + }); + + test('Should be able to delete existing project', () => { + const projects: cheApi.workspace.devfile.Project[] = [ + { + name: 'che', + source: { + type: 'git', + location: 'https://github.com/eclipse/che.git', + branch: 'che-13112' + } + }, + { + name: 'che-theia', + source: { + type: 'git', + location: 'https://github.com/eclipse/che-theia.git', + branch: 'issue-12321' + } } - } - ]; - - projecthelper.updateOrCreateGitProjectInDevfile( - projects, - CUSTOM_PROJECT_PATH, - CHE_THEIA_REPOSITORY, - BRANCH2 - ); - - expect(projects.length).toBe(2); - - expect(projects[0].name).toBe('che'); - expect(projects[0].source.location).toBe(CHE_REPOSITORY); - expect(projects[0].source.branch).toBe(BRANCH1); - - expect(projects[1].name).toBe('che-theia'); - expect(projects[1].clonePath).toBe(CUSTOM_PROJECT_PATH); - expect(projects[1].source.location).toBe(CHE_THEIA_REPOSITORY); - expect(projects[1].source.branch).toBe(BRANCH2); - }); - - test('Should be able to delete project with custom location', () => { - const projects: cheApi.workspace.devfile.Project[] = [ - { - name: 'che', - source: { - type: 'git', - location: CHE_REPOSITORY, - branch: BRANCH1 + ]; + + projecthelper.deleteProjectFromDevfile( + projects, + 'che-theia' + ); + + expect(projects.length).toBe(1); + expect(projects[0].name).toBe('che'); + expect(projects[0].source.location).toBe('https://github.com/eclipse/che.git'); + expect(projects[0].source.branch).toBe('che-13112'); + }); + + test('Should be able to add project with custom location', () => { + const projects: cheApi.workspace.devfile.Project[] = [ + { + name: 'che', + source: { + type: 'git', + location: 'https://github.com/eclipse/che.git', + branch: 'che-13112' + } } - }, - { - name: 'che-theia', - clonePath: CUSTOM_PROJECT_PATH, - source: { - type: 'git', - location: CHE_THEIA_REPOSITORY, - branch: BRANCH2 + ]; + + projecthelper.updateOrCreateGitProjectInDevfile( + projects, + 'theia/packages/che-theia', + 'https://github.com/eclipse/che-theia.git', + 'issue-12321' + ); + + expect(projects.length).toBe(2); + expect(projects[0].name).toBe('che'); + expect(projects[0].clonePath).toBe(undefined); + expect(projects[0].source.location).toBe('https://github.com/eclipse/che.git'); + expect(projects[0].source.branch).toBe('che-13112'); + expect(projects[1].name).toBe('che-theia'); + expect(projects[1].clonePath).toBe('theia/packages/che-theia'); + expect(projects[1].source.location).toBe('https://github.com/eclipse/che-theia.git'); + expect(projects[1].source.branch).toBe('issue-12321'); + }); + + test('Should be able to delete project with custom location', () => { + const projects: cheApi.workspace.devfile.Project[] = [ + { + name: 'che', + source: { + type: 'git', + location: 'https://github.com/eclipse/che.git', + branch: 'che-13112' + } + }, + { + name: 'che-theia', + clonePath: 'theia/packages/che-theia', + source: { + type: 'git', + location: 'https://github.com/eclipse/che-theia.git', + branch: 'issue-12321' + } } - } - ]; - - projecthelper.deleteProjectFromDevfile( - projects, - CUSTOM_PROJECT_PATH - ); - - expect(projects.length).toBe(1); - - expect(projects[0].name).toBe('che'); - expect(projects[0].source.location).toBe(CHE_REPOSITORY); - expect(projects[0].source.branch).toBe(BRANCH1); + ]; + + projecthelper.deleteProjectFromDevfile( + projects, + 'theia/packages/che-theia' + ); + + expect(projects.length).toBe(1); + expect(projects[0].name).toBe('che'); + expect(projects[0].source.location).toBe('https://github.com/eclipse/che.git'); + expect(projects[0].source.branch).toBe('che-13112'); + }); }); -}); - -describe('Workspace config: Testing projects updater when file is triggered', () => { - test('update and create project', async () => { - const projects: cheApi.workspace.ProjectConfig[] = [ - { - 'name': 'theia', - 'attributes': {}, - 'source': { - 'location': 'https://github.com/theia-ide/theia.git', - 'type': 'git', - 'parameters': {} + describe('Testing projects updater when file is triggered:', () => { + test('update and create project', async () => { + const projects: cheApi.workspace.devfile.Project[] = [ + { + 'name': 'theia', + 'source': { + 'type': 'git', + 'location': 'https://github.com/theia-ide/theia.git' + } }, - 'path': '/theia', - 'description': '', - 'mixins': [], - 'problems': [] - }, - { - 'links': [], - 'name': 'che-theia-factory-extension', - 'attributes': {}, - 'type': 'blank', - 'source': { - 'location': 'https://github.com/eclipse/che-theia-factory-extension.git', - 'type': 'git', - 'parameters': { + { + 'name': 'che-theia-factory-extension', + 'source': { + 'type': 'git', + 'location': 'https://github.com/eclipse/che-theia-factory-extension.git', 'branch': 'master', 'tag': 'v42.0' } - }, - 'path': '/che-theia-factory-extension', - 'description': '', - 'mixins': [], - 'problems': [] - } - ]; - expect(projects[0].source.location).toBe('https://github.com/theia-ide/theia.git'); - expect(projects[1].source.location).toBe('https://github.com/eclipse/che-theia-factory-extension.git'); - - projecthelper.updateOrCreateGitProjectInWorkspaceConfig(projects, - '/che-theia-factory-extension', - 'https://github.com/sunix/che-theia-factory-extension.git', - 'wip-sunix'); - expect(projects[1].source.location).toBe('https://github.com/sunix/che-theia-factory-extension.git'); - expect(projects[1].source.parameters['branch']).toBe('wip-sunix'); - expect(projects[1].source.parameters['tag']).toBe(undefined); - - projecthelper.updateOrCreateGitProjectInWorkspaceConfig(projects, - '/che/che-theia-factory-extension', - 'https://github.com/sunix/che-theia-factory-extension.git', - 'wip-theia'); - expect(projects[2].source.location).toBe('https://github.com/sunix/che-theia-factory-extension.git'); - expect(projects[2].source.parameters['branch']).toBe('wip-theia'); - expect(projects[2].name).toBe('che-theia-factory-extension'); - }); - - test('delete project', async () => { - const projects: cheApi.workspace.ProjectConfig[] = [ - { - 'name': 'theia', - 'attributes': {}, - 'source': { - 'location': 'https://github.com/theia-ide/theia.git', - 'type': 'git', - 'parameters': {} - }, - 'path': '/theia', - 'description': '', - 'mixins': [], - 'problems': [] - }, - { - 'links': [], - 'name': 'che-theia-factory-extension', - 'attributes': {}, - 'type': 'blank', - 'source': { - 'location': 'https://github.com/eclipse/che-theia-factory-extension.git', - 'type': 'git', - 'parameters': { - 'branch': 'master' + } + ]; + expect(projects[0].source.location).toBe('https://github.com/theia-ide/theia.git'); + expect(projects[1].source.location).toBe('https://github.com/eclipse/che-theia-factory-extension.git'); + + projecthelper.updateOrCreateGitProjectInDevfile(projects, + 'che-theia-factory-extension', + 'https://github.com/sunix/che-theia-factory-extension.git', + 'wip-sunix'); + expect(projects[1].source.location).toBe('https://github.com/sunix/che-theia-factory-extension.git'); + expect(projects[1].source['branch']).toBe('wip-sunix'); + expect(projects[1].source['tag']).toBe(undefined); + + projecthelper.updateOrCreateGitProjectInDevfile(projects, + '/che/che-theia-factory-extension', + 'https://github.com/sunix/che-theia-factory-extension.git', + 'wip-theia'); + expect(projects[2].source.location).toBe('https://github.com/sunix/che-theia-factory-extension.git'); + expect(projects[2].source['branch']).toBe('wip-theia'); + expect(projects[2].name).toBe('che-theia-factory-extension'); + }); + + test('delete project', async () => { + const projects: cheApi.workspace.devfile.Project[] = [ + { + 'name': 'theia', + 'source': { + 'type': 'git', + 'location': 'https://github.com/theia-ide/theia.git' } }, - 'path': '/che-theia-factory-extension', - 'description': '', - 'mixins': [], - 'problems': [] - } - ]; - expect(projects[0].source.location).toBe('https://github.com/theia-ide/theia.git'); - expect(projects[1].source.location).toBe('https://github.com/eclipse/che-theia-factory-extension.git'); - - projecthelper.deleteProjectFromWorkspaceConfig(projects, '/che-theia-factory-extension'); - expect(projects.length).toBe(1); - expect(projects[0].source.location).toBe('https://github.com/theia-ide/theia.git'); - - projecthelper.deleteProjectFromWorkspaceConfig(projects, '/theia'); - expect(projects.length).toBe(0); - - projecthelper.updateOrCreateGitProjectInWorkspaceConfig(projects, - '/che/che-theia-factory-extension', - 'https://github.com/sunix/che-theia-factory-extension.git', - 'wip-theia'); - expect(projects.length).toBe(1); - expect(projects[0].source.location).toBe('https://github.com/sunix/che-theia-factory-extension.git'); - expect(projects[0].source.parameters['branch']).toBe('wip-theia'); - expect(projects[0].name).toBe('che-theia-factory-extension'); - - projecthelper.deleteProjectFromWorkspaceConfig(projects, '/che/che-theia-factory-extension'); - expect(projects.length).toBe(0); + { + 'name': 'che-theia-factory-extension', + 'source': { + 'type': 'git', + 'location': 'https://github.com/eclipse/che-theia-factory-extension.git', + 'branch': 'master', + 'tag': 'v42.0' + } + } + ]; + expect(projects[0].source.location).toBe('https://github.com/theia-ide/theia.git'); + expect(projects[1].source.location).toBe('https://github.com/eclipse/che-theia-factory-extension.git'); + + projecthelper.deleteProjectFromDevfile(projects, 'che-theia-factory-extension'); + expect(projects.length).toBe(1); + expect(projects[0].source.location).toBe('https://github.com/theia-ide/theia.git'); + + projecthelper.deleteProjectFromDevfile(projects, 'theia'); + expect(projects.length).toBe(0); + + projecthelper.updateOrCreateGitProjectInDevfile(projects, + 'che/che-theia-factory-extension', + 'https://github.com/sunix/che-theia-factory-extension.git', + 'wip-theia'); + expect(projects.length).toBe(1); + expect(projects[0].source.location).toBe('https://github.com/sunix/che-theia-factory-extension.git'); + expect(projects[0].source.branch).toBe('wip-theia'); + expect(projects[0].name).toBe('che-theia-factory-extension'); + + projecthelper.deleteProjectFromDevfile(projects, 'che/che-theia-factory-extension'); + expect(projects.length).toBe(0); + }); }); });