Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: Misc fixes to "Report an issue" in the documentation functionality. #64788

Merged
merged 7 commits into from
Nov 9, 2023
Merged
52 changes: 42 additions & 10 deletions doc/_extensions/zephyr/gh_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,44 @@
import os
import re
import subprocess
import sys
from datetime import datetime
from pathlib import Path
from textwrap import dedent
from typing import Optional, Tuple
from typing import Final, Optional, Tuple
from urllib.parse import quote

from sphinx.application import Sphinx
from sphinx.util.i18n import format_date

ZEPHYR_BASE : Final[str] = Path(__file__).parents[3]
SCRIPTS : Final[str] = ZEPHYR_BASE / "scripts"
sys.path.insert(0, str(SCRIPTS))

from get_maintainer import Maintainers
Thalley marked this conversation as resolved.
Show resolved Hide resolved

MAINTAINERS : Final[Maintainers] = Maintainers()


__version__ = "0.1.0"


def get_page_prefix(app: Sphinx, pagename: str) -> str:
if not os.path.isfile(app.env.project.doc2path(pagename)):
"""Return the prefix that needs to be added to the page path to get its location in the
repository.

If pagename refers to a page that is automatically generated by Sphinx or if it matches one of
the patterns in ``gh_link_exclude`` configuration option, return None.

Args:
app: Sphinx instance.
pagename: Page name (path).

Returns:
Prefix if applicable, None otherwise.
"""

if not os.path.isfile(app.env.doc2path(pagename)):
Thalley marked this conversation as resolved.
Show resolved Hide resolved
return None

for exclude in app.config.gh_link_exclude:
Expand Down Expand Up @@ -80,7 +103,7 @@ def gh_link_get_url(app: Sphinx, pagename: str, mode: str = "blob") -> Optional[
"""

page_prefix = get_page_prefix(app, pagename)
if not page_prefix:
if page_prefix is None:
return None

return "/".join(
Expand All @@ -89,7 +112,7 @@ def gh_link_get_url(app: Sphinx, pagename: str, mode: str = "blob") -> Optional[
mode,
app.config.gh_link_version,
page_prefix,
app.env.project.doc2path(pagename, basedir=False),
app.env.doc2path(pagename, False),
]
)

Expand All @@ -106,11 +129,21 @@ def gh_link_get_open_issue_url(app: Sphinx, pagename: str, sha1: str) -> Optiona
URL to open a new issue if applicable, None otherwise.
"""

if not os.path.isfile(app.env.project.doc2path(pagename)):
page_prefix = get_page_prefix(app, pagename)
if page_prefix is None:
return None

title = quote(f"[doc] Documentation issue in '{pagename}'")
rel_path = os.path.join(
os.path.relpath(ZEPHYR_BASE),
page_prefix,
app.env.doc2path(pagename, False),
)
Thalley marked this conversation as resolved.
Show resolved Hide resolved

title = quote(f"doc: Documentation issue in '{pagename}'")
labels = quote("area: Documentation")
areas = MAINTAINERS.path2areas(rel_path)
if areas:
labels += "," + ",".join([label for area in areas for label in area.labels])
body = quote(
dedent(
f"""\
Expand Down Expand Up @@ -144,13 +177,13 @@ def git_info_filter(app: Sphinx, pagename) -> Optional[Tuple[str, str]]:
"""

page_prefix = get_page_prefix(app, pagename)
if not page_prefix:
if page_prefix is None:
return None

orig_path = os.path.join(
app.config.ZEPHYR_BASE,
ZEPHYR_BASE,
page_prefix,
app.env.project.doc2path(pagename, basedir=False),
app.env.doc2path(pagename, False),
)

try:
Expand Down Expand Up @@ -200,7 +233,6 @@ def add_jinja_filter(app: Sphinx):


def setup(app: Sphinx):
app.add_config_value("ZEPHYR_BASE", Path(__file__).resolve().parents[3], "html")
app.add_config_value("gh_link_version", "", "")
app.add_config_value("gh_link_base_url", "", "")
app.add_config_value("gh_link_prefixes", {}, "")
Expand Down
2 changes: 1 addition & 1 deletion doc/_templates/breadcrumbs.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{%- set git_last_updated, sha1 = pagename | git_info | default((None, None), true) %}
{%- if sha1 %}
<a href="{{ pagename | gh_link_get_open_issue_url(sha1) }}" class="fa fa-bug">
{{ _('Report an issue')}}
{{ _('Report an issue with this page')}}
</a>
{% endif %}
{% endif %}
Expand Down