@@ -309,7 +309,46 @@ def build_site(org: str, token: str | None) -> tuple[dict, list[tuple[str, list[
309309 return org_meta , ordered
310310
311311
312- def assemble (org_meta : dict , categorized : list [tuple [str , list [dict ]]]) -> str :
312+ def detect_community_url (org_login : str , categorized : list [tuple [str , list [dict ]]], token : str | None ) -> str | None :
313+ """Pick the right 'community' destination.
314+
315+ Order of preference:
316+ 1. org-level GitHub Discussions is actually reachable
317+ (probe by HEAD request, not by the org API field, which lags).
318+ 2. a repo was bucketed into 'community' -> that repo's html_url.
319+ 3. None (caller should drop the link).
320+ """
321+ disc_url = f"https://github.com/orgs/{ org_login } /discussions"
322+ try :
323+ head = urllib .request .Request (disc_url , method = "HEAD" , headers = {
324+ "User-Agent" : USER_AGENT ,
325+ "Accept" : "text/html" ,
326+ })
327+ if token :
328+ head .add_header ("Authorization" , f"Bearer { token } " )
329+ with urllib .request .urlopen (head , timeout = 15 ) as resp :
330+ # The GitHub discussions page returns 200 when enabled.
331+ if resp .status == 200 :
332+ return disc_url
333+ except (urllib .error .HTTPError , urllib .error .URLError , TimeoutError , OSError ):
334+ pass
335+
336+ for cat_key , items in categorized :
337+ if cat_key == "community" and items :
338+ url = items [0 ].get ("html_url" )
339+ if isinstance (url , str ) and url :
340+ return url
341+ return None
342+
343+
344+ def first_section_anchor (categorized : list [tuple [str , list [dict ]]]) -> str | None :
345+ """Anchor of the first category section that actually renders."""
346+ for cat_key , _ in categorized :
347+ return f"#{ cat_key } "
348+ return None
349+
350+
351+ def assemble (org_meta : dict , categorized : list [tuple [str , list [dict ]]], token : str | None ) -> str :
313352 if not TEMPLATE .exists ():
314353 raise SystemExit (f"Missing template: { TEMPLATE } " )
315354 tpl = TEMPLATE .read_text (encoding = "utf-8" )
@@ -318,20 +357,37 @@ def assemble(org_meta: dict, categorized: list[tuple[str, list[dict]]]) -> str:
318357
319358 title = org_meta .get ("name" ) or org_meta .get ("login" ) or ""
320359 tagline = org_meta .get ("description" ) or ""
360+ org_login = org_meta .get ("login" ) or ""
321361
322362 section_blocks = [render_category (k , items ) for k , items in categorized ]
323363 projects_html = "\n " .join (b for b in section_blocks if b )
324364 stats_html = render_stats (all_repos )
325365 marquee_html = render_marquee (all_repos )
326366
367+ cta_href = first_section_anchor (categorized ) or "#"
368+ community_url = detect_community_url (org_login , categorized , token )
369+
327370 tpl = tpl .replace ("{{site_title}}" , esc (title ))
328371 tpl = tpl .replace ("{{site_tagline}}" , esc (tagline ))
372+ tpl = tpl .replace ("{{hero_cta_href}}" , esc (cta_href ))
373+ tpl = tpl .replace ("{{community_url}}" , esc (community_url or "" ))
329374 if "{{projects}}" not in tpl :
330375 raise SystemExit ("Template missing {{projects}} placeholder" )
331376 tpl = tpl .replace ("{{projects}}" , projects_html )
332377 for ph , body in (("{{stats}}" , stats_html ), ("{{marquee}}" , marquee_html )):
333378 if ph in tpl :
334379 tpl = tpl .replace (ph , body )
380+
381+ # If there is no community destination at all, strip the placeholder
382+ # anchors (left as empty href) by removing the whole CTA / footer
383+ # community link if its href is empty.
384+ if not community_url :
385+ tpl = re .sub (
386+ r'<a href="" target="_blank"[^>]*>(?:<span>)?(?:Join discussions|Discussions)(?:</span>)?</a>' ,
387+ "" ,
388+ tpl ,
389+ )
390+
335391 return tpl
336392
337393
@@ -347,7 +403,7 @@ def main(argv: list[str] | None = None) -> int:
347403
348404 token = os .environ .get ("GH_TOKEN" ) or os .environ .get ("GITHUB_TOKEN" )
349405 org_meta , categorized = build_site (args .org , token )
350- rendered = assemble (org_meta , categorized )
406+ rendered = assemble (org_meta , categorized , token )
351407
352408 if args .stdout :
353409 sys .stdout .buffer .write (rendered .encode ("utf-8" ))
0 commit comments