Skip to content

Commit 007b1b0

Browse files
ixuzSchulteDevgithub-actions[bot]TimothyJones
authored
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 <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Timothy Jones <[email protected]>
1 parent 7a5b806 commit 007b1b0

8 files changed

+144
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
2222
- [Maven Support (Java/Kotlin)](#maven-support-javakotlin)
2323
- [Gradle Support (Java/Kotlin)](#gradle-support-javakotlin)
2424
- [.NET Support](#net-support)
25+
- [OpenAPI Support](#openapi-support)
2526
- [Installing `commit-and-tag-version`](#installing-commit-and-tag-version)
2627
- [As a local `npm run` script](#as-a-local-npm-run-script)
2728
- [As global `bin`](#as-global-bin)
@@ -117,6 +118,14 @@ This is going to read and update only the `version:` tag in the file.
117118
commit-and-tag-version --packageFiles file.yaml --bumpFiles file.yaml
118119
```
119120

121+
### OpenAPI Support
122+
123+
If you are using OpenAPI, then just point to your `openapi.yaml` file.
124+
125+
```sh
126+
commit-and-tag-version --packageFiles openapi.yaml --bumpFiles openapi.yaml
127+
```
128+
120129
## Installing `commit-and-tag-version`
121130

122131
### As a local `npm run` script

lib/updaters/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const updatersByType = {
77
gradle: require('./types/gradle'),
88
csproj: require('./types/csproj'),
99
yaml: require('./types/yaml'),
10+
openapi: require('./types/openapi'),
1011
};
1112
const PLAIN_TEXT_BUMP_FILES = ['VERSION.txt', 'version.txt'];
1213

@@ -37,6 +38,9 @@ function getUpdaterByFilename(filename) {
3738
if (/\.ya?ml$/.test(filename)) {
3839
return getUpdaterByType('yaml');
3940
}
41+
if (/openapi.yaml/.test(filename)) {
42+
return getUpdaterByType('openapi');
43+
}
4044
throw Error(
4145
`Unsupported file (${filename}) provided for bumping.\n Please specify the updater \`type\` or use a custom \`updater\`.`,
4246
);

lib/updaters/types/openapi.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const yaml = require('yaml');
2+
const detectNewline = require('detect-newline');
3+
4+
module.exports.readVersion = function (contents) {
5+
return yaml.parse(contents).info.version;
6+
};
7+
8+
module.exports.writeVersion = function (contents, version) {
9+
const newline = detectNewline(contents);
10+
const document = yaml.parseDocument(contents);
11+
12+
document.get('info').set('version', version);
13+
14+
return document.toString().replace(/\r?\n/g, newline);
15+
};

test/core.spec.js

+68
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,74 @@ describe('cli', function () {
11801180
});
11811181
});
11821182

1183+
it('bumps version in OpenAPI `openapi.yaml` file with CRLF Line Endings', async function () {
1184+
const expected = fs.readFileSync(
1185+
'./test/mocks/openapi-1.3.0-crlf.yaml',
1186+
'utf-8',
1187+
);
1188+
const filename = 'openapi.yaml';
1189+
mock({
1190+
bump: 'minor',
1191+
realTestFiles: [
1192+
{
1193+
filename,
1194+
path: './test/mocks/openapi-1.2.3-crlf.yaml',
1195+
},
1196+
],
1197+
});
1198+
await exec({
1199+
packageFiles: [{ filename, type: 'openapi' }],
1200+
bumpFiles: [{ filename, type: 'openapi' }],
1201+
});
1202+
1203+
// filePath is the first arg passed to writeFileSync
1204+
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1205+
writeFileSyncSpy,
1206+
filename,
1207+
});
1208+
1209+
if (!packageJsonWriteFileSynchCall) {
1210+
throw new Error(`writeFileSynch not invoked with path ${filename}`);
1211+
}
1212+
1213+
const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1214+
expect(calledWithContentStr).toEqual(expected);
1215+
});
1216+
1217+
it('bumps version in OpenAPI `openapi.yaml` file with LF Line Endings', async function () {
1218+
const expected = fs.readFileSync(
1219+
'./test/mocks/openapi-1.3.0-lf.yaml',
1220+
'utf-8',
1221+
);
1222+
const filename = 'openapi.yaml';
1223+
mock({
1224+
bump: 'minor',
1225+
realTestFiles: [
1226+
{
1227+
filename,
1228+
path: './test/mocks/openapi-1.2.3-lf.yaml',
1229+
},
1230+
],
1231+
});
1232+
await exec({
1233+
packageFiles: [{ filename, type: 'openapi' }],
1234+
bumpFiles: [{ filename, type: 'openapi' }],
1235+
});
1236+
1237+
// filePath is the first arg passed to writeFileSync
1238+
const packageJsonWriteFileSynchCall = findWriteFileCallForPath({
1239+
writeFileSyncSpy,
1240+
filename,
1241+
});
1242+
1243+
if (!packageJsonWriteFileSynchCall) {
1244+
throw new Error(`writeFileSynch not invoked with path ${filename}`);
1245+
}
1246+
1247+
const calledWithContentStr = packageJsonWriteFileSynchCall[1];
1248+
expect(calledWithContentStr).toEqual(expected);
1249+
});
1250+
11831251
it('bumps version in Maven `pom.xml` file with CRLF Line Endings', async function () {
11841252
const expected = fs.readFileSync(
11851253
'./test/mocks/pom-6.4.0-crlf.xml',

test/mocks/openapi-1.2.3-crlf.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openapi: "3.0.2"
2+
info:
3+
title: Mock API
4+
description: >-
5+
Description of Mock API
6+
version: "1.2.3"
7+
termsOfService: http://swagger.io/terms/
8+
externalDocs:
9+
description: Find out more
10+
url: https://example.com/foo/bar
11+
servers:
12+
- url: http://example.com

test/mocks/openapi-1.2.3-lf.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openapi: "3.0.2"
2+
info:
3+
title: Mock API
4+
description: >-
5+
Description of Mock API
6+
version: "1.2.3"
7+
termsOfService: http://swagger.io/terms/
8+
externalDocs:
9+
description: Find out more
10+
url: https://example.com/foo/bar
11+
servers:
12+
- url: http://example.com

test/mocks/openapi-1.3.0-crlf.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openapi: "3.0.2"
2+
info:
3+
title: Mock API
4+
description: >-
5+
Description of Mock API
6+
version: "1.3.0"
7+
termsOfService: http://swagger.io/terms/
8+
externalDocs:
9+
description: Find out more
10+
url: https://example.com/foo/bar
11+
servers:
12+
- url: http://example.com

test/mocks/openapi-1.3.0-lf.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
openapi: "3.0.2"
2+
info:
3+
title: Mock API
4+
description: >-
5+
Description of Mock API
6+
version: "1.3.0"
7+
termsOfService: http://swagger.io/terms/
8+
externalDocs:
9+
description: Find out more
10+
url: https://example.com/foo/bar
11+
servers:
12+
- url: http://example.com

0 commit comments

Comments
 (0)