From bbd66d32dc312717b79319c478e66f5fc65778f9 Mon Sep 17 00:00:00 2001 From: Valerii Svydenko Date: Wed, 20 Sep 2023 13:57:21 +0300 Subject: [PATCH] feat: add an attribute with starterProject to the devworkspace (#786) * chore: add attribute with starterProject to the attributes Signed-off-by: Valeriy Svydenko --------- Signed-off-by: Valeriy Svydenko --- tools/devworkspace-generator/src/generate.ts | 9 ++- .../tests/generate.spec.ts | 77 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/tools/devworkspace-generator/src/generate.ts b/tools/devworkspace-generator/src/generate.ts index c1716231d..d5a0bbf5c 100644 --- a/tools/devworkspace-generator/src/generate.ts +++ b/tools/devworkspace-generator/src/generate.ts @@ -93,7 +93,7 @@ export class Generate { // transform it into a devWorkspace const devfileMetadata = this.createDevWorkspaceMetadata(devfile, true); - const devfileCopy = Object.assign({}, devfile); + const devfileCopy: V221Devfile = Object.assign({}, devfile); delete devfileCopy.schemaVersion; delete devfileCopy.metadata; const editorSpecContribution: V1alpha2DevWorkspaceSpecContributions = { @@ -114,6 +114,13 @@ export class Generate { }, }; + // if the devfile has a starter project, we use it for the devWorkspace + if (devfileCopy.starterProjects && devfileCopy.starterProjects.length > 0) { + devWorkspace.spec.template.attributes = { + 'controller.devfile.io/use-starter-project': devfileCopy.starterProjects[0].name, + }; + } + // for now the list of devWorkspace templates is only the editor template const devWorkspaceTemplates = [editorDevWorkspaceTemplate]; diff --git a/tools/devworkspace-generator/tests/generate.spec.ts b/tools/devworkspace-generator/tests/generate.spec.ts index df950c0b6..89c08aa83 100644 --- a/tools/devworkspace-generator/tests/generate.spec.ts +++ b/tools/devworkspace-generator/tests/generate.spec.ts @@ -94,6 +94,83 @@ metadata: }); }); + describe('Devfile contains starterProjects', () => { + test('basics', async () => { + const devfileContent = ` +schemaVersion: 2.2.0 +metadata: + name: starter-project +starterProjects: + - name: go-starter + description: A Go project + git: + checkoutFrom: + revision: main + remotes: + origin: https://github.com/devfile-samples/devfile-stack-go.git + - name: vertx-http-example + git: + remotes: + origin: https://github.com +`; + const editorContent = ` +schemaVersion: 2.2.0 +metadata: + name: che-code +`; + + const fsWriteFileSpy = jest.spyOn(fs, 'writeFile'); + fsWriteFileSpy.mockReturnValue({}); + + let context = await generate.generate(devfileContent, editorContent); + // expect not to write the file + expect(fsWriteFileSpy).not.toBeCalled(); + const expectedDevWorkspace = { + apiVersion: 'workspace.devfile.io/v1alpha2', + kind: 'DevWorkspace', + metadata: { + name: 'starter-project', + annotations: { + 'che.eclipse.org/devfile': jsYaml.dump(jsYaml.load(devfileContent)), + }, + }, + spec: { + started: true, + routingClass: 'che', + template: { + attributes: { + 'controller.devfile.io/use-starter-project': 'go-starter', + }, + starterProjects: [ + { + name: 'go-starter', + description: 'A Go project', + git: { + checkoutFrom: { + revision: 'main', + }, + remotes: { + origin: 'https://github.com/devfile-samples/devfile-stack-go.git', + }, + }, + }, + { + name: 'vertx-http-example', + git: { + remotes: { + origin: 'https://github.com', + }, + }, + }, + ], + }, + contributions: [{ name: 'editor', kubernetes: { name: 'che-code-starter-project' } }], + }, + }; + expect(context.devWorkspace).toStrictEqual(expectedDevWorkspace); + }); + }); + describe('Without writing an output file', () => { test('basics', async () => { const devfileContent = `