Skip to content

Commit

Permalink
feat: Add schemas generation workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Feb 7, 2024
1 parent fc70e06 commit fcca2f7
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 24 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/schemas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: 📄 Schemas

on:
push:
branches: [main]

pull_request:
branches: ["**"]

permissions:
contents: write
pull-requests: write

jobs:
schemas:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.HOMESTAR_UPDATE_TOKEN }}
ref: ${{ github.event.pull_request.head.sha }}

- name: Install Rust Toolchain
uses: dtolnay/rust-toolchain@stable

- name: Run generate schemas
run: cargo run --bin schemas

# - name: Check for modified files
# id: git-check
# run: echo modified=$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi) >> $GITHUB_OUTPUT

- name: Check for changed files
id: changed-files
uses: tj-actions/changed-files@v42
with:
files_yaml: |
docs:
- 'homestar-runtime/schemas/docs/*.json'
- name: Push changes
# if: steps.git-check.outputs.modified == 'true'
if: steps.changed-files-yaml.outputs.docs_any_changed == 'true'
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
git commit -am "chore(schemas): update OpenRPC API doc and JSON schemas"
git push --force-with-lease origin HEAD:refs/heads/${{ github.head_ref }}
73 changes: 49 additions & 24 deletions homestar-runtime/schemas/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,62 @@ use openrpc::document::{
};

fn main() {
println!("{}", env!("CARGO_MANIFEST_DIR"));
let health_schema = schema_for!(Health);
let _ = fs::File::create("schemas/docs/health.json")
.unwrap()
.write_all(&serde_json::to_vec_pretty(&health_schema).unwrap());
let _ = fs::File::create(format!(
"{}/schemas/docs/health.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&health_schema).unwrap());

let metrics_schema = schema_for!(PrometheusData);
let _ = fs::File::create("schemas/docs/metrics.json")
.unwrap()
.write_all(&serde_json::to_vec_pretty(&metrics_schema).unwrap());
let _ = fs::File::create(format!(
"{}/schemas/docs/metrics.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&metrics_schema).unwrap());

let node_info_schema = schema_for!(NodeInfo);
let _ = fs::File::create("schemas/docs/node_info.json")
.unwrap()
.write_all(&serde_json::to_vec_pretty(&node_info_schema).unwrap());
let _ = fs::File::create(format!(
"{}/schemas/docs/node_info.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&node_info_schema).unwrap());

let network_schema = schema_for!(NetworkNotification);
let _ = fs::File::create("schemas/docs/network.json")
.unwrap()
.write_all(&serde_json::to_vec_pretty(&network_schema).unwrap());
let _ = fs::File::create(format!(
"{}/schemas/docs/network.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&network_schema).unwrap());

let workflow_schema = schema_for!(Workflow<'static, ()>);
let _ = fs::File::create("schemas/docs/workflow.json")
.unwrap()
.write_all(&serde_json::to_vec_pretty(&workflow_schema).unwrap());
let _ = fs::File::create(format!(
"{}/schemas/docs/workflow.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&workflow_schema).unwrap());

let receipt_schema = schema_for!(Receipt<()>);
let _ = fs::File::create("schemas/docs/receipt.json")
.unwrap()
.write_all(&serde_json::to_vec_pretty(&receipt_schema).unwrap());
let _ = fs::File::create(format!(
"{}/schemas/docs/receipt.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&receipt_schema).unwrap());

let receipt_notification_schema = schema_for!(ReceiptNotification);
let _ = fs::File::create("schemas/docs/receipt_notification.json")
.unwrap()
.write_all(&serde_json::to_vec_pretty(&receipt_notification_schema).unwrap());
let _ = fs::File::create(format!(
"{}/schemas/docs/receipt_notification.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&receipt_notification_schema).unwrap());

let api_doc = generate_api_doc(
health_schema,
Expand All @@ -63,9 +85,12 @@ fn main() {
workflow_schema,
receipt_notification_schema,
);
let _ = fs::File::create("schemas/docs/api.json")
.unwrap()
.write_all(&serde_json::to_vec_pretty(&api_doc).unwrap());
let _ = fs::File::create(format!(
"{}/schemas/docs/api.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
.write_all(&serde_json::to_vec_pretty(&api_doc).unwrap());
}

// Spec: https://github.com/open-rpc/spec/blob/1.2.6/spec.md
Expand Down

0 comments on commit fcca2f7

Please sign in to comment.