diff --git a/requirements.txt b/requirements.txt index 231fb55..d9be547 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -requests==2.31.0 +requests==2.32.3 boto3==1.34.102 stopit==1.1.2 psycopg2==2.9.9 diff --git a/src/build.py b/src/build.py index 207110e..0e1d51a 100644 --- a/src/build.py +++ b/src/build.py @@ -25,11 +25,10 @@ from steps import ( build_hugo, build_jekyll, build_static, download_hugo, - fetch_repo, publish, run_build_script, fetch_commit_sha, + fetch_repo, publish, run_build_script, run_step, fetch_commit_sha, setup_bundler, setup_node, setup_ruby, StepException, update_repo ) - TIMEOUT_SECONDS = 45 * 60 # 45 minutes GENERATORS = ['hugo', 'jekyll', 'node.js', 'static'] @@ -97,13 +96,6 @@ def build( # partially apply the callback url to post_metrics post_metrics_p = partial(post_metrics, status_callback) - def run_step(step, msg, *args, **kwargs): - try: - step(*args, **kwargs) - except Exception as e: - logger.error(e) - raise StepException(msg) - logger.info(f'Running build for {owner}/{repository}/{branch}') if generator not in GENERATORS: diff --git a/src/steps/__init__.py b/src/steps/__init__.py index 3a6c8c3..967befd 100644 --- a/src/steps/__init__.py +++ b/src/steps/__init__.py @@ -1,12 +1,13 @@ from .build import ( build_hugo, build_jekyll, build_static, - download_hugo, run_build_script, setup_bundler, + download_hugo, run_build_script, run_step, setup_bundler, setup_node, setup_ruby, ) from .exceptions import StepException from .fetch import fetch_repo, update_repo, fetch_commit_sha from .publish import publish + __all__ = [ 'build_hugo', 'build_jekyll', @@ -15,6 +16,7 @@ 'fetch_repo', 'publish', 'run_build_script', + 'run_step', 'setup_bundler', 'setup_node', 'setup_ruby', diff --git a/src/steps/build.py b/src/steps/build.py index 93a96b1..ac9d9a3 100644 --- a/src/steps/build.py +++ b/src/steps/build.py @@ -14,6 +14,7 @@ from log_utils import get_logger from runner import run, setuser from .cache import CacheFolder +from .exceptions import StepException HUGO_BIN = 'hugo' HUGO_VERSION = '.hugo-version' @@ -222,6 +223,13 @@ def run_build_script(branch, owner, repository, site_prefix, return +def run_step(step, msg, *args, **kwargs): + try: + step(*args, **kwargs) + except Exception: + raise StepException(msg) + + def download_hugo(post_metrics): logger = get_logger('download-hugo') diff --git a/test/test_build.py b/test/test_build.py index 5f2b8a0..8e3a895 100644 --- a/test/test_build.py +++ b/test/test_build.py @@ -1,3 +1,4 @@ +from pytest import raises import json import os from io import StringIO @@ -12,7 +13,7 @@ import steps from steps import ( build_hugo, build_jekyll, build_static, download_hugo, - run_build_script, setup_bundler, setup_node, setup_ruby + run_build_script, run_step, setup_bundler, setup_node, setup_ruby, StepException ) from steps.build import ( build_env, check_supported_ruby_version, BUNDLER_VERSION, GEMFILE, @@ -214,6 +215,30 @@ def test_it_does_not_run_otherwise(self, mock_get_logger, mock_run): mock_run.assert_not_called() +class TestRunStep(): + def test_it_should_raise_an_exception_from_step(self): + msg = 'testing-msg' + arg1 = 'arg1' + kwarg1 = 'kwarg1' + mock_step = Mock(side_effect=KeyError) + + with raises(StepException): + run_step(mock_step, msg, arg1, kwarg1=kwarg1) + + mock_step.assert_called_once_with(arg1, kwarg1=kwarg1) + + def test_it_should_run_step_successfully(self): + msg = 'testing-msg' + arg1 = 'arg1' + kwarg1 = 'kwarg1' + mock_step = Mock() + mock_step.return_value() + + run_step(mock_step, msg, arg1, kwarg1=kwarg1) + + mock_step.assert_called_once_with(arg1, kwarg1=kwarg1) + + @patch('steps.build.run') @patch('steps.build.get_logger') @patch('steps.build.check_supported_ruby_version')