diff --git a/README.md b/README.md index 1b45045..aa4cf5d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ your account, charts your contribution timeline, highlights your top collaborators, breaks down your languages, tracks your commit streaks, and earns you tags like `Backend`, `ML`, or `Founder #42`. +[![example card](https://gh-stats.com/api/shaymanor)](https://gh-stats.com) + +That card is live, not a screenshot. Swap in your own username: + ```md ![my stats](https://gh-stats.com/api/YOUR_USERNAME) ``` diff --git a/generator/frontend/src/screens/ResultScreen.tsx b/generator/frontend/src/screens/ResultScreen.tsx index d32435d..0776058 100644 --- a/generator/frontend/src/screens/ResultScreen.tsx +++ b/generator/frontend/src/screens/ResultScreen.tsx @@ -34,6 +34,12 @@ export function ResultScreen({ username, generating, generatedSvg, generateError const fallbackUrl = `${window.location.origin}/api/${encodeURIComponent(username)}` const finalUrl = embedUrl || fallbackUrl const embedSnippet = `[![${username}](${finalUrl})](${window.location.origin})` + // The snippet is useless without somewhere to paste it: GitHub only renders a + // profile README from the repo named exactly after the account. Most people + // arriving here don't have that repo yet, so offer both paths. + const profileRepo = encodeURIComponent(username) + const editReadmeUrl = `https://github.com/${profileRepo}/${profileRepo}/edit/main/README.md` + const createRepoUrl = `https://github.com/new?name=${profileRepo}&description=${encodeURIComponent('My GitHub profile')}&visibility=public` const copy = async () => { try { @@ -148,6 +154,12 @@ export function ResultScreen({ username, generating, generatedSvg, generateError )} +

+ Paste it into {username}/{username}, the repo GitHub shows on your profile.{' '} + Open your README + {' \u00b7 '} + Create the repo +

)} diff --git a/generator/src/api.py b/generator/src/api.py index 06fa7a3..9b35fae 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -709,10 +709,20 @@ def internal_analytics_events(): return jsonify({"ingested": n}) +# Literal placeholders from the README snippets. Lowercased because the +# username normaliser lowercases before this is reached. +_DOC_PLACEHOLDERS = {"your_username", "yourusername", "your-username", "username"} + + @app.route("/api/", methods=["GET"]) @track_request("composite") @rate_limited("read") def get_widget(username: str): + if username.lower() in _DOC_PLACEHOLDERS: + # People paste the README snippet without swapping YOUR_USERNAME for + # their own name, so GitHub renders a broken image on their profile. + # A card that says what to do is more use to them than a JSON 400. + return _placeholder_response("unsubstituted", username) if not is_valid_username(username): return jsonify({"error": "invalid_username"}), 400 return _serve(username, widget_name="composite") diff --git a/generator/src/placeholder.py b/generator/src/placeholder.py index 5b60f25..65fa92f 100644 --- a/generator/src/placeholder.py +++ b/generator/src/placeholder.py @@ -10,6 +10,7 @@ _MESSAGES = { "building": ("Building @{u}'s widget\u2026", "This usually takes under a minute."), "rate_limited": ("Too many new users today", "Try again tomorrow."), "not_found": ("GitHub user @{u} doesn't exist", "Check the spelling of the username."), + "unsubstituted": ("Replace YOUR_USERNAME in the URL", "Put your own GitHub username there, e.g. /api/octocat."), } diff --git a/generator/tests/test_api.py b/generator/tests/test_api.py index 7479d8e..935a453 100644 --- a/generator/tests/test_api.py +++ b/generator/tests/test_api.py @@ -458,6 +458,20 @@ def test_data_unknown_user_enrolls_when_under_both_caps(client, monkeypatch): assert dbmod.get_settings("testuser") is not None +# ---- README snippet pasted without substituting the username ---- + + +@pytest.mark.parametrize("name", ["YOUR_USERNAME", "your_username", "yourusername", "username"]) +def test_unsubstituted_placeholder_returns_card_not_400(client, name): + """The README snippet uses YOUR_USERNAME. People paste it as-is, which + renders a broken image on their profile. Serve instructions instead.""" + r = client.get(f"/api/{name}") + assert r.status_code == 200 + assert r.mimetype == "image/svg+xml" + assert r.headers["X-Widget-Status"] == "unsubstituted" + assert b"Replace YOUR_USERNAME" in r.data + + # ---- /api/top-langs compat shim for upstream github-readme-stats URLs ---- diff --git a/generator/tests/test_attribution.py b/generator/tests/test_attribution.py index 4563559..dabc5df 100644 --- a/generator/tests/test_attribution.py +++ b/generator/tests/test_attribution.py @@ -105,7 +105,7 @@ def test_attribution_is_legible_and_consistent(name): assert node.get("text-anchor") == "middle" -@pytest.mark.parametrize("variant", ["building", "rate_limited", "not_found"]) +@pytest.mark.parametrize("variant", ["building", "rate_limited", "not_found", "unsubstituted"]) def test_placeholder_cards_have_attribution(variant): svg = placeholder.render(variant, "alice", theme="dark") assert "Generated with gh-stats.com" in svg