|
3 | 3 |
|
4 | 4 | """Trestle Bot Sync CaC Content Tasks"""
|
5 | 5 |
|
| 6 | +import datetime |
6 | 7 | import json
|
7 | 8 | import logging
|
8 | 9 | import os
|
|
33 | 34 | RuleInfo,
|
34 | 35 | RulesTransformer,
|
35 | 36 | get_component_info,
|
36 |
| - update_component_definition, |
37 | 37 | )
|
38 | 38 |
|
39 | 39 |
|
@@ -190,17 +190,25 @@ def _create_or_update_compdef(self, compdef_type: str = "service") -> None:
|
190 | 190 | cd_json = cd_dir / "component-definition.json"
|
191 | 191 | if cd_json.exists():
|
192 | 192 | logger.info(f"The component definition for {self.product} exists.")
|
193 |
| - with open(cd_json, "r", encoding="utf-8") as f: |
194 |
| - data = json.load(f) |
195 |
| - components = data["component-definition"]["components"] |
196 |
| - for index, component in enumerate(components): |
197 |
| - if component.get("title") == oscal_component.title: |
198 |
| - # The update should be skipped if no content changes |
199 |
| - logger.info(f"Update props of component {product_name}") |
200 |
| - data["component-definition"]["components"][index][ |
201 |
| - "props" |
202 |
| - ] = oscal_component.props |
203 |
| - update_component_definition(cd_json) |
| 193 | + compdef = ComponentDefinition.oscal_read(cd_json) |
| 194 | + updated = False |
| 195 | + for index, component in enumerate(compdef.components): |
| 196 | + if component.title == oscal_component.title: |
| 197 | + if component.props != oscal_component.props: |
| 198 | + compdef.components[index].props = oscal_component.props |
| 199 | + updated = True |
| 200 | + break |
| 201 | + if updated: |
| 202 | + logger.info(f"Update component definition: {cd_json}") |
| 203 | + compdef.metadata.version = str( |
| 204 | + "{:.1f}".format(float(compdef.metadata.version) + 0.1) |
| 205 | + ) |
| 206 | + compdef.metadata.last_modified = ( |
| 207 | + datetime.datetime.now(datetime.timezone.utc) |
| 208 | + .replace(microsecond=0) |
| 209 | + .isoformat() |
| 210 | + ) |
| 211 | + compdef.oscal_write(cd_json) |
204 | 212 | else:
|
205 | 213 | logger.info(f"Creating component definition for product {self.product}")
|
206 | 214 | cd_dir.mkdir(exist_ok=True, parents=True)
|
|
0 commit comments