Skip to content

Commit

Permalink
Merge pull request #102 from ralfhandl/schema-publish
Browse files Browse the repository at this point in the history
schema-publish workflow
  • Loading branch information
ralfhandl authored Dec 3, 2024
2 parents c2611ff + ef6d5a2 commit d494492
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/schema-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: schema-publish

# author: @ralfhandl

#
# This workflow copies the x.y schemas to the gh-pages branch
#

# run this on push to main
on:
push:
branches:
- main
workflow_dispatch: {}

jobs:
publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4 # checkout main branch
with:
fetch-depth: 0

- uses: actions/setup-node@v4 # setup Node.js
with:
node-version: 20.x

- name: Install dependencies
run: npm ci

- uses: actions/checkout@v4 # checkout gh-pages branch
with:
token: ${{ secrets.OAS_REPO_TOKEN }}
repository: OAI/OpenAPI-Specification
ref: gh-pages
path: deploy

- name: run main script
run: scripts/schema-publish.sh

- name: Create Pull Request
uses: peter-evans/create-pull-request@v7
with:
# A personal access token is required to push changes to the repository.
# This token needs to be refreshed regularly and stored in the repository secrets.
token: ${{ secrets.OAS_REPO_TOKEN }}
branch: publish-overlay-schema-iteration
base: gh-pages
delete-branch: true
path: deploy
labels: Housekeeping,Schema
reviewers: darrelmiller,webron,earth2marsh,lornajane,mikekistler,miqui,ralfhandl,handrews
title: Publish Overlay Schema Iterations
commit-message: New Overlay schema iterations
signoff: true
body: |
This pull request is automatically triggered by GitHub action `schema-publish` in the OAI/Overlay-Specification repo.
The `schemas/**/*.yaml` files have changed and JSON files are automatically generated.
54 changes: 54 additions & 0 deletions scripts/schema-publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

# Author: @ralfhandl

# Run this script from the root of the repo. It is designed to be run by a GitHub workflow.

for schemaDir in schemas/v* ; do
vVersion=$(basename "$schemaDir")
version=${vVersion:1}
echo $version

# list of schemas to process, dependent schemas come first
schemas=(schema.yaml)

# find the newest commit date for each schema
maxDate=""
declare -A datesHash
for schema in "${schemas[@]}"; do
if [ -f "$schemaDir/$schema" ]; then
newestCommitDate=$(git log -1 --format="%ad" --date=short "$schemaDir/$schema")

# the newest date across a schema and all its dependencies is its date stamp
if [ "$newestCommitDate" \> "$maxDate" ]; then
maxDate=$newestCommitDate
fi
datesHash["$schema"]=$maxDate
echo $schema changed at $newestCommitDate
fi
done

# construct sed command
sedCmd=()
for schema in "${!datesHash[@]}"; do
base=$(basename "$schema" .yaml)
sedCmd+=("-e s/$base\/WORK-IN-PROGRESS/$base\/${datesHash[$schema]}/g")
done

# create the date-stamped schemas
for schema in "${!datesHash[@]}"; do
base=$(basename "$schema" .yaml)
target=deploy/overlay/$version/$base/${datesHash[$schema]}

mkdir -p "deploy/overlay/$version/$base"

sed ${sedCmd[@]} $schemaDir/$schema > $target.yaml
node scripts/yaml2json/yaml2json.js $target.yaml
rm $target.yaml
mv $target.json $target

mv deploy/overlay/$version/$base/*.md $target.md
done

echo ""
done
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]);
}
}

0 comments on commit d494492

Please sign in to comment.