Skip to content

Commit

Permalink
Generate schema for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Nov 25, 2024
1 parent a7674ae commit c139d7b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/retasc/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions src/retasc/models/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 6 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c139d7b

Please sign in to comment.