Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add OpenAPI version support #136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading