Skip to content

Commit

Permalink
feat: initialize command for cac to oscal transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
qduanmu committed Dec 20, 2024
1 parent 0314389 commit 6bc5073
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/trestlebot/cli/test_sync_cac_content_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2024 Red Hat, Inc.

""" Unit test for sync-cac-content command"""
import pathlib
from typing import Tuple

from click.testing import CliRunner
from git import Repo

from trestlebot.cli.commands.sync_cac_content import sync_cac_content_cmd


test_product = "ocp4"


def test_missing_required_option(tmp_repo: Tuple[str, Repo]) -> None:
"""Tests missing required options in sync-cac-content command."""

repo_dir, _ = tmp_repo
repo_path = pathlib.Path(repo_dir)

runner = CliRunner()
result = runner.invoke(
sync_cac_content_cmd,
[
"--product",
test_product,
"--repo-path",
str(repo_path.resolve()),
"--committer-email",
"test@email.com",
"--committer-name",
"test name",
"--branch",
"test",
],
)
assert result.exit_code == 2
60 changes: 60 additions & 0 deletions trestlebot/cli/commands/sync_cac_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2024 Red Hat, Inc.

"""Module for sync cac content command"""
import logging
from typing import Any

import click

from trestlebot.cli.options.common import common_options, git_options, handle_exceptions


logger = logging.getLogger(__name__)


@click.command(
name="sync-cac-content",
help="Transform CaC content to component definition in OSCAL.",
)
@click.pass_context
@common_options
@git_options
@click.option(
"--cac-content-root",
help="Root of the CaC content project.",
required=True,
)
@click.option(
"--product",
type=str,
help="Product to build OSCAL component definition with",
required=True,
)
@click.option(
"--cac-profile",
type=str,
help="CaC profile used to collect product data for transformation",
required=True,
)
@click.option(
"--oscal-profile",
type=str,
help="Main profile href, or name of the profile in trestle workspace",
required=True,
)
@click.option(
"--component-definition-type",
type=click.Choice(["service", "validation"]),
help="Type of component definition. Default: service",
required=False,
default="service",
)
@handle_exceptions
def sync_cac_content_cmd(ctx: click.Context, **kwargs: Any) -> None:
"""Transform CaC content to OSCAL component definition."""

# Steps:
# 1. Check options, logger errors if any and exit.
# 2. Create a new task to run the data transformation.
# 3. Initialize a Trestlebot object and run the task(s).
2 changes: 2 additions & 0 deletions trestlebot/cli/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from trestlebot.cli.commands.create import create_cmd
from trestlebot.cli.commands.init import init_cmd
from trestlebot.cli.commands.rule_transform import rule_transform_cmd
from trestlebot.cli.commands.sync_cac_content import sync_cac_content_cmd
from trestlebot.cli.commands.sync_upstreams import sync_upstreams_cmd


Expand All @@ -33,4 +34,5 @@ def root_cmd(ctx: click.Context) -> None:
root_cmd.add_command(autosync_cmd)
root_cmd.add_command(create_cmd)
root_cmd.add_command(rule_transform_cmd)
root_cmd.add_command(sync_cac_content_cmd)
root_cmd.add_command(sync_upstreams_cmd)

0 comments on commit 6bc5073

Please sign in to comment.