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
3 changes: 2 additions & 1 deletion scripts/devstream/license_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ def file_bytes(gh, r, data: dict, what: str) -> bytes:
if data.get("encoding") != "base64":
raise Undecided(0, f"{what} of {r} has no readable content")
try:
return base64.b64decode("".join((data.get("content") or "").split()), validate=False)
# validate=True: otherwise stray characters are dropped and garbage decodes as a short file.
return base64.b64decode("".join((data.get("content") or "").split()), validate=True)
except (binascii.Error, ValueError) as e:
raise Undecided(0, f"{what} of {r} could not be decoded: {e}") from e

Expand Down
37 changes: 31 additions & 6 deletions scripts/devstream/quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ class RepoResult:
def _readme_text_length(text: str) -> int:
text = text[:README_SCAN] # also bounds the regex work below
text = re.sub(r"```.*?(```|\Z)", " ", text, flags=re.S)
text = re.sub(r"^\s{0,3}#.*$", " ", text, flags=re.M)
# ATX headings need a space (or nothing) after the #s, so a #hashtag line is prose.
text = re.sub(r"^ {0,3}#{1,6}(?:[ \t].*)?$", " ", text, flags=re.M)
# Setext headings: a line underlined with === or ---.
# The lookahead scans each line once; [^\n]*\S[^\n]* would backtrack on a long blank line.
text = re.sub(r"^(?=[^\n]*\S)[^\n]*\n {0,3}(?:=+|-+)[ \t]*$", " ", text, flags=re.M)
text = re.sub(r"<[^>]{0,2000}>", " ", text)
text = re.sub(r"!\[[^\]]{0,500}\]\([^)]{0,2000}\)", " ", text)
text = re.sub(r"\[([^\]]{0,500})\]\([^)]{0,2000}\)", r"\1", text)
Expand Down Expand Up @@ -102,6 +106,25 @@ def _fork_diverged(repo, gh) -> bool:
return cmp_ is not None and cmp_.get("ahead_by", 0) > 0


def _community_files(gh, r) -> dict[str, bool]:
"""Which community files exist. The profile 404s for forks, so then look where GitHub would."""
profile = gh.get(f"/repos/{r}/community/profile")
if profile is not None:
return {k: v is not None for k, v in (profile.get("files") or {}).items()}
names = set()
for d in ("", "/.github", "/docs"):
listing = gh.get(f"/repos/{r}/contents{d}")
if isinstance(listing, list):
names |= {(f.get("name") or "").lower() for f in listing}

def has(stem):
return any(n == stem or n.startswith(stem + ".") for n in names)
return {k: has(k) for k in ("contributing", "code_of_conduct", "issue_template", "pull_request_template")}


EXTRAS = 7


def _extras_missing(repo, gh, r) -> list[str]:
"""Empty list when 2+ extras exist. Cheap checks first; stops at 2."""
found = 0
Expand All @@ -117,11 +140,11 @@ def tally(ok, label):

if tally(len(repo.get("topics") or []) >= 1, "a topic or two"):
return []
files = (gh.get(f"/repos/{r}/community/profile") or {}).get("files") or {}
files = _community_files(gh, r)
checks = [
(files.get("contributing") is not None, "a CONTRIBUTING guide"),
(files.get("code_of_conduct") is not None, "a code of conduct"),
(files.get("issue_template") is not None or files.get("pull_request_template") is not None,
(files.get("contributing", False), "a CONTRIBUTING guide"),
(files.get("code_of_conduct", False), "a code of conduct"),
(files.get("issue_template", False) or files.get("pull_request_template", False),
"an issue or pull request template"),
]
for ok, label in checks:
Expand Down Expand Up @@ -214,5 +237,7 @@ def check_repo(full: str, gh, now: datetime, mode: Literal["submit", "rot"],
failures.append(MSG_DEPRECATED.format(deprecated))
missing = _extras_missing(repo, gh, r)
if missing:
failures.append("Add at least two of these to the repo: " + ", ".join(missing) + ".")
# Only a full miss reaches here, so every extra was checked and the rest were found.
need = "one more" if EXTRAS - len(missing) == 1 else "at least two"
failures.append(f"Add {need} of these to the repo: " + ", ".join(missing) + ".")
return RepoResult(True, r, failures, "ok", meta, repo.get("id"))
40 changes: 34 additions & 6 deletions scripts/tests/test_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ def test_readme_noise_not_counted(self):
self.assertEqual(len(run(gh(readme=noise)).failures), 1)
self.assertEqual(run(gh(readme=noise + "x" * 300)).failures, [])

def test_readme_hashtags_are_prose_and_setext_headings_are_not(self):
tags = "\n".join(["#rustlang #cli #tools #terminal #productivity"] * 7)
self.assertGreater(len(tags), 300)
self.assertEqual(run(gh(readme=tags)).failures, [])
setext = "\n".join([f"A heading that is long enough number {i}\n=====" for i in range(8)] +
[f"Another long subheading number {i}\n---" for i in range(4)])
self.assertGreater(len(setext), 300)
self.assertEqual(len(run(gh(readme=setext)).failures), 1)
self.assertEqual(len(run(gh(readme="#\n## Usage\n### " + "x" * 400)).failures), 1)

def test_readme_exactly_300(self):
self.assertEqual(run(gh(readme="a" * 300)).failures, [])
self.assertEqual(len(run(gh(readme="a" * 299)).failures), 1)
Expand All @@ -189,8 +199,11 @@ def test_extras_counts_two_needed(self):
r = run(none)
self.assertEqual(len(r.failures), 1)
self.assertIn("CONTRIBUTING", r.failures[0])
# one extra is not enough
self.assertEqual(len(run(gh(topics=(), coc=False)).failures), 1)
self.assertIn("Add at least two of these", r.failures[0])
# one extra is not enough, and the message says one more is
r = run(gh(topics=(), coc=False))
self.assertEqual(len(r.failures), 1)
self.assertTrue(r.failures[0].startswith("Add one more of these to the repo: a topic or two, a code of conduct"))
# two is
self.assertEqual(run(gh(topics=(), coc=False, contributing=True, extra={f"GET {R}/releases?per_page=1": [{}]})).failures, [])

Expand All @@ -208,6 +221,19 @@ def test_extras_each_kind(self):
r = run(gh(**base, extra=extra))
self.assertEqual(len(r.failures), 1, name)

def test_extras_without_a_community_profile(self):
# Forks have no community profile (404), so the files are looked up directly.
base = dict(topics=(), extra={f"GET {R}/community/profile": None})
self.assertEqual(len(run(gh(**base)).failures), 1)
listings = {f"GET {R}/contents": [{"name": "README.md"}, {"name": "CONTRIBUTING.md"}],
f"GET {R}/contents/.github": [{"name": "ISSUE_TEMPLATE", "type": "dir"}]}
g = gh(topics=(), extra={f"GET {R}/community/profile": None, **listings})
self.assertEqual(run(g).failures, [])
listings = {f"GET {R}/contents/docs": [{"name": "code_of_conduct.md"}, {"name": "contributing-notes.md"}]}
r = run(gh(topics=(), extra={f"GET {R}/community/profile": None, **listings}))
self.assertEqual(len(r.failures), 1)
self.assertIn("one more", r.failures[0])

def test_extras_short_circuit(self):
g = gh() # topics + contributing already = 2
run(g)
Expand Down Expand Up @@ -349,10 +375,12 @@ def test_readme_encoding_none_without_sha_is_undecided(self):
run(gh(extra={f"GET {R}/readme": bad}))

def test_bad_base64_is_undecided(self):
bad = {"encoding": "base64", "content": "abcde"}
for mode in ("submit", "rot"):
with self.assertRaises(Undecided):
run(gh(extra={f"GET {R}/readme": bad}), mode)
# "!!!!" used to be dropped silently and read as an empty, short README.
for content in ("abcde", "!!!!", b64(README)["content"] + "!!!!"):
bad = {"encoding": "base64", "content": content}
for mode in ("submit", "rot"):
with self.assertRaises(Undecided, msg=content):
run(gh(extra={f"GET {R}/readme": bad}), mode)

def test_null_pushed_at(self):
r = run(gh({"pushed_at": None}))
Expand Down
Loading