codeveto 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codeveto/__init__.py +3 -0
- codeveto/__main__.py +8 -0
- codeveto/ai/__init__.py +19 -0
- codeveto/ai/harness.py +303 -0
- codeveto/ai/packets.py +855 -0
- codeveto/ai/policy.py +222 -0
- codeveto/ai/prompt.py +101 -0
- codeveto/ai/providers/__init__.py +178 -0
- codeveto/ai/providers/claude.py +247 -0
- codeveto/ai/providers/openai_compat.py +246 -0
- codeveto/ai/runner.py +293 -0
- codeveto/ai/verify.py +324 -0
- codeveto/anchor.py +211 -0
- codeveto/cli.py +247 -0
- codeveto/cli_explain.py +49 -0
- codeveto/cli_hook.py +31 -0
- codeveto/cli_init.py +254 -0
- codeveto/cli_mcp.py +29 -0
- codeveto/cli_packets.py +35 -0
- codeveto/cli_publish.py +29 -0
- codeveto/cli_review.py +362 -0
- codeveto/cli_rules.py +78 -0
- codeveto/cli_verify.py +47 -0
- codeveto/config.py +1229 -0
- codeveto/data/README.md +16 -0
- codeveto/data/ai-wire.schema.json +53 -0
- codeveto/data/prompts/system.md +57 -0
- codeveto/data/report-v1.schema.json +268 -0
- codeveto/data/templates/forgejo-codeveto.yml +104 -0
- codeveto/data/templates/github-codeveto.yml +18 -0
- codeveto/embedded.py +311 -0
- codeveto/engine.py +1570 -0
- codeveto/git.py +1549 -0
- codeveto/globs.py +348 -0
- codeveto/integrations/__init__.py +1 -0
- codeveto/integrations/hooks.py +327 -0
- codeveto/integrations/mcp.py +770 -0
- codeveto/integrations/mcp_ai.py +77 -0
- codeveto/integrations/prepush.py +222 -0
- codeveto/jsonsafe.py +60 -0
- codeveto/languages.py +539 -0
- codeveto/lexers.py +2560 -0
- codeveto/model.py +372 -0
- codeveto/publish/__init__.py +28 -0
- codeveto/publish/action.py +330 -0
- codeveto/publish/common.py +84 -0
- codeveto/publish/diff.py +208 -0
- codeveto/publish/event.py +241 -0
- codeveto/publish/forgejo.py +184 -0
- codeveto/publish/github.py +380 -0
- codeveto/publish/http.py +263 -0
- codeveto/publish/pull.py +218 -0
- codeveto/publish/untrusted.py +110 -0
- codeveto/py.typed +0 -0
- codeveto/redact.py +113 -0
- codeveto/render/__init__.py +64 -0
- codeveto/render/agent.py +43 -0
- codeveto/render/github.py +101 -0
- codeveto/render/json.py +23 -0
- codeveto/render/markdown.py +208 -0
- codeveto/render/safe.py +249 -0
- codeveto/render/sarif.py +217 -0
- codeveto/render/text.py +210 -0
- codeveto/report.py +252 -0
- codeveto/rules/__init__.py +84 -0
- codeveto/rules/agent.py +2972 -0
- codeveto/rules/base.py +1026 -0
- codeveto/rules/ci.py +1708 -0
- codeveto/rules/config.py +493 -0
- codeveto/rules/deps.py +1693 -0
- codeveto/rules/docker.py +479 -0
- codeveto/rules/examples/__init__.py +148 -0
- codeveto/rules/examples/agent.py +839 -0
- codeveto/rules/examples/ci.py +1046 -0
- codeveto/rules/examples/config.py +191 -0
- codeveto/rules/examples/deps.py +369 -0
- codeveto/rules/examples/docker.py +157 -0
- codeveto/rules/examples/go.py +241 -0
- codeveto/rules/examples/hygiene.py +466 -0
- codeveto/rules/examples/js.py +462 -0
- codeveto/rules/examples/python.py +595 -0
- codeveto/rules/examples/rust.py +268 -0
- codeveto/rules/examples/secrets.py +561 -0
- codeveto/rules/examples/security.py +608 -0
- codeveto/rules/examples/shell.py +174 -0
- codeveto/rules/examples/sql.py +194 -0
- codeveto/rules/examples/tests.py +497 -0
- codeveto/rules/go.py +328 -0
- codeveto/rules/hygiene.py +1094 -0
- codeveto/rules/js.py +768 -0
- codeveto/rules/python.py +1642 -0
- codeveto/rules/rust.py +209 -0
- codeveto/rules/secrets.py +1335 -0
- codeveto/rules/security.py +3680 -0
- codeveto/rules/shell.py +867 -0
- codeveto/rules/sql.py +629 -0
- codeveto/rules/tests.py +1404 -0
- codeveto/schema.py +384 -0
- codeveto/statements.py +607 -0
- codeveto/worktree.py +1176 -0
- codeveto/yamlish.py +1179 -0
- codeveto-0.1.0.dist-info/METADATA +253 -0
- codeveto-0.1.0.dist-info/RECORD +106 -0
- codeveto-0.1.0.dist-info/WHEEL +4 -0
- codeveto-0.1.0.dist-info/entry_points.txt +2 -0
- codeveto-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"""The trusted workflow context of a CI run: event payload, repository and API URLs.
|
|
2
|
+
|
|
3
|
+
Everything here comes from the runner (``GITHUB_*`` variables and the event
|
|
4
|
+
payload at ``GITHUB_EVENT_PATH``, which the forge writes), never from the
|
|
5
|
+
change under review. Values are still validated strictly: repository names,
|
|
6
|
+
pull-request numbers and commit IDs must have their exact shape, and the
|
|
7
|
+
event's repository must be the repository the job runs in.
|
|
8
|
+
|
|
9
|
+
Supported events (SPEC 10.4, 10.5):
|
|
10
|
+
|
|
11
|
+
- ``pull_request_target`` (GitHub and Forgejo): base ``pull_request.base.sha``,
|
|
12
|
+
head ``pull_request.head.sha``, merge-base comparison.
|
|
13
|
+
- ``merge_group`` (GitHub): ``merge_group.base_sha..head_sha``, direct.
|
|
14
|
+
- ``push`` (GitHub): ``before..after``, direct; a zero ``before`` (new
|
|
15
|
+
branch) compares with the merge base with the default branch, so the base
|
|
16
|
+
is not known from the event alone (``base`` is ``None``); ``deleted``
|
|
17
|
+
marks a branch deletion.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import re
|
|
23
|
+
from collections.abc import Mapping
|
|
24
|
+
from dataclasses import dataclass
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
from typing import Any
|
|
27
|
+
|
|
28
|
+
from codeveto import jsonsafe
|
|
29
|
+
from codeveto.publish import PublishError
|
|
30
|
+
from codeveto.publish.http import validate_base_url
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"MAX_EVENT_BYTES",
|
|
34
|
+
"Event",
|
|
35
|
+
"is_oid",
|
|
36
|
+
"read_capped",
|
|
37
|
+
"read_event",
|
|
38
|
+
"strict_json",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
MAX_EVENT_BYTES = 32 * 1024 * 1024
|
|
42
|
+
_OID = re.compile(r"[0-9a-f]{40}|[0-9a-f]{64}")
|
|
43
|
+
_REPO = re.compile(r"[A-Za-z0-9_.-]{1,100}/[A-Za-z0-9_.-]{1,100}")
|
|
44
|
+
_NUMBER = re.compile(r"[1-9][0-9]{0,17}")
|
|
45
|
+
_GITHUB_EVENTS = frozenset({"pull_request_target", "merge_group", "push"})
|
|
46
|
+
_FORGEJO_EVENTS = frozenset({"pull_request_target"})
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def is_oid(value: object) -> bool:
|
|
50
|
+
"""Is ``value`` a full lowercase SHA-1 or SHA-256 commit ID?"""
|
|
51
|
+
return isinstance(value, str) and _OID.fullmatch(value) is not None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _zero(value: str) -> bool:
|
|
55
|
+
return set(value) == {"0"}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _unique(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
|
|
59
|
+
out: dict[str, Any] = {}
|
|
60
|
+
for key, value in pairs:
|
|
61
|
+
if key in out:
|
|
62
|
+
raise ValueError(f"duplicate key {key!r}")
|
|
63
|
+
out[key] = value
|
|
64
|
+
return out
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _constant(name: str) -> Any:
|
|
68
|
+
raise ValueError(f"JSON constant {name} is not allowed")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def strict_json(raw: bytes, *, what: str) -> Any:
|
|
72
|
+
"""Parse ``raw`` as strict JSON: UTF-8, no duplicate keys, no ``NaN``/``Infinity``."""
|
|
73
|
+
try:
|
|
74
|
+
return jsonsafe.loads(
|
|
75
|
+
raw.decode("utf-8"), object_pairs_hook=_unique, parse_constant=_constant
|
|
76
|
+
)
|
|
77
|
+
except (UnicodeDecodeError, ValueError, RecursionError) as exc:
|
|
78
|
+
raise PublishError(f"{what} is not strict JSON") from exc
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def read_capped(path: str, cap: int, what: str) -> bytes:
|
|
82
|
+
try:
|
|
83
|
+
with Path(path).open("rb") as stream:
|
|
84
|
+
data = stream.read(cap + 1)
|
|
85
|
+
except OSError as exc:
|
|
86
|
+
raise PublishError(f"cannot read the {what}") from exc
|
|
87
|
+
if len(data) > cap:
|
|
88
|
+
raise PublishError(f"the {what} is larger than {cap} bytes")
|
|
89
|
+
return data
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _oid(value: object, what: str) -> str:
|
|
93
|
+
if not is_oid(value):
|
|
94
|
+
raise PublishError(f"the event's {what} is not a commit ID")
|
|
95
|
+
return str(value)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _get(data: object, *keys: str) -> Any:
|
|
99
|
+
for key in keys:
|
|
100
|
+
if not isinstance(data, Mapping):
|
|
101
|
+
return None
|
|
102
|
+
data = data.get(key)
|
|
103
|
+
return data
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _repo(value: object, what: str) -> str:
|
|
107
|
+
if (
|
|
108
|
+
not isinstance(value, str)
|
|
109
|
+
or not _REPO.fullmatch(value)
|
|
110
|
+
or any(part in {".", ".."} for part in value.split("/"))
|
|
111
|
+
):
|
|
112
|
+
raise PublishError(f"invalid {what}")
|
|
113
|
+
return value
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclass(frozen=True, slots=True)
|
|
117
|
+
class Event:
|
|
118
|
+
"""One validated CI event (see the module docstring)."""
|
|
119
|
+
|
|
120
|
+
forge: str
|
|
121
|
+
name: str
|
|
122
|
+
repo: str
|
|
123
|
+
server_url: str
|
|
124
|
+
api_url: str
|
|
125
|
+
head: str
|
|
126
|
+
base: str | None
|
|
127
|
+
comparison: str
|
|
128
|
+
number: int | None = None
|
|
129
|
+
fork: bool = False
|
|
130
|
+
deleted: bool = False
|
|
131
|
+
ref: str | None = None
|
|
132
|
+
default_branch: str | None = None
|
|
133
|
+
|
|
134
|
+
@property
|
|
135
|
+
def is_pull_request(self) -> bool:
|
|
136
|
+
return self.number is not None
|
|
137
|
+
|
|
138
|
+
@property
|
|
139
|
+
def owner(self) -> str:
|
|
140
|
+
return self.repo.split("/", 1)[0]
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def name_only(self) -> str:
|
|
144
|
+
return self.repo.split("/", 1)[1]
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def read_event(environ: Mapping[str, str], forge: str) -> Event:
|
|
148
|
+
"""The validated event of this run, or :class:`PublishError` (``unsupported event`` etc.)."""
|
|
149
|
+
name = environ.get("GITHUB_EVENT_NAME", "")
|
|
150
|
+
allowed = _GITHUB_EVENTS if forge == "github" else _FORGEJO_EVENTS
|
|
151
|
+
if name not in allowed:
|
|
152
|
+
raise PublishError(f"unsupported event {name!r}" if name else "no GITHUB_EVENT_NAME")
|
|
153
|
+
repo = _repo(environ.get("GITHUB_REPOSITORY"), "GITHUB_REPOSITORY")
|
|
154
|
+
server = validate_base_url(
|
|
155
|
+
environ.get("GITHUB_SERVER_URL") or ("https://github.com" if forge == "github" else ""),
|
|
156
|
+
what="GITHUB_SERVER_URL",
|
|
157
|
+
)
|
|
158
|
+
if forge == "github":
|
|
159
|
+
api = validate_base_url(
|
|
160
|
+
environ.get("GITHUB_API_URL") or "https://api.github.com", what="GITHUB_API_URL"
|
|
161
|
+
)
|
|
162
|
+
else:
|
|
163
|
+
api = server + "/api/v1"
|
|
164
|
+
path = environ.get("GITHUB_EVENT_PATH")
|
|
165
|
+
if not path:
|
|
166
|
+
raise PublishError("no GITHUB_EVENT_PATH")
|
|
167
|
+
payload = strict_json(read_capped(path, MAX_EVENT_BYTES, "event payload"), what="the event")
|
|
168
|
+
if not isinstance(payload, Mapping):
|
|
169
|
+
raise PublishError("the event payload is not an object")
|
|
170
|
+
full_name = _get(payload, "repository", "full_name")
|
|
171
|
+
if not isinstance(full_name, str) or full_name.casefold() != repo.casefold():
|
|
172
|
+
raise PublishError("the event's repository is not GITHUB_REPOSITORY")
|
|
173
|
+
default_branch = _get(payload, "repository", "default_branch")
|
|
174
|
+
if not isinstance(default_branch, str) or not default_branch:
|
|
175
|
+
default_branch = None
|
|
176
|
+
common = {"forge": forge, "name": name, "repo": repo, "server_url": server, "api_url": api}
|
|
177
|
+
if name == "pull_request_target":
|
|
178
|
+
pr = payload.get("pull_request")
|
|
179
|
+
number = payload.get("number", _get(pr, "number"))
|
|
180
|
+
if isinstance(number, bool) or not _NUMBER.fullmatch(str(number)):
|
|
181
|
+
raise PublishError("the event has no pull request number")
|
|
182
|
+
base_repo = _get(pr, "base", "repo", "full_name")
|
|
183
|
+
if not isinstance(base_repo, str) or base_repo.casefold() != repo.casefold():
|
|
184
|
+
raise PublishError("the pull request's base repository is not GITHUB_REPOSITORY")
|
|
185
|
+
head_repo = _get(pr, "head", "repo", "full_name")
|
|
186
|
+
fork = not isinstance(head_repo, str) or head_repo.casefold() != repo.casefold()
|
|
187
|
+
return Event(
|
|
188
|
+
**common,
|
|
189
|
+
head=_oid(_get(pr, "head", "sha"), "pull_request.head.sha"),
|
|
190
|
+
base=_oid(_get(pr, "base", "sha"), "pull_request.base.sha"),
|
|
191
|
+
comparison="merge-base",
|
|
192
|
+
number=int(number),
|
|
193
|
+
fork=fork,
|
|
194
|
+
ref=f"refs/pull/{int(number)}/head",
|
|
195
|
+
default_branch=default_branch,
|
|
196
|
+
)
|
|
197
|
+
if name == "merge_group":
|
|
198
|
+
group = payload.get("merge_group")
|
|
199
|
+
head_ref = _get(group, "head_ref")
|
|
200
|
+
return Event(
|
|
201
|
+
**common,
|
|
202
|
+
head=_oid(_get(group, "head_sha"), "merge_group.head_sha"),
|
|
203
|
+
base=_oid(_get(group, "base_sha"), "merge_group.base_sha"),
|
|
204
|
+
comparison="direct",
|
|
205
|
+
ref=head_ref if isinstance(head_ref, str) and head_ref.startswith("refs/") else None,
|
|
206
|
+
default_branch=default_branch,
|
|
207
|
+
)
|
|
208
|
+
ref = payload.get("ref")
|
|
209
|
+
if not isinstance(ref, str) or not ref.startswith("refs/"):
|
|
210
|
+
raise PublishError("the push event has no ref")
|
|
211
|
+
after = payload.get("after")
|
|
212
|
+
before = payload.get("before")
|
|
213
|
+
if payload.get("deleted") is True or (isinstance(after, str) and after and _zero(after)):
|
|
214
|
+
return Event(
|
|
215
|
+
**common,
|
|
216
|
+
head=_oid(before, "before") if is_oid(before) else "0" * 40,
|
|
217
|
+
base=None,
|
|
218
|
+
comparison="direct",
|
|
219
|
+
deleted=True,
|
|
220
|
+
ref=ref,
|
|
221
|
+
default_branch=default_branch,
|
|
222
|
+
)
|
|
223
|
+
head = _oid(after, "after")
|
|
224
|
+
before_oid = _oid(before, "before")
|
|
225
|
+
if _zero(before_oid):
|
|
226
|
+
return Event(
|
|
227
|
+
**common,
|
|
228
|
+
head=head,
|
|
229
|
+
base=None,
|
|
230
|
+
comparison="merge-base",
|
|
231
|
+
ref=ref,
|
|
232
|
+
default_branch=default_branch,
|
|
233
|
+
)
|
|
234
|
+
return Event(
|
|
235
|
+
**common,
|
|
236
|
+
head=head,
|
|
237
|
+
base=before_oid,
|
|
238
|
+
comparison="direct",
|
|
239
|
+
ref=ref,
|
|
240
|
+
default_branch=default_branch,
|
|
241
|
+
)
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"""``codeveto publish forgejo``: one commit status and one sticky comment (SPEC 10.5).
|
|
2
|
+
|
|
3
|
+
The API base is ``GITHUB_SERVER_URL`` + ``/api/v1`` (the runner's own
|
|
4
|
+
instance), the token ``GITHUB_TOKEN`` (the automatic workflow token, sent as
|
|
5
|
+
``Authorization: token``). The event must be ``pull_request_target``.
|
|
6
|
+
|
|
7
|
+
1. The report is checked as untrusted exactly as for GitHub
|
|
8
|
+
(:mod:`.untrusted`). A missing or rejected report still publishes: the
|
|
9
|
+
status is ``error`` and the comment says why.
|
|
10
|
+
2. The pull request's diff (``GET /repos/{o}/{r}/pulls/{n}.diff``) decides
|
|
11
|
+
which findings are on lines it changes (:mod:`.diff`); the rest are
|
|
12
|
+
dropped and counted.
|
|
13
|
+
3. The token's identity and the existing comments are read, and the pull
|
|
14
|
+
request is re-read: when it is closed or its head or base moved, nothing is
|
|
15
|
+
posted (a newer run will).
|
|
16
|
+
4. ``POST /repos/{o}/{r}/statuses/{head}`` with context ``codeveto / review``:
|
|
17
|
+
``success`` (clear), ``failure`` (vetoed), ``error`` (incomplete, missing or
|
|
18
|
+
rejected report). Then the sticky comment (``<!-- codeveto:summary -->``),
|
|
19
|
+
created or updated, only this identity's (``CODEVETO_COMMENT=false`` skips
|
|
20
|
+
it).
|
|
21
|
+
|
|
22
|
+
Forgejo runners may not honour every workflow command, so the text rendering
|
|
23
|
+
goes to the log with every line prefixed ``codeveto | ``. The exit code is 0
|
|
24
|
+
when everything was published (or skipped because the pull request moved),
|
|
25
|
+
2 otherwise; it never changes the review's own exit code.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import re
|
|
31
|
+
from collections.abc import Callable, Mapping
|
|
32
|
+
from typing import Any, TextIO
|
|
33
|
+
|
|
34
|
+
from codeveto.publish import PublishError
|
|
35
|
+
from codeveto.publish.common import Output
|
|
36
|
+
from codeveto.publish.diff import parse_unified, placement
|
|
37
|
+
from codeveto.publish.event import Event, read_event
|
|
38
|
+
from codeveto.publish.http import Client
|
|
39
|
+
from codeveto.publish.pull import (
|
|
40
|
+
SUMMARY_MARKER,
|
|
41
|
+
Checked,
|
|
42
|
+
bot_login,
|
|
43
|
+
failure_body,
|
|
44
|
+
identity,
|
|
45
|
+
moved,
|
|
46
|
+
read_checked_report,
|
|
47
|
+
upsert_sticky,
|
|
48
|
+
)
|
|
49
|
+
from codeveto.render import RenderOptions, markdown
|
|
50
|
+
from codeveto.render import text as text_renderer
|
|
51
|
+
|
|
52
|
+
Sleep = Callable[[float], None] | None
|
|
53
|
+
|
|
54
|
+
__all__ = ["BOT_LOGIN", "CONTEXT", "publish", "status_for"]
|
|
55
|
+
|
|
56
|
+
CONTEXT = "codeveto / review"
|
|
57
|
+
BOT_LOGIN = "forgejo-actions"
|
|
58
|
+
MAX_COMMENT_PAGES = 50
|
|
59
|
+
_RUN = re.compile(r"[1-9][0-9]{0,17}")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def status_for(checked: Checked) -> tuple[str, str]:
|
|
63
|
+
"""The commit status ``(state, description)`` for a report outcome."""
|
|
64
|
+
report = checked.report
|
|
65
|
+
if report is None:
|
|
66
|
+
if checked.missing:
|
|
67
|
+
return "error", "CodeVeto did not complete"
|
|
68
|
+
return "error", "CodeVeto report rejected"
|
|
69
|
+
verdict = report["verdict"]
|
|
70
|
+
blocking = report["summary"]["blocking"]
|
|
71
|
+
if verdict == "clear":
|
|
72
|
+
return "success", "No blocking findings"
|
|
73
|
+
if verdict == "vetoed":
|
|
74
|
+
return "failure", f"{blocking} blocking finding{'s' if blocking != 1 else ''}"
|
|
75
|
+
return "error", "Review incomplete"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def publish(
|
|
79
|
+
environ: Mapping[str, str],
|
|
80
|
+
report_path: str,
|
|
81
|
+
*,
|
|
82
|
+
sleep: Sleep = None,
|
|
83
|
+
stdout: TextIO | None = None,
|
|
84
|
+
stderr: TextIO | None = None,
|
|
85
|
+
) -> int:
|
|
86
|
+
"""Publish the report at ``report_path`` for this Forgejo Actions run."""
|
|
87
|
+
out = Output(environ, stdout=stdout, stderr=stderr)
|
|
88
|
+
try:
|
|
89
|
+
return _publish(environ, report_path, out, sleep)
|
|
90
|
+
finally:
|
|
91
|
+
out.finish()
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _log(out: Output, report: Mapping[str, Any]) -> None:
|
|
95
|
+
body = text_renderer.render(report, RenderOptions(color=False))
|
|
96
|
+
for line in body.splitlines():
|
|
97
|
+
out.stdout.write(f"codeveto | {line}\n")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _target_url(environ: Mapping[str, str], event: Event) -> str | None:
|
|
101
|
+
run = environ.get("GITHUB_RUN_NUMBER", "")
|
|
102
|
+
if not _RUN.fullmatch(run):
|
|
103
|
+
return None
|
|
104
|
+
return f"{event.server_url}/{event.repo}/actions/runs/{run}"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _publish(environ: Mapping[str, str], report_path: str, out: Output, sleep: Sleep) -> int:
|
|
108
|
+
try:
|
|
109
|
+
event = read_event(environ, "forgejo")
|
|
110
|
+
client = Client(event.api_url, environ.get("GITHUB_TOKEN", ""), auth="token", sleep=sleep)
|
|
111
|
+
except PublishError as exc:
|
|
112
|
+
out.warn(f"not published: {exc}")
|
|
113
|
+
return 2
|
|
114
|
+
comment = environ.get("CODEVETO_COMMENT", "true").strip().lower() != "false"
|
|
115
|
+
checked = read_checked_report(report_path, event, environ)
|
|
116
|
+
notes: list[str] = []
|
|
117
|
+
kept: list[Mapping[str, Any]] = []
|
|
118
|
+
if checked.report is None:
|
|
119
|
+
out.warn(checked.problem or "no report")
|
|
120
|
+
out.body_in_summary = not checked.summarized
|
|
121
|
+
body = failure_body(checked)
|
|
122
|
+
else:
|
|
123
|
+
report = checked.report
|
|
124
|
+
_log(out, report)
|
|
125
|
+
try:
|
|
126
|
+
diff = client.request(
|
|
127
|
+
"GET", f"/repos/{event.repo}/pulls/{event.number}.diff", accept="text/plain"
|
|
128
|
+
).body.decode("utf-8", "surrogateescape")
|
|
129
|
+
except PublishError as exc:
|
|
130
|
+
out.warn(f"the pull request's diff could not be read; no comment posted: {exc}")
|
|
131
|
+
comment = False
|
|
132
|
+
kept = list(report["findings"])
|
|
133
|
+
else:
|
|
134
|
+
files = parse_unified(diff)
|
|
135
|
+
kept = [f for f in report["findings"] if placement(f, files) is not None]
|
|
136
|
+
dropped = len(report["findings"]) - len(kept)
|
|
137
|
+
if dropped:
|
|
138
|
+
notes.append(
|
|
139
|
+
f"{dropped} finding(s) not on a line this pull request changes "
|
|
140
|
+
"(as Forgejo shows it) not published here; the job log lists every finding."
|
|
141
|
+
)
|
|
142
|
+
body = markdown.summary(report, findings=kept, notes=notes)
|
|
143
|
+
out.body = body
|
|
144
|
+
try:
|
|
145
|
+
login = identity(client, bot_login(environ, BOT_LOGIN)) if comment else BOT_LOGIN
|
|
146
|
+
comments: list[Any] = []
|
|
147
|
+
if comment:
|
|
148
|
+
comments, _ = client.paginate(
|
|
149
|
+
f"/repos/{event.repo}/issues/{event.number}/comments",
|
|
150
|
+
per_page=50,
|
|
151
|
+
limit_param="limit",
|
|
152
|
+
max_pages=MAX_COMMENT_PAGES,
|
|
153
|
+
)
|
|
154
|
+
if moved(client, event):
|
|
155
|
+
out.body = (
|
|
156
|
+
body.rstrip("\n") + "\n\nThe pull request moved (closed, or a new head or base) "
|
|
157
|
+
"while this run reviewed it: nothing was posted to it.\n"
|
|
158
|
+
)
|
|
159
|
+
return 0
|
|
160
|
+
except PublishError as exc:
|
|
161
|
+
out.warn(f"the pull request could not be read; nothing was posted: {exc}")
|
|
162
|
+
return 2
|
|
163
|
+
state, description = status_for(checked)
|
|
164
|
+
status: dict[str, Any] = {"state": state, "context": CONTEXT, "description": description}
|
|
165
|
+
target = _target_url(environ, event)
|
|
166
|
+
if target:
|
|
167
|
+
status["target_url"] = target
|
|
168
|
+
try:
|
|
169
|
+
client.json("POST", f"/repos/{event.repo}/statuses/{event.head}", body=status)
|
|
170
|
+
except PublishError as exc:
|
|
171
|
+
out.warn(f"commit status not posted: {exc}")
|
|
172
|
+
if comment:
|
|
173
|
+
try:
|
|
174
|
+
upsert_sticky(
|
|
175
|
+
client,
|
|
176
|
+
repo=event.repo,
|
|
177
|
+
number=event.number or 0,
|
|
178
|
+
login=login,
|
|
179
|
+
comments=comments,
|
|
180
|
+
body=f"{SUMMARY_MARKER}\n{body}",
|
|
181
|
+
)
|
|
182
|
+
except PublishError as exc:
|
|
183
|
+
out.warn(f"summary comment not posted: {exc}")
|
|
184
|
+
return 2 if out.warnings else 0
|