From 1d43f25e4a95bc36a5f6505f98790cc0f6d3876f Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Tue, 11 Jul 2023 20:17:35 -0400 Subject: [PATCH] chore: remove magic number error codes and replace with constants Signed-off-by: Jennifer Power --- trestlebot/cli.py | 16 ++++++++-------- trestlebot/const.py | 21 +++++++++++++++++++++ trestlebot/tasks/assemble_task.py | 10 ++++++++-- trestlebot/tasks/regenerate_task.py | 10 ++++++++-- 4 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 trestlebot/const.py diff --git a/trestlebot/cli.py b/trestlebot/cli.py index 59f4d7ae..f019d533 100644 --- a/trestlebot/cli.py +++ b/trestlebot/cli.py @@ -23,7 +23,7 @@ import sys from typing import List, Optional -from trestlebot import bot, log +from trestlebot import bot, const, log from trestlebot.github import GitHub from trestlebot.provider import GitProvider from trestlebot.tasks.assemble_task import AssembleTask @@ -163,7 +163,7 @@ def handle_exception( """Log the exception and return the exit code""" logger.error(msg + f": {exception}", exc_info=True) - return 1 + return const.ERROR_EXIT_CODE def run() -> None: @@ -185,15 +185,15 @@ def run() -> None: f"Invalid value {args.oscal_model} for oscal model. " f"Please use catalog, profile, compdef, or ssp." ) - sys.exit(1) + sys.exit(const.ERROR_EXIT_CODE) if not args.markdown_path: logger.error("Must set markdown path with oscal model.") - sys.exit(1) + sys.exit(const.ERROR_EXIT_CODE) if args.oscal_model == "ssp" and args.ssp_index_path == "": logger.error("Must set ssp_index_path when using SSP as oscal model.") - sys.exit(1) + sys.exit(const.ERROR_EXIT_CODE) # Assuming an edit has occurred assemble would be run before regenerate. # Adding this to the list first @@ -228,15 +228,15 @@ def run() -> None: "If testing locally with the GitHub API, set " "the GITHUB_ACTIONS environment variable to true." ) - sys.exit(1) + sys.exit(const.ERROR_EXIT_CODE) if not args.with_token: logger.error("with-token value cannot be empty") - sys.exit(1) + sys.exit(const.ERROR_EXIT_CODE) git_provider = GitHub(access_token=args.with_token.read().strip()) - exit_code: int = 0 + exit_code: int = const.SUCCESS_EXIT_CODE # Assume it is a successful run, if the bot # throws an exception update the exit code accordingly diff --git a/trestlebot/const.py b/trestlebot/const.py new file mode 100644 index 00000000..b471cdd7 --- /dev/null +++ b/trestlebot/const.py @@ -0,0 +1,21 @@ +#!/usr/bin/python + +# Copyright 2023 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Global constants""" + +# Common exit codes +SUCCESS_EXIT_CODE = 0 +ERROR_EXIT_CODE = 1 diff --git a/trestlebot/tasks/assemble_task.py b/trestlebot/tasks/assemble_task.py index 900c7450..dc01ebf0 100644 --- a/trestlebot/tasks/assemble_task.py +++ b/trestlebot/tasks/assemble_task.py @@ -19,6 +19,7 @@ import os from typing import List +from trestlebot import const from trestlebot.tasks.authored import types from trestlebot.tasks.authored.base_authored import ( AuthoredObjectException, @@ -62,7 +63,12 @@ def execute(self) -> int: return self._assemble() def _assemble(self) -> int: - """Assemble all objects in markdown directory""" + """ + Assemble all objects in markdown directory + + Returns: + 0 on success, raises an exception if not successful + """ authored_object: AuthorObjectBase = types.get_authored_object( self._authored_model, self.get_working_dir(), self._ssp_index_path ) @@ -79,4 +85,4 @@ def _assemble(self) -> int: except AuthoredObjectException as e: raise TaskException(f"Assemble task failed for model {model_path}: {e}") - return 0 + return const.SUCCESS_EXIT_CODE diff --git a/trestlebot/tasks/regenerate_task.py b/trestlebot/tasks/regenerate_task.py index 9477d05b..3e0fb272 100644 --- a/trestlebot/tasks/regenerate_task.py +++ b/trestlebot/tasks/regenerate_task.py @@ -19,6 +19,7 @@ import os from typing import List +from trestlebot import const from trestlebot.tasks.authored import types from trestlebot.tasks.authored.base_authored import ( AuthoredObjectException, @@ -62,7 +63,12 @@ def execute(self) -> int: return self._regenerate() def _regenerate(self) -> int: - """Regenerate all objects in model JSON directory""" + """ + Regenerate all objects in model JSON directory + + Returns: + 0 on success, raises an exception if not successful + """ authored_object: AuthorObjectBase = types.get_authored_object( self._authored_model, self.get_working_dir(), self._ssp_index_path ) @@ -82,4 +88,4 @@ def _regenerate(self) -> int: except AuthoredObjectException as e: raise TaskException(f"Regenerate task failed for model {model}: {e}") - return 0 + return const.SUCCESS_EXIT_CODE