Skip to content

Commit

Permalink
fix: adds an example row to final CSV to account for skipped rows
Browse files Browse the repository at this point in the history
The csv_to_oscal_cd task skips the first two rows. The output CSV only had a
header row so the first rule was always skipped. This change adds an example
row to where the descriptions would be.

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Oct 16, 2023
1 parent 23b290b commit 39b6e3e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions tests/data/yaml/test_complete_rule_no_params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ x-trestle-rule-info:
include-controls:
- id: ac-1
x-trestle-component-info:
name: Component 1
description: Component 1 description
name: Component 2
description: Component 2 description
type: service
13 changes: 12 additions & 1 deletion tests/trestlebot/tasks/test_rule_transform_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,22 @@ def test_rule_transform_task(tmp_trestle_dir: str) -> None:
assert orig_comp is not None
assert orig_comp.metadata.title == "Component definition for test_comp"
assert orig_comp.components is not None
assert len(orig_comp.components) == 1
assert len(orig_comp.components) == 2

component = orig_comp.components[0]

assert component.props is not None
assert component.title == "Component 2"
assert len(component.props) == 2
assert component.props[0].name == RULE_ID
assert component.props[0].value == "example_rule_2"
assert component.props[1].name == RULE_DESCRIPTION
assert component.props[1].value == "My rule description for example rule 2"

component = orig_comp.components[1]

assert component.props is not None
assert component.title == "Component 1"
assert len(component.props) == 5
assert component.props[0].name == RULE_ID
assert component.props[0].value == "example_rule_1"
Expand Down
7 changes: 7 additions & 0 deletions trestlebot/transformers/csv_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
Parameter,
Profile,
TrestleRule,
get_default_rule,
)


Expand Down Expand Up @@ -218,7 +219,13 @@ def write_to_file(self, filepath: pathlib.Path) -> None:
fieldnames.extend(self._csv_columns.get_required_column_names())
fieldnames.extend(self._csv_columns.get_optional_column_names())

# The trestle csv_to_oscal_cd task skips the header row and the
# first row which is meant to have descriptions. We will just write a default right now.
default_rule: TrestleRule = get_default_rule()
example_row = self._transformer.transform(default_rule)

writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerow(example_row)
for row in self._rows:
writer.writerow(row)
18 changes: 18 additions & 0 deletions trestlebot/transformers/trestle_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,21 @@ class TrestleRule(BaseModel):
component: ComponentInfo
parameter: Optional[Parameter]
profile: Profile


def get_default_rule() -> TrestleRule:
"""Create a default rule for template purposes."""
return TrestleRule(
name="example rule",
description="example description",
component=ComponentInfo(
name="example component",
type="service",
description="example description",
),
profile=Profile(
description="example profile",
href="example href",
include_controls=[Control(id="example")],
),
)

0 comments on commit 39b6e3e

Please sign in to comment.