Skip to content

Commit

Permalink
feat: Add OpenAPI version support (#136)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Timothy Jones <[email protected]>
  • Loading branch information
4 people authored Apr 19, 2024
1 parent 7a5b806 commit 007b1b0
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/updaters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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\`.`,
);
Expand Down
15 changes: 15 additions & 0 deletions lib/updaters/types/openapi.js
Original file line number Diff line number Diff line change
@@ -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);
};
68 changes: 68 additions & 0 deletions test/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 12 additions & 0 deletions test/mocks/openapi-1.2.3-crlf.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/mocks/openapi-1.2.3-lf.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/mocks/openapi-1.3.0-crlf.yaml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/mocks/openapi-1.3.0-lf.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 007b1b0

Please sign in to comment.