diff --git a/package-lock.json b/package-lock.json index b2af771bce1..400e46f87f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,13 +17,14 @@ "@asyncapi/modelina": "^1.9.1", "@asyncapi/openapi-schema-parser": "^3.0.5", "@asyncapi/optimizer": "^0.2.1", - "@asyncapi/parser": "^3.0.0-next-major-spec.2", + "@asyncapi/parser": "^3.0.0-next-major-spec.3", "@asyncapi/protobuf-schema-parser": "3.0.0", "@asyncapi/raml-dt-schema-parser": "^4.0.3", "@asyncapi/studio": "^0.17.3", "@oclif/core": "^1.26.2", "@oclif/errors": "^1.3.6", "@oclif/plugin-not-found": "^2.3.22", + "@smoya/asyncapi-adoption-metrics": "^1.1.1", "@smoya/multi-parser": "^4.0.0", "@stoplight/spectral-cli": "6.9.0", "ajv": "^8.12.0", @@ -1138,9 +1139,9 @@ } }, "node_modules/@asyncapi/parser": { - "version": "3.0.0-next-major-spec.2", - "resolved": "https://registry.npmjs.org/@asyncapi/parser/-/parser-3.0.0-next-major-spec.2.tgz", - "integrity": "sha512-/gJgCYNYlUSDJhySK3IagjiyFfnwEsAZd5rTe396CB+HxJ6yDWDOZYdHzkFgU2RnCULSGzVOWZGx8t3PK+yuVg==", + "version": "3.0.0-next-major-spec.3", + "resolved": "https://registry.npmjs.org/@asyncapi/parser/-/parser-3.0.0-next-major-spec.3.tgz", + "integrity": "sha512-LCrAQqJpGxraMyU2k1Nh1X6Q1dz7a/YhTRRFFrQHOEo+TUT/kRdoUkRDP++e58dO7h9MBN+/hZK5TaqE+/jQiw==", "dependencies": { "@asyncapi/specs": "^6.0.0-next-major-spec.6", "@openapi-contrib/openapi-schema-to-json-schema": "~3.2.0", @@ -5475,10 +5476,18 @@ "url": "https://github.com/sindresorhus/is?sponsor=1" } }, + "node_modules/@smoya/asyncapi-adoption-metrics": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@smoya/asyncapi-adoption-metrics/-/asyncapi-adoption-metrics-1.1.1.tgz", + "integrity": "sha512-1UTfJsw3+iUSaROu03s6OXA32HkM+h9kT1P1gZCOCddRybL5PJzvP/X3FIJEM9IICu1B7btVXdy3VNIZ/9LCaw==", + "dependencies": { + "@smoya/multi-parser": "^4.1.0" + } + }, "node_modules/@smoya/multi-parser": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/@smoya/multi-parser/-/multi-parser-4.0.0.tgz", - "integrity": "sha512-NgPxSaB3YqwrIVe7AtQ/wh9I2J0BHR4lP0PdqirYYrc0XXRwdDjIRrywEc2jjECWsL7tuGU/QtGMGIVaJe6ZYA==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@smoya/multi-parser/-/multi-parser-4.1.0.tgz", + "integrity": "sha512-0q4805I7ZdkKNEbNrbxmBNAdYEAH9aBYCuXiIXCbu8WC0sdRqLWmTsvg/DaNO8ZnsrGhVzy5VITf+8CKlVnGoQ==", "dependencies": { "@asyncapi/avro-schema-parser": "^3.0.3", "@asyncapi/openapi-schema-parser": "^3.0.4", diff --git a/package.json b/package.json index a724b554496..62c36eca24c 100644 --- a/package.json +++ b/package.json @@ -16,13 +16,14 @@ "@asyncapi/modelina": "^1.9.1", "@asyncapi/openapi-schema-parser": "^3.0.5", "@asyncapi/optimizer": "^0.2.1", - "@asyncapi/parser": "^3.0.0-next-major-spec.2", + "@asyncapi/parser": "^3.0.0-next-major-spec.3", "@asyncapi/protobuf-schema-parser": "3.0.0", "@asyncapi/raml-dt-schema-parser": "^4.0.3", "@asyncapi/studio": "^0.17.3", "@oclif/core": "^1.26.2", "@oclif/errors": "^1.3.6", "@oclif/plugin-not-found": "^2.3.22", + "@smoya/asyncapi-adoption-metrics": "^1.1.1", "@smoya/multi-parser": "^4.0.0", "@stoplight/spectral-cli": "6.9.0", "ajv": "^8.12.0", diff --git a/src/base.ts b/src/base.ts index c2761523adf..3ac8a66e16e 100644 --- a/src/base.ts +++ b/src/base.ts @@ -1,6 +1,9 @@ import { Command } from '@oclif/core'; +import { Recorder, StdOutSink } from '@smoya/asyncapi-adoption-metrics'; export default abstract class extends Command { + recorder = new Recorder('asyncapi-adoption', new StdOutSink()); + async catch(err: Error & { exitCode?: number; }): Promise { try { return await super.catch(err); diff --git a/src/commands/validate.ts b/src/commands/validate.ts index 7254b52f4f5..d889671d9b5 100644 --- a/src/commands/validate.ts +++ b/src/commands/validate.ts @@ -5,6 +5,8 @@ import { validate, validationFlags } from '../parser'; import { load } from '../models/SpecificationFile'; import { specWatcher } from '../globals'; import { watchFlag } from '../flags'; +import { MetadataFromDocument } from '@smoya/asyncapi-adoption-metrics'; +import { Parser } from '@asyncapi/parser'; export default class Validate extends Command { static description = 'validate asyncapi file'; @@ -19,6 +21,8 @@ export default class Validate extends Command { { name: 'spec-file', description: 'spec path, url, or context-name', required: false }, ]; + parser = new Parser(); + async run() { const { args, flags } = await this.parse(Validate); //NOSONAR const filePath = args['spec-file']; @@ -29,6 +33,22 @@ export default class Validate extends Command { specWatcher({ spec: specFile, handler: this, handlerName: 'validate' }); } - await validate(this, specFile, flags); + const result = await validate(this, specFile, flags); + + try { + // Metrics recording. + const {document} = await this.parser.parse(specFile.text()); + if (document !== undefined) { + const metadata = MetadataFromDocument(document); + metadata['success'] = true; + metadata['validation_result'] = result; + await this.recorder.recordActionExecution('validate', metadata); + await this.recorder.flush(); + } + } catch (e: any) { + if (e instanceof Error) { + this.log(`Skipping submitting anonymous metrics due to the following error: ${e.name}: ${e.message}`); + } + } } }