Skip to content

Imports Validation Schemas into the Examples README during CI #803

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

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions .ci/examples-readme-hydration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
38 changes: 38 additions & 0 deletions .ci/examples-readme-hydration/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require('fs');
const path = require('path');
const YAML = require('yaml');

const examplesPath = path.resolve(__dirname, '../../examples/');
const readMeTemplatePath = path.resolve(examplesPath, 'README_TEMPLATE.md');
const readMeOutputPath = path.resolve(examplesPath, 'README.md');
const extractor = /<include file="(.*)" format="(.*)" \/>/g;
const disclaimer = `<!--


!!! THIS IS A GENERATED FILE !!!
Please do not update this file but the TEMPLATE instead!


-->
`;

(async () => {
let readMe = await fs.promises.readFile(readMeTemplatePath, 'utf8');
const includes = readMe.matchAll(extractor);
for await(let include of includes) {
const fileName = include[1];
const format = include[2];
let fileContent = await fs.promises.readFile(path.resolve(examplesPath, fileName), 'utf8');
if (format === 'yaml') {
try {
const schema = JSON.parse(fileContent);
fileContent = YAML.stringify(schema);
}
catch(ex) {
console.error('Enable to parse JSON or convert it to YAML, output as it is.', ex);
}
}
readMe = readMe.replace(include[0], fileContent);
};
await fs.promises.writeFile(readMeOutputPath, disclaimer + readMe, { encoding: 'utf8', flag: 'w' });
})();
24 changes: 24 additions & 0 deletions .ci/examples-readme-hydration/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .ci/examples-readme-hydration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "examples-readme-hydration",
"version": "0.1.0",
"description": "Builds ./examples/README.md based on ./examples/README_TEMPLATE.md",
"main": "src/index.js",
"scripts": {
"start": "node ./index.js"
},
"keywords": ["cncf", "serverless", "workflow", "specification"],
"author": "CNCF Serverless Workflow Specification",
"license": "ISC",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which license is this? Can we change it to Apache?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it's the default when generating the npm project I think. As far as I recall, I copy/pasted from https://github.com/serverlessworkflow/specification/blob/main/.ci/validation/package.json

"dependencies": {
"yaml": "^2.3.4"
}
}
21 changes: 21 additions & 0 deletions .github/workflows/examples-readme-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Check examples README manual updates

on:
pull_request:
paths:
- 'examples/README.md'

jobs:
verification:
runs-on: ubuntu-latest
steps:
- if: contains(github.head_ref, 'automation-examples-readme')
name: pass
run: echo "The update is made by the bot, as expected."

- if: contains(github.head_ref, 'automation-examples-readme') == false
name: fail
run: |
echo "The file examples/README.md should not be manually edited !"
echo "Please update examples/README_TEMPLATE.md instead"
exit 1
63 changes: 63 additions & 0 deletions .github/workflows/examples-readme-hydration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Examples README hydration

on:
pull_request:
types:
- closed
branches: [ 'main']
paths:
- 'examples/**/*'
- '!examples/README.md'

jobs:
build:
if: github.repository == 'serverlessworkflow/specification' && github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.BOT_PAT }}


- uses: actions/setup-node@v4
with:
node-version: 21

- name: Import GPG key
id: import-gpg
uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.BOT_GPG_PRIVATE_KEY }}
git_config_global: true
git_user_signingkey: true
git_commit_gpgsign: true
git_tag_gpgsign: true

- run: |
set -e

# Reset origin
git remote set-url origin https://${{ secrets.BOT_USERNAME }}:${{ secrets.BOT_PAT }}@github.com/${{ github.repository }}.git
git checkout ${{ github.ref_name }}

# Create a new git branch
git checkout -b automation-examples-readme-${{ github.event.pull_request.number }}

# Install & run JS scripts
cd .ci/examples-readme-hydration/
npm ci
npm start

# Commit & push changes
git config user.name '${{ secrets.BOT_USERNAME }}'
git config user.email '${{ secrets.BOT_EMAIL }}'
git commit -S -a -m 'Rebuilt examples README.md'
git push origin automation-examples-readme-${{ github.event.pull_request.number }}

# Create a PR on GH
gh pr create -B main -H automation-examples-readme-${{ github.event.pull_request.number }} --title '[From CI] Rebuilt examples README' --body 'Automatic hydration of examples README.md'
env:
GITHUB_TOKEN: ${{ secrets.BOT_PAT }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ reference the [Ownership of Copyrights in CNCF Project Contributions](https://gi
### Communication

- Community Slack Channel: [https://slack.cncf.io/](https://slack.cncf.io/) - #serverless-workflow
- [Weekly project meetings](#Meetings)
- [Weekly project meetings](#weekly-meetings)
- Project Maintainers Email: [cncf-serverlessws-maintainers](mailto:[email protected])
- Serverless WG Email: [cncf-wg-serverless](mailto:[email protected])
- Serverless WG Subscription: [https://lists.cncf.io/g/cncf-wg-serverless](https://lists.cncf.io/g/cncf-wg-serverless)
Expand Down
Loading