Skip to content

Commit 1e5d7b2

Browse files
committed
fix: Add support for devfile attributes for schema version 2.0.0
Signed-off-by: Oleksii Orel <[email protected]>
1 parent 8f851b4 commit 1e5d7b2

File tree

2 files changed

+146
-25
lines changed

2 files changed

+146
-25
lines changed

src/generate.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,27 @@ export class Generate {
9191
};
9292

9393
// transform it into a devWorkspace
94-
const devfileMetadata = this.createDevWorkspaceMetadata(devfile, true);
94+
const devfileMetadata = this.createDevWorkspaceMetadata(devfile);
9595
const devfileCopy: V230Devfile = Object.assign({}, devfile);
96+
if (devfileCopy.metadata.attributes) {
97+
if (devfileCopy.attributes) {
98+
for (const key in devfileCopy.metadata.attributes) {
99+
if (devfileCopy.attributes[key] === undefined) {
100+
devfileCopy.attributes[key] = devfileCopy.metadata.attributes[key];
101+
} else {
102+
if (typeof devfileCopy.attributes[key] === 'object' && typeof devfileCopy.metadata.attributes[key] === 'object') {
103+
devfileCopy.attributes[key] = Object.assign(
104+
devfileCopy.metadata.attributes[key],
105+
devfileCopy.attributes[key],
106+
);
107+
}
108+
}
109+
}
110+
Object.assign(devfileCopy.attributes, devfileCopy.metadata.attributes);
111+
} else {
112+
devfileCopy.attributes = devfileCopy.metadata.attributes;
113+
}
114+
}
96115
delete devfileCopy.schemaVersion;
97116
delete devfileCopy.metadata;
98117
const editorSpecContribution: V1alpha2DevWorkspaceSpecContributions = {
@@ -136,7 +155,7 @@ export class Generate {
136155
return context;
137156
}
138157

139-
private createDevWorkspaceMetadata(devfile: DevfileLike, addDevfileContent = false): V1alpha2DevWorkspaceMetadata {
158+
private createDevWorkspaceMetadata(devfile: DevfileLike): V1alpha2DevWorkspaceMetadata {
140159
const devWorkspaceMetadata = {} as V1alpha2DevWorkspaceMetadata;
141160
const devfileMetadata = devfile.metadata;
142161

@@ -146,11 +165,6 @@ export class Generate {
146165
if (devfileMetadata.generateName) {
147166
devWorkspaceMetadata.generateName = devfileMetadata.generateName;
148167
}
149-
if (addDevfileContent) {
150-
devWorkspaceMetadata.annotations = {
151-
'che.eclipse.org/devfile': jsYaml.dump(devfile),
152-
};
153-
}
154168

155169
return devWorkspaceMetadata;
156170
}

tests/generate.spec.ts

Lines changed: 125 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ metadata:
6363
kind: 'DevWorkspace',
6464
metadata: {
6565
name: 'my-dummy-project',
66-
annotations: {
67-
'che.eclipse.org/devfile': jsYaml.dump(jsYaml.load(devfileContent)),
68-
},
6966
},
7067
spec: {
7168
started: true,
@@ -130,9 +127,6 @@ metadata:
130127
kind: 'DevWorkspace',
131128
metadata: {
132129
name: 'starter-project',
133-
annotations: {
134-
'che.eclipse.org/devfile': jsYaml.dump(jsYaml.load(devfileContent)),
135-
},
136130
},
137131
spec: {
138132
started: true,
@@ -203,9 +197,6 @@ metadata:
203197
kind: 'DevWorkspace',
204198
metadata: {
205199
name: 'my-dummy-project',
206-
annotations: {
207-
'che.eclipse.org/devfile': jsYaml.dump(jsYaml.load(devfileContent)),
208-
},
209200
},
210201
spec: {
211202
started: true,
@@ -270,9 +261,6 @@ metadata:
270261
kind: 'DevWorkspace',
271262
metadata: {
272263
generateName: 'custom-project',
273-
annotations: {
274-
'che.eclipse.org/devfile': jsYaml.dump(jsYaml.load(devfileContent)),
275-
},
276264
},
277265
spec: {
278266
started: true,
@@ -338,9 +326,6 @@ metadata:
338326
kind: 'DevWorkspace',
339327
metadata: {
340328
name: 'my-dummy-project',
341-
annotations: {
342-
'che.eclipse.org/devfile': jsYaml.dump(jsYaml.load(devfileContent)),
343-
},
344329
},
345330
spec: {
346331
started: true,
@@ -406,9 +391,6 @@ metadata:
406391
kind: 'DevWorkspace',
407392
metadata: {
408393
name: 'my-dummy-project',
409-
annotations: {
410-
'che.eclipse.org/devfile': jsYaml.dump(jsYaml.load(devfileContent)),
411-
},
412394
},
413395
spec: {
414396
started: true,
@@ -519,4 +501,129 @@ metadata:
519501
expect(context.devWorkspace.spec?.template?.components?.[0].name).toBe('dev');
520502
expect(context.devWorkspace.spec?.template?.components?.[0].container?.image).toBe(image);
521503
});
504+
505+
describe('Has attributes', () => {
506+
test('devfile schema 2.0', async () => {
507+
const devfileContent = `
508+
schemaVersion: 2.0.0
509+
metadata:
510+
name: my-dummy-project
511+
attributes:
512+
dw.metadata.annotations:
513+
che.eclipse.org/devfile-source: "scm:\\n repo: https://github.com/dummy-repo.git\\n fileName: devfile.yaml\\nfactory:\\n params: storageType=ephemeral\\n"
514+
`;
515+
const editorContent = `
516+
schemaVersion: 2.2.0
517+
metadata:
518+
name: che-code
519+
`;
520+
521+
let context = await generate.generate(devfileContent, editorContent);
522+
523+
const expectedDevWorkspace = {
524+
apiVersion: 'workspace.devfile.io/v1alpha2',
525+
kind: 'DevWorkspace',
526+
metadata: {
527+
name: 'my-dummy-project',
528+
},
529+
spec: {
530+
started: true,
531+
routingClass: 'che',
532+
template: {
533+
attributes: {
534+
'dw.metadata.annotations': {
535+
'che.eclipse.org/devfile-source': 'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
536+
},
537+
},
538+
},
539+
contributions: [{ name: 'editor', kubernetes: { name: 'che-code-my-dummy-project' } }],
540+
},
541+
};
542+
expect(context.devWorkspace).toStrictEqual(expectedDevWorkspace);
543+
});
544+
545+
test('devfile schema greater than 2.0 with deprecated metadata', async () => {
546+
const devfileContent = `
547+
schemaVersion: 2.2.0
548+
metadata:
549+
name: my-dummy-project
550+
attributes:
551+
dummy: dummy
552+
dw.metadata.annotations:
553+
che.eclipse.org/devfile-source: "scm:\\n repo: https://github.com/dummy-repo.git\\n fileName: devfile.yaml\\nfactory:\\n params: storageType=ephemeral\\n"
554+
attributes:
555+
dw.metadata.annotations:
556+
che.eclipse.org/devfile: "schemaVersion: 2.2.0\\nmetadata:\\n name: my-dummy-project"
557+
`;
558+
const editorContent = `
559+
schemaVersion: 2.2.0
560+
metadata:
561+
name: che-code
562+
`;
563+
564+
let context = await generate.generate(devfileContent, editorContent);
565+
566+
const expectedDevWorkspace = {
567+
apiVersion: 'workspace.devfile.io/v1alpha2',
568+
kind: 'DevWorkspace',
569+
metadata: {
570+
name: 'my-dummy-project',
571+
},
572+
spec: {
573+
started: true,
574+
routingClass: 'che',
575+
template: {
576+
attributes: {
577+
dummy: 'dummy',
578+
'dw.metadata.annotations': {
579+
'che.eclipse.org/devfile-source': 'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
580+
'che.eclipse.org/devfile': 'schemaVersion: 2.2.0\nmetadata:\n name: my-dummy-project',
581+
},
582+
},
583+
},
584+
contributions: [{ name: 'editor', kubernetes: { name: 'che-code-my-dummy-project' } }],
585+
},
586+
};
587+
expect(context.devWorkspace).toStrictEqual(expectedDevWorkspace);
588+
});
589+
});
590+
591+
test('devfile schema greater than 2.0', async () => {
592+
const devfileContent = `
593+
schemaVersion: 2.2.0
594+
metadata:
595+
name: my-dummy-project
596+
attributes:
597+
dw.metadata.annotations:
598+
che.eclipse.org/devfile-source: "scm:\\n repo: https://github.com/dummy-repo.git\\n fileName: devfile.yaml\\nfactory:\\n params: storageType=ephemeral\\n"
599+
`;
600+
const editorContent = `
601+
schemaVersion: 2.2.0
602+
metadata:
603+
name: che-code
604+
`;
605+
606+
let context = await generate.generate(devfileContent, editorContent);
607+
608+
const expectedDevWorkspace = {
609+
apiVersion: 'workspace.devfile.io/v1alpha2',
610+
kind: 'DevWorkspace',
611+
metadata: {
612+
name: 'my-dummy-project',
613+
},
614+
spec: {
615+
started: true,
616+
routingClass: 'che',
617+
template: {
618+
attributes: {
619+
'dw.metadata.annotations': {
620+
'che.eclipse.org/devfile-source': 'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
621+
},
622+
},
623+
},
624+
contributions: [{ name: 'editor', kubernetes: { name: 'che-code-my-dummy-project' } }],
625+
},
626+
};
627+
expect(context.devWorkspace).toStrictEqual(expectedDevWorkspace);
628+
});
522629
});

0 commit comments

Comments
 (0)