pubcheck 0.0.2__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.
- pubcheck/__init__.py +28 -0
- pubcheck/__main__.py +106 -0
- pubcheck/base.py +157 -0
- pubcheck/core.py +274 -0
- pubcheck/correspond.py +151 -0
- pubcheck/detectors.py +212 -0
- pubcheck/hold.py +189 -0
- pubcheck/sources.py +277 -0
- pubcheck-0.0.2.dist-info/METADATA +90 -0
- pubcheck-0.0.2.dist-info/RECORD +13 -0
- pubcheck-0.0.2.dist-info/WHEEL +4 -0
- pubcheck-0.0.2.dist-info/entry_points.txt +3 -0
- pubcheck-0.0.2.dist-info/licenses/LICENSE +21 -0
pubcheck/correspond.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""correspond's ``before_send`` check: the publication check for every write correspond makes.
|
|
2
|
+
|
|
3
|
+
Set it once at the top of correspond's config file (``~/.config/correspond/config.toml``)::
|
|
4
|
+
|
|
5
|
+
before_send = "pubcheck.correspond:before_send"
|
|
6
|
+
|
|
7
|
+
For a write whose audience is **public** (correspond resolves an unknown audience to
|
|
8
|
+
public), it runs :func:`pubcheck.check` on the draft's title and text, unattended: a
|
|
9
|
+
correspond write has nobody reading it before it goes out, so @mentions are held and an
|
|
10
|
+
unavailable check holds too (fail closed). Assignees and the ``notify`` list notify people,
|
|
11
|
+
so they hold as mentions; labels, a milestone, a discussion category and attachment names
|
|
12
|
+
are checked with the text. Any other audience (a private repo, the operator's own devices)
|
|
13
|
+
passes unchanged.
|
|
14
|
+
|
|
15
|
+
A held write raises ``correspond.errors.NeedsApproval`` with the categories (never the
|
|
16
|
+
text) in its details. On a real send, not a dry run, the held post also becomes a desk
|
|
17
|
+
item (:func:`pubcheck.hold.file_hold`), whose URL is in the details as ``desk_item``.
|
|
18
|
+
:func:`make_before_send` builds a check with other settings or another ``hold``.
|
|
19
|
+
|
|
20
|
+
Not covered: the contents of attached files (the check reads prose, not files).
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
from collections.abc import Callable, Mapping
|
|
26
|
+
from typing import Any
|
|
27
|
+
|
|
28
|
+
from pubcheck.core import check as _check
|
|
29
|
+
from pubcheck.hold import file_hold
|
|
30
|
+
|
|
31
|
+
WRITER = "correspond"
|
|
32
|
+
PUBLIC = "public"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _is_public(audience: Any) -> bool:
|
|
36
|
+
"""Public unless correspond computed a narrower scope; an audience it could not compute is public."""
|
|
37
|
+
scope = getattr(audience, "scope", None)
|
|
38
|
+
return scope is None or str(getattr(scope, "value", scope)) == PUBLIC
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _repo_of(ref: Any) -> str | None:
|
|
42
|
+
"""``owner/repo`` of a ``github:`` conversation, else ``None``.
|
|
43
|
+
|
|
44
|
+
>>> class Ref: channel, id = "github", "octo/app#12"
|
|
45
|
+
>>> _repo_of(Ref())
|
|
46
|
+
'octo/app'
|
|
47
|
+
"""
|
|
48
|
+
if str(getattr(ref, "channel", "")) != "github":
|
|
49
|
+
return None
|
|
50
|
+
slug = str(getattr(ref, "id", "") or "").split("#", 1)[0]
|
|
51
|
+
return slug if slug.count("/") == 1 else None
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _strings(value: Any) -> list[str]:
|
|
55
|
+
"""The text values in plain data (strings and numbers, through mappings and lists); objects are skipped."""
|
|
56
|
+
if isinstance(value, Mapping):
|
|
57
|
+
return [s for v in value.values() for s in _strings(v)]
|
|
58
|
+
if isinstance(value, (list, tuple, set)):
|
|
59
|
+
return [s for v in value for s in _strings(v)]
|
|
60
|
+
return (
|
|
61
|
+
[str(value)]
|
|
62
|
+
if isinstance(value, (str, int, float)) and not isinstance(value, bool)
|
|
63
|
+
else []
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _published_with(draft: Any) -> list[str]:
|
|
68
|
+
"""What a draft publishes besides its title and text, as strings to check.
|
|
69
|
+
|
|
70
|
+
The people it notifies (its ``notify`` list and any native ``assignees``) as
|
|
71
|
+
``@login``, so the mention detector holds them; every other native value (labels, a
|
|
72
|
+
milestone, a discussion category); the attachments' names.
|
|
73
|
+
"""
|
|
74
|
+
native = getattr(draft, "native", None) or {}
|
|
75
|
+
native = native if isinstance(native, Mapping) else {}
|
|
76
|
+
people = list(getattr(draft, "notify", ()) or ()) + _strings(native.get("assignees"))
|
|
77
|
+
out = ["@" + str(p).lstrip("@") for p in people]
|
|
78
|
+
out += [s for k, v in native.items() if k != "assignees" for s in _strings(v)]
|
|
79
|
+
for a in getattr(draft, "attachments", ()) or ():
|
|
80
|
+
name = (
|
|
81
|
+
getattr(a, "name", None)
|
|
82
|
+
or str(getattr(a, "ref", "") or "").rsplit("/", 1)[-1]
|
|
83
|
+
)
|
|
84
|
+
out += _strings(name or None)
|
|
85
|
+
return out
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def make_before_send(
|
|
89
|
+
*,
|
|
90
|
+
config: Mapping | None = None,
|
|
91
|
+
hold: Callable[..., dict] = file_hold,
|
|
92
|
+
check: Callable = _check,
|
|
93
|
+
) -> Callable[..., None]:
|
|
94
|
+
"""A correspond ``before_send`` check; ``config`` overrides pubcheck's settings (``hold_repo``…)."""
|
|
95
|
+
|
|
96
|
+
def before_send(
|
|
97
|
+
ref,
|
|
98
|
+
draft,
|
|
99
|
+
audience,
|
|
100
|
+
*,
|
|
101
|
+
operation="send",
|
|
102
|
+
dry_run=False,
|
|
103
|
+
message_id=None,
|
|
104
|
+
**context,
|
|
105
|
+
):
|
|
106
|
+
if not _is_public(audience):
|
|
107
|
+
return None
|
|
108
|
+
from correspond.errors import NeedsApproval
|
|
109
|
+
|
|
110
|
+
text = getattr(draft, "text", None) or ""
|
|
111
|
+
title = getattr(draft, "title", None)
|
|
112
|
+
more = _published_with(draft)
|
|
113
|
+
target = str(getattr(ref, "encoded", None) or ref)
|
|
114
|
+
verdict = check(
|
|
115
|
+
text,
|
|
116
|
+
title=title,
|
|
117
|
+
more=more,
|
|
118
|
+
repo=_repo_of(ref),
|
|
119
|
+
public=True,
|
|
120
|
+
unattended=True,
|
|
121
|
+
surface=f"correspond {operation}",
|
|
122
|
+
writer=WRITER,
|
|
123
|
+
budget=(config or {}).get("budget_seconds"),
|
|
124
|
+
)
|
|
125
|
+
if not verdict.held:
|
|
126
|
+
return None
|
|
127
|
+
details = {
|
|
128
|
+
"check": "pubcheck",
|
|
129
|
+
"decision": verdict.decision,
|
|
130
|
+
"categories": ",".join(verdict.categories),
|
|
131
|
+
"text_hash": verdict.text_hash,
|
|
132
|
+
}
|
|
133
|
+
if dry_run:
|
|
134
|
+
details["desk_item"] = "not filed (dry run)"
|
|
135
|
+
else:
|
|
136
|
+
filed = hold(
|
|
137
|
+
verdict, target=target, text=text, title=title, more=more, config=config
|
|
138
|
+
)
|
|
139
|
+
details["desk_item"] = filed.get("url") or f"not filed: {filed.get('reason')}"
|
|
140
|
+
why = (
|
|
141
|
+
f"held for review: {', '.join(verdict.categories)} in a post to a public audience"
|
|
142
|
+
if verdict.decision == "flag"
|
|
143
|
+
else f"held: the publication check is unavailable ({verdict.reason})"
|
|
144
|
+
)
|
|
145
|
+
raise NeedsApproval(why, **details)
|
|
146
|
+
|
|
147
|
+
return before_send
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
#: The check correspond's config names: ``before_send = "pubcheck.correspond:before_send"``.
|
|
151
|
+
before_send = make_before_send()
|
pubcheck/detectors.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""The detectors: what in a text a public reader may not see.
|
|
2
|
+
|
|
3
|
+
A detector is a callable ``(text, ctx) -> [(category, term)]``. ``ctx`` carries what the
|
|
4
|
+
check knows about the post: ``repo`` (the target), ``surface`` (``git commit``, ``gh issue
|
|
5
|
+
create``, ``correspond send``…), ``terms`` (the registry's terms for that repo) and
|
|
6
|
+
``private_slugs`` (repos known to be private). Whitespace is normalised before any detector
|
|
7
|
+
runs, so a name wrapped across lines still matches. ``term`` is what a local author needs
|
|
8
|
+
to reword the text; it never leaves the machine (the CLI prints it only with ``--terms``,
|
|
9
|
+
and the log never holds it). A credential's term is its pattern name, never the secret.
|
|
10
|
+
|
|
11
|
+
- ``registry``: a term of an acquaint record a public reader may not see;
|
|
12
|
+
- ``owner-relation``: "the operator stated", "the owner's preference", "my agents"…
|
|
13
|
+
(text inside double quotes, curly quotes or backticks is skipped: a quoted phrase is an
|
|
14
|
+
example, not a claim);
|
|
15
|
+
- ``private-ref``: a repo known to be private, a private companion
|
|
16
|
+
(``<private_owner>/_<name>``), this machine's home folder, and the operator's
|
|
17
|
+
``private_patterns`` (``ctx["private_patterns"]``, from the settings);
|
|
18
|
+
- ``session-link``: ``claude.ai/code/session_…`` (not on commits, whose session links a
|
|
19
|
+
commit hook strips);
|
|
20
|
+
- ``credential``: a credential-shaped string (:func:`openloops.egress.find_credentials`);
|
|
21
|
+
- ``mention``: an ``@login`` or ``@org/team`` outside code, which notifies a person:
|
|
22
|
+
sending it is messaging them. Only in :data:`UNATTENDED_DETECTORS`, since an attended
|
|
23
|
+
author chose to notify.
|
|
24
|
+
|
|
25
|
+
>>> findings(["As the owner asked, we fixed it"], {})
|
|
26
|
+
[('owner-relation', 'the owner asked')]
|
|
27
|
+
>>> findings(["ping @octocat about it"], {}, detectors=UNATTENDED_DETECTORS)
|
|
28
|
+
[('mention', '@octocat')]
|
|
29
|
+
>>> findings(["write to me@example.org or see `@octocat`"], {}, detectors=UNATTENDED_DETECTORS)
|
|
30
|
+
[]
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
from __future__ import annotations
|
|
34
|
+
|
|
35
|
+
import getpass
|
|
36
|
+
import os
|
|
37
|
+
import re
|
|
38
|
+
from collections.abc import Callable, Iterable, Mapping
|
|
39
|
+
from functools import lru_cache
|
|
40
|
+
from pathlib import Path
|
|
41
|
+
|
|
42
|
+
Finding = tuple[str, str]
|
|
43
|
+
Detector = Callable[[str, Mapping], list]
|
|
44
|
+
|
|
45
|
+
DICTIONARY = "/usr/share/dict/words"
|
|
46
|
+
DICTIONARY_ENV = "PUBCHECK_DICTIONARY" # another word list, one word per line
|
|
47
|
+
STOP_WORDS = frozenset(
|
|
48
|
+
"admin bot candidate client contact dev hello info operator owner support team user".split()
|
|
49
|
+
)
|
|
50
|
+
MIN_TERM_LEN = 3
|
|
51
|
+
|
|
52
|
+
_VERB = (
|
|
53
|
+
r"(?:stated|states|said|says|asked|asks|wants|wanted|prefers|preferred|requested|"
|
|
54
|
+
r"told|tells|instructed|insisted|corrected|decided|noticed|complained|approved|"
|
|
55
|
+
r"objected|wrote|mentioned|pointed out)"
|
|
56
|
+
)
|
|
57
|
+
# "the user" is also ordinary product talk ("the user types a query"), so it gets the narrow lists.
|
|
58
|
+
_USER_VERB = r"(?:stated|asked|requested|instructed|insisted|corrected)"
|
|
59
|
+
_OWNER = r"(?:operator|owner)"
|
|
60
|
+
OWNER_RELATION = [
|
|
61
|
+
re.compile(rf"\bthe {_OWNER} {_VERB}\b", re.I),
|
|
62
|
+
re.compile(rf"\bthe user {_USER_VERB}\b", re.I),
|
|
63
|
+
re.compile(r"\bper the (?:operator|owner)\b", re.I),
|
|
64
|
+
re.compile(
|
|
65
|
+
rf"\bthe {_OWNER}['’]s (?:preference|correction|request|instruction|feedback|words|"
|
|
66
|
+
r"rule|decision|complaint|correspondence)s?\b",
|
|
67
|
+
re.I,
|
|
68
|
+
),
|
|
69
|
+
re.compile(
|
|
70
|
+
r"\bthe user['’]s (?:preference|correction|instruction|feedback|rule)s?\b", re.I
|
|
71
|
+
),
|
|
72
|
+
re.compile(r"\bmy (?:agents?|fleet|clients?)\b", re.I),
|
|
73
|
+
]
|
|
74
|
+
_QUOTED = re.compile(r"```.*?```|\"[^\"]*\"|“[^”]*”|`[^`]*`", re.S)
|
|
75
|
+
_CODE = re.compile(r"```.*?```|`[^`]*`", re.S)
|
|
76
|
+
|
|
77
|
+
SESSION_LINK = re.compile(r"claude\.ai/code/session_\w+")
|
|
78
|
+
# GitHub logins: alphanumerics and single hyphens, up to 39; a team is @org/team. GitHub
|
|
79
|
+
# notifies after any non-word character, so only a word character (an address,
|
|
80
|
+
# me@example.org; a version, pkg@1.2) or another @ rules a mention out. A URL's /@name
|
|
81
|
+
# counts: holding it costs a review, missing it costs a notification.
|
|
82
|
+
MENTION = re.compile(
|
|
83
|
+
r"(?<![\w@`])@([A-Za-z0-9](?:[A-Za-z0-9-]{0,38})(?:/[A-Za-z0-9_.-]+)?)(?![\w-])"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@lru_cache(maxsize=1)
|
|
88
|
+
def _local_paths():
|
|
89
|
+
"""This machine's home folder and user name, as patterns (never other people's paths)."""
|
|
90
|
+
out = []
|
|
91
|
+
home = str(Path.home())
|
|
92
|
+
if home not in ("", "/"):
|
|
93
|
+
out.append(re.compile(rf"(?<![\w.]){re.escape(home)}(?=/|\b)"))
|
|
94
|
+
try:
|
|
95
|
+
user = getpass.getuser()
|
|
96
|
+
except Exception:
|
|
97
|
+
user = ""
|
|
98
|
+
if user and user != "root":
|
|
99
|
+
out += [re.compile(rf"(?<![\w.])/(?:Users|home)/{re.escape(user)}\b")]
|
|
100
|
+
return out
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@lru_cache(maxsize=1)
|
|
104
|
+
def dictionary_words() -> frozenset:
|
|
105
|
+
"""Lowercased words of the system dictionary; empty when the machine has none."""
|
|
106
|
+
try:
|
|
107
|
+
with open(
|
|
108
|
+
os.environ.get(DICTIONARY_ENV) or DICTIONARY,
|
|
109
|
+
encoding="utf-8",
|
|
110
|
+
errors="ignore",
|
|
111
|
+
) as f:
|
|
112
|
+
return frozenset(w.strip().lower() for w in f)
|
|
113
|
+
except OSError:
|
|
114
|
+
return frozenset()
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def is_common(word: str) -> bool:
|
|
118
|
+
"""A stop word or a dictionary word (any case)."""
|
|
119
|
+
return word.lower() in STOP_WORDS or word.lower() in dictionary_words()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@lru_cache(maxsize=4096)
|
|
123
|
+
def _term_pattern(term):
|
|
124
|
+
body = r"\s+".join(re.escape(w) for w in term.split())
|
|
125
|
+
single = len(term.split()) == 1
|
|
126
|
+
if single and term.isalpha() and term.islower() and not dictionary_words():
|
|
127
|
+
return re.compile(
|
|
128
|
+
rf"(?<=[@/]){body}(?![\w-])", re.I
|
|
129
|
+
) # no dictionary: handles only
|
|
130
|
+
flags = 0 if single and term[:1].isupper() and is_common(term) else re.I
|
|
131
|
+
return re.compile(rf"(?<![\w-]){body}(?![\w-])", flags)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def registry(text, ctx):
|
|
135
|
+
"""The registry's terms for this repo (``ctx["terms"]``) found in the text."""
|
|
136
|
+
return [
|
|
137
|
+
("registry", t) for t in ctx.get("terms", ()) if _term_pattern(t).search(text)
|
|
138
|
+
]
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def owner_relation(text, ctx):
|
|
142
|
+
"""Phrases that relay the owner's own preferences, corrections or relations."""
|
|
143
|
+
bare = _QUOTED.sub(" ", text)
|
|
144
|
+
return [
|
|
145
|
+
("owner-relation", m.group(0)) for p in OWNER_RELATION for m in p.finditer(bare)
|
|
146
|
+
]
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@lru_cache(maxsize=256)
|
|
150
|
+
def _compiled(pattern):
|
|
151
|
+
return re.compile(pattern)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def private_ref(text, ctx):
|
|
155
|
+
"""Private repos, companions, the operator's private patterns and this machine's paths."""
|
|
156
|
+
patterns = [_compiled(p) for p in ctx.get("private_patterns", ())]
|
|
157
|
+
if ctx.get("private_owner"):
|
|
158
|
+
patterns.append(_compiled(rf"\b{re.escape(ctx['private_owner'])}/_[\w.-]+"))
|
|
159
|
+
if ctx.get("surface") != "unparsed command":
|
|
160
|
+
patterns += _local_paths()
|
|
161
|
+
out = [("private-ref", m.group(0)) for p in patterns for m in p.finditer(text)]
|
|
162
|
+
target = (ctx.get("repo") or "").lower()
|
|
163
|
+
for slug in ctx.get("private_slugs", ()):
|
|
164
|
+
if slug.lower() != target and re.search(
|
|
165
|
+
rf"(?<![\w.-]){re.escape(slug)}(?![\w.-])", text, re.I
|
|
166
|
+
):
|
|
167
|
+
out.append(("private-ref", slug))
|
|
168
|
+
return out
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def session_link(text, ctx):
|
|
172
|
+
"""Claude session links (not on commits, whose session links a commit hook strips)."""
|
|
173
|
+
if ctx.get("surface") == "git commit":
|
|
174
|
+
return []
|
|
175
|
+
return [("session-link", m.group(0)) for m in SESSION_LINK.finditer(text)]
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def credential(text, ctx):
|
|
179
|
+
"""Credential-shaped strings, named by pattern; the secret itself is never returned."""
|
|
180
|
+
from openloops.egress import find_credentials # lazy: the CLI starts without it
|
|
181
|
+
|
|
182
|
+
return [("credential", name) for name, _ in find_credentials(text)]
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def mention(text, ctx):
|
|
186
|
+
"""``@login`` and ``@org/team`` outside code spans and blocks: each notifies someone."""
|
|
187
|
+
bare = _CODE.sub(" ", text)
|
|
188
|
+
return [("mention", "@" + m.group(1)) for m in MENTION.finditer(bare)]
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
#: The detectors for an attended writer (a Claude Code session): the seam is this tuple.
|
|
192
|
+
DETECTORS: tuple[Detector, ...] = (
|
|
193
|
+
registry,
|
|
194
|
+
owner_relation,
|
|
195
|
+
private_ref,
|
|
196
|
+
session_link,
|
|
197
|
+
credential,
|
|
198
|
+
)
|
|
199
|
+
#: An unattended writer also holds every @mention: nobody chose to notify that person.
|
|
200
|
+
UNATTENDED_DETECTORS: tuple[Detector, ...] = DETECTORS + (mention,)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def findings(texts: Iterable[str], ctx: Mapping, *, detectors=DETECTORS) -> list[Finding]:
|
|
204
|
+
"""``[(category, term)]``, each once, found by ``detectors`` in any of ``texts``."""
|
|
205
|
+
seen: list[Finding] = []
|
|
206
|
+
for text in texts:
|
|
207
|
+
text = re.sub(r"\s+", " ", text or "")
|
|
208
|
+
for detect in detectors:
|
|
209
|
+
for hit in detect(text, ctx):
|
|
210
|
+
if hit not in seen:
|
|
211
|
+
seen.append(hit)
|
|
212
|
+
return seen
|
pubcheck/hold.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""A held post becomes a desk item: an issue in a private repo, labelled for the desk.
|
|
2
|
+
|
|
3
|
+
When an unattended writer's text is held (flagged, or the check unavailable), nothing is
|
|
4
|
+
lost and nothing is refused silently: :func:`file_hold` opens one issue in the hold repo
|
|
5
|
+
(setting ``hold_repo``, by default the desk's own private repo) with the label the desk
|
|
6
|
+
reads (``hold_label``, by default ``escalate``), carrying the target, the title, the text
|
|
7
|
+
and the categories, so the owner can publish it as is, edit it, or drop it.
|
|
8
|
+
|
|
9
|
+
The hold repo receives the text, so it must be private: :func:`file_hold` asks GitHub
|
|
10
|
+
(never a cache) just before filing, and files nothing when it is public or unknown, or
|
|
11
|
+
when no ``hold_repo`` is configured. Credential-shaped strings are redacted from the copy
|
|
12
|
+
(:func:`redact`). The same text held twice (a retry) is filed once: the body carries the
|
|
13
|
+
text's hash, and an open issue with that hash is reused.
|
|
14
|
+
|
|
15
|
+
>>> redact("token: ghp_" + "A1" * 18)
|
|
16
|
+
'token: [credential redacted: github_token]'
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import re
|
|
22
|
+
import shutil
|
|
23
|
+
from collections.abc import Mapping, Sequence
|
|
24
|
+
|
|
25
|
+
from pubcheck.base import Budget, settings, text_hash
|
|
26
|
+
from pubcheck.core import HINTS, Verdict
|
|
27
|
+
from pubcheck.sources import gh_repo_view
|
|
28
|
+
|
|
29
|
+
MARKER = "pubcheck-hold"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
#: A whole PEM private key, BEGIN to END (the shared patterns match only the BEGIN line).
|
|
33
|
+
PEM_BLOCK = re.compile(
|
|
34
|
+
r"-----BEGIN [A-Z ]*PRIVATE KEY-----.*?(?:-----END [A-Z ]*PRIVATE KEY-----|\Z)", re.S
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def redact(text: str) -> str:
|
|
39
|
+
r"""``text`` with every credential-shaped string replaced by its pattern's name.
|
|
40
|
+
|
|
41
|
+
>>> redact("a -----BEGIN RSA PRIVATE KEY-----\nMIIE\nxyz\n-----END RSA PRIVATE KEY----- b")
|
|
42
|
+
'a [credential redacted: private_key_block] b'
|
|
43
|
+
"""
|
|
44
|
+
from openloops.egress import CREDENTIAL_PATTERNS
|
|
45
|
+
|
|
46
|
+
text = PEM_BLOCK.sub("[credential redacted: private_key_block]", text)
|
|
47
|
+
for name, pattern in CREDENTIAL_PATTERNS:
|
|
48
|
+
text = pattern.sub(f"[credential redacted: {name}]", text)
|
|
49
|
+
return text
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def hold_title(target: str, title: str | None) -> str:
|
|
53
|
+
"""``held: <target>: <title>``, short enough for a desk card.
|
|
54
|
+
|
|
55
|
+
>>> hold_title("github:o/r", "Fix the thing")
|
|
56
|
+
'held: github:o/r: Fix the thing'
|
|
57
|
+
"""
|
|
58
|
+
head = f"held: {target}"
|
|
59
|
+
return f"{head}: {redact(title)}"[:200] if title else head
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def hold_key(verdict: Verdict, target: str) -> str:
|
|
63
|
+
"""What makes two holds the same desk item: the same text to the same target."""
|
|
64
|
+
return text_hash(target, verdict.text_hash)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def hold_body(
|
|
68
|
+
verdict: Verdict,
|
|
69
|
+
*,
|
|
70
|
+
target: str,
|
|
71
|
+
text: str,
|
|
72
|
+
title: str | None,
|
|
73
|
+
more: Sequence[str] = (),
|
|
74
|
+
) -> str:
|
|
75
|
+
"""The desk item's body: why it was held, what would have been published, and the key."""
|
|
76
|
+
why = (
|
|
77
|
+
[f"- {c}: {HINTS.get(c, c)}" for c in verdict.categories]
|
|
78
|
+
if verdict.decision == "flag"
|
|
79
|
+
else [f"- the check was unavailable: {verdict.reason}"]
|
|
80
|
+
)
|
|
81
|
+
text, title = redact(text), redact(title) if title else title
|
|
82
|
+
also = (
|
|
83
|
+
[f"**Also published with it:** {', '.join(redact(m) for m in more)}", ""]
|
|
84
|
+
if more
|
|
85
|
+
else []
|
|
86
|
+
)
|
|
87
|
+
return "\n".join(
|
|
88
|
+
[
|
|
89
|
+
f"A post to `{target}` ({verdict.visibility}) was held by the publication check, "
|
|
90
|
+
"and nothing was published.",
|
|
91
|
+
"",
|
|
92
|
+
"**Why:**",
|
|
93
|
+
*why,
|
|
94
|
+
"",
|
|
95
|
+
"**Accept** (publish as is, after reading it), **edit** (reword, then send again) "
|
|
96
|
+
"or **ignore** (close this issue).",
|
|
97
|
+
"",
|
|
98
|
+
f"**Title:** {title or '(none)'}",
|
|
99
|
+
"",
|
|
100
|
+
*also,
|
|
101
|
+
"**Text:**",
|
|
102
|
+
"",
|
|
103
|
+
"````",
|
|
104
|
+
text,
|
|
105
|
+
"````",
|
|
106
|
+
"",
|
|
107
|
+
f"<!-- {MARKER}: {hold_key(verdict, target)} -->",
|
|
108
|
+
]
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _existing(repo: str, key: str, budget: Budget):
|
|
113
|
+
r = budget.run(
|
|
114
|
+
[
|
|
115
|
+
"gh",
|
|
116
|
+
"issue",
|
|
117
|
+
"list",
|
|
118
|
+
"-R",
|
|
119
|
+
repo,
|
|
120
|
+
"--state",
|
|
121
|
+
"open",
|
|
122
|
+
"--search",
|
|
123
|
+
f'"{MARKER}: {key}" in:body',
|
|
124
|
+
"--json",
|
|
125
|
+
"url",
|
|
126
|
+
"-q",
|
|
127
|
+
".[0].url",
|
|
128
|
+
],
|
|
129
|
+
cap=8,
|
|
130
|
+
)
|
|
131
|
+
url = (r.stdout.strip() if r is not None and r.returncode == 0 else "") or None
|
|
132
|
+
return url
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def file_hold(
|
|
136
|
+
verdict: Verdict,
|
|
137
|
+
*,
|
|
138
|
+
target: str,
|
|
139
|
+
text: str,
|
|
140
|
+
title: str | None = None,
|
|
141
|
+
more: Sequence[str] = (),
|
|
142
|
+
config: Mapping | None = None,
|
|
143
|
+
budget: Budget | None = None,
|
|
144
|
+
) -> dict:
|
|
145
|
+
"""File (or find) the desk item for a held post; ``{"filed": bool, "url"?, "reason"?}``. Never raises."""
|
|
146
|
+
s = settings(config)
|
|
147
|
+
repo, label = s["hold_repo"], s["hold_label"]
|
|
148
|
+
budget = budget or Budget(s["budget_seconds"])
|
|
149
|
+
if not repo:
|
|
150
|
+
return {"filed": False, "reason": "no hold_repo is configured"}
|
|
151
|
+
if not shutil.which("gh"):
|
|
152
|
+
return {"filed": False, "reason": "gh is not installed"}
|
|
153
|
+
try:
|
|
154
|
+
private = gh_repo_view(
|
|
155
|
+
repo, budget=budget
|
|
156
|
+
) # fresh: never trust a cache before copying
|
|
157
|
+
if private is not True:
|
|
158
|
+
return {
|
|
159
|
+
"filed": False,
|
|
160
|
+
"reason": f"the hold repo {repo} is not known to be private, so the text is not copied there",
|
|
161
|
+
}
|
|
162
|
+
url = _existing(repo, hold_key(verdict, target), budget)
|
|
163
|
+
if url:
|
|
164
|
+
return {"filed": True, "url": url, "repo": repo, "existing": True}
|
|
165
|
+
body = hold_body(verdict, target=target, text=text, title=title, more=more)
|
|
166
|
+
argv = [
|
|
167
|
+
"gh",
|
|
168
|
+
"issue",
|
|
169
|
+
"create",
|
|
170
|
+
"-R",
|
|
171
|
+
repo,
|
|
172
|
+
"--title",
|
|
173
|
+
hold_title(target, title),
|
|
174
|
+
"--body-file",
|
|
175
|
+
"-",
|
|
176
|
+
]
|
|
177
|
+
if label:
|
|
178
|
+
argv += ["--label", label]
|
|
179
|
+
r = budget.run(argv, cap=20, stdin=body)
|
|
180
|
+
if r is None or r.returncode != 0:
|
|
181
|
+
err = (
|
|
182
|
+
(r.stderr.strip().splitlines() or ["?"])[-1]
|
|
183
|
+
if r is not None
|
|
184
|
+
else "out of time"
|
|
185
|
+
)
|
|
186
|
+
return {"filed": False, "reason": f"gh issue create failed: {err}"}
|
|
187
|
+
return {"filed": True, "url": r.stdout.strip().splitlines()[-1], "repo": repo}
|
|
188
|
+
except Exception as error:
|
|
189
|
+
return {"filed": False, "reason": f"filing failed ({type(error).__name__})"}
|