-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathissue_role.py
69 lines (52 loc) · 2.2 KB
/
issue_role.py
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
"""Support for referencing issues in the tracker."""
from __future__ import annotations
from typing import TYPE_CHECKING
from docutils import nodes
from sphinx.util.docutils import SphinxRole
if TYPE_CHECKING:
from docutils.nodes import Element
from sphinx.application import Sphinx
from sphinx.util.typing import ExtensionMetadata
class BPOIssue(SphinxRole):
ISSUE_URI = "https://bugs.python.org/issue?@action=redirect&bpo={0}"
def run(self) -> tuple[list[Element], list[nodes.system_message]]:
issue = self.text
# sanity check: there are no bpo issues within these two values
if 47_261 < int(issue) < 400_000:
msg = self.inliner.reporter.error(
f"The BPO ID {issue!r} seems too high. "
"Use :gh:`...` for GitHub IDs.",
line=self.lineno,
)
prb = self.inliner.problematic(self.rawtext, self.rawtext, msg)
return [prb], [msg]
issue_url = self.ISSUE_URI.format(issue)
refnode = nodes.reference(issue, f"bpo-{issue}", refuri=issue_url)
self.set_source_info(refnode)
return [refnode], []
class GitHubIssue(SphinxRole):
ISSUE_URI = "https://github.com/python/cpython/issues/{0}"
def run(self) -> tuple[list[Element], list[nodes.system_message]]:
issue = self.text
# sanity check: all GitHub issues have ID >= 32426
# even though some of them are also valid BPO IDs
if int(issue) < 32_426:
msg = self.inliner.reporter.error(
f"The GitHub ID {issue!r} seems too low. "
"Use :issue:`...` for BPO IDs.",
line=self.lineno,
)
prb = self.inliner.problematic(self.rawtext, self.rawtext, msg)
return [prb], [msg]
issue_url = self.ISSUE_URI.format(issue)
refnode = nodes.reference(issue, f"gh-{issue}", refuri=issue_url)
self.set_source_info(refnode)
return [refnode], []
def setup(app: Sphinx) -> ExtensionMetadata:
app.add_role("issue", BPOIssue())
app.add_role("gh", GitHubIssue())
return {
"version": "1.0",
"parallel_read_safe": True,
"parallel_write_safe": True,
}