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
21 changes: 18 additions & 3 deletions .github/workflows/reports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,27 @@ jobs:
with:
python-version: "3.12"
# Org block reads need an App token; GITHUB_TOKEN can't see them.
# actions: read lists recent health checks, so a dead report doesn't queue a repeat.
- id: token
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-organization-user-blocking: read
permission-issues: read
permission-actions: read
# An API failure exits non-zero with no result file, so nothing is acted on.
# It also writes undecided.json, counted below; a rate limit that hasn't reset yet writes nothing.
# A dispatched issue that is already handled, or a bot's issue, exits 0 with no result file.
- env:
GITHUB_TOKEN: ${{ steps.token.outputs.token }}
EXCLUDED_USERS: ${{ vars.EXCLUDED_USERS }}
ISSUE_INPUT: ${{ inputs.issue }}
run: |
if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then
python -m scripts.reports decide --issue "$ISSUE_INPUT" --out result.json
python -m scripts.reports decide --issue "$ISSUE_INPUT" --out result.json --undecided undecided.json
else
python -m scripts.reports decide --event "$GITHUB_EVENT_PATH" --out result.json
python -m scripts.reports decide --event "$GITHUB_EVENT_PATH" --out result.json --undecided undecided.json
fi
# Its own token, so the one that reads issue text never carries actions: write.
- id: dispatch_token
Expand All @@ -71,7 +74,7 @@ jobs:
- if: hashFiles('result.json') != ''
env:
GITHUB_TOKEN: ${{ steps.dispatch_token.outputs.token }}
run: python -m scripts.reports dispatch --result result.json
run: python -m scripts.reports dispatch --result result.json --undecided undecided.json
- if: hashFiles('result.json') != ''
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
Expand All @@ -87,6 +90,18 @@ jobs:
print("has=" + ("true" if r["actions"] else "false"))
print("hide=" + ("true" if any(a["kind"] == "hide" for a in r["actions"]) else "false"))
PY
# After 3 runs without a decision the report is parked as needs-review, so the sweeper moves on.
- id: undecided_token
if: failure() && hashFiles('undecided.json') != ''
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-issues: write
- if: failure() && steps.undecided_token.outputs.token != ''
env:
GITHUB_TOKEN: ${{ steps.undecided_token.outputs.token }}
run: python -m scripts.reports undecided --file undecided.json

hide:
needs: decide
Expand Down
18 changes: 17 additions & 1 deletion .github/workflows/rot.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Health check
# The sweeper only counts runs named exactly "Health check" as a full check.
run-name: ${{ inputs.repo && 'Health check (one repo)' || 'Health check' }}
# A one-repo run names its repo, so reports.py can see a recent check of that entry.
run-name: "${{ inputs.repo && format('Health check: {0}', inputs.repo) || 'Health check' }}"

# Re-checks every listed repo each Monday: warns stale ones, archives them 30
# days later, brings back the ones that recover, follows renames and removes
Expand Down Expand Up @@ -54,6 +55,21 @@ jobs:
GITHUB_TOKEN: ${{ steps.token.outputs.token }}
REPO_INPUT: ${{ inputs.repo }}
run: python -m scripts.rot plan --out "$RUNNER_TEMP/plan.json" >> "$GITHUB_OUTPUT"
# Entries left undecided 3 weeks running get a needs-review issue. Its own token, so the one
# that can push never carries issues: write, and a failure here never stops the check.
- id: issues_token
if: steps.plan.outputs.review == 'true'
continue-on-error: true
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-issues: write
- if: steps.issues_token.outputs.token != ''
continue-on-error: true
env:
GITHUB_TOKEN: ${{ steps.issues_token.outputs.token }}
run: python -m scripts.rot review --plan "$RUNNER_TEMP/plan.json"
- id: pr
if: steps.plan.outputs.changed == 'true'
env:
Expand Down
18 changes: 16 additions & 2 deletions .github/workflows/submit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,31 @@ jobs:
permission-organization-user-blocking: read
permission-issues: read
# An API failure exits non-zero with no result file, so nothing is accepted or rejected.
# Any failure (an undecidable repo, an API error) also writes undecided.json, counted below.
# A rate limit that hasn't reset yet writes nothing, so waiting for it spends no attempt.
# A dispatched issue that is already decided exits 0 with no result file, so nothing runs.
- env:
GITHUB_TOKEN: ${{ steps.token.outputs.token }}
EXCLUDED_USERS: ${{ vars.EXCLUDED_USERS }}
ISSUE_INPUT: ${{ inputs.issue }}
run: |
if [ "$GITHUB_EVENT_NAME" = workflow_dispatch ]; then
python -m scripts.submit decide --issue "$ISSUE_INPUT" --out result.json
python -m scripts.submit decide --issue "$ISSUE_INPUT" --out result.json --undecided undecided.json
else
python -m scripts.submit decide --event "$GITHUB_EVENT_PATH" --out result.json
python -m scripts.submit decide --event "$GITHUB_EVENT_PATH" --out result.json --undecided undecided.json
fi
# After 3 runs without a decision the issue is parked as needs-review, so the sweeper moves on.
- id: undecided_token
if: failure() && hashFiles('undecided.json') != ''
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
permission-issues: write
- if: failure() && steps.undecided_token.outputs.token != ''
env:
GITHUB_TOKEN: ${{ steps.undecided_token.outputs.token }}
run: python -m scripts.submit undecided --file undecided.json
- if: hashFiles('result.json') != ''
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
Expand Down
59 changes: 59 additions & 0 deletions scripts/devstream/attempts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Bound how often an issue's decide run can fail, so one stuck issue can't hold the sweeper."""

import json
import re
import time

from scripts.devstream.gh import RateLimited

API = "/repos/devstreamhq/devstream"
BOT = "devstreamhq-bot[bot]"
MAX_FAILURES = 3
COUNTER = re.compile(r"<!-- devstream:undecided ([0-9]{1,4}) -->")
RETRYING = "We couldn't finish checking this one yet, so we'll try again shortly.\n\n<!-- devstream:undecided {} -->\n"
PARKED = "We couldn't finish checking this one, so it's waiting for a maintainer.\n\n<!-- devstream:undecided {} -->\n"


def spends_attempt(exc: BaseException) -> bool:
"""A rate limit still in force just waits for its reset; any other failure counts."""
return not (isinstance(exc, RateLimited) and exc.reset_at > time.time())


def note_failure(path: str, issue: int, exc: BaseException) -> None:
with open(path, "w", encoding="utf-8") as f:
json.dump({"issue": issue, "error": str(exc)}, f)


def record_failure(gh, n: int, error: str, error_body: str) -> bool:
"""Count one more failed run on issue n; from the MAX_FAILURES-th on, park it. True when parked.

The count lives in a hidden marker in the bot's own comment, so the issue's author can't forge it.
"""
mine = next((c for c in gh.paginate(f"{API}/issues/{n}/comments?per_page=100")
if (c.get("user") or {}).get("login") == BOT and COUNTER.search(c.get("body") or "")), None)
# Read-then-write, so two runs failing at once can count as one. That's fine: the sweeper
# dispatches one item at a time, and a lost count only costs one extra retry.
count = int(COUNTER.search(mine["body"]).group(1)) + 1 if mine else 1
body = (RETRYING if count < MAX_FAILURES else PARKED).format(count)
if mine:
gh.patch(f"{API}/issues/comments/{mine['id']}", {"body": body})
else:
gh.post(f"{API}/issues/{n}/comments", {"body": body})
if count < MAX_FAILURES:
return False
# The sweeper skips needs-review, so it moves on. Re-added each time, so a maintainer's retry
# that fails again can't bring the starvation back.
gh.post(f"{API}/issues/{n}/labels", {"labels": ["needs-review"]})
if count > MAX_FAILURES:
return True
detail = " ".join(error.replace("`", "'").split())[:300]
gh.post(f"{API}/issues/{n}/comments", {"body": error_body + f"\n`{detail}`\n"})
return True


def record_from_file(gh, path: str, error_body: str) -> None:
with open(path, encoding="utf-8") as f:
u = json.load(f)
if type(u["issue"]) is not int or u["issue"] <= 0:
raise SystemExit(f"bad issue number {u['issue']!r}")
record_failure(gh, u["issue"], str(u["error"]), error_body)
5 changes: 4 additions & 1 deletion scripts/devstream/gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
ATTEMPTS = 3
TRANSIENT = (502, 503, 504)
NETWORK = (urllib.error.URLError, TimeoutError, ConnectionError)
# Only a document that opens as a query is a read; anything else (a fragment first, say) may be a mutation.
LEADING_COMMENTS = re.compile(r"(\s*#[^\n]*(\n|$))*")
READ_QUERY = re.compile(r"\s*(query\b|\{)")


class GitHubError(Exception):
Expand Down Expand Up @@ -185,7 +188,7 @@ def graphql(self, query: str, variables: dict, allow_not_found: bool = False) ->
"""allow_not_found returns data with nulls where only NOT_FOUND errors occurred."""
self.last_redirect = None
r = self._request("POST", "/graphql", body={"query": query, "variables": variables},
read=not query.lstrip().startswith("mutation"))
read=bool(READ_QUERY.match(query, LEADING_COMMENTS.match(query).end())))
if r is None:
raise GitHubError(404, "graphql endpoint not found")
out = r[1] or {}
Expand Down
32 changes: 27 additions & 5 deletions scripts/devstream/license_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def match(name):
FILE_RE = _FileRe()
MAX_FILES = 5
MAX_BYTES = 100_000
# The blob comes down whole before any cap, so nothing larger is fetched at all.
MAX_BLOB = 5_000_000

NON_OSI = [
("Server Side Public License", "Server Side Public License"),
Expand Down Expand Up @@ -106,6 +108,10 @@ class Undecided(GitHubError):
pass


class TooLarge(Undecided):
pass


def _norm(text: str) -> str:
return re.sub(r"\s+", " ", re.sub("[\"\u201c\u201d]", "", text)).lower()

Expand Down Expand Up @@ -258,16 +264,32 @@ def _readme_license(readme: str, allowed: frozenset = frozenset()) -> tuple[str
return None, None


def file_bytes(gh, r, data: dict, what: str) -> bytes:
"""The bytes of a contents-API file. Undecided only when the content can't be decoded."""
if data.get("encoding") == "none" and data.get("sha"):
if (data.get("size") or 0) > MAX_BLOB:
raise TooLarge(0, f"{what} of {r} is over {MAX_BLOB // 1_000_000} MB")
# Over 1 MB the contents API leaves content empty; the blobs API serves up to 100 MB.
data = gh.get(f"/repos/{r}/git/blobs/{data['sha']}")
if data is None:
raise GitHubError(404, f"blob for {what} of {r} not found")
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)
except (binascii.Error, ValueError) as e:
raise Undecided(0, f"{what} of {r} could not be decoded: {e}") from e


def _read_file(gh, r, path) -> str:
data = gh.get(f"/repos/{r}/contents/{path}")
if not data:
return ""
if data.get("encoding") != "base64":
raise Undecided(0, f"{path} of {r} is too large to read")
try:
raw = base64.b64decode("".join((data.get("content") or "").split()), validate=False)
except (binascii.Error, ValueError) as e:
raise Undecided(0, f"{path} of {r} could not be decoded: {e}") from e
raw = file_bytes(gh, r, data, path)
except TooLarge:
# No license text, so the rule fails closed; rot's second sighting still guards a removal.
return ""
return raw[:MAX_BYTES].decode("utf-8", errors="replace")


Expand Down
16 changes: 4 additions & 12 deletions scripts/devstream/quality.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import base64
import binascii
import json
import re
from dataclasses import dataclass, field
Expand All @@ -8,7 +6,7 @@
from typing import Literal

from scripts.devstream.gh import GitHubError
from scripts.devstream.license_detect import OSI, Undecided, find_license
from scripts.devstream.license_detect import OSI, Undecided, file_bytes, find_license

NO_LICENSE = {None, "NOASSERTION", "NONE"}
DEPRECATED = re.compile(r"\b(deprecated|unmaintained|no longer maintained)\b", re.I)
Expand Down Expand Up @@ -87,18 +85,12 @@ def _license(repo, gh, r, readme_text, allowed=frozenset(), mode="submit") -> st
return MSG_NOT_FOUND_CC if cc_only and mode == "submit" else MSG_NOT_FOUND


def _readme(gh, r) -> tuple[str | None, int] | None:
"""None for no README, else (text, length). text is None when GitHub withholds it (over 1 MB)."""
def _readme(gh, r) -> tuple[str, int] | None:
"""None for no README, else (text, length)."""
data = gh.get(f"/repos/{r}/readme")
if data is None:
return None
if data.get("encoding") == "none":
return None, data.get("size") or 0
try:
raw = base64.b64decode("".join((data.get("content") or "").split()), validate=False)
except (binascii.Error, ValueError) as e:
raise Undecided(0, f"README of {r} could not be decoded: {e}") from e
text = raw.decode("utf-8", errors="replace")[:README_SCAN]
text = file_bytes(gh, r, data, "README").decode("utf-8", errors="replace")[:README_SCAN]
return text, _readme_text_length(text)


Expand Down
Loading
Loading