Skip to content

Commit fded5a8

Browse files
committed
Try using workflow commands rather than checks API.
1 parent 6484561 commit fded5a8

File tree

3 files changed

+23
-104
lines changed

3 files changed

+23
-104
lines changed

flake8_github_action/__init__.py

Lines changed: 14 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,7 @@
2828

2929
# stdlib
3030
import json
31-
from typing import List, Tuple, Union
32-
33-
# 3rd party
34-
import click
35-
import dulwich.errors
36-
from apeye import URL
37-
from domdf_python_tools.iterative import chunks
38-
from domdf_python_tools.secrets import Secret
39-
from dulwich.repo import Repo
40-
from requests import Response
31+
from typing import List
4132

4233
# this package
4334
from flake8_github_action.annotation import Annotation
@@ -54,57 +45,14 @@
5445

5546

5647
def action(
57-
token: Union[str, Secret],
58-
repo: Union[str, URL, None] = None,
5948
*args,
60-
) -> Tuple[Response, int]:
49+
) -> int:
6150
r"""
6251
Action!
6352
64-
:param token: The token to authenticate with the GitHub API.
65-
:param repo: The repository name (in the format <username>/<repository>) or the complete GitHub URL.
6653
:param \*args: flake8 command line arguments.
6754
"""
6855

69-
if not isinstance(token, Secret):
70-
token = Secret(token)
71-
72-
dulwich_repo = Repo('.')
73-
74-
if repo is None:
75-
try:
76-
config = dulwich_repo.get_config()
77-
repo = URL(config.get(("remote", "origin"), "url").decode("UTF-8"))
78-
except dulwich.errors.NotGitRepository as e:
79-
raise click.UsageError(str(e))
80-
81-
elif not isinstance(repo, URL):
82-
repo = URL(repo)
83-
84-
if repo.suffix == ".git":
85-
repo = repo.with_suffix('')
86-
87-
repo_name = repo.name
88-
89-
# first case is for full url, second for github/hello_world
90-
github_username = repo.parent.name or repo.domain.domain
91-
92-
check = Checks(
93-
owner=github_username,
94-
repository_name=repo_name,
95-
check_name="Flake8",
96-
head_sha=dulwich_repo.head().decode("UTF-8"),
97-
token=token.value,
98-
)
99-
100-
# check_run_id = check.create_check_run()
101-
check_run_id = check.find_run_for_action()
102-
check.update_check_run(
103-
check_run_id,
104-
status="in_progress",
105-
output={"title": "Flake8 checks", "summary": "Output from Flake8"},
106-
)
107-
10856
flake8_app = Application()
10957
flake8_app.run(args)
11058
flake8_app.exit()
@@ -115,34 +63,16 @@ def action(
11563
for filename, raw_annotations in json_annotations:
11664
annotations.extend(Annotation.from_flake8json(filename, ann) for ann in raw_annotations)
11765

66+
if flake8_app.result_count:
67+
ret = 1
68+
else:
69+
ret = 0
70+
11871
if annotations:
119-
# Github limits updates to 50 annotations at a time
120-
annotation_chunks = list(chunks(annotations, 50))
121-
122-
if flake8_app.result_count:
123-
conclusion = "failure"
124-
ret = 1
125-
else:
126-
conclusion = "success"
127-
ret = 0
128-
129-
for chunk in annotation_chunks[:-1]:
130-
check.update_check_run(
131-
check_run_id,
132-
conclusion=conclusion,
133-
output={
134-
"title": "Flake8 checks",
135-
"summary": "Output from Flake8",
136-
"annotations": [a.to_dict() for a in chunk],
137-
},
138-
)
139-
140-
output = {
141-
"title": "Flake8 checks",
142-
"summary": "Output from Flake8",
143-
"annotations": [a.to_dict() for a in annotation_chunks[-1]],
144-
}
145-
146-
return check.complete_check_run(check_run_id, conclusion=conclusion, output=output), ret
147-
148-
return check.complete_check_run(check_run_id, conclusion="success"), 0
72+
for annotation in annotations:
73+
print(annotation.to_str())
74+
75+
return ret
76+
77+
78+

flake8_github_action/__main__.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,6 @@
4242
token_var = "GITHUB_TOKEN"
4343

4444

45-
@click.option(
46-
"-t",
47-
"--token",
48-
type=click.STRING,
49-
help=(
50-
"The token to authenticate with the GitHub API. "
51-
f"Can also be provided via the '{token_var}' environment variable."
52-
),
53-
envvar=token_var,
54-
required=True,
55-
)
56-
@click.option(
57-
"-r",
58-
"--repo",
59-
type=click.STRING,
60-
default=None,
61-
help="The repository name (in the format <username>/<repository>) or the complete GitHub URL.",
62-
)
6345
@click.option(
6446
"--annotate-only",
6547
is_flag=True,
@@ -68,15 +50,15 @@
6850
)
6951
@click.command(context_settings={"ignore_unknown_options": True, "allow_extra_args": True, **CONTEXT_SETTINGS})
7052
@click.pass_context
71-
def main(ctx: click.Context, token: str, repo: Union[str, URL, None] = None, annotate_only: bool = False,):
53+
def main(ctx: click.Context, repo: Union[str, URL, None] = None, annotate_only: bool = False,):
7254
"""
7355
Run flake8 and add the errors as annotations on GitHub.
7456
"""
7557

7658
# this package
7759
from flake8_github_action import action
7860

79-
response, ret = action(token, repo, *ctx.args)
61+
response, ret = action(repo, *ctx.args)
8062

8163
# if response.status_code == 200:
8264
# sys.exit(0)

flake8_github_action/annotation.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,10 @@ def from_flake8json(cls, filename: str, data: Mapping[str, Any]):
108108
annotation_level="warning",
109109
message="{code}: {text}".format_map(data)
110110
)
111+
112+
def to_str(self) -> str:
113+
return (
114+
f"::{self.annotation_level} "
115+
f"file={self.path},line={self.start_line},col={self.start_column}"
116+
f"::{self.message}"
117+
)

0 commit comments

Comments
 (0)