1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
#!/usr/bin/env python3
#
# issues.py
"""
Roles and nodes for GitHub issues and Pull Requests.
"""
#
# Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# Based on pyspecific.py from the Python documentation.
# Copyright 2008-2014 by Georg Brandl.
# Licensed under the PSF License 2.0
#
# Parts of the docstrings based on https://docutils.sourceforge.io/docs/howto/rst-roles.html
#
# stdlib
import warnings
import xml.sax.saxutils
from typing import Any, Dict, List, Optional, Tuple, Union
# 3rd party
import requests # nodep
from apeye.url import URL
from bs4 import BeautifulSoup
from docutils import nodes
from docutils.nodes import system_message
from docutils.parsers.rst.states import Inliner
from sphinx.util.nodes import split_explicit_title
from sphinx.writers.html5 import HTML5Translator
from sphinx.writers.latex import LaTeXTranslator
# this package
from sphinx_toolbox.cache import cache
from sphinx_toolbox.utils import make_github_url
__all__ = (
"IssueNode",
"IssueNodeWithName",
"issue_role",
"pull_role",
"visit_issue_node",
"depart_issue_node",
"get_issue_title",
)
class IssueNode(nodes.reference):
"""
Docutils Node to represent a link to a GitHub *Issue* or *Pull Request*.
:param issue_number: The number of the issue or pull request.
:param refuri: The URL of the issue / pull request on GitHub.
"""
has_tooltip: bool
issue_number: int
issue_url: str
def __init__(
self,
issue_number: Union[str, int],
refuri: Union[str, URL],
**kwargs,
):
self.has_tooltip = False
self.issue_number = int(issue_number)
self.issue_url = str(refuri)
source = f"#{issue_number}"
super().__init__(source, source, refuri=self.issue_url)
@property
def _copy_kwargs(self): # pragma: no cover # noqa: MAN002
return {"issue_number": self.issue_number, "refuri": self.issue_url}
def copy(self) -> "IssueNode": # pragma: no cover
"""
Return a copy of the :class:`sphinx_toolbox.github.issues.IssueNode`.
"""
# This was required to stop some breakage, but it doesn't seem to run during the tests.
obj = self.__class__(**self._copy_kwargs)
obj.document = self.document
obj.has_tooltip = self.has_tooltip
obj.line = self.line
return obj
class IssueNodeWithName(IssueNode):
"""
Docutils Node to represent a link to a GitHub *Issue* or *Pull Request*, with the repository name shown.
.. versionadded:: 2.4.0
:param repo_name: The full name of the repository, in the form ``owner/name``.
:param issue_number: The number of the issue or pull request.
:param refuri: The URL of the issue / pull request on GitHub.
"""
repo_name: str
def __init__(
self,
repo_name: str,
issue_number: Union[str, int],
refuri: Union[str, URL],
**kwargs,
):
self.has_tooltip = False
self.issue_number = int(issue_number)
self.issue_url = str(refuri)
self.repo_name = str(repo_name)
source = f"{repo_name}#{issue_number}"
nodes.reference.__init__(self, source, source, refuri=self.issue_url)
@property
def _copy_kwargs(self) -> Dict[str, Any]: # pragma: no cover
return {"repo_name": self.repo_name, "issue_number": self.issue_number, "refuri": self.issue_url}
def issue_role(
name: str,
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
options: Dict[str, Any] = {},
content: List[str] = []
) -> Tuple[List[IssueNode], List[system_message]]:
"""
Adds a link to the given issue on GitHub.
:param name: The local name of the interpreted role, the role name actually used in the document.
:param rawtext: A string containing the entire interpreted text input, including the role and markup.
:param text: The interpreted text content.
:param lineno: The line number where the interpreted text begins.
:param inliner: The :class:`docutils.parsers.rst.states.Inliner` object that called :func:`~.issue_role`.
It contains the several attributes useful for error reporting and document tree access.
:param options: A dictionary of directive options for customization (from the ``role`` directive),
to be interpreted by the function.
Used for additional attributes for the generated elements and other functionality.
:param content: A list of strings, the directive content for customization (from the ``role`` directive).
To be interpreted by the function.
:return: A list containing the created node, and a list containing any messages generated during the function.
.. latex:clearpage::
"""
has_t, issue_number, repository = split_explicit_title(text)
issue_number = nodes.unescape(issue_number)
messages: List[system_message] = []
refnode: IssueNode
if has_t:
repository_parts = nodes.unescape(repository).split('/')
if len(repository_parts) != 2:
warning_message = inliner.document.reporter.warning(
f"Invalid repository '{repository}' for issue #{issue_number}.",
)
messages.append(warning_message)
else:
refnode = IssueNodeWithName(
repo_name=repository,
issue_number=issue_number,
refuri=make_github_url(*repository_parts) / "issues" / str(int(issue_number)),
)
return [refnode], messages
issues_url = inliner.document.settings.env.app.config.github_issues_url
refnode = IssueNode(issue_number=issue_number, refuri=issues_url / str(int(issue_number)))
return [refnode], messages
def pull_role(
name: str,
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
options: Dict[str, Any] = {},
content: List[str] = []
) -> Tuple[List[IssueNode], List[system_message]]:
"""
Adds a link to the given pulll request on GitHub.
:param name: The local name of the interpreted role, the role name actually used in the document.
:param rawtext: A string containing the entire interpreted text input, including the role and markup.
:param text: The interpreted text content.
:param lineno: The line number where the interpreted text begins.
:param inliner: The :class:`docutils.parsers.rst.states.Inliner` object that called :func:`~.pull_role`.
It contains the several attributes useful for error reporting and document tree access.
:param options: A dictionary of directive options for customization (from the ``role`` directive),
to be interpreted by the function.
Used for additional attributes for the generated elements and other functionality.
:param content: A list of strings, the directive content for customization (from the ``role`` directive).
To be interpreted by the function.
:return: A list containing the created node, and a list containing any messages generated during the function.
"""
has_t, issue_number, repository = split_explicit_title(text)
issue_number = nodes.unescape(issue_number)
messages: List[system_message] = []
refnode: IssueNode
if has_t:
repository_parts = nodes.unescape(repository).split('/')
if len(repository_parts) != 2:
warning_message = inliner.document.reporter.warning(
f"Invalid repository '{repository}' for pull request #{issue_number}."
)
messages.append(warning_message)
else:
refnode = IssueNodeWithName(
repo_name=repository,
issue_number=issue_number,
refuri=make_github_url(*repository_parts) / "pull" / str(int(issue_number)),
)
return [refnode], messages
pull_url = inliner.document.settings.env.app.config.github_pull_url
refnode = IssueNode(issue_number=issue_number, refuri=pull_url / str(int(issue_number)))
return [refnode], messages
def visit_issue_node(translator: HTML5Translator, node: IssueNode) -> None:
"""
Visit an :class:`~.IssueNode`.
If the node points to a valid issue / pull request,
add a tooltip giving the title of the issue / pull request and a hyperlink to the page on GitHub.
:param translator:
:param node: The node being visited.
"""
issue_title = get_issue_title(node.issue_url)
if issue_title:
node.has_tooltip = True
translator.body.append(f'<abbr title="{issue_title}">')
translator.visit_reference(node)
else:
warnings.warn(f"Issue/Pull Request #{node.issue_number} not found.")
def depart_issue_node(translator: HTML5Translator, node: IssueNode) -> None:
"""
Depart an :class:`~.IssueNode`.
:param translator:
:param node: The node being visited.
"""
if node.has_tooltip:
translator.depart_reference(node)
translator.body.append("</abbr>")
def _visit_issue_node_latex(translator: LaTeXTranslator, node: IssueNode) -> None:
"""
Visit an :class:`~.IssueNode`.
If the node points to a valid issue / pull request,
add a tooltip giving the title of the issue / pull request and a hyperlink to the page on GitHub.
:param translator:
:param node: The node being visited.
"""
node.children = node.children[:1]
translator.visit_reference(node)
def _depart_issue_node_latex(translator: LaTeXTranslator, node: IssueNode) -> None:
"""
Depart an :class:`~.IssueNode`.
:param translator:
:param node: The node being visited.
"""
translator.depart_reference(node)
def get_issue_title(issue_url: str) -> Optional[str]:
"""
Returns the title of the issue with the given url,
or :py:obj:`None` if the issue isn't found.
:param issue_url:
""" # noqa: D400
try:
r = cache.session.get(issue_url, timeout=30)
except requests.exceptions.RequestException:
return None
if r.status_code == 200:
soup = BeautifulSoup(r.content, "html5lib")
try:
content = soup.find_all("span", attrs={"class": "js-issue-title"})[0].text
except IndexError:
try:
# As of 13 Jan 2023 GitHub seems to use a bidirectional text tag instead
content = soup.find_all("bdi", attrs={"class": "js-issue-title"})[0].text
except IndexError:
try:
# As of 17 2025, GitHub seems to have changed this again...
content = soup.find_all("bdi", attrs={"class": "Box-sc-g0xbh4-0 markdown-title"})[0].text
except IndexError:
# Give up
return None
content = xml.sax.saxutils.escape(content).replace('"', """)
return content.strip()
return None
|