Skip to content

Commit

Permalink
Merge pull request #43 from jpower432/refactor/update-to-repo-api
Browse files Browse the repository at this point in the history
refactor: update to repository api from trestle
  • Loading branch information
Alex Flom authored Sep 18, 2023
2 parents b7abb4e + 37d7571 commit eed25ed
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 85 deletions.
34 changes: 16 additions & 18 deletions trestlebot/tasks/authored/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import pathlib

from trestle.common.err import TrestleError
from trestle.core.commands.author.catalog import CatalogAssemble, CatalogGenerate
from trestle.core.commands.common.return_codes import CmdReturnCodes
from trestle.core.repository import AgileAuthoring

from trestlebot.tasks.authored.base_authored import (
AuthoredObjectException,
Expand All @@ -44,17 +43,17 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None:
"""Run assemble actions for catalog type at the provided path"""
trestle_root = pathlib.Path(self.get_trestle_root())
catalog = os.path.basename(markdown_path)
authoring = AgileAuthoring(trestle_root)
try:
exit_code = CatalogAssemble.assemble_catalog(
trestle_root=trestle_root,
md_name=markdown_path,
assem_cat_name=catalog,
parent_cat_name="",
set_parameters_flag=True,
success = authoring.assemble_catalog_markdown(
name=catalog,
output=catalog,
markdown_dir=markdown_path,
set_parameters=True,
regenerate=False,
version=version_tag,
)
if exit_code != CmdReturnCodes.SUCCESS.value:
if not success:
raise AuthoredObjectException(
f"Unknown error occurred while assembling {catalog}"
)
Expand All @@ -63,20 +62,19 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None:

def regenerate(self, model_path: str, markdown_path: str) -> None:
"""Run assemble actions for catalog type at the provided path"""
trestle_root = self.get_trestle_root()
trestle_path = pathlib.Path(trestle_root)
catalog_generate: CatalogGenerate = CatalogGenerate()
trestle_root = pathlib.Path(self.get_trestle_root())
authoring = AgileAuthoring(trestle_root)

catalog = os.path.basename(model_path)
try:
exit_code = catalog_generate.generate_markdown(
trestle_root=trestle_path,
catalog_path=pathlib.Path(trestle_root, model_path, "catalog.json"),
markdown_path=pathlib.Path(trestle_root, markdown_path, catalog),
yaml_header={},
success = authoring.generate_catalog_markdown(
name=catalog,
output=os.path.join(markdown_path, catalog),
force_overwrite=False,
yaml_header="",
overwrite_header_values=False,
)
if exit_code != CmdReturnCodes.SUCCESS.value:
if not success:
raise AuthoredObjectException(
f"Unknown error occurred while regenerating {catalog}"
)
Expand Down
32 changes: 16 additions & 16 deletions trestlebot/tasks/authored/compdef.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
from trestle.common.load_validate import load_validate_model_name
from trestle.common.model_utils import ModelUtils
from trestle.core.catalog.catalog_interface import CatalogInterface
from trestle.core.commands.author.component import ComponentAssemble, ComponentGenerate
from trestle.core.commands.common.return_codes import CmdReturnCodes
from trestle.core.models.file_content_type import FileContentType
from trestle.core.profile_resolver import ProfileResolver
from trestle.core.repository import AgileAuthoring

from trestlebot.tasks.authored.base_authored import (
AuthoredObjectException,
Expand All @@ -55,16 +54,17 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None:
"""Run assemble actions for compdef type at the provided path"""
trestle_root = pathlib.Path(self.get_trestle_root())
compdef = os.path.basename(markdown_path)

authoring = AgileAuthoring(trestle_root)
try:
exit_code = ComponentAssemble.assemble_component(
trestle_root=trestle_root,
md_name=markdown_path,
assem_comp_name=compdef,
parent_comp_name="",
success = authoring.assemble_component_definition_markdown(
name=compdef,
output=compdef,
markdown_dir=os.path.join(markdown_path, compdef),
regenerate=False,
version=version_tag,
)
if exit_code != CmdReturnCodes.SUCCESS.value:
if not success:
raise AuthoredObjectException(
f"Unknown error occurred while assembling {compdef}"
)
Expand All @@ -73,18 +73,18 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None:

def regenerate(self, model_path: str, markdown_path: str) -> None:
"""Run assemble actions for compdef type at the provided path"""
trestle_root = self.get_trestle_root()
trestle_path = pathlib.Path(trestle_root)
comp_generate: ComponentGenerate = ComponentGenerate()
trestle_root = pathlib.Path(self.get_trestle_root())
authoring = AgileAuthoring(trestle_root)

comp_name = os.path.basename(model_path)

try:
exit_code = comp_generate.component_generate_all(
trestle_root=trestle_path,
comp_def_name=comp_name,
markdown_dir_name=os.path.join(trestle_root, markdown_path, comp_name),
success = authoring.generate_component_definition_markdown(
name=comp_name,
output=markdown_path,
force_overwrite=False,
)
if exit_code != CmdReturnCodes.SUCCESS.value:
if not success:
raise AuthoredObjectException(
f"Unknown error occurred while regenerating {comp_name}"
)
Expand Down
46 changes: 23 additions & 23 deletions trestlebot/tasks/authored/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
from trestle.common.err import TrestleError, TrestleNotFoundError
from trestle.common.load_validate import load_validate_model_name
from trestle.common.model_utils import ModelUtils
from trestle.core.commands.author.profile import ProfileAssemble, ProfileGenerate
from trestle.core.commands.common.return_codes import CmdReturnCodes
from trestle.core.models.file_content_type import FileContentType
from trestle.core.repository import AgileAuthoring
from trestle.oscal.common import IncludeAll

from trestlebot.tasks.authored.base_authored import (
Expand All @@ -52,21 +51,23 @@ def __init__(self, trestle_root: str) -> None:
def assemble(self, markdown_path: str, version_tag: str = "") -> None:
"""Run assemble actions for profile type at the provided path"""
trestle_root = pathlib.Path(self.get_trestle_root())
authoring = AgileAuthoring(trestle_root)

profile = os.path.basename(markdown_path)

try:
exit_code = ProfileAssemble.assemble_profile(
trestle_root=trestle_root,
md_name=markdown_path,
assem_prof_name=profile,
parent_prof_name="",
set_parameters_flag=True,
success = authoring.assemble_profile_markdown(
name=profile,
output=profile,
markdown_dir=markdown_path,
set_parameters=True,
regenerate=False,
version=version_tag,
allowed_sections=None,
required_sections=[],
sections_dict={},
sections="",
required_sections="",
allowed_sections="",
)
if exit_code != CmdReturnCodes.SUCCESS.value:
if not success:
raise AuthoredObjectException(
f"Unknown error occurred while assembling {profile}"
)
Expand All @@ -75,22 +76,21 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None:

def regenerate(self, model_path: str, markdown_path: str) -> None:
"""Run assemble actions for profile type at the provided path"""
trestle_root = self.get_trestle_root()
trestle_path = pathlib.Path(trestle_root)
profile_generate: ProfileGenerate = ProfileGenerate()
trestle_root = pathlib.Path(self.get_trestle_root())
authoring = AgileAuthoring(trestle_root)

profile = os.path.basename(model_path)
try:
exit_code = profile_generate.generate_markdown(
trestle_root=trestle_path,
profile_path=pathlib.Path(trestle_root, model_path, "profile.json"),
markdown_path=pathlib.Path(trestle_root, markdown_path, profile),
yaml_header={},
success = authoring.generate_profile_markdown(
name=profile,
output=os.path.join(markdown_path, profile),
force_overwrite=False,
yaml_header="",
overwrite_header_values=False,
sections_dict={},
required_sections=[],
sections="",
required_sections="",
)
if exit_code != CmdReturnCodes.SUCCESS.value:
if not success:
raise AuthoredObjectException(
f"Unknown error occurred while regenerating {profile}"
)
Expand Down
50 changes: 22 additions & 28 deletions trestlebot/tasks/authored/ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

"""Trestle Bot functions for SSP authoring"""

import argparse
import json
import logging
import os
import pathlib
from typing import Any, Dict, List, Optional

from trestle.common.err import TrestleError
from trestle.core.commands.author.ssp import SSPAssemble, SSPFilter, SSPGenerate
from trestle.core.commands.author.ssp import SSPFilter
from trestle.core.commands.common.return_codes import CmdReturnCodes
from trestle.core.repository import AgileAuthoring

from trestlebot.const import COMPDEF_KEY_NAME, LEVERAGED_SSP_KEY_NAME, PROFILE_KEY_NAME
from trestlebot.tasks.authored.base_authored import (
Expand Down Expand Up @@ -137,11 +137,6 @@ def write_out(self) -> None:
json.dump(data, file, indent=4)


# TODO: Move away from using private run to a public function.
# Done initially because a lot of required high level logic for SSP is private.
# See - https://github.com/IBM/compliance-trestle/pull/1432


class AuthoredSSP(AuthorObjectBase):
"""
Class for authoring OSCAL SSPs in automation
Expand All @@ -156,26 +151,24 @@ def __init__(self, trestle_root: str, ssp_index: SSPIndex) -> None:

def assemble(self, markdown_path: str, version_tag: str = "") -> None:
"""Run assemble actions for ssp type at the provided path"""
ssp_assemble: SSPAssemble = SSPAssemble()
ssp = os.path.basename(markdown_path)

comps = self.ssp_index.get_comps_by_ssp(ssp)
component_str = ",".join(comps)

trestle_root = pathlib.Path(self.get_trestle_root())
authoring = AgileAuthoring(trestle_root)

try:
args = argparse.Namespace(
trestle_root=self.get_trestle_root(),
markdown=markdown_path,
success = authoring.assemble_ssp_markdown(
name=ssp,
output=ssp,
verbose=0,
markdown_dir=markdown_path,
regenerate=False,
version=version_tag,
name=None,
compdefs=component_str,
)

exit_code = ssp_assemble._run(args)
if exit_code != CmdReturnCodes.SUCCESS.value:
if not success:
raise AuthoredObjectException(
f"Unknown error occurred while assembling {ssp}"
)
Expand All @@ -184,30 +177,31 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None:

def regenerate(self, model_path: str, markdown_path: str) -> None:
"""Run regenerate actions for ssp type at the provided path"""
trestle_root = self.get_trestle_root()
trestle_path = pathlib.Path(trestle_root)
ssp_generate: SSPGenerate = SSPGenerate()

ssp = os.path.basename(model_path)
comps = self.ssp_index.get_comps_by_ssp(ssp)
component_str = ",".join(comps)

profile = self.ssp_index.get_profile_by_ssp(ssp)

leveraged_ssp = self.ssp_index.get_leveraged_by_ssp(ssp)
if leveraged_ssp is None:
leveraged_ssp = ""

trestle_root = pathlib.Path(self.get_trestle_root())
authoring = AgileAuthoring(trestle_root)

try:
exit_code = ssp_generate._generate_ssp_markdown(
trestle_root=trestle_path,
profile_name_or_href=profile,
compdef_name_list=comps,
md_path=pathlib.Path(trestle_root, markdown_path, ssp),
yaml_header={},
overwrite_header_values=False,
success = authoring.generate_ssp_markdown(
output=os.path.join(markdown_path, ssp),
force_overwrite=False,
leveraged_ssp_name_or_href=leveraged_ssp,
yaml_header="",
overwrite_header_values=False,
compdefs=component_str,
profile=profile,
leveraged_ssp=leveraged_ssp,
)
if exit_code != CmdReturnCodes.SUCCESS.value:
if not success:
raise AuthoredObjectException(
f"Unknown error occurred while regenerating {ssp}"
)
Expand Down

0 comments on commit eed25ed

Please sign in to comment.