Skip to content

Commit 0dfdf42

Browse files
authored
Merge pull request #805 from JBBianchi/feat-examples-readme-templating
Imports Validation Schemas into the Examples README during CI
2 parents cd992ed + e0316ec commit 0dfdf42

35 files changed

+4809
-2613
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const YAML = require('yaml');
4+
5+
const examplesPath = path.resolve(__dirname, '../../examples/');
6+
const readMeTemplatePath = path.resolve(examplesPath, 'README_TEMPLATE.md');
7+
const readMeOutputPath = path.resolve(examplesPath, 'README.md');
8+
const extractor = /<include file="(.*)" format="(.*)" \/>/g;
9+
const disclaimer = `<!--
10+
11+
12+
!!! THIS IS A GENERATED FILE !!!
13+
Please do not update this file but the TEMPLATE instead!
14+
15+
16+
-->
17+
`;
18+
19+
(async () => {
20+
let readMe = await fs.promises.readFile(readMeTemplatePath, 'utf8');
21+
const includes = readMe.matchAll(extractor);
22+
for await(let include of includes) {
23+
const fileName = include[1];
24+
const format = include[2];
25+
let fileContent = await fs.promises.readFile(path.resolve(examplesPath, fileName), 'utf8');
26+
if (format === 'yaml') {
27+
try {
28+
const schema = JSON.parse(fileContent);
29+
fileContent = YAML.stringify(schema);
30+
}
31+
catch(ex) {
32+
console.error('Enable to parse JSON or convert it to YAML, output as it is.', ex);
33+
}
34+
}
35+
readMe = readMe.replace(include[0], fileContent);
36+
};
37+
await fs.promises.writeFile(readMeOutputPath, disclaimer + readMe, { encoding: 'utf8', flag: 'w' });
38+
})();

.ci/examples-readme-hydration/package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "examples-readme-hydration",
3+
"version": "0.1.0",
4+
"description": "Builds ./examples/README.md based on ./examples/README_TEMPLATE.md",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"start": "node ./index.js"
8+
},
9+
"keywords": ["cncf", "serverless", "workflow", "specification"],
10+
"author": "CNCF Serverless Workflow Specification",
11+
"license": "ISC",
12+
"dependencies": {
13+
"yaml": "^2.3.4"
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Check examples README manual updates
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'examples/README.md'
7+
8+
jobs:
9+
verification:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- if: contains(github.head_ref, 'automation-examples-readme')
13+
name: pass
14+
run: echo "The update is made by the bot, as expected."
15+
16+
- if: contains(github.head_ref, 'automation-examples-readme') == false
17+
name: fail
18+
run: |
19+
echo "The file examples/README.md should not be manually edited !"
20+
echo "Please update examples/README_TEMPLATE.md instead"
21+
exit 1
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Examples README hydration
2+
3+
on:
4+
pull_request:
5+
types:
6+
- closed
7+
branches: [ 'main']
8+
paths:
9+
- 'examples/**/*'
10+
- '!examples/README.md'
11+
12+
jobs:
13+
build:
14+
if: github.repository == 'serverlessworkflow/specification' && github.event.pull_request.merged == true
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: write
18+
pull-requests: write
19+
steps:
20+
- uses: actions/checkout@v4
21+
with:
22+
token: ${{ secrets.BOT_PAT }}
23+
24+
25+
- uses: actions/setup-node@v4
26+
with:
27+
node-version: 21
28+
29+
- name: Import GPG key
30+
id: import-gpg
31+
uses: crazy-max/ghaction-import-gpg@v6
32+
with:
33+
gpg_private_key: ${{ secrets.BOT_GPG_PRIVATE_KEY }}
34+
git_config_global: true
35+
git_user_signingkey: true
36+
git_commit_gpgsign: true
37+
git_tag_gpgsign: true
38+
39+
- run: |
40+
set -e
41+
42+
# Reset origin
43+
git remote set-url origin https://${{ secrets.BOT_USERNAME }}:${{ secrets.BOT_PAT }}@github.com/${{ github.repository }}.git
44+
git checkout ${{ github.ref_name }}
45+
46+
# Create a new git branch
47+
git checkout -b automation-examples-readme-${{ github.event.pull_request.number }}
48+
49+
# Install & run JS scripts
50+
cd .ci/examples-readme-hydration/
51+
npm ci
52+
npm start
53+
54+
# Commit & push changes
55+
git config user.name '${{ secrets.BOT_USERNAME }}'
56+
git config user.email '${{ secrets.BOT_EMAIL }}'
57+
git commit -S -a -m 'Rebuilt examples README.md'
58+
git push origin automation-examples-readme-${{ github.event.pull_request.number }}
59+
60+
# Create a PR on GH
61+
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'
62+
env:
63+
GITHUB_TOKEN: ${{ secrets.BOT_PAT }}

0 commit comments

Comments
 (0)