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
2 changes: 2 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Each source receipt names the repo, UTC check time, verified-public flag,
status (complete/partial/unavailable/excluded) and bounded gap descriptions.
Only a successful repository metadata response with `private: false` permits
collection. Failed metadata reads and private repositories publish no rows.
A metadata read that answers 404 (a source the collector cannot see: private
to its token, or missing) is `excluded`, not `unavailable`.
URLs are constructed from validated identities, never accepted from source
text. Snapshot inputs are trusted local evidence envelopes, not proof of API
authenticity; validate their source before using them for public publication.
Expand Down
14 changes: 14 additions & 0 deletions ears/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class ReadError(Exception):
"""Bounded public description, never an API payload or secret."""


class NotFound(ReadError):
"""The source answered 404: private to this token, renamed or missing."""


class GitHub:
def __init__(self, binary="gh"):
self.binary = binary
Expand All @@ -99,6 +103,10 @@ def get(self, endpoint):
except (OSError, subprocess.TimeoutExpired):
raise ReadError("GitHub client unavailable or timed out") from None
if r.returncode:
# gh reports "HTTP 404" on stderr; GitHub answers 404, not 403, for
# a private repository the token cannot see.
if "HTTP 404" in (r.stderr or ""):
raise NotFound("source not found or not visible publicly")
raise ReadError("GitHub read failed (permissions, rate limit or endpoint)")
try:
return json.loads(r.stdout)
Expand Down Expand Up @@ -504,6 +512,12 @@ def collect(api, repo_homes, config, pending=None):
receipt["status"] = "excluded"
receipt["gaps"] = ["source is not verified public"]
continue
except NotFound:
# Private to the collector's token (or missing): not public, so
# excluded rather than a collection failure.
receipt["status"] = "excluded"
receipt["gaps"] = ["source is not visible publicly (private or missing)"]
continue
except ReadError as exc:
receipt["gaps"] = [str(exc)]
continue
Expand Down
32 changes: 32 additions & 0 deletions tests/test_ears.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ def test_public_only_and_failed_metadata_never_leak_titles():
assert {r["status"] for r in s["receipts"]} == {"complete", "excluded", "unavailable"}


def test_invisible_source_is_excluded_but_other_failures_stay_unavailable():
api = API({"repos/hidden/lib": collect.NotFound("source not found or not visible publicly"),
"repos/limited/lib": collect.ReadError("GitHub read failed (permissions, rate limit or endpoint)"),
listing("hidden/lib"): [thread(title="HIDDEN TITLE")]})
s = collect.collect(api, ["hidden/lib", "limited/lib"], CONFIG)
receipts = {r["repo"]: r for r in s["receipts"]}
hidden, limited = receipts["hidden/lib"], receipts["limited/lib"]
assert hidden["status"] == "excluded" and hidden["public_verified"] is False
assert hidden["gaps"] == ["source is not visible publicly (private or missing)"]
assert limited["status"] == "unavailable" and limited["public_verified"] is False
assert not any(c.startswith(("repos/hidden/lib/", "repos/limited/lib/")) for c in api.calls)
assert "HIDDEN TITLE" not in json.dumps(s)
collect.validate(s)


def test_pagination_and_oldest_conversation_beyond_thirty():
api = API({listing(): [thread(n) for n in range(1, 101)],
listing(page=2): [thread(101, created_at="2020-01-01T00:00:00Z")]})
Expand Down Expand Up @@ -236,6 +251,23 @@ def run(args, **kw):
collect.GitHub().get("https://example.invalid/upload")


@pytest.mark.parametrize("stderr, error", [
("gh: Not Found (HTTP 404)\n", collect.NotFound),
("gh: API rate limit exceeded (HTTP 403)\n", collect.ReadError),
("gh: Server Error (HTTP 502)\n", collect.ReadError),
(None, collect.ReadError),
])
def test_gh_adapter_classifies_not_found(monkeypatch, stderr, error):
payload = '{"message": "SECRET PAYLOAD"}'
def run(args, **kw):
return type("Result", (), {"returncode": 1, "stdout": payload, "stderr": stderr})()
monkeypatch.setattr(collect.subprocess, "run", run)
with pytest.raises(collect.ReadError) as caught:
collect.GitHub().get("repos/example/lib")
assert type(caught.value) is error
assert "SECRET" not in str(caught.value)


def test_bot_pr_requested_review_is_not_lost():
pr = thread(login="bot[bot]", pull_request={"url": "unused"})
pr["user"]["type"] = "Bot"
Expand Down
Loading