@eclipse-che/che-devworkspace-generator 7.78.0-next-5aa7ed4 → 7.78.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/lib/api/devfile-context.d.ts +10 -0
  2. package/lib/api/devfile-context.js +12 -0
  3. package/lib/api/devfile-context.js.map +1 -1
  4. package/lib/devfile-schema/2.0.0/devfile.json +2852 -0
  5. package/lib/devfile-schema/2.1.0/devfile.json +1430 -0
  6. package/lib/devfile-schema/2.2.0/devfile.json +2037 -0
  7. package/lib/devfile-schema/2.2.1/devfile.json +2048 -0
  8. package/lib/devfile-schema/2.2.2/devfile.json +2211 -0
  9. package/lib/devfile-schema/devfile-schema-module.d.ts +12 -0
  10. package/lib/devfile-schema/devfile-schema-module.js +19 -0
  11. package/lib/devfile-schema/devfile-schema-module.js.map +1 -0
  12. package/lib/devfile-schema/devfile-schema-validator.d.ts +5878 -0
  13. package/lib/devfile-schema/devfile-schema-validator.js +77 -0
  14. package/lib/devfile-schema/devfile-schema-validator.js.map +1 -0
  15. package/lib/generate.js +1 -1
  16. package/lib/generate.js.map +1 -1
  17. package/lib/inversify/inversify-binding.js +2 -0
  18. package/lib/inversify/inversify-binding.js.map +1 -1
  19. package/lib/main.js +14 -1
  20. package/lib/main.js.map +1 -1
  21. package/package.json +2 -1
  22. package/src/api/devfile-context.ts +11 -0
  23. package/src/devfile-schema/2.0.0/devfile.json +2852 -0
  24. package/src/devfile-schema/2.1.0/devfile.json +1430 -0
  25. package/src/devfile-schema/2.2.0/devfile.json +2037 -0
  26. package/src/devfile-schema/2.2.1/devfile.json +2048 -0
  27. package/src/devfile-schema/2.2.2/devfile.json +2211 -0
  28. package/src/devfile-schema/devfile-schema-module.ts +18 -0
  29. package/src/devfile-schema/devfile-schema-validator.ts +46 -0
  30. package/src/generate.ts +1 -2
  31. package/src/inversify/inversify-binding.ts +2 -0
  32. package/src/main.ts +20 -0
@@ -0,0 +1,18 @@
1
+ /**********************************************************************
2
+ * Copyright (c) 2023 Red Hat, Inc.
3
+ *
4
+ * This program and the accompanying materials are made
5
+ * available under the terms of the Eclipse Public License 2.0
6
+ * which is available at https://www.eclipse.org/legal/epl-2.0/
7
+ *
8
+ * SPDX-License-Identifier: EPL-2.0
9
+ ***********************************************************************/
10
+ import { ContainerModule, interfaces } from 'inversify';
11
+
12
+ import { DevfileSchemaValidator } from './devfile-schema-validator';
13
+
14
+ const devfileSchemaModule = new ContainerModule((bind: interfaces.Bind) => {
15
+ bind(DevfileSchemaValidator).toSelf().inSingletonScope();
16
+ });
17
+
18
+ export { devfileSchemaModule };
@@ -0,0 +1,46 @@
1
+ /**********************************************************************
2
+ * Copyright (c) 2023 Red Hat, Inc.
3
+ *
4
+ * This program and the accompanying materials are made
5
+ * available under the terms of the Eclipse Public License 2.0
6
+ * which is available at https://www.eclipse.org/legal/epl-2.0/
7
+ *
8
+ * SPDX-License-Identifier: EPL-2.0
9
+ ***********************************************************************/
10
+ import { injectable } from 'inversify';
11
+
12
+ import * as devfileSchemaV200 from './2.0.0/devfile.json';
13
+ import * as devfileSchemaV210 from './2.1.0/devfile.json';
14
+ import * as devfileSchemaV220 from './2.2.0/devfile.json';
15
+ import * as devfileSchemaV221 from './2.2.1/devfile.json';
16
+ import * as devfileSchemaV222 from './2.2.2/devfile.json';
17
+ import * as Validator from 'jsonschema';
18
+ import { DevfileSchemaVersion } from '../api/devfile-context';
19
+
20
+ @injectable()
21
+ export class DevfileSchemaValidator {
22
+ getDevfileSchema(version: string) {
23
+ switch (version) {
24
+ case DevfileSchemaVersion.V200:
25
+ return devfileSchemaV200;
26
+ case DevfileSchemaVersion.V210:
27
+ return devfileSchemaV210;
28
+ case DevfileSchemaVersion.V220:
29
+ return devfileSchemaV220;
30
+ case DevfileSchemaVersion.V221:
31
+ return devfileSchemaV221;
32
+ case DevfileSchemaVersion.V222:
33
+ return devfileSchemaV222;
34
+ default:
35
+ throw new Error(`Dev Workspace generator tool doesn't support devfile version: ${version}`);
36
+ }
37
+ }
38
+
39
+ // Validates devfile against schema
40
+ validateDevfile(devfile: any, version: string) {
41
+ const schema = this.getDevfileSchema(version);
42
+ const validatorResult = Validator.validate(devfile, schema, { nestedErrors: true });
43
+
44
+ return validatorResult;
45
+ }
46
+ }
package/src/generate.ts CHANGED
@@ -59,7 +59,7 @@ export class Generate {
59
59
  await fs.writeFile(outputFile, generatedContent, 'utf-8');
60
60
  }
61
61
 
62
- console.log(`DevWorkspace ${context.devWorkspaceTemplates[0].metadata.name} was generated.`);
62
+ console.log(`DevWorkspace ${context.devWorkspaceTemplates[0].metadata.name} was generated`);
63
63
  return context;
64
64
  }
65
65
 
@@ -71,7 +71,6 @@ export class Generate {
71
71
  ): Promise<DevfileContext> {
72
72
  const devfile = jsYaml.load(devfileContent);
73
73
 
74
- // const originalDevfile = Object.assign({}, devfile);
75
74
  // sets the suffix to the devfile name
76
75
  const suffix = devfile.metadata.name || '';
77
76
 
@@ -16,6 +16,7 @@ import { fetchModule } from '../fetch/fetch-module';
16
16
  import { githubModule } from '../github/github-module';
17
17
  import { resolveModule } from '../resolve/resolve-module';
18
18
  import { pluginRegistryModule } from '../plugin-registry/plugin-registry-module';
19
+ import { devfileSchemaModule } from '../devfile-schema/devfile-schema-module';
19
20
  import { bitbucketModule } from '../bitbucket/bitbucket-module';
20
21
  import { bitbucketServerModule } from '../bitbucket-server/bitbucket-server-module';
21
22
 
@@ -35,6 +36,7 @@ export class InversifyBinding {
35
36
  this.container.load(bitbucketServerModule);
36
37
  this.container.load(resolveModule);
37
38
  this.container.load(pluginRegistryModule);
39
+ this.container.load(devfileSchemaModule);
38
40
 
39
41
  this.container.bind(Symbol.for('AxiosInstance')).toConstantValue(options.axiosInstance);
40
42
  this.container.bind('string').toConstantValue(options.pluginRegistryUrl).whenTargetNamed('PLUGIN_REGISTRY_URL');
package/src/main.ts CHANGED
@@ -11,6 +11,7 @@
11
11
  import * as axios from 'axios';
12
12
  import * as fs from 'fs-extra';
13
13
  import { Generate } from './generate';
14
+ import { DevfileSchemaValidator } from './devfile-schema/devfile-schema-validator';
14
15
  import * as jsYaml from 'js-yaml';
15
16
  import { InversifyBinding } from './inversify/inversify-binding';
16
17
  import { UrlFetcher } from './fetch/url-fetcher';
@@ -18,6 +19,7 @@ import { PluginRegistryResolver } from './plugin-registry/plugin-registry-resolv
18
19
  import { V1alpha2DevWorkspaceSpecTemplate } from '@devfile/api';
19
20
  import { DevfileContext } from './api/devfile-context';
20
21
  import { GitUrlResolver } from './resolve/git-url-resolver';
22
+ import { ValidatorResult } from 'jsonschema';
21
23
 
22
24
  export class Main {
23
25
  /**
@@ -100,6 +102,24 @@ export class Main {
100
102
  devfileContent = params.devfileContent;
101
103
  }
102
104
 
105
+ const jsYamlDevfileContent = jsYaml.load(devfileContent);
106
+ const schemaVersion = jsYamlDevfileContent.schemaVersion;
107
+ if (!schemaVersion) {
108
+ throw new Error(`Devfile is not valid, schemaVersion is required`);
109
+ }
110
+
111
+ // validate devfile
112
+ const devfileSchemaValidator = container.get(DevfileSchemaValidator);
113
+ console.log(`Validating devfile`);
114
+ const validationResult: ValidatorResult = devfileSchemaValidator.validateDevfile(
115
+ jsYamlDevfileContent,
116
+ schemaVersion
117
+ );
118
+ if (!validationResult.valid) {
119
+ throw new Error(`Devfile schema validation failed. Error: ${validationResult.toString()}`);
120
+ }
121
+ console.log(`Devfile is valid with schema version ${schemaVersion}`);
122
+
103
123
  // enhance projects
104
124
  devfileContent = this.replaceIfExistingProjects(devfileContent, params.projects);
105
125