Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions adapters/seen.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
out, but a merge by anyone, which reconciliation reads for an Override. A dismissed review was an approval or a change
request, and is read as an approval: the machine user's that no Effect recorded alarms either way. The timeline's
events that change a pull request's state are read too, though no Factory write makes them yet, so that one made by a
Factory identity alarms. A thread GitHub names no resolver for, as when an App resolved it, is `unattributed`. A list
Factory identity alarms. A thread's resolver is told by its account id alone, since GitHub types it a User whoever
resolved it (decision 0067); a thread GitHub names no resolver for is `unattributed`. A list
that cannot be read whole, or a write whose time or key cannot be read, leaves what was read `whole` false, so that
reconciliation is short, never silent; nothing is guessed. Ids and codes alone: no body or title is kept.

Expand Down Expand Up @@ -224,7 +225,10 @@ def add(by, kind, key, at):
ident = _object(resolver).get("databaseId")
# A resolver GitHub names but whose account cannot be read cannot be told from a Factory identity.
whole &= resolver is None or (isinstance(ident, int) and not isinstance(ident, bool) and ident > 0)
by = "unattributed" if resolver is None else _who({"id": ident, "type": "User"}, identities)
# GraphQL types resolvedBy a User whoever resolved it, an App among them (trials/P-49.json): it is told by its
# account id alone, which names one account whatever its type (decision 0067).
by = "unattributed" if resolver is None else next(
(code for (told, _), code in identities.items() if told == ident and _positive(ident)), None)
if by is None:
continue
whole &= _key(t.get("id")) is not None
Expand Down
38 changes: 34 additions & 4 deletions adapters/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

One GraphQL read of the threads, page by page. Who opened a thread is its first comment's author, and when, that
comment's time: a first comment that replies to another is not the opener's, and the thread is refused. Who resolved
it is GitHub's `resolvedBy`, which is typed a User: a resolved thread GitHub names no resolver for, as it is taken to
when an App resolved it or the account is gone, has `resolved_by` null, and the Kernel defers on that thread alone
(`unreadable_thread`). No trial has recorded either yet. Each actor is told by its account id and type,
it is GitHub's `resolvedBy`, by its account id alone: GraphQL types it a User whoever resolved it, an App among them
(trials/P-49.json), so its type is never read. The id is matched to the thread's comment author, whose type GraphQL
gives, or else to the repo's permission read of its login, which answers the account's id and type
(trials/P-53.json), once a login a pass. A resolver neither answers, or that GitHub names none for, has `resolved_by`
null, and the Kernel defers on that thread alone (`unreadable_thread`). A thread whose first comment was deleted reads
its reply as its opener, since GitHub keeps no link to the comment (trials/P-53.json); holding it to the Lens's own
record waits on the collector. Each actor is told by its account id and type,
never by a name's spelling (decision 0005, L-57), and its write access read once a login, as `adapters/timeline.py`
does. No comment's text is read. A thread whose opener GitHub no longer names, a deleted account's, is refused with the
snapshot, as an unreadable actor is anywhere: the known cost is that such a pull request is never read whole. A thread
Expand Down Expand Up @@ -61,6 +65,32 @@ def _account(author, kind=None):
return {"login": author.get("login"), "id": author.get("databaseId"), "type": kind}


def _resolver(by, author, accounts: timeline.Accounts):
"""Who resolved a thread, as `facts/1` holds an actor, or None where it cannot be told (decision 0067, issue 331).

GraphQL's `resolvedBy` is typed a User whoever resolved it, an App among them (trials/P-49.json), so its type is
never read. Its account id is matched to the thread's own comment author, whose type GraphQL gives; failing that,
to the repo's permission read of its login, which answers the account's id and type (trials/P-53.json), held to
the id. An id neither answers, or a read that fails, leaves the resolver unread, and the Kernel defers on the thread
(`kernel/derive.py`'s `unreadable_thread`)."""
if not isinstance(by, dict):
return None
ident = by.get("databaseId")
if not isinstance(ident, int) or isinstance(ident, bool) or ident < 1:
return None
if isinstance(author, dict) and author.get("databaseId") == ident and author.get("__typename") in ("User", "Bot") \
and not isinstance(author.get("databaseId"), bool):
return accounts.actor(_account(author))
kind = accounts.typed(by.get("login"), ident)
if kind is None:
return None
try:
return accounts.actor({"login": by["login"], "id": ident, "type": kind})
except Unanswered:
# An account typed but not read whole, its access among it: the resolver alone is unread.
return None


def thread(node: dict, accounts: timeline.Accounts) -> dict:
"""One thread as `facts/1` holds it; refused when any part GitHub gives cannot be read."""
ident, resolved = node.get("id"), node.get("isResolved")
Expand All @@ -78,7 +108,7 @@ def thread(node: dict, accounts: timeline.Accounts) -> dict:
by = node.get("resolvedBy")
if not resolved and by is not None:
raise Unanswered("an open review thread that names who resolved it")
resolved_by = accounts.actor(_account(by, "User")) if by is not None else None
resolved_by = _resolver(by, first[0].get("author"), accounts) if by is not None else None
human = opened_by["kind"] == "human"
return {"id": ident, "resolved": resolved, "opened_by": opened_by, "category": "human" if human else None,
"severity": None, "resolved_by": resolved_by, "opened_at": opened_at}
Expand Down
51 changes: 39 additions & 12 deletions adapters/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
`lens` is a Bot the caller names as a Lens's App, read as that Lens's name; any other Bot is `app`; any other User is
`human`. A User's write access is read from the repository's collaborator permission, `admin`, `maintain` or `write`,
never `triage`, once a login for the snapshot and never from an association (the Maintainer's ruling on #222); the
machine user's too, since its approval stands only while it can write. An App's is never asked. A User whose login
machine user's too, since its approval stands only while it can write. An App's write access is never asked; the same
read types a thread's resolver by its id where its comments cannot (decision 0067). A User whose login
ends `[bot]`, or is no login GitHub gives, is refused: the read was wrong.

The timeline and the reviews are read page by page, following the `link` header's next page, and are whole only when
Expand Down Expand Up @@ -64,19 +65,45 @@ class Accounts:

def __init__(self, transport, repo: str, approver: tuple, lenses: dict, factory=()):
self.transport, self.repo, self.approver = transport, repo, tuple(approver)
self.lenses, self.factory, self.access = dict(lenses), set(factory), {}
self.lenses, self.factory, self.answers = dict(lenses), set(factory), {}

def _can_write(self, login: str) -> bool:
if login not in self.access:
def _permission(self, login: str) -> dict:
"""The repo's permission answer for `login`, read once a login for all the reader serves."""
if login not in self.answers:
path = "/repos/%s/collaborators/%s/permission" % (self.repo, urllib.parse.quote(login, safe=""))
answer = self.transport.send(request("GET", path))
if answer["status"] != 200 or not isinstance(answer["body"], dict):
raise Unanswered("the permission of %s answered %d" % (login, answer["status"]))
level = answer["body"].get("permission")
if level not in PERMISSIONS:
raise Unanswered("a permission GitHub does not give: %r" % (level,))
self.access[login] = level in WRITE
return self.access[login]
# An answer that failed is kept too, one that never came among them, so a login is asked once however
# often it appears.
try:
answer = self.transport.send(request("GET", path))
except Unanswered:
answer = {"status": "no answer", "body": None}
ok = answer["status"] == 200 and isinstance(answer["body"], dict)
self.answers[login] = answer["body"] if ok else answer["status"]
if not isinstance(self.answers[login], dict):
raise Unanswered("the permission of %s answered %s" % (login, self.answers[login]))
return self.answers[login]

def _can_write(self, login: str) -> bool:
level = self._permission(login).get("permission")
if level not in PERMISSIONS:
raise Unanswered("a permission GitHub does not give: %r" % (level,))
return level in WRITE

def typed(self, login, ident):
"""The type, a User or a Bot, of the account `ident` by the repo's permission read of `login`, which answers
the account with its id and type (trials/P-53.json); None where that read fails, or answers another id, or no
type GitHub gives. The login only asks: what answers is held to the id."""
if not (isinstance(login, str) and login and isinstance(ident, int) and not isinstance(ident, bool)
and ident >= 1):
return None
try:
user = self._permission(login).get("user")
except Unanswered:
return None
if not isinstance(user, dict) or user.get("id") != ident or isinstance(user.get("id"), bool) \
or user.get("type") not in ("User", "Bot"):
return None
return user["type"]

@staticmethod
def account(raw_actor) -> tuple:
Expand Down
26 changes: 20 additions & 6 deletions controller/reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
MATCHES = {
"approver": {"approval": (APPLIED, "approve")},
"role": {"review": (APPLIED, "submit_review"), "comment": (APPLIED, "comment"),
"check_run": (APPLIED, "create_check_run")},
"check_run": (APPLIED, "create_check_run"), "resolution": (APPLIED, "resolve_thread")},
Comment thread
don-petry marked this conversation as resolved.
Resolved
"controller": {"check_run": (WRITTEN, "status_on_item"), "dequeue": (WRITTEN, "dequeue")},
}
# A write GitHub gives no key for, an event of the timeline, is matched by its item: each recorded once.
BY_ITEM = ("dequeue",)
DISPATCHES = ("dispatch_run", "dispatch_approval")
RESOLVE = "resolve_thread"
# A Decision that says the item had what a merge needs: an approval of its content standing, read by the Kernel.
MERGEABLE = ("converged", "queued")
LANDED = ("applied", "already_applied")
Expand All @@ -54,14 +55,21 @@ def _identities(accounts) -> tuple:
return found, known


def _keys(effects: dict) -> tuple:
"""`(keys, items)`: the key of every Effect recorded as landed, by where it was recorded and its type; and of a
write matched by its item, how many were recorded on each."""
def _keys(effects: dict, runs) -> tuple:
"""`(keys, items)`: the key of every Effect recorded as landed, by where it was recorded and its type, and a
thread's resolution by the Role whose Run recorded it too; and of a write matched by its item, how many were
recorded on each."""
keys, items = {}, {}
# A thread's id outlives its reopening, so one Role's recorded resolution must not cover another's: it is bound to
# the Role of the Run that recorded it, by the run records; one whose Run is not among them binds to none.
role_of = {r["run"]: r["role"] for r in runs} if isinstance(runs, list) else {}
for done in effects["applied"]:
for e in done["effects"]:
if e["state"] in LANDED and e["key"] is not None:
keys.setdefault((APPLIED, e["type"]), set()).add(e["key"])
if e["type"] == RESOLVE:
keys.setdefault((APPLIED, RESOLVE, role_of.get(done["run"])), set()).add(e["key"])
else:
keys.setdefault((APPLIED, e["type"]), set()).add(e["key"])
for done in effects["written"]:
for w in done["writes"]:
if w["state"] not in LANDED:
Expand Down Expand Up @@ -91,7 +99,7 @@ def reconcile(seen, effects, runs, before, accounts) -> dict:
readable = seen is not None and not errors(seen, "seen")
recorded = _readable(effects)
short = not (readable and seen["whole"] is True and recorded and effects.get("whole") is True and known)
keys, items = _keys(effects) if recorded else ({}, {})
keys, items = _keys(effects, runs) if recorded else ({}, {})
unmatched, override = [], []
for w in seen["writes"] if readable else []:
kind = identities.get(w["by"])
Expand All @@ -104,6 +112,12 @@ def reconcile(seen, effects, runs, before, accounts) -> dict:
slot = (where[1], key(w["item"]) if w["item"] else None)
landed = items.get(slot, 0) > 0
items[slot] = items.get(slot, 0) - 1
elif where == (APPLIED, RESOLVE) and not isinstance(runs, list):
# Run records not read whole bind no recorded resolution to its Role: what was seen is short, and the
# resolution is not called unmatched for it.
short, landed = True, True
elif where == (APPLIED, RESOLVE):
landed = w["key"] is not None and w["key"] in keys.get((APPLIED, RESOLVE, w["by"]), set())
Comment thread
don-petry marked this conversation as resolved.
Resolved
else:
landed = where is not None and w["key"] is not None and w["key"] in keys.get(where, set())
if not landed:
Expand Down
Loading
Loading