|
30 | 30 | # Copyright (C) 2012-2016 Ian Cordasco <graffatcolmingov@gmail.com>
|
31 | 31 | # MIT Licensed
|
32 | 32 | #
|
| 33 | +# "GitHubFormatter" based on https://gitlab.com/pycqa/flake8-json |
| 34 | +# Copyright (C) 2017-2018 Ian Stapleton Cordasco <graffatcolmingov@gmail.com> |
| 35 | +# MIT Licensed |
| 36 | +# |
33 | 37 |
|
34 | 38 | # stdlib
|
35 |
| -import json |
36 | 39 | from functools import partial
|
37 | 40 | from gettext import ngettext
|
38 |
| -from io import StringIO |
39 |
| -from typing import List, Optional, Tuple, Type |
| 41 | +from typing import Optional, Tuple, Type |
40 | 42 |
|
41 | 43 | # 3rd party
|
42 | 44 | import click
|
43 | 45 | import flake8.main.application # type: ignore
|
44 | 46 | from flake8.formatting.base import BaseFormatter # type: ignore
|
45 |
| -from flake8_json_reporter.reporters import DefaultJSON # type: ignore |
46 | 47 | from typing_extensions import NoReturn
|
47 | 48 |
|
48 |
| -# this package |
49 |
| -from flake8_github_action.annotation import Annotation |
50 |
| - |
51 |
| -__all__ = ["Application", "JsonFormatter"] |
| 49 | +__all__ = ["Application", "GitHubFormatter"] |
52 | 50 |
|
53 | 51 | _error = partial(ngettext, "error", "errors")
|
54 | 52 | _file = partial(ngettext, "file", "files")
|
@@ -125,31 +123,47 @@ def make_formatter(self, formatter_class: Optional[Type[BaseFormatter]] = None)
|
125 | 123 | Initialize a formatter based on the parsed options.
|
126 | 124 | """
|
127 | 125 |
|
128 |
| - self.formatter = JsonFormatter(self.options) |
| 126 | + self.formatter = GitHubFormatter(self.options) |
129 | 127 |
|
130 |
| - def report(self): |
131 |
| - """ |
132 |
| - Report errors, statistics, and benchmarks. |
133 |
| - """ |
134 | 128 |
|
135 |
| - super().report() |
| 129 | +class GitHubFormatter(BaseFormatter): |
136 | 130 |
|
137 |
| - json_annotations = json.loads(self.formatter.output_fd.getvalue()).items() |
| 131 | + def write_line(self, line): |
| 132 | + """ |
| 133 | + Override write for convenience. |
| 134 | + """ |
| 135 | + self.write(line, None) |
138 | 136 |
|
139 |
| - for filename, raw_annotations in json_annotations: |
140 |
| - if raw_annotations: |
141 |
| - print(filename) |
| 137 | + def start(self): |
| 138 | + super().start() |
| 139 | + self.files_reported_count = 0 |
142 | 140 |
|
143 |
| - for annotation in raw_annotations: |
144 |
| - print(Annotation.from_flake8json(filename, annotation).to_str()) |
| 141 | + def beginning(self, filename): |
| 142 | + """ |
| 143 | + We're starting a new file. |
| 144 | + """ |
145 | 145 |
|
| 146 | + self.reported_errors_count = 0 |
146 | 147 |
|
147 |
| -class JsonFormatter(DefaultJSON): |
| 148 | + def finished(self, filename): |
| 149 | + """ |
| 150 | + We've finished processing a file. |
| 151 | + """ |
148 | 152 |
|
149 |
| - def __init__(self, *args, **kwargs): |
150 |
| - super().__init__(*args, **kwargs) |
| 153 | + self.files_reported_count += 1 |
151 | 154 |
|
152 |
| - self.output_fd = StringIO() |
| 155 | + def format(self, violation): |
| 156 | + """ |
| 157 | + Format a violation. |
| 158 | + """ |
153 | 159 |
|
| 160 | + if self.reported_errors_count == 0: |
| 161 | + self.write_line(violation.filename) |
154 | 162 |
|
| 163 | + self.write_line( |
| 164 | + f"::warning " |
| 165 | + f"file={violation.filename},line={violation.line_number},col={violation.column_number}" |
| 166 | + f"::{violation.code}: {violation.text}" |
| 167 | + ) |
155 | 168 |
|
| 169 | + self.reported_errors_count += 1 |
0 commit comments