diff options
Diffstat (limited to 'rh/results.py')
-rw-r--r-- | rh/results.py | 51 |
1 files changed, 42 insertions, 9 deletions
diff --git a/rh/results.py b/rh/results.py index a7a4b49..65e0052 100644 --- a/rh/results.py +++ b/rh/results.py @@ -16,6 +16,7 @@ import os import sys +from typing import List, NamedTuple, Optional _path = os.path.realpath(__file__ + '/../..') if sys.path[0] != _path: @@ -26,7 +27,8 @@ del _path class HookResult(object): """A single hook result.""" - def __init__(self, hook, project, commit, error, files=(), fixup_func=None): + def __init__(self, hook, project, commit, error, files=(), + fixup_cmd: Optional[List[str]] = None): """Initialize. Args: @@ -36,22 +38,23 @@ class HookResult(object): error: A string representation of the hook's result. Empty on success. files: The list of files that were involved in the hook execution. - fixup_func: A callable that will attempt to automatically fix errors - found in the hook's execution. Returns an non-empty string if - this, too, fails. Can be None if the hook does not support - automatically fixing errors. + fixup_cmd: A command that can automatically fix errors found in the + hook's execution. Can be None if the hook does not support + automatic fixups. """ self.hook = hook self.project = project self.commit = commit self.error = error self.files = files - self.fixup_func = fixup_func + self.fixup_cmd = fixup_cmd def __bool__(self): + """Whether this result is an error.""" return bool(self.error) def is_warning(self): + """Whether this result is a non-fatal warning.""" return False @@ -59,14 +62,44 @@ class HookCommandResult(HookResult): """A single hook result based on a CompletedProcess.""" def __init__(self, hook, project, commit, result, files=(), - fixup_func=None): + fixup_cmd=None): HookResult.__init__(self, hook, project, commit, result.stderr if result.stderr else result.stdout, - files=files, fixup_func=fixup_func) + files=files, fixup_cmd=fixup_cmd) self.result = result def __bool__(self): - return self.result.returncode not in (None, 0) + """Whether this result is an error.""" + return self.result.returncode not in (None, 0, 77) def is_warning(self): + """Whether this result is a non-fatal warning.""" return self.result.returncode == 77 + + +class ProjectResults(NamedTuple): + """All results for a single project.""" + + project: str + workdir: str + + # All the results from running all the hooks. + results: List[HookResult] = [] + + # Whether there were any non-hook related errors. For example, trying to + # parse the project configuration. + internal_failure: bool = False + + def add_results(self, results: Optional[List[HookResult]]) -> None: + """Add |results| to our results.""" + if results: + self.results.extend(results) + + @property + def fixups(self): + """Yield results that have a fixup available.""" + yield from (x for x in self.results if x and x.fixup_cmd) + + def __bool__(self): + """Whether there are any errors in this set of results.""" + return self.internal_failure or any(self.results) |