Skip to content

Commit

Permalink
feat: removes provider from init entrypoint
Browse files Browse the repository at this point in the history
To simplfy this feature, the provider workflows
will be available in the repo under TEMPLATES to copy
manually.

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Sep 12, 2024
1 parent 67f6283 commit b52fdb5
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 178 deletions.
8 changes: 8 additions & 0 deletions TEMPLATES/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Templates


This directory contains workflow templates using `trestle-bot` to facilitate an editing workflow for different OSCAL models and integration with CI/CD providers.

`trestle-bot` provides a ready-made integrations for GitLab CI/CD and GitHub Actions though it can be used in multiple contexts using additional flags.

> Adding GitLab CI/CD workflows is on the ROADMAP
126 changes: 1 addition & 125 deletions tests/trestlebot/entrypoints/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

import pytest
from trestle.common.const import TRESTLE_CONFIG_DIR, TRESTLE_KEEP_FILE
from trestle.common.file_utils import is_hidden

from tests.testutils import args_dict_to_list, configure_test_logger, setup_for_init
from trestlebot.const import GITHUB, GITLAB, TRESTLEBOT_CONFIG_DIR, TRESTLEBOT_KEEP_FILE
from trestlebot.const import TRESTLEBOT_CONFIG_DIR
from trestlebot.entrypoints.init import InitEntrypoint
from trestlebot.entrypoints.init import main as cli_main
from trestlebot.tasks.authored import types as model_types
Expand All @@ -27,7 +26,6 @@
def args_dict() -> Dict[str, str]:
return {
"working-dir": ".",
"provider": GITHUB,
"oscal-model": OSCAL_MODEL_COMPDEF,
}

Expand Down Expand Up @@ -81,128 +79,6 @@ def test_init_if_not_git_repo(
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_init_ssp_github(
tmp_init_dir: str, args_dict: Dict[str, str], caplog: pytest.LogCaptureFixture
) -> None:
"""Tests for expected init command directories and files"""
args_dict["working-dir"] = tmp_init_dir
args_dict["oscal-model"] = OSCAL_MODEL_SSP
args_dict["provider"] = GITHUB
setup_for_init(pathlib.Path(tmp_init_dir))
with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(SystemExit, match="0"):
cli_main()

# .keep file should exist in .trestlebot repo
tmp_dir = pathlib.Path(tmp_init_dir)
trestlebot_dir = tmp_dir / pathlib.Path(TRESTLEBOT_CONFIG_DIR)
keep_file = trestlebot_dir / pathlib.Path(TRESTLEBOT_KEEP_FILE)
assert keep_file.exists() is True

# directories for ssp model should exist
model_dirs = [d.name for d in tmp_dir.iterdir() if not is_hidden(d)]
expected = InitEntrypoint.MODEL_DIRS[args_dict["oscal-model"]] + [
InitEntrypoint.MARKDOWN_DIR
]
assert sorted(model_dirs) == sorted(expected)

# directories for github workflows should exist
workflow_dir = tmp_dir.joinpath(".github/workflows")
workflow_files = [f.name for f in workflow_dir.iterdir()]
expected = InitEntrypoint.PROVIDER_TEMPLATES[args_dict["provider"]][
args_dict["oscal-model"]
]
assert sorted(workflow_files) == sorted(expected)

# markdown directories should exist
markdown_dir = tmp_dir.joinpath(InitEntrypoint.MARKDOWN_DIR)
expected_subdirs = InitEntrypoint.MODEL_DIRS[args_dict["oscal-model"]]
markdown_subdirs = [f.name for f in markdown_dir.iterdir()]
assert sorted(markdown_subdirs) == sorted(expected_subdirs)

assert any(
record.levelno == logging.INFO
and f"Initialized trestlebot project successfully in {tmp_init_dir}"
in record.message
for record in caplog.records
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_init_ssp_gitlab(
tmp_init_dir: str, args_dict: Dict[str, str], caplog: pytest.LogCaptureFixture
) -> None:
"""Tests for expected logging message"""
args_dict["working-dir"] = tmp_init_dir
args_dict["oscal-model"] = OSCAL_MODEL_SSP
args_dict["provider"] = GITLAB
setup_for_init(pathlib.Path(tmp_init_dir))
with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(SystemExit, match="0"):
cli_main()

assert any(
record.levelno == logging.WARNING
and "GitLab is not yet supported, no provider files will be created."
in record.message
for record in caplog.records
)


@patch(
"trestlebot.entrypoints.log.configure_logger",
Mock(side_effect=configure_test_logger),
)
def test_init_compdef_github(
tmp_init_dir: str, args_dict: Dict[str, str], caplog: pytest.LogCaptureFixture
) -> None:
"""Tests for expected init command directories and files"""
args_dict["working-dir"] = tmp_init_dir
args_dict["oscal-model"] = model_types.AuthoredType.COMPDEF.value
args_dict["provider"] = GITHUB
setup_for_init(pathlib.Path(tmp_init_dir))

with patch("sys.argv", ["trestlebot", *args_dict_to_list(args_dict)]):
with pytest.raises(SystemExit, match="0"):
cli_main()

# directories for compdef model should exist
tmp_dir = pathlib.Path(tmp_init_dir)
model_dirs = [d.name for d in tmp_dir.iterdir() if not is_hidden(d)]
expected = InitEntrypoint.MODEL_DIRS[args_dict["oscal-model"]] + [
InitEntrypoint.MARKDOWN_DIR
]
assert sorted(model_dirs) == sorted(expected)

# directories for github workflows should exist
workflow_dir = tmp_dir.joinpath(".github/workflows")
workflow_files = [f.name for f in workflow_dir.iterdir()]
expected = InitEntrypoint.PROVIDER_TEMPLATES[args_dict["provider"]][
args_dict["oscal-model"]
]
assert sorted(workflow_files) == sorted(expected)

# markdown directories should exist
markdown_dir = tmp_dir.joinpath(InitEntrypoint.MARKDOWN_DIR)
expected_subdirs = InitEntrypoint.MODEL_DIRS[args_dict["oscal-model"]]
markdown_subdirs = [f.name for f in markdown_dir.iterdir()]
assert sorted(markdown_subdirs) == sorted(expected_subdirs)

assert any(
record.levelno == logging.INFO
and f"Initialized trestlebot project successfully in {tmp_init_dir}"
in record.message
for record in caplog.records
)


def test_call_trestle_init(tmp_init_dir: str) -> None:
"""Tests for expected results of calling trestle init"""
parser = argparse.ArgumentParser()
Expand Down
1 change: 0 additions & 1 deletion trestlebot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
GITHUB = "github"
GITLAB = "gitlab"
GITHUB_SERVER_URL = "https://github.com"
GITHUB_WORKFLOWS_DIR = ".github/workflows"

# Trestlebot init constants
TRESTLEBOT_CONFIG_DIR = ".trestlebot"
Expand Down
52 changes: 0 additions & 52 deletions trestlebot/entrypoints/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
import argparse
import logging
import pathlib
import shutil
import sys
import traceback

import importlib_resources
from trestle.common import file_utils
from trestle.core.commands.common.return_codes import CmdReturnCodes
from trestle.core.commands.init import InitCmd

from trestlebot.const import (
ERROR_EXIT_CODE,
GITHUB,
GITHUB_WORKFLOWS_DIR,
GITLAB,
SUCCESS_EXIT_CODE,
TRESTLEBOT_CONFIG_DIR,
TRESTLEBOT_KEEP_FILE,
Expand All @@ -50,26 +45,10 @@ class InitEntrypoint:
"""Entrypoint for the init command."""

MARKDOWN_DIR = "markdown"
TEMPLATES_MODULE = "trestlebot.entrypoints.templates"
SUPPORTED_PROVIDERS = [GITHUB, GITLAB]
SUPPORTED_MODELS = [
OSCAL_MODEL_SSP,
OSCAL_MODEL_COMPDEF,
]
PROVIDER_TEMPLATES = {
GITHUB: {
OSCAL_MODEL_SSP: [
"trestlebot-autosync-catalog.yml",
"trestlebot-autosync-profile.yml",
],
OSCAL_MODEL_COMPDEF: [
"trestlebot-create-component-definition.yml",
"trestlebot-autosync-catalog.yml",
"trestlebot-autosync-profile.yml",
"trestlebot-rules-transform.yml",
],
}
}
MODEL_DIRS = {
OSCAL_MODEL_SSP: [
"system-security-plans",
Expand Down Expand Up @@ -105,14 +84,6 @@ def setup_init_arguments(self) -> None:
default=".",
help="Working directory wit git repository",
)
self.parser.add_argument(
"--provider",
required=False,
type=str,
choices=self.SUPPORTED_PROVIDERS,
default="github",
help="Name of CI/CD provider",
)
self.parser.add_argument(
"--oscal-model",
required=True,
Expand Down Expand Up @@ -159,28 +130,6 @@ def _create_directories(self, args: argparse.Namespace) -> None:
keep_file = directory.joinpath(pathlib.Path(TRESTLEBOT_KEEP_FILE))
file_utils.make_hidden_file(keep_file)

def _copy_provider_files(self, args: argparse.Namespace) -> None:
"""Copy the CICD provider files to the new trestlebot workspace"""
if args.provider == GITLAB:
logger.warning(
"GitLab is not yet supported, no provider files will be created."
)
return

if args.provider == GITHUB:
provider_dir = pathlib.Path(args.working_dir).joinpath(
pathlib.Path(GITHUB_WORKFLOWS_DIR)
)
provider_dir.mkdir(parents=True, exist_ok=True)

templates_dir = importlib_resources.files(
f"{self.TEMPLATES_MODULE}.{args.provider}"
)
for template_file in self.PROVIDER_TEMPLATES[args.provider][args.oscal_model]:
template_path = templates_dir.joinpath(template_file)
dest_path = provider_dir.joinpath(pathlib.Path(template_file))
shutil.copyfile(str(template_path), dest_path)

def run(self, args: argparse.Namespace) -> None:
"""Run the init entrypoint"""
exit_code: int = SUCCESS_EXIT_CODE
Expand Down Expand Up @@ -213,7 +162,6 @@ def run(self, args: argparse.Namespace) -> None:

self._create_directories(args)
self._call_trestle_init(args)
self._copy_provider_files(args)

except Exception as e:
traceback_str = traceback.format_exc()
Expand Down

0 comments on commit b52fdb5

Please sign in to comment.