From fcca2f7ef7d3fdafaa8b90a07ac71e3562a4b878 Mon Sep 17 00:00:00 2001 From: Brian Ginsburg Date: Wed, 7 Feb 2024 15:24:59 -0800 Subject: [PATCH] feat: Add schemas generation workflow --- .github/workflows/schemas.yml | 51 +++++++++++++++++++ homestar-runtime/schemas/generate.rs | 73 +++++++++++++++++++--------- 2 files changed, 100 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/schemas.yml diff --git a/.github/workflows/schemas.yml b/.github/workflows/schemas.yml new file mode 100644 index 00000000..487428be --- /dev/null +++ b/.github/workflows/schemas.yml @@ -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 }} diff --git a/homestar-runtime/schemas/generate.rs b/homestar-runtime/schemas/generate.rs index 4dda89f9..9e893b4b 100644 --- a/homestar-runtime/schemas/generate.rs +++ b/homestar-runtime/schemas/generate.rs @@ -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, @@ -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