From 007b1b0386651f2fbd6e8a9f552a2a34702086ca Mon Sep 17 00:00:00 2001 From: Anton Wiklund Date: Fri, 19 Apr 2024 16:44:51 +0200 Subject: [PATCH] feat: Add OpenAPI version support (#136) * test: add openapi crlf mocks * test: add openapi lf mocks * feat: add support for openapi version bumping (almost identical to yaml-updater) * test: OpenAPI updater tests * fix: Add debug messages for exclusions during bump lifecycle (#131) * chore: Add debug messages for exclusions in lifecycle bump Code has been modified to include debug messages in the bump lifecycle. When a filename is ignored by Git or is not a file, the program will now log a debug-level message. * chore(#131): Update formatting * chore(#131): Improve debug messages for exclusions during bump lifecycle * chore(master): release 12.3.0 (#139) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * docs: add documentation about how to bump OpenAPI version --------- Co-authored-by: Markus Schulte Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Timothy Jones --- README.md | 9 ++++ lib/updaters/index.js | 4 ++ lib/updaters/types/openapi.js | 15 +++++++ test/core.spec.js | 68 ++++++++++++++++++++++++++++++ test/mocks/openapi-1.2.3-crlf.yaml | 12 ++++++ test/mocks/openapi-1.2.3-lf.yaml | 12 ++++++ test/mocks/openapi-1.3.0-crlf.yaml | 12 ++++++ test/mocks/openapi-1.3.0-lf.yaml | 12 ++++++ 8 files changed, 144 insertions(+) create mode 100644 lib/updaters/types/openapi.js create mode 100644 test/mocks/openapi-1.2.3-crlf.yaml create mode 100644 test/mocks/openapi-1.2.3-lf.yaml create mode 100644 test/mocks/openapi-1.3.0-crlf.yaml create mode 100644 test/mocks/openapi-1.3.0-lf.yaml diff --git a/README.md b/README.md index 4c21f4f55..dceec3b8f 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla - [Maven Support (Java/Kotlin)](#maven-support-javakotlin) - [Gradle Support (Java/Kotlin)](#gradle-support-javakotlin) - [.NET Support](#net-support) + - [OpenAPI Support](#openapi-support) - [Installing `commit-and-tag-version`](#installing-commit-and-tag-version) - [As a local `npm run` script](#as-a-local-npm-run-script) - [As global `bin`](#as-global-bin) @@ -117,6 +118,14 @@ This is going to read and update only the `version:` tag in the file. commit-and-tag-version --packageFiles file.yaml --bumpFiles file.yaml ``` +### OpenAPI Support + +If you are using OpenAPI, then just point to your `openapi.yaml` file. + +```sh +commit-and-tag-version --packageFiles openapi.yaml --bumpFiles openapi.yaml +``` + ## Installing `commit-and-tag-version` ### As a local `npm run` script diff --git a/lib/updaters/index.js b/lib/updaters/index.js index 29521d4af..683381f94 100644 --- a/lib/updaters/index.js +++ b/lib/updaters/index.js @@ -7,6 +7,7 @@ const updatersByType = { gradle: require('./types/gradle'), csproj: require('./types/csproj'), yaml: require('./types/yaml'), + openapi: require('./types/openapi'), }; const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt']; @@ -37,6 +38,9 @@ function getUpdaterByFilename(filename) { if (/\.ya?ml$/.test(filename)) { return getUpdaterByType('yaml'); } + if (/openapi.yaml/.test(filename)) { + return getUpdaterByType('openapi'); + } throw Error( `Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`, ); diff --git a/lib/updaters/types/openapi.js b/lib/updaters/types/openapi.js new file mode 100644 index 000000000..981386be8 --- /dev/null +++ b/lib/updaters/types/openapi.js @@ -0,0 +1,15 @@ +const yaml = require('yaml'); +const detectNewline = require('detect-newline'); + +module.exports.readVersion = function (contents) { + return yaml.parse(contents).info.version; +}; + +module.exports.writeVersion = function (contents, version) { + const newline = detectNewline(contents); + const document = yaml.parseDocument(contents); + + document.get('info').set('version', version); + + return document.toString().replace(/\r?\n/g, newline); +}; diff --git a/test/core.spec.js b/test/core.spec.js index b64cc9c3a..880220216 100644 --- a/test/core.spec.js +++ b/test/core.spec.js @@ -1180,6 +1180,74 @@ describe('cli', function () { }); }); + it('bumps version in OpenAPI `openapi.yaml` file with CRLF Line Endings', async function () { + const expected = fs.readFileSync( + './test/mocks/openapi-1.3.0-crlf.yaml', + 'utf-8', + ); + const filename = 'openapi.yaml'; + mock({ + bump: 'minor', + realTestFiles: [ + { + filename, + path: './test/mocks/openapi-1.2.3-crlf.yaml', + }, + ], + }); + await exec({ + packageFiles: [{ filename, type: 'openapi' }], + bumpFiles: [{ filename, type: 'openapi' }], + }); + + // filePath is the first arg passed to writeFileSync + const packageJsonWriteFileSynchCall = findWriteFileCallForPath({ + writeFileSyncSpy, + filename, + }); + + if (!packageJsonWriteFileSynchCall) { + throw new Error(`writeFileSynch not invoked with path ${filename}`); + } + + const calledWithContentStr = packageJsonWriteFileSynchCall[1]; + expect(calledWithContentStr).toEqual(expected); + }); + + it('bumps version in OpenAPI `openapi.yaml` file with LF Line Endings', async function () { + const expected = fs.readFileSync( + './test/mocks/openapi-1.3.0-lf.yaml', + 'utf-8', + ); + const filename = 'openapi.yaml'; + mock({ + bump: 'minor', + realTestFiles: [ + { + filename, + path: './test/mocks/openapi-1.2.3-lf.yaml', + }, + ], + }); + await exec({ + packageFiles: [{ filename, type: 'openapi' }], + bumpFiles: [{ filename, type: 'openapi' }], + }); + + // filePath is the first arg passed to writeFileSync + const packageJsonWriteFileSynchCall = findWriteFileCallForPath({ + writeFileSyncSpy, + filename, + }); + + if (!packageJsonWriteFileSynchCall) { + throw new Error(`writeFileSynch not invoked with path ${filename}`); + } + + const calledWithContentStr = packageJsonWriteFileSynchCall[1]; + expect(calledWithContentStr).toEqual(expected); + }); + it('bumps version in Maven `pom.xml` file with CRLF Line Endings', async function () { const expected = fs.readFileSync( './test/mocks/pom-6.4.0-crlf.xml', diff --git a/test/mocks/openapi-1.2.3-crlf.yaml b/test/mocks/openapi-1.2.3-crlf.yaml new file mode 100644 index 000000000..b5e7741ea --- /dev/null +++ b/test/mocks/openapi-1.2.3-crlf.yaml @@ -0,0 +1,12 @@ +openapi: "3.0.2" +info: + title: Mock API + description: >- + Description of Mock API + version: "1.2.3" + termsOfService: http://swagger.io/terms/ +externalDocs: + description: Find out more + url: https://example.com/foo/bar +servers: + - url: http://example.com diff --git a/test/mocks/openapi-1.2.3-lf.yaml b/test/mocks/openapi-1.2.3-lf.yaml new file mode 100644 index 000000000..19ac91bbe --- /dev/null +++ b/test/mocks/openapi-1.2.3-lf.yaml @@ -0,0 +1,12 @@ +openapi: "3.0.2" +info: + title: Mock API + description: >- + Description of Mock API + version: "1.2.3" + termsOfService: http://swagger.io/terms/ +externalDocs: + description: Find out more + url: https://example.com/foo/bar +servers: + - url: http://example.com diff --git a/test/mocks/openapi-1.3.0-crlf.yaml b/test/mocks/openapi-1.3.0-crlf.yaml new file mode 100644 index 000000000..74551f047 --- /dev/null +++ b/test/mocks/openapi-1.3.0-crlf.yaml @@ -0,0 +1,12 @@ +openapi: "3.0.2" +info: + title: Mock API + description: >- + Description of Mock API + version: "1.3.0" + termsOfService: http://swagger.io/terms/ +externalDocs: + description: Find out more + url: https://example.com/foo/bar +servers: + - url: http://example.com diff --git a/test/mocks/openapi-1.3.0-lf.yaml b/test/mocks/openapi-1.3.0-lf.yaml new file mode 100644 index 000000000..21dbaeabd --- /dev/null +++ b/test/mocks/openapi-1.3.0-lf.yaml @@ -0,0 +1,12 @@ +openapi: "3.0.2" +info: + title: Mock API + description: >- + Description of Mock API + version: "1.3.0" + termsOfService: http://swagger.io/terms/ +externalDocs: + description: Find out more + url: https://example.com/foo/bar +servers: + - url: http://example.com