aboutsummaryrefslogtreecommitdiff
path: root/rh/results.py
blob: 65e005231835385bd8f286473ad89968324eb39b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright 2016 The Android Open Source Project
#
# 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.

"""Common errors thrown when repo preupload checks fail."""

import os
import sys
from typing import List, NamedTuple, Optional

_path = os.path.realpath(__file__ + '/../..')
if sys.path[0] != _path:
    sys.path.insert(0, _path)
del _path


class HookResult(object):
    """A single hook result."""

    def __init__(self, hook, project, commit, error, files=(),
                 fixup_cmd: Optional[List[str]] = None):
        """Initialize.

        Args:
          hook: The name of the hook.
          project: The name of the project.
          commit: The git commit sha.
          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_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_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


class HookCommandResult(HookResult):
    """A single hook result based on a CompletedProcess."""

    def __init__(self, hook, project, commit, result, files=(),
                 fixup_cmd=None):
        HookResult.__init__(self, hook, project, commit,
                            result.stderr if result.stderr else result.stdout,
                            files=files, fixup_cmd=fixup_cmd)
        self.result = result

    def __bool__(self):
        """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)