Skip to content

Commit

Permalink
Add code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
emikeb committed Jul 30, 2024
1 parent 0fee64e commit 5b5de9e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 28 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/update-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name: Update Schema

"on":
pull_request:
push:
branches:
- main
Expand All @@ -20,8 +21,11 @@ jobs:
with:
python-version: "3.12"

- name: Install
run: python -m pip install retasc
- name: Install Poetry
run: python -m pip install poetry

- name: Install Dependencies
run: poetry install --only=main

- name: Generate schema
run: retasc generate-schema schema.yaml
Expand All @@ -32,6 +36,7 @@ jobs:
cp schema.yaml public/
- name: Deploy to GitHub Pages
if: github.ref == 'refs/heads/main'
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
16 changes: 7 additions & 9 deletions src/retasc/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
import argparse
import sys
import logging

from retasc import __doc__ as doc
from retasc import __version__
Expand All @@ -10,6 +10,8 @@
from retasc.validator.generate_schema import generate_schema
from retasc.validator.validate_rules import validate_rule

logger = logging.getLogger(__name__)


def parse_args():
parser = argparse.ArgumentParser(description=doc)
Expand Down Expand Up @@ -39,11 +41,7 @@ def main():
init_logging()
init_tracing()

try:
if args.command == "validate-rule":
validate_rule(args.rule_file)
elif args.command == "generate-schema":
generate_schema(args.schema_file)
except Exception as e:
print(f"An error occurred: {e}")
sys.exit(1)
if args.command == "validate-rule":
validate_rule(args.rule_file)
elif args.command == "generate-schema":
generate_schema(args.schema_file)
1 change: 1 addition & 0 deletions src/retasc/validator/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SCHEMA_VERSION = 1
2 changes: 1 addition & 1 deletion src/retasc/validator/example_rule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 1
name: "Example Rule"
prerequisites:
pp_schedule_item_name: "Release Date"
days_before_or_after: "5"
days_before_or_after: -7
dependent_rules:
- "Dependent Rule 1"
- "Dependent Rule 2"
Expand Down
9 changes: 3 additions & 6 deletions src/retasc/validator/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
def generate_schema(output_path: str) -> None:
schema = Rule.model_json_schema()
schema_yaml = yaml.dump(schema, sort_keys=False)
try:
with open(output_path, "w") as schema_file:
schema_file.write(schema_yaml)
except Exception as e:
logger.error(f"Error generating schema: {e}")
raise e

with open(output_path, "w") as schema_file:
schema_file.write(schema_yaml)
6 changes: 5 additions & 1 deletion src/retasc/validator/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pydantic import BaseModel, Field

from retasc.validator.config import SCHEMA_VERSION


class JiraIssue(BaseModel):
"""Represents a Jira issue, which can have subtasks."""
Expand Down Expand Up @@ -31,7 +33,9 @@ class Prerequisites(BaseModel):
class Rule(BaseModel):
"""Represents a rule which includes prerequisites and Jira issues."""

version: int = Field(description="The version of the rule.")
version: int = Field(
description=f"The version of the rule schema. The latest version is {SCHEMA_VERSION}."
)
name: str = Field(description="The name of the rule.")
prerequisites: Prerequisites = Field(description="The prerequisites for the rule.")
jira_issues: list[JiraIssue] = Field(description="The jira issues for the rule.")
7 changes: 7 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import retasc.validator.config as config


def test_schema_version_exists():
assert hasattr(
config, "SCHEMA_VERSION"
), "SCHEMA_VERSION is not defined in the config module"
9 changes: 0 additions & 9 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,3 @@ def test_generate_schema(mock_generate_schema, capsys):
stdout, stderr = capsys.readouterr()
assert stdout == ""
assert stderr == ""


def test_main_exception(capsys):
with patch(
"retasc.__main__.validate_rule", side_effect=Exception("Test exception")
):
run_main("validate-rule", "test_rule.yaml", code=1)
stdout, stderr = capsys.readouterr()
assert "An error occurred: Test exception" in stdout

0 comments on commit 5b5de9e

Please sign in to comment.