Skip to content

Commit

Permalink
[ci] Update openapi.json in api.md #1145 (#1148)
Browse files Browse the repository at this point in the history
add api.md update
  • Loading branch information
michaelvlach authored Jul 14, 2024
1 parent 3963731 commit 309a6b8
Show file tree
Hide file tree
Showing 4 changed files with 467 additions and 124 deletions.
27 changes: 27 additions & 0 deletions agdb_ci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const AGDB_PROJECT: &str = "agdb";
const CARGO_TOML: &str = "Cargo.toml";
const PACKAGE_JSON: &str = "package.json";
const OPENAPI_JSON: &str = "schema.json";
const API_MD: &str = "api.md";
const IGNORE: [&str; 8] = [
"node_modules",
"tests",
Expand All @@ -16,6 +17,10 @@ const IGNORE: [&str; 8] = [
"test-results",
];
const TYPESCRIPT_PROJECTS: [&str; 1] = ["@agnesoft/agdb_api"];
#[cfg(windows)]
const LN: &str = "\r\n";
#[cfg(not(windows))]
const LN: &str = "\n";

#[derive(Debug)]
struct CIError {
Expand All @@ -27,6 +32,8 @@ struct CIError {
struct ProjectFiles {
tomls: Vec<std::path::PathBuf>,
jsons: Vec<std::path::PathBuf>,
schema: std::path::PathBuf,
api: std::path::PathBuf,
}

impl<E: std::error::Error> From<E> for CIError {
Expand Down Expand Up @@ -68,6 +75,9 @@ fn project_files(path: &std::path::Path, files: &mut ProjectFiles) -> Result<(),
files.jsons.push(dir.path().join(PACKAGE_JSON));
} else if dir.path().join(OPENAPI_JSON).exists() {
files.jsons.push(dir.path().join(OPENAPI_JSON));
files.schema = dir.path().join(OPENAPI_JSON);
} else if dir.path().join(API_MD).exists() {
files.api = dir.path().join(API_MD);
}

project_files(&dir.path(), files)?;
Expand Down Expand Up @@ -159,6 +169,22 @@ fn update_jsons(
Ok(())
}

fn update_api_md(schema: &std::path::PathBuf, api: &std::path::PathBuf) -> Result<(), CIError> {
let schema_content = std::fs::read_to_string(schema)?;
let api_content = std::fs::read_to_string(api)?;
let api_docs = api_content
.split_once("## openapi.json")
.expect("invalid api.md")
.0;
let new_api = format!(
"{}## openapi.json{LN}{LN}```json{LN}{}{LN}```",
api_docs,
schema_content.trim()
);
std::fs::write(api, new_api)?;
Ok(())
}

fn main() -> Result<(), CIError> {
let current_version = current_version()?;
let new_version = std::fs::read_to_string(std::path::Path::new("Version"))?
Expand All @@ -172,6 +198,7 @@ fn main() -> Result<(), CIError> {
project_files(std::path::Path::new("."), &mut files)?;
update_tomls(&current_version, &new_version, &files.tomls)?;
update_jsons(&current_version, &new_version, &files.jsons)?;
update_api_md(&files.schema, &files.api)?;

println!("DONE");

Expand Down
9 changes: 9 additions & 0 deletions agdb_ci/tests/test_data_before/docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# API

Some text

## openapi.json

```json
{}
```
19 changes: 19 additions & 0 deletions agdb_ci/tests/test_data_expected/docs/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# API

Some text

## openapi.json

```json
{
"openapi": "3.0.3",
"info": {
"title": "agdb_server",
"description": "Agnesoft Graph Database Server",
"license": {
"name": "Apache-2.0"
},
"version": "0.3.0"
}
}
```
Loading

0 comments on commit 309a6b8

Please sign in to comment.