second-source 0.5.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.
- second_source/__init__.py +3 -0
- second_source/action/__init__.py +4 -0
- second_source/action/commands.py +88 -0
- second_source/action/event.py +89 -0
- second_source/action/inputs.py +118 -0
- second_source/action/main.py +203 -0
- second_source/action/publish.py +184 -0
- second_source/action/setup.py +143 -0
- second_source/action/sweep.py +85 -0
- second_source/action/wait.py +49 -0
- second_source/benchmark/__init__.py +1 -0
- second_source/benchmark/aidev.py +248 -0
- second_source/benchmark/aidev_local.py +85 -0
- second_source/benchmark/golden.py +314 -0
- second_source/benchmark/redact.py +45 -0
- second_source/benchmark/rescore.py +388 -0
- second_source/benchmark/runner.py +159 -0
- second_source/benchmark/sample.py +243 -0
- second_source/benchmark/stats.py +363 -0
- second_source/cli/__init__.py +1 -0
- second_source/cli/bench.py +427 -0
- second_source/cli/main.py +182 -0
- second_source/core/__init__.py +4 -0
- second_source/core/aggregate.py +102 -0
- second_source/core/checks.py +470 -0
- second_source/core/engine.py +98 -0
- second_source/core/evidence.py +29 -0
- second_source/core/expr.py +206 -0
- second_source/core/hashing.py +41 -0
- second_source/core/model.py +112 -0
- second_source/core/pack.py +156 -0
- second_source/packs/__init__.py +1 -0
- second_source/packs/coding/__init__.py +16 -0
- second_source/packs/coding/checks/ci_config_caution.yaml +23 -0
- second_source/packs/coding/checks/ci_result.yaml +133 -0
- second_source/packs/coding/checks/ci_weakening_caution.yaml +27 -0
- second_source/packs/coding/checks/deployed.yaml +36 -0
- second_source/packs/coding/checks/docs_updated.yaml +22 -0
- second_source/packs/coding/checks/fixes_issue.yaml +58 -0
- second_source/packs/coding/checks/fixes_issue_durable.yaml +60 -0
- second_source/packs/coding/checks/integrity_guard.yaml +34 -0
- second_source/packs/coding/checks/integrity_inspection.yaml +21 -0
- second_source/packs/coding/checks/lint_caution.yaml +22 -0
- second_source/packs/coding/checks/matching_jobs_caution.yaml +22 -0
- second_source/packs/coding/checks/not_reverted.yaml +38 -0
- second_source/packs/coding/checks/tests_added.yaml +25 -0
- second_source/packs/coding/checks/tests_added_guard.yaml +26 -0
- second_source/packs/coding/checks/tests_removed_caution.yaml +23 -0
- second_source/packs/coding/detection.py +94 -0
- second_source/packs/coding/diff_facts.py +665 -0
- second_source/packs/coding/extract.py +911 -0
- second_source/packs/coding/functions.py +241 -0
- second_source/packs/coding/pack.yaml +33 -0
- second_source/packs/coding/trusted_rules.yaml +50 -0
- second_source/pipeline.py +112 -0
- second_source/report/__init__.py +1 -0
- second_source/report/render.py +503 -0
- second_source/sources/__init__.py +1 -0
- second_source/sources/github/__init__.py +4 -0
- second_source/sources/github/auth.py +48 -0
- second_source/sources/github/cache.py +67 -0
- second_source/sources/github/client.py +261 -0
- second_source/sources/github/mappers.py +454 -0
- second_source/sources/github/source.py +385 -0
- second_source/sources/github/urls.py +51 -0
- second_source-0.5.0.dist-info/METADATA +311 -0
- second_source-0.5.0.dist-info/RECORD +71 -0
- second_source-0.5.0.dist-info/WHEEL +4 -0
- second_source-0.5.0.dist-info/entry_points.txt +2 -0
- second_source-0.5.0.dist-info/licenses/LICENSE +202 -0
- second_source-0.5.0.dist-info/licenses/NOTICE +9 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
"""Pure functions that turn GitHub API JSON into Second Source objects.
|
|
2
|
+
|
|
3
|
+
No network and no clock: given the same JSON they always return the same result, so they
|
|
4
|
+
are tested exhaustively with fixtures. The shapes produced here are the coding pack's evidence
|
|
5
|
+
contract (docs/packs/coding.md); keep the two in sync.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
from collections.abc import Iterable, Sequence
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from second_source.core.model import Episode, Evidence
|
|
16
|
+
from second_source.packs.coding import diff_facts
|
|
17
|
+
from second_source.packs.coding.detection import PullRequestSignals
|
|
18
|
+
from second_source.packs.coding.extract import TextSource
|
|
19
|
+
|
|
20
|
+
Json = dict[str, Any]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def parse_time(value: str | None) -> datetime | None:
|
|
24
|
+
"""GitHub timestamps look like ``2026-11-20T09:00:00Z``; return an aware datetime."""
|
|
25
|
+
if not value:
|
|
26
|
+
return None
|
|
27
|
+
return datetime.fromisoformat(value.replace("Z", "+00:00"))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _required_time(value: str | None, field: str) -> datetime:
|
|
31
|
+
parsed = parse_time(value)
|
|
32
|
+
if parsed is None:
|
|
33
|
+
raise ValueError(f"GitHub response is missing {field}")
|
|
34
|
+
return parsed
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _without_none(values: dict[str, Any]) -> dict[str, Any]:
|
|
38
|
+
"""Drop missing values: the contract expresses "no value" as an absent key."""
|
|
39
|
+
return {key: value for key, value in values.items() if value is not None}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# ---- the pull request itself -----------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def episode_from_pull(pull: Json, agent: str) -> Episode:
|
|
46
|
+
repo = pull["base"]["repo"]["full_name"]
|
|
47
|
+
return Episode(
|
|
48
|
+
id=f"{repo.lower()}#{pull['number']}",
|
|
49
|
+
kind="pull_request",
|
|
50
|
+
agent=agent,
|
|
51
|
+
opened_at=_required_time(pull.get("created_at"), "created_at"),
|
|
52
|
+
closed_at=parse_time(pull.get("closed_at")),
|
|
53
|
+
attributes=_without_none(
|
|
54
|
+
{
|
|
55
|
+
"repo": repo.lower(),
|
|
56
|
+
"number": pull["number"],
|
|
57
|
+
"url": pull.get("html_url"),
|
|
58
|
+
"head_branch": pull["head"].get("ref"),
|
|
59
|
+
"base_branch": pull["base"].get("ref"),
|
|
60
|
+
}
|
|
61
|
+
),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def signals_from_pull(pull: Json, commits: Sequence[Json]) -> PullRequestSignals:
|
|
66
|
+
"""What agent detection looks at: author, branch, body and commit messages."""
|
|
67
|
+
return PullRequestSignals(
|
|
68
|
+
author_login=(pull.get("user") or {}).get("login", ""),
|
|
69
|
+
head_branch=pull["head"].get("ref") or "",
|
|
70
|
+
body=pull.get("body") or "",
|
|
71
|
+
commit_messages=tuple(commit["commit"].get("message") or "" for commit in commits),
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def pr_state_evidence(pull: Json, observed_at: datetime) -> Evidence:
|
|
76
|
+
merged = bool(pull.get("merged_at"))
|
|
77
|
+
return Evidence(
|
|
78
|
+
record_type="pr_state",
|
|
79
|
+
external_ref=str(pull["number"]),
|
|
80
|
+
observed_at=observed_at,
|
|
81
|
+
attributes=_without_none(
|
|
82
|
+
{
|
|
83
|
+
"state": pull.get("state"),
|
|
84
|
+
"merged": merged,
|
|
85
|
+
"merged_at": parse_time(pull.get("merged_at")),
|
|
86
|
+
"merge_sha": pull.get("merge_commit_sha") if merged else None,
|
|
87
|
+
# GitHub closes linked issues only for merges into the default branch.
|
|
88
|
+
"base_ref": (pull.get("base") or {}).get("ref"),
|
|
89
|
+
"default_branch": ((pull.get("base") or {}).get("repo") or {}).get(
|
|
90
|
+
"default_branch"
|
|
91
|
+
),
|
|
92
|
+
}
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
# ---- texts and the commit each one refers to -------------------------------------------------
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _commit_time(commit: Json) -> datetime | None:
|
|
101
|
+
details = commit["commit"]
|
|
102
|
+
return parse_time((details.get("committer") or {}).get("date")) or parse_time(
|
|
103
|
+
(details.get("author") or {}).get("date")
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def ref_at(commits: Sequence[Json], when: datetime, fallback: str | None) -> str | None:
|
|
108
|
+
"""The newest commit dated at or before ``when``; otherwise the first commit.
|
|
109
|
+
|
|
110
|
+
Commit dates can mislead after rebases; see docs/sources/github.md section 3.
|
|
111
|
+
"""
|
|
112
|
+
dated = [(time, commit["sha"]) for commit in commits if (time := _commit_time(commit))]
|
|
113
|
+
earlier = [item for item in dated if item[0] <= when]
|
|
114
|
+
if earlier:
|
|
115
|
+
return max(earlier)[1]
|
|
116
|
+
if commits:
|
|
117
|
+
return commits[0]["sha"]
|
|
118
|
+
return fallback
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def texts_from_pull(
|
|
122
|
+
pull: Json, commits: Sequence[Json], comments: Sequence[Json]
|
|
123
|
+
) -> list[TextSource]:
|
|
124
|
+
"""Every text the agent wrote on the PR: title, body, commit messages, own comments."""
|
|
125
|
+
opened_at = _required_time(pull.get("created_at"), "created_at")
|
|
126
|
+
head_sha = pull["head"].get("sha")
|
|
127
|
+
pr_ref = ref_at(commits, opened_at, head_sha)
|
|
128
|
+
author = (pull.get("user") or {}).get("login")
|
|
129
|
+
|
|
130
|
+
# Title and body can be edited after the PR opens; what we read is their latest version.
|
|
131
|
+
final_ref = head_sha if head_sha and head_sha != pr_ref else None
|
|
132
|
+
final_at = None
|
|
133
|
+
if final_ref:
|
|
134
|
+
head = next((commit for commit in commits if commit["sha"] == final_ref), None)
|
|
135
|
+
final_at = (_commit_time(head) if head else None) or opened_at
|
|
136
|
+
texts = [
|
|
137
|
+
TextSource("title", pull.get("title") or "", opened_at, pr_ref, final_ref, final_at),
|
|
138
|
+
TextSource("body", pull.get("body") or "", opened_at, pr_ref, final_ref, final_at),
|
|
139
|
+
]
|
|
140
|
+
for commit in commits:
|
|
141
|
+
written_at = _commit_time(commit) or opened_at
|
|
142
|
+
texts.append(
|
|
143
|
+
TextSource("commit", commit["commit"].get("message") or "", written_at, commit["sha"])
|
|
144
|
+
)
|
|
145
|
+
for comment in comments:
|
|
146
|
+
# Only the PR author's comments are the agent's claims; reviewers' words are not.
|
|
147
|
+
if (comment.get("user") or {}).get("login") != author:
|
|
148
|
+
continue
|
|
149
|
+
written_at = _required_time(comment.get("created_at"), "comment created_at")
|
|
150
|
+
texts.append(
|
|
151
|
+
TextSource(
|
|
152
|
+
"comment",
|
|
153
|
+
comment.get("body") or "",
|
|
154
|
+
written_at,
|
|
155
|
+
ref_at(commits, written_at, head_sha),
|
|
156
|
+
)
|
|
157
|
+
)
|
|
158
|
+
return [text for text in texts if text.text.strip()]
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# ---- CI results ------------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def check_run_evidence(run: Json, ref: str, observed_at: datetime) -> Evidence:
|
|
165
|
+
"""One check run. Keyed by commit and job name; the run id is the version, so a re-run
|
|
166
|
+
(which gets a new, larger id) replaces the older result during de-duplication.
|
|
167
|
+
|
|
168
|
+
GitHub sets a conclusion only when a run completes, yet it has been seen answering
|
|
169
|
+
``status: in_progress`` together with a conclusion and ``completed_at`` (a lagging field).
|
|
170
|
+
A run with a conclusion is treated as completed, so a claim does not wait on it for hours.
|
|
171
|
+
"""
|
|
172
|
+
status = "completed" if run.get("conclusion") else run.get("status")
|
|
173
|
+
return Evidence(
|
|
174
|
+
record_type="check_result",
|
|
175
|
+
external_ref=f"{ref}/{run['name']}",
|
|
176
|
+
observed_at=observed_at,
|
|
177
|
+
version=int(run["id"]),
|
|
178
|
+
occurred_at=parse_time(run.get("completed_at")),
|
|
179
|
+
attributes=_without_none(
|
|
180
|
+
{
|
|
181
|
+
"ref": ref,
|
|
182
|
+
"name": run["name"],
|
|
183
|
+
"status": status,
|
|
184
|
+
"conclusion": run.get("conclusion"),
|
|
185
|
+
"source": "check_run",
|
|
186
|
+
# The app that posted it (github-actions, a third-party CI, a bot). Recorded so
|
|
187
|
+
# expired Actions runs can be told apart from surviving third-party checks.
|
|
188
|
+
"app": (run.get("app") or {}).get("slug"),
|
|
189
|
+
}
|
|
190
|
+
),
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def workflow_run_evidence(run: Json, ref: str, observed_at: datetime) -> Evidence:
|
|
195
|
+
"""One GitHub Actions workflow run on a commit. Used only to know whether CI is still
|
|
196
|
+
coming: a run that is queued, waiting on ``needs:`` jobs or held for approval (Copilot and
|
|
197
|
+
fork PRs) may have no check runs yet. Never evidence that a claim is true or false."""
|
|
198
|
+
# Keyed by run id, not workflow name: two runs of one workflow on a commit (different
|
|
199
|
+
# events, or a newer run beside an older one still going) must both be seen. A re-run keeps
|
|
200
|
+
# its id and counts up run_attempt, which is the version.
|
|
201
|
+
return Evidence(
|
|
202
|
+
record_type="ci_workflow_run",
|
|
203
|
+
external_ref=f"{ref}/workflow-run:{run['id']}",
|
|
204
|
+
observed_at=observed_at,
|
|
205
|
+
version=int(run.get("run_attempt") or 1),
|
|
206
|
+
occurred_at=parse_time(run.get("created_at")),
|
|
207
|
+
attributes=_without_none(
|
|
208
|
+
{
|
|
209
|
+
"ref": ref,
|
|
210
|
+
"name": run["name"],
|
|
211
|
+
"status": run.get("status"),
|
|
212
|
+
"conclusion": run.get("conclusion"),
|
|
213
|
+
"event": run.get("event"),
|
|
214
|
+
# When the run (or its latest attempt) started, close to when the commit was
|
|
215
|
+
# pushed, approved or re-run (a commit's own date can be much older): CI
|
|
216
|
+
# waiting is counted from here.
|
|
217
|
+
"created_at": parse_time(run.get("run_started_at") or run.get("created_at")),
|
|
218
|
+
}
|
|
219
|
+
),
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
# Commit statuses use a different vocabulary than check runs; map them onto it.
|
|
224
|
+
_STATUS_CONCLUSION = {"success": "success", "failure": "failure", "error": "failure"}
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def status_evidence(status: Json, ref: str, observed_at: datetime) -> Evidence:
|
|
228
|
+
"""One commit status (older CI systems). Prefixed so it never collides with a check run."""
|
|
229
|
+
state = status.get("state")
|
|
230
|
+
return Evidence(
|
|
231
|
+
record_type="check_result",
|
|
232
|
+
external_ref=f"{ref}/status:{status['context']}",
|
|
233
|
+
observed_at=observed_at,
|
|
234
|
+
version=int(status.get("id") or 0),
|
|
235
|
+
occurred_at=parse_time(status.get("updated_at")),
|
|
236
|
+
attributes=_without_none(
|
|
237
|
+
{
|
|
238
|
+
"ref": ref,
|
|
239
|
+
"name": status["context"],
|
|
240
|
+
"status": "in_progress" if state == "pending" else "completed",
|
|
241
|
+
"conclusion": _STATUS_CONCLUSION.get(state or ""),
|
|
242
|
+
"source": "status",
|
|
243
|
+
}
|
|
244
|
+
),
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
# ---- files, issues, reverts, deployments -----------------------------------------------------
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def changed_file_evidence(file: Json, observed_at: datetime) -> Evidence:
|
|
252
|
+
"""Paths, line counts and integrity counts. The patch (code) is read once to count skipped
|
|
253
|
+
or removed tests and weakened test commands (packs/coding/diff_facts.py), then dropped."""
|
|
254
|
+
path = file["filename"]
|
|
255
|
+
status = file.get("status", "modified")
|
|
256
|
+
return Evidence(
|
|
257
|
+
record_type="changed_file",
|
|
258
|
+
external_ref=path,
|
|
259
|
+
observed_at=observed_at,
|
|
260
|
+
attributes={
|
|
261
|
+
"path": path,
|
|
262
|
+
"status": status,
|
|
263
|
+
"additions": int(file.get("additions", 0)),
|
|
264
|
+
"deletions": int(file.get("deletions", 0)),
|
|
265
|
+
**diff_facts.facts(path, status, file.get("patch")),
|
|
266
|
+
},
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def files_summary_evidence(
|
|
271
|
+
file_records: Sequence[Evidence],
|
|
272
|
+
number: int,
|
|
273
|
+
observed_at: datetime,
|
|
274
|
+
expected_files: int | None = None,
|
|
275
|
+
) -> Evidence:
|
|
276
|
+
"""One record with the PR's integrity totals (CEL cannot sum per-file counts).
|
|
277
|
+
|
|
278
|
+
``expected_files`` is the PR's own file count: GitHub's file list stops at 3,000 without an
|
|
279
|
+
error, so a shorter list means files were never inspected.
|
|
280
|
+
"""
|
|
281
|
+
totals = diff_facts.summarize([dict(record.attributes) for record in file_records])
|
|
282
|
+
if expected_files is not None and expected_files > len(file_records):
|
|
283
|
+
totals["uninspected"] += expected_files - len(file_records)
|
|
284
|
+
totals["facts_version"] = diff_facts.FACTS_VERSION
|
|
285
|
+
return Evidence(
|
|
286
|
+
record_type="changed_files_summary",
|
|
287
|
+
external_ref=str(number),
|
|
288
|
+
observed_at=observed_at,
|
|
289
|
+
attributes=totals,
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def issue_evidence(
|
|
294
|
+
issue: Json, events: Iterable[Json], repo: str, observed_at: datetime
|
|
295
|
+
) -> Evidence | None:
|
|
296
|
+
"""An issue's state. Returns None for pull requests (the issues API also lists PRs)."""
|
|
297
|
+
if "pull_request" in issue:
|
|
298
|
+
return None
|
|
299
|
+
reopened = [
|
|
300
|
+
time
|
|
301
|
+
for event in events
|
|
302
|
+
if event.get("event") == "reopened" and (time := parse_time(event.get("created_at")))
|
|
303
|
+
]
|
|
304
|
+
return Evidence(
|
|
305
|
+
record_type="issue_state",
|
|
306
|
+
external_ref=f"{repo.lower()}#{issue['number']}",
|
|
307
|
+
observed_at=observed_at,
|
|
308
|
+
attributes=_without_none(
|
|
309
|
+
{
|
|
310
|
+
"repo": repo.lower(),
|
|
311
|
+
"number": issue["number"],
|
|
312
|
+
"state": issue.get("state"),
|
|
313
|
+
"closed_at": parse_time(issue.get("closed_at")),
|
|
314
|
+
"last_reopened_at": max(reopened) if reopened else None,
|
|
315
|
+
}
|
|
316
|
+
),
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
_REVERTS_COMMIT = re.compile(r"this reverts commit ([0-9a-f]{7,40})", re.IGNORECASE)
|
|
321
|
+
_ANY_PR_REVERT = re.compile(r"\breverts\s+[\w.-]+/[\w.-]+#\d+\b|\breverts\s+#\d+\b", re.IGNORECASE)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def revert_evidence(
|
|
325
|
+
commits: Iterable[Json],
|
|
326
|
+
merge_sha: str,
|
|
327
|
+
repo: str,
|
|
328
|
+
number: int,
|
|
329
|
+
observed_at: datetime,
|
|
330
|
+
title: str = "",
|
|
331
|
+
other_names: Sequence[str] = (),
|
|
332
|
+
) -> list[Evidence]:
|
|
333
|
+
"""Commits on the base branch that revert the merged PR.
|
|
334
|
+
|
|
335
|
+
``other_names`` are other names of the repository (its current name after a rename or
|
|
336
|
+
transfer): the Revert button writes whichever name the repository had at the time.
|
|
337
|
+
|
|
338
|
+
Three forms are recognised:
|
|
339
|
+
- ``git revert``: "This reverts commit <sha>" naming the merge commit;
|
|
340
|
+
- GitHub's "Revert" button: a commit mentioning "Reverts owner/repo#<number>";
|
|
341
|
+
- the Revert button followed by a squash merge, whose message may keep only the title:
|
|
342
|
+
``Revert "<PR title>"`` or ``Revert "<PR title> (#<number>)"`` (also the
|
|
343
|
+
conventional-commit form ``revert: "<PR title>"``). The whole quoted title
|
|
344
|
+
must match, so a short title ("Fix") cannot match an unrelated revert, and the title is
|
|
345
|
+
used only when the message names no reverted PR or commit at all: an explicit
|
|
346
|
+
"Reverts o/r#150" or "This reverts commit <sha>" says what was reverted, and two PRs
|
|
347
|
+
may share a generic title ("Update README.md").
|
|
348
|
+
"""
|
|
349
|
+
names = "|".join(re.escape(name) for name in (repo, *other_names))
|
|
350
|
+
pr_reference = re.compile(rf"\breverts\s+(?:{names})#{number}\b", re.IGNORECASE)
|
|
351
|
+
title_revert = (
|
|
352
|
+
re.compile(rf'^revert:?\s+"{re.escape(title.strip())}(\s+\(#{number}\))?"', re.IGNORECASE)
|
|
353
|
+
if title.strip()
|
|
354
|
+
else None
|
|
355
|
+
)
|
|
356
|
+
found = []
|
|
357
|
+
reverting: list[Json] = []
|
|
358
|
+
for commit in commits:
|
|
359
|
+
message = commit["commit"].get("message") or ""
|
|
360
|
+
named = [sha for sha in _REVERTS_COMMIT.findall(message) if merge_sha.startswith(sha)]
|
|
361
|
+
explicit = _REVERTS_COMMIT.search(message) or _ANY_PR_REVERT.search(message)
|
|
362
|
+
by_title = not explicit and title_revert is not None and title_revert.match(message.strip())
|
|
363
|
+
if named or pr_reference.search(message) or by_title:
|
|
364
|
+
reverting.append(commit)
|
|
365
|
+
for commit in reverting:
|
|
366
|
+
found.append(
|
|
367
|
+
Evidence(
|
|
368
|
+
record_type="revert",
|
|
369
|
+
external_ref=commit["sha"],
|
|
370
|
+
observed_at=observed_at,
|
|
371
|
+
occurred_at=_commit_time(commit),
|
|
372
|
+
attributes=_without_none(
|
|
373
|
+
{
|
|
374
|
+
"reverted_sha": merge_sha,
|
|
375
|
+
"at": _commit_time(commit),
|
|
376
|
+
# A later revert of this revert put the change back.
|
|
377
|
+
"undone": _is_undone(commit, commits),
|
|
378
|
+
}
|
|
379
|
+
),
|
|
380
|
+
)
|
|
381
|
+
)
|
|
382
|
+
return found
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
_SQUASH_SUFFIX = re.compile(r"(?: \(#[0-9]{1,9}\))+$")
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def _subject(message: str) -> str:
|
|
389
|
+
# Whitespace squeezed first: the suffix pattern then needs no leading \s+, which was
|
|
390
|
+
# retried at every position of a long run (security review 2, N5).
|
|
391
|
+
subject = " ".join((message.strip().splitlines() or [""])[0].split())
|
|
392
|
+
return _SQUASH_SUFFIX.sub("", subject).lower()
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def _is_undone(revert: Json, commits: Sequence[Json]) -> bool:
|
|
396
|
+
"""True when another scanned commit reverts this revert: it names the revert's sha, or its
|
|
397
|
+
subject is exactly ``Revert "<the revert's subject>"`` (the Revert button's wording)."""
|
|
398
|
+
sha = revert["sha"]
|
|
399
|
+
undo_subject = f'revert "{_subject(revert["commit"].get("message") or "")}"'
|
|
400
|
+
for commit in commits:
|
|
401
|
+
if commit["sha"] == sha:
|
|
402
|
+
continue
|
|
403
|
+
message = commit["commit"].get("message") or ""
|
|
404
|
+
names_it = any(sha.startswith(s) for s in _REVERTS_COMMIT.findall(message))
|
|
405
|
+
if names_it or _subject(message) == undo_subject:
|
|
406
|
+
return True
|
|
407
|
+
return False
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
# Files that change in almost every PR. A revert scan filtered on one of them would list many
|
|
411
|
+
# unrelated commits, so another changed file is preferred.
|
|
412
|
+
_BUSY_FILES = re.compile(
|
|
413
|
+
r"(^|/)(package-lock\.json|yarn\.lock|pnpm-lock\.yaml|cargo\.lock|poetry\.lock|uv\.lock|"
|
|
414
|
+
r"go\.sum|gemfile\.lock|composer\.lock|changelog(\.[a-z]+)?)$",
|
|
415
|
+
re.IGNORECASE,
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def revert_probe_path(files: Iterable[Json]) -> str | None:
|
|
420
|
+
"""One file the PR changed, to narrow the revert scan.
|
|
421
|
+
|
|
422
|
+
A full revert touches every file the PR changed, so listing only the commits that touched
|
|
423
|
+
one of them finds it with a short, exact list instead of every commit on a busy branch.
|
|
424
|
+
"""
|
|
425
|
+
paths = sorted(file["filename"] for file in files if file.get("filename"))
|
|
426
|
+
quiet = [path for path in paths if not _BUSY_FILES.search(path)]
|
|
427
|
+
return (quiet or paths or [None])[0]
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def deployment_evidence(
|
|
431
|
+
deployment: Json, statuses: Sequence[Json], observed_at: datetime, *, complete: bool = False
|
|
432
|
+
) -> Evidence | None:
|
|
433
|
+
"""A deployment and its latest status (GitHub lists statuses newest first).
|
|
434
|
+
|
|
435
|
+
``complete`` says the source saw every deployment of the commit; only then can a failure
|
|
436
|
+
contradict a "deployed" claim (a success may hide among unlisted deployments)."""
|
|
437
|
+
if not statuses:
|
|
438
|
+
return None
|
|
439
|
+
latest = statuses[0]
|
|
440
|
+
return Evidence(
|
|
441
|
+
record_type="deployment_status",
|
|
442
|
+
external_ref=str(deployment["id"]),
|
|
443
|
+
observed_at=observed_at,
|
|
444
|
+
version=int(latest.get("id") or 0),
|
|
445
|
+
attributes=_without_none(
|
|
446
|
+
{
|
|
447
|
+
"ref": deployment.get("sha"),
|
|
448
|
+
"environment": (deployment.get("environment") or "").lower(),
|
|
449
|
+
"state": latest.get("state"),
|
|
450
|
+
"at": parse_time(latest.get("created_at")),
|
|
451
|
+
"complete": complete,
|
|
452
|
+
}
|
|
453
|
+
),
|
|
454
|
+
)
|