Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat: add an attribute with starterProject to the devworkspace (#786)
Browse files Browse the repository at this point in the history
* chore: add attribute with starterProject to the attributes

Signed-off-by: Valeriy Svydenko <[email protected]>

---------

Signed-off-by: Valeriy Svydenko <[email protected]>
  • Loading branch information
svor authored Sep 20, 2023
1 parent 3276b98 commit bbd66d3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tools/devworkspace-generator/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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];

Expand Down
77 changes: 77 additions & 0 deletions tools/devworkspace-generator/tests/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down

0 comments on commit bbd66d3

Please sign in to comment.