From b9dadc2a56b5ca423c0b2f8af95b3c562131bbf2 Mon Sep 17 00:00:00 2001 From: SubaruArai <78188579+SubaruArai@users.noreply.github.com> Date: Wed, 16 Aug 2023 04:21:46 +0900 Subject: [PATCH] restore CI output group folding in github actions (#38167) * restore CI output group folding in github actions See official docs for details: https://docs.github.com/en/actions/using-workflows/ workflow-commands-for-github-actions#grouping-log-lines * CI fold for both GHA and travis * bugfixes * ci: fix fold: add newline before fold start * remove travis from ci folding * allow multi-level inheritance for ci fold classes --- test/fold_block.py | 71 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/test/fold_block.py b/test/fold_block.py index 7cd0abd7e93ad..c6e11d1d4ffe7 100644 --- a/test/fold_block.py +++ b/test/fold_block.py @@ -1,30 +1,65 @@ +import itertools import os +import typing -next_block_id = 1 - -class Fold(object): +class BaseCiCfg: + _block_id_gen = itertools.count(1) def __init__(self): - global next_block_id - self.block_id = next_block_id - next_block_id += 1 - - def get_message(self, msg=''): - if os.environ.get('TRAVIS') == 'true': - if msg: - msg += ', ' - msg += "see folded block '%s' above for details" % self.get_block_name() + self.block_id = next(self._block_id_gen) + + @classmethod + def is_ci(cls) -> bool: + raise NotImplementedError("Use BaseCiCfg only as fallback") + + def get_message(self, msg: str = "") -> str: + return msg + + def _get_message_folded(self, msg: str = "") -> str: + if msg: + msg += ", " + msg += "see folded block '%s' above for details" % self.get_block_name() return msg - def get_block_name(self): - return 'block%d' % self.block_id + def get_block_name(self) -> str: + return "block%d" % self.block_id + + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + pass + + +class GithubActionsCiCfg(BaseCiCfg): + @classmethod + def is_ci(cls) -> bool: + return os.environ.get("GITHUB_ACTIONS") == "true" + + def get_message(self, msg=""): + return self._get_message_folded(msg) def __enter__(self): - if os.environ.get('TRAVIS') == 'true': - print('travis_fold:start:%s' % self.get_block_name()) + print("\n::group::%s" % self.get_block_name()) return self def __exit__(self, type, value, traceback): - if os.environ.get('TRAVIS') == 'true': - print('travis_fold:end:%s' % self.get_block_name()) + print("\n::endgroup::") + + +# determine CI system, and set as Fold +def _determine_ci_system() -> typing.Type[BaseCiCfg]: + def visitor(cls: typing.Type[BaseCiCfg]) -> typing.Optional[typing.Type[BaseCiCfg]]: + for sub in cls.__subclasses__(): + if sub.is_ci(): + return sub + res = visitor(sub) + if res: + return res + return None + + return visitor(BaseCiCfg) or BaseCiCfg + + +Fold = _determine_ci_system()