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

actions: convert-examples-to-json (js version) #2199

Merged
merged 1 commit into from
Jun 18, 2020
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
50 changes: 50 additions & 0 deletions .github/workflows/convert-examples-to-json.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: convert-examples-to-json

# author: @MikeRalphson / @cebe
# issue: https://github.com/OAI/OpenAPI-Specification/issues/1385

#
# This workflow updates the *.json files in the examples/v3.x directories,
# when the corresponding *.yaml files change.
# JSON example files are automatically generated from the YAML example files.
# Only the YAML files should be adjusted manually.
#

# run this on push to master
on:
push:
branches:
- master

jobs:
yaml2json:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1 # checkout repo content

- name: Install dependencies
run: npm i

- name: convert YAML examples to JSON
run: find examples/v3* -type f -name "*.yaml" | xargs node scripts/yaml2json/yaml2json.js

- name: git diff
run: |
git add examples/**/*.json
git --no-pager -c color.diff=always diff --staged

- name: Create Pull Request
uses: peter-evans/create-pull-request@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch-suffix: none
branch: update-json-examples
title: Update JSON example files
commit-message: Update JSON example files
body: |
This pull request is automatically triggered by GitHub action `convert-examples-to-json`.

The examples/v3.* YAML files have changed, so the JSON files are automatically being recreated.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
target
atlassian-ide-plugin.xml
node_modules/
package-lock.json
Gemfile.lock
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"schemas/*"
],
"dependencies": {},
"devDependencies": {},
"devDependencies": {
"mdv": "^1.0.7",
"yaml": "^1.8.3"
},
"keywords": [
"OpenAPI",
"OAS",
Expand Down
30 changes: 30 additions & 0 deletions scripts/yaml2json/yaml2json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

'use strict';

const fs = require('fs');
const yaml = require('yaml');

function convert(filename) {
console.log(filename);
const s = fs.readFileSync(filename,'utf8');
let obj;
try {
obj = yaml.parse(s, {prettyErrors: true});
fs.writeFileSync(filename.replace('.yaml','.json'),JSON.stringify(obj,null,2),'utf8');
}
catch (ex) {
console.warn(' ',ex.message);
process.exitCode = 1;
}
}

if (process.argv.length<3) {
console.warn('Usage: yaml2json {infiles}');
}
else {
for (let i=2;i<process.argv.length;i++) {
convert(process.argv[i]);
}
}