Skip to content

Commit 2281234

Browse files
MikeRalphsonphilsturgeon
authored andcommitted
actions: convert-examples-to-json (js version) (OAI#2199)
1 parent ab0a086 commit 2281234

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: convert-examples-to-json
2+
3+
# author: @MikeRalphson / @cebe
4+
# issue: https://github.com/OAI/OpenAPI-Specification/issues/1385
5+
6+
#
7+
# This workflow updates the *.json files in the examples/v3.x directories,
8+
# when the corresponding *.yaml files change.
9+
# JSON example files are automatically generated from the YAML example files.
10+
# Only the YAML files should be adjusted manually.
11+
#
12+
13+
# run this on push to master
14+
on:
15+
push:
16+
branches:
17+
- master
18+
19+
jobs:
20+
yaml2json:
21+
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- uses: actions/checkout@v1 # checkout repo content
26+
27+
- name: Install dependencies
28+
run: npm i
29+
30+
- name: convert YAML examples to JSON
31+
run: find examples/v3* -type f -name "*.yaml" | xargs node scripts/yaml2json/yaml2json.js
32+
33+
- name: git diff
34+
run: |
35+
git add examples/**/*.json
36+
git --no-pager -c color.diff=always diff --staged
37+
38+
- name: Create Pull Request
39+
uses: peter-evans/create-pull-request@v1
40+
with:
41+
token: ${{ secrets.GITHUB_TOKEN }}
42+
branch-suffix: none
43+
branch: update-json-examples
44+
title: Update JSON example files
45+
commit-message: Update JSON example files
46+
body: |
47+
This pull request is automatically triggered by GitHub action `convert-examples-to-json`.
48+
49+
The examples/v3.* YAML files have changed, so the JSON files are automatically being recreated.
50+

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
target
66
atlassian-ide-plugin.xml
77
node_modules/
8+
package-lock.json
89
Gemfile.lock

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
"schemas/*"
1919
],
2020
"dependencies": {},
21-
"devDependencies": {},
21+
"devDependencies": {
22+
"mdv": "^1.0.7",
23+
"yaml": "^1.8.3"
24+
},
2225
"keywords": [
2326
"OpenAPI",
2427
"OAS",

scripts/yaml2json/yaml2json.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const fs = require('fs');
6+
const yaml = require('yaml');
7+
8+
function convert(filename) {
9+
console.log(filename);
10+
const s = fs.readFileSync(filename,'utf8');
11+
let obj;
12+
try {
13+
obj = yaml.parse(s, {prettyErrors: true});
14+
fs.writeFileSync(filename.replace('.yaml','.json'),JSON.stringify(obj,null,2),'utf8');
15+
}
16+
catch (ex) {
17+
console.warn(' ',ex.message);
18+
process.exitCode = 1;
19+
}
20+
}
21+
22+
if (process.argv.length<3) {
23+
console.warn('Usage: yaml2json {infiles}');
24+
}
25+
else {
26+
for (let i=2;i<process.argv.length;i++) {
27+
convert(process.argv[i]);
28+
}
29+
}
30+

0 commit comments

Comments
 (0)