OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

Workflow to publish schemas

Open ralfhandl opened this issue 1 year ago • 3 comments

  • [x] GitHub workflow
  • [x] bash script called from workflow
  • [x] JS script to convert YAML to JSON and patch iteration date
  • [x] unit tests for JS script
  • [ ] Align with outcome of
    • #4146

Example PR created by this workflow

  • https://github.com/ralfhandl/OpenAPI-Specification/pull/26/files

ralfhandl avatar Oct 16 '24 14:10 ralfhandl

to confirm, these are both edited in the files?

  • dates in the $id keywords
  • $comment links in each definition, that currently go to the 3.1.0 spec and should go to 3.1.1

karenetheridge avatar Oct 17 '24 16:10 karenetheridge

  • What about the dates in the $ids?

Those are replaced when converting from YAML to JSON, see example PR https://github.com/ralfhandl/OpenAPI-Specification/pull/26/files

ralfhandl avatar Oct 17 '24 16:10 ralfhandl

  • What about the description links in each definition, that currently go to the 3.1.0 spec and should go to 3.1.1?

These need to be adjusted manually. Changing them in main will then trigger the schema-publish workflow.

ralfhandl avatar Oct 17 '24 16:10 ralfhandl

@ralfhandl I realize I am asking for a substantially more complex workflow, so I took a pass at it. I was going to use the "suggest" feature on this PR but... my JavaScript skills are very poor and my bash skills are atrocious.

So here's something that I got working locally that you can use as a proof-of-concept (it doesn't update the markdown but I'm not quite sure what's going on there and I think this should get the point across). Or if you'd prefer, I can try to clean this up and submit it myself. I'm also happy to help with updates to the spec site for the date-using vocabulary and dialect schemas.

In local testing it handled various combinations of only certain schemas being updated correctly, including all updated, only the schema updated (but for both versions) and having the vocab and/or dialect schema changes cause further updates.

thisCommit="$GITHUB_SHA"

# Note that for 3.0, "noDialectSchema" is the only schema
noDialectWIP="schema/WORK-IN-PROGRESS"
vocabWIP="meta/WORK-IN-PROGRESS"
dialectWIP="dialect/WORK-IN-PROGRESS"
strictDialectWIP="schema-base/WORK-IN-PROGRESS"

for schemaDir in schemas/v3* ; do
  version=$(basename "$schemaDir")

  noDialectSchema="$schemaDir/schema.yaml"
  vocabSchema="$schemaDir/meta/base.schema.yaml"
  dialectSchema="$schemaDir/dialect/base.schema.yaml"
  strictDialectSchema="$schemaDir/schema-base.yaml"
  echo $noDialectSchema
  echo $vocabSchema
  echo $dialectSchema
  echo $strictDialectSchema
  echo ""

  if [ -f "$noDialectSchema" ]; then
    noDialectCommit=$(git log -1 --format="%H" -- "$noDialectSchema")
    noDialectCommitDate=$(git log -1 --format="%ad" --date=short -- "$noDialectSchema")
    if [ "$noDialectCommit" = "$thisCommit" ]; then
      updateNoDialect="1"
    fi
  fi

  if [ -f "$vocabSchema" ]; then
    vocabCommit=$(git log -1 --format="%H" -- "$vocabSchema")
    vocabCommitDate=$(git log -1 --format="%ad" --date=short -- "$vocabSchema")
    if [ "$vocabCommit" = "$thisCommit" ]; then
      updateVocab="1"
    fi
  fi

  if [ -f "$dialectSchema" ]; then
    dialectCommit=$(git log -1 --format="%H" -- "$dialectSchema")
    dialectCommitDate=$(git log -1 --format="%ad" --date=short -- "$dialectSchema")
    updateDialect="$updateVocab"
    if [ "$dialectCommit" = "$thisCommit" ]; then
      updateDialect="1"
    fi
  fi

  if [ -f "$strictDialectSchema" ]; then
    strictDialectCommit=$(git log -1 --format="%H" -- "$strictDialectSchema")
    strictDialectCommitDate=$(git log -1 --format="%ad" --date=short -- "$strictDialectSchema")
    updateStrictDialect="$updateDialect"
    if [ -n "$updateNoDialect" ]; then
      updateStrictDialect="1"
    fi
    if [ "$strictDialectCommit" = "$thisCommit" ]; then
      updateStrictDialect="1"
    fi
  fi

  echo $thisCommit
  echo $version $noDialectCommit $noDialectCommitDate $updateNoDialect
  echo $version $vocabCommit $vocabCommitDate $updateVocab
  echo $version $dialectCommit $dialectCommitDate $updateDialect
  echo $version $strictDialectCommit $strictDialectCommitDate $updateStrictDialect
  echo ""

  mkdir -p deploy/oas/$version/schema
  if [ -f "$vocabSchema" ]; then
    mkdir -p deploy/oas/$version/meta
    mkdir -p deploy/oas/$version/dialect
    if [ -n "$updateNoDialect" ]; then
        node scripts/schema-convert.js "$noDialectSchema" $noDialectCommitDate $vocabCommitDate $dialectCommitDate $strictDialectCommitDate > deploy/oas/$version/schema/$noDialectCommitDate
    fi
    if [ -n "$updateVocab" ]; then
        node scripts/schema-convert.js "$vocabSchema" $noDialectCommitDate $vocabCommitDate $dialectCommitDate $strictDialectCommitDate > deploy/oas/$version/meta/$vocabCommitDate
    fi
    if [ -n "$updateDialect" ]; then
        node scripts/schema-convert.js "$dialectSchema" $noDialectCommitDate $vocabCommitDate $dialectCommitDate $strictDialectCommitDate > deploy/oas/$version/dialect/$dialectCommitDate
    fi
    if [ -n "$updateStrictDialect" ]; then
        node scripts/schema-convert.js "$strictDialectSchema" $noDialectCommitDate $vocabCommitDate $dialectCommitDate $strictDialectCommitDate > deploy/oas/$version/schema-base/$strictDialectCommitDate
    fi
  else
    if [ -n "$updateNoDialect" ]; then
        node scripts/schema-convert.js "$noDialectSchema" $noDialectCommitDate > deploy/oas/$version/schema/$noDialectCommitDate
    fi
  fi
done
#!/usr/bin/env node

'use strict';

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

function convert(
    filename,
    noDialectDate,
    vocabDate=false,
    dialectDate=false,
    strictDialectDate=false,
) {
    try {
        var s = fs.readFileSync(filename,'utf8');
        s = s.replace(
            /schema\/WORK-IN-PROGRESS/g,
            'schema/' + noDialectDate,
        );
        if (vocabDate) {
            s = s.replace(
                /meta\/WORK-IN-PROGRESS/g,
                'meta/' + vocabDate,
            );
        }
        if (dialectDate) {
            s = s.replace(
                /dialect\/WORK-IN-PROGRESS/g,
                'dialect/' + dialectDate,
            );
        }
        if (strictDialectDate) {
            s = s.replace(
                /schema-base\/WORK-IN-PROGRESS/g,
                'schema-base/' + strictDialectDate,
            );
        }
        const obj = yaml.parse(s, {prettyErrors: true});
        console.log(JSON.stringify(obj,null,2));
    }
    catch (ex) {
        console.warn('  ',ex.message);
        process.exitCode = 1;
    }
}

if (process.argv.length<4) {
    console.warn('Usage: convert-schema.js file.yaml YYYY-MM-DD [YYYY-MM-DD YYYY-MM-DD YYYY-MM-DD]');
}
else {
  if (process.argv.length>4) {
    convert(process.argv[2], process.argv[3], process.argv[4], process.argv[5], process.argv[6]);
  } else {
    convert(process.argv[2], process.argv[3]);
  }
}

handrews avatar Oct 22 '24 00:10 handrews

@handrews Thanks, will look into this once #4146 is merged.

ralfhandl avatar Oct 22 '24 07:10 ralfhandl

Replaced with simpler approach:

  • #4162

ralfhandl avatar Oct 25 '24 14:10 ralfhandl