Skip to content

Commit d494492

Browse files
authored
Merge pull request #102 from ralfhandl/schema-publish
schema-publish workflow
2 parents c2611ff + ef6d5a2 commit d494492

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

.github/workflows/schema-publish.yaml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: schema-publish
2+
3+
# author: @ralfhandl
4+
5+
#
6+
# This workflow copies the x.y schemas to the gh-pages branch
7+
#
8+
9+
# run this on push to main
10+
on:
11+
push:
12+
branches:
13+
- main
14+
workflow_dispatch: {}
15+
16+
jobs:
17+
publish:
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v4 # checkout main branch
22+
with:
23+
fetch-depth: 0
24+
25+
- uses: actions/setup-node@v4 # setup Node.js
26+
with:
27+
node-version: 20.x
28+
29+
- name: Install dependencies
30+
run: npm ci
31+
32+
- uses: actions/checkout@v4 # checkout gh-pages branch
33+
with:
34+
token: ${{ secrets.OAS_REPO_TOKEN }}
35+
repository: OAI/OpenAPI-Specification
36+
ref: gh-pages
37+
path: deploy
38+
39+
- name: run main script
40+
run: scripts/schema-publish.sh
41+
42+
- name: Create Pull Request
43+
uses: peter-evans/create-pull-request@v7
44+
with:
45+
# A personal access token is required to push changes to the repository.
46+
# This token needs to be refreshed regularly and stored in the repository secrets.
47+
token: ${{ secrets.OAS_REPO_TOKEN }}
48+
branch: publish-overlay-schema-iteration
49+
base: gh-pages
50+
delete-branch: true
51+
path: deploy
52+
labels: Housekeeping,Schema
53+
reviewers: darrelmiller,webron,earth2marsh,lornajane,mikekistler,miqui,ralfhandl,handrews
54+
title: Publish Overlay Schema Iterations
55+
commit-message: New Overlay schema iterations
56+
signoff: true
57+
body: |
58+
This pull request is automatically triggered by GitHub action `schema-publish` in the OAI/Overlay-Specification repo.
59+
The `schemas/**/*.yaml` files have changed and JSON files are automatically generated.

scripts/schema-publish.sh

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
# Author: @ralfhandl
4+
5+
# Run this script from the root of the repo. It is designed to be run by a GitHub workflow.
6+
7+
for schemaDir in schemas/v* ; do
8+
vVersion=$(basename "$schemaDir")
9+
version=${vVersion:1}
10+
echo $version
11+
12+
# list of schemas to process, dependent schemas come first
13+
schemas=(schema.yaml)
14+
15+
# find the newest commit date for each schema
16+
maxDate=""
17+
declare -A datesHash
18+
for schema in "${schemas[@]}"; do
19+
if [ -f "$schemaDir/$schema" ]; then
20+
newestCommitDate=$(git log -1 --format="%ad" --date=short "$schemaDir/$schema")
21+
22+
# the newest date across a schema and all its dependencies is its date stamp
23+
if [ "$newestCommitDate" \> "$maxDate" ]; then
24+
maxDate=$newestCommitDate
25+
fi
26+
datesHash["$schema"]=$maxDate
27+
echo $schema changed at $newestCommitDate
28+
fi
29+
done
30+
31+
# construct sed command
32+
sedCmd=()
33+
for schema in "${!datesHash[@]}"; do
34+
base=$(basename "$schema" .yaml)
35+
sedCmd+=("-e s/$base\/WORK-IN-PROGRESS/$base\/${datesHash[$schema]}/g")
36+
done
37+
38+
# create the date-stamped schemas
39+
for schema in "${!datesHash[@]}"; do
40+
base=$(basename "$schema" .yaml)
41+
target=deploy/overlay/$version/$base/${datesHash[$schema]}
42+
43+
mkdir -p "deploy/overlay/$version/$base"
44+
45+
sed ${sedCmd[@]} $schemaDir/$schema > $target.yaml
46+
node scripts/yaml2json/yaml2json.js $target.yaml
47+
rm $target.yaml
48+
mv $target.json $target
49+
50+
mv deploy/overlay/$version/$base/*.md $target.md
51+
done
52+
53+
echo ""
54+
done

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)