Skip to content

Commit 81feab7

Browse files
committed
fix: code refactoring
Signed-off-by: Oleksii Orel <[email protected]>
1 parent 2f00bc8 commit 81feab7

File tree

6 files changed

+32
-42
lines changed

6 files changed

+32
-42
lines changed

.deps/dev.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@
279279
| [`[email protected]`](https://github.com/sindresorhus/locate-path.git) | MIT | clearlydefined |
280280
| [`[email protected]`](https://github.com/lodash/lodash.git) | MIT | clearlydefined |
281281
| [`[email protected]`](https://github.com/lodash/lodash.git) | MIT | clearlydefined |
282-
| [`[email protected]`](https://github.com/lodash/lodash.git) | MIT | #2096 |
283282
| [`[email protected]`](git://github.com/isaacs/node-lru-cache.git) | ISC | clearlydefined |
284283
| [`[email protected]`](https://github.com/sindresorhus/make-dir.git) | MIT | clearlydefined |
285284
| [`[email protected]`](git://github.com/JsCommunity/make-error.git) | ISC | clearlydefined |

.deps/prod.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| [`[email protected]`](https://github.com/microsoft/node-jsonc-parser) | MIT | #15491 |
2424
| [`[email protected]`]([email protected]:jprichardson/node-jsonfile.git) | MIT | clearlydefined |
2525
| [`[email protected]`](git://github.com/tdegrunt/jsonschema.git) | MIT | clearlydefined |
26+
| [`[email protected]`](https://github.com/lodash/lodash.git) | MIT | #2096 |
2627
| [`[email protected]`](https://github.com/jshttp/mime-db.git) | MIT | clearlydefined |
2728
| [`[email protected]`](https://github.com/jshttp/mime-types.git) | MIT | clearlydefined |
2829
| [`[email protected]`](https://github.com/bitinn/node-fetch.git) | MIT | #10509 |

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"axios": "^1.7.4",
4545
"fs-extra": "^11.2.0",
4646
"inversify": "^6.0.2",
47+
"lodash": "^4.17.21",
4748
"js-yaml": "^4.0.0",
4849
"jsonc-parser": "^3.0.0",
4950
"jsonschema": "^1.4.1",

src/generate.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
V1alpha2DevWorkspaceTemplateSpec,
1919
} from '@devfile/api';
2020
import { injectable, inject } from 'inversify';
21+
import { cloneDeep, merge } from 'lodash';
2122
import * as jsYaml from 'js-yaml';
2223
import * as fs from 'fs-extra';
2324
import { DevfileContext } from './api/devfile-context';
@@ -29,6 +30,8 @@ type DevfileLike = V230Devfile & {
2930
};
3031
};
3132

33+
const DEVWORKSPACE_METADATA_ANNOTATION = 'dw.metadata.annotations';
34+
3235
@injectable()
3336
export class Generate {
3437
@inject(DevContainerComponentFinder)
@@ -91,30 +94,15 @@ export class Generate {
9194
};
9295

9396
// transform it into a devWorkspace
94-
const devfileMetadata = this.createDevWorkspaceMetadata(devfile);
95-
const devfileCopy: V230Devfile = Object.assign({}, devfile);
97+
const devfileCopy: V230Devfile = cloneDeep(devfile);
9698
if (devfileCopy.metadata.attributes) {
9799
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 (
103-
typeof devfileCopy.attributes[key] === 'object' &&
104-
typeof devfileCopy.metadata.attributes[key] === 'object'
105-
) {
106-
devfileCopy.attributes[key] = Object.assign(
107-
devfileCopy.metadata.attributes[key],
108-
devfileCopy.attributes[key],
109-
);
110-
}
111-
}
112-
}
113-
Object.assign(devfileCopy.attributes, devfileCopy.metadata.attributes);
100+
devfileCopy.attributes = merge(devfileCopy.attributes, devfileCopy.metadata.attributes);
114101
} else {
115102
devfileCopy.attributes = devfileCopy.metadata.attributes;
116103
}
117104
}
105+
const devWorkspaceMetadata = this.createDevWorkspaceMetadata(devfileCopy as DevfileLike);
118106
delete devfileCopy.schemaVersion;
119107
delete devfileCopy.metadata;
120108
const editorSpecContribution: V1alpha2DevWorkspaceSpecContributions = {
@@ -126,7 +114,7 @@ export class Generate {
126114
const devWorkspace: V1alpha2DevWorkspace = {
127115
apiVersion: 'workspace.devfile.io/v1alpha2',
128116
kind: 'DevWorkspace',
129-
metadata: devfileMetadata,
117+
metadata: devWorkspaceMetadata,
130118
spec: {
131119
started: true,
132120
routingClass: 'che',
@@ -168,6 +156,13 @@ export class Generate {
168156
if (devfileMetadata.generateName) {
169157
devWorkspaceMetadata.generateName = devfileMetadata.generateName;
170158
}
159+
if (devfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]) {
160+
devWorkspaceMetadata.annotations = Object.assign({}, devfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION]);
161+
delete devfile.attributes?.[DEVWORKSPACE_METADATA_ANNOTATION];
162+
if (Object.keys(devfile.attributes).length === 0) {
163+
delete devfile.attributes;
164+
}
165+
}
171166

172167
return devWorkspaceMetadata;
173168
}

tests/generate.spec.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -522,18 +522,15 @@ metadata:
522522
kind: 'DevWorkspace',
523523
metadata: {
524524
name: 'my-dummy-project',
525+
annotations: {
526+
'che.eclipse.org/devfile-source':
527+
'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
528+
},
525529
},
526530
spec: {
527531
started: true,
528532
routingClass: 'che',
529-
template: {
530-
attributes: {
531-
'dw.metadata.annotations': {
532-
'che.eclipse.org/devfile-source':
533-
'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
534-
},
535-
},
536-
},
533+
template: {},
537534
contributions: [{ name: 'editor', kubernetes: { name: 'che-code-my-dummy-project' } }],
538535
},
539536
};
@@ -566,18 +563,18 @@ metadata:
566563
kind: 'DevWorkspace',
567564
metadata: {
568565
name: 'my-dummy-project',
566+
annotations: {
567+
'che.eclipse.org/devfile-source':
568+
'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
569+
'che.eclipse.org/devfile': 'schemaVersion: 2.2.0\nmetadata:\n name: my-dummy-project',
570+
},
569571
},
570572
spec: {
571573
started: true,
572574
routingClass: 'che',
573575
template: {
574576
attributes: {
575577
dummy: 'dummy',
576-
'dw.metadata.annotations': {
577-
'che.eclipse.org/devfile-source':
578-
'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
579-
'che.eclipse.org/devfile': 'schemaVersion: 2.2.0\nmetadata:\n name: my-dummy-project',
580-
},
581578
},
582579
},
583580
contributions: [{ name: 'editor', kubernetes: { name: 'che-code-my-dummy-project' } }],
@@ -609,18 +606,15 @@ metadata:
609606
kind: 'DevWorkspace',
610607
metadata: {
611608
name: 'my-dummy-project',
609+
annotations: {
610+
'che.eclipse.org/devfile-source':
611+
'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
612+
},
612613
},
613614
spec: {
614615
started: true,
615616
routingClass: 'che',
616-
template: {
617-
attributes: {
618-
'dw.metadata.annotations': {
619-
'che.eclipse.org/devfile-source':
620-
'scm:\n repo: https://github.com/dummy-repo.git\n fileName: devfile.yaml\nfactory:\n params: storageType=ephemeral\n',
621-
},
622-
},
623-
},
617+
template: {},
624618
contributions: [{ name: 'editor', kubernetes: { name: 'che-code-my-dummy-project' } }],
625619
},
626620
};

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2531,7 +2531,7 @@ lodash.merge@^4.6.2:
25312531
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
25322532
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
25332533

2534-
lodash@^4.7.0:
2534+
lodash@^4.17.21, lodash@^4.7.0:
25352535
version "4.17.21"
25362536
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
25372537
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

0 commit comments

Comments
 (0)