From c139d7b658a15ae263398978f5e6bed81958b96f Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Mon, 25 Nov 2024 12:04:56 +0100 Subject: [PATCH] Generate schema for configuration --- README.md | 5 +++-- src/retasc/__main__.py | 8 +++++++- src/retasc/models/generate_schema.py | 14 +++++++++----- tests/test_main.py | 10 ++++++---- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5943da1..eebfb7a 100644 --- a/README.md +++ b/README.md @@ -68,13 +68,14 @@ retasc validate-rules examples/rules retasc validate-rules examples/rules/rules.yaml ``` -## Generate Rule Schema +## Generate Schema Files -Example of generating YAML and JSON schema for rule files: +Examples of generating YAML and JSON schema for rule and configuration files: ``` retasc generate-schema rule_schema.yaml retasc generate-schema --json rule_schema.json +retasc generate-schema --config --json config.json ``` ## Development diff --git a/src/retasc/__main__.py b/src/retasc/__main__.py index e6ffc85..1d88c49 100755 --- a/src/retasc/__main__.py +++ b/src/retasc/__main__.py @@ -45,6 +45,12 @@ def parse_args(): help="Generate JSON instead of the default YAML", action="store_true", ) + generate_parser.add_argument( + "-c", + "--config", + help="Generate schema for the configuration instead of rules", + action="store_true", + ) subparsers.add_parser( "run", help="Process rules, data from Product Pages and apply changes to Jira" @@ -70,7 +76,7 @@ def main(): sys.exit(1) print("Validation succeeded: The rule files are valid") elif args.command == "generate-schema": - generate_schema(args.schema_file, output_json=args.json) + generate_schema(args.schema_file, output_json=args.json, config=args.config) elif args.command in ("run", "dry-run"): dry_run = args.command == "dry-run" run(dry_run=dry_run) diff --git a/src/retasc/models/generate_schema.py b/src/retasc/models/generate_schema.py index 05fbea4..765109e 100644 --- a/src/retasc/models/generate_schema.py +++ b/src/retasc/models/generate_schema.py @@ -5,18 +5,22 @@ import yaml +from retasc.models.config import Config from retasc.models.rule import Rule -def _generate_schema(file: TextIO, generator) -> None: - schema = Rule.model_json_schema() +def _generate_schema(cls, file: TextIO, generator) -> None: + schema = cls.model_json_schema() generator.dump(schema, file, indent=2, sort_keys=False) -def generate_schema(output_path: str | None, *, output_json: bool) -> None: +def generate_schema( + output_path: str | None, *, output_json: bool, config: bool = False +) -> None: generator = json if output_json else yaml + cls = Config if config else Rule if output_path is None: - _generate_schema(sys.stdout, generator) + _generate_schema(cls, sys.stdout, generator) else: with open(output_path, "w") as schema_file: - _generate_schema(schema_file, generator) + _generate_schema(cls, schema_file, generator) diff --git a/tests/test_main.py b/tests/test_main.py index 9b9350a..56b84dd 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -102,23 +102,25 @@ def test_run(arg, issue_key, capsys): def test_generate_schema_yaml(mock_generate_schema): run_main("generate-schema", "output_schema.yaml", expected_exit_code=0) mock_generate_schema.assert_called_once_with( - "output_schema.yaml", output_json=False + "output_schema.yaml", output_json=False, config=False ) def test_generate_schema_yaml_to_stdout(mock_generate_schema): run_main("generate-schema", expected_exit_code=0) - mock_generate_schema.assert_called_once_with(None, output_json=False) + mock_generate_schema.assert_called_once_with(None, output_json=False, config=False) def test_generate_schema_json(mock_generate_schema): run_main("generate-schema", "--json", "output_schema.json", expected_exit_code=0) - mock_generate_schema.assert_called_once_with("output_schema.json", output_json=True) + mock_generate_schema.assert_called_once_with( + "output_schema.json", output_json=True, config=False + ) def test_generate_schema_json_to_stdout(mock_generate_schema): run_main("generate-schema", "--json", expected_exit_code=0) - mock_generate_schema.assert_called_once_with(None, output_json=True) + mock_generate_schema.assert_called_once_with(None, output_json=True, config=False) def test_validate_rules_output(valid_rule_file, capsys):