Skip to content

Commit

Permalink
chore: remove magic number error codes and replace with constants
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jul 12, 2023
1 parent 2ffd784 commit 1d43f25
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
16 changes: 8 additions & 8 deletions trestlebot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions trestlebot/const.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions trestlebot/tasks/assemble_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
Expand All @@ -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
10 changes: 8 additions & 2 deletions trestlebot/tasks/regenerate_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
Expand All @@ -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

0 comments on commit 1d43f25

Please sign in to comment.