Skip to content

Commit be9f10d

Browse files
authored
Fix url shortening for gitlab, and more test for github (#1888)
1 parent d7ced40 commit be9f10d

File tree

4 files changed

+128
-7
lines changed

4 files changed

+128
-7
lines changed

src/pydata_sphinx_theme/short_link.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def parse_url(self, uri: ParseResult) -> str:
6363
the reformated url title
6464
"""
6565
path = uri.path
66-
6766
if path == "":
6867
# plain url passed, return platform only
6968
return self.platform
@@ -99,12 +98,18 @@ def parse_url(self, uri: ParseResult) -> str:
9998
map(uri.path.__contains__, ["issues", "merge_requests"])
10099
):
101100
group_and_subgroups, parts, *_ = path.split("/-/")
102-
parts = parts.split("/")
103-
url_type, element_number, *_ = parts
104-
if url_type == "issues":
105-
text = f"{group_and_subgroups}#{element_number}"
106-
elif url_type == "merge_requests":
107-
text = f"{group_and_subgroups}!{element_number}"
101+
parts = parts.rstrip("/")
102+
if "/" not in parts:
103+
text = f"{group_and_subgroups}/{parts}"
104+
else:
105+
parts = parts.split("/")
106+
url_type, element_number, *_ = parts
107+
if not element_number:
108+
text = group_and_subgroups
109+
elif url_type == "issues":
110+
text = f"{group_and_subgroups}#{element_number}"
111+
elif url_type == "merge_requests":
112+
text = f"{group_and_subgroups}!{element_number}"
108113
else:
109114
# display the whole uri (after "gitlab.com/") including parameters
110115
# for example "<group>/<subgroup1>/<subgroup2>/<repository>"

tests/sites/base/page1.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Page 1
2424
https://gitlab.com/gitlab-org/gitlab
2525
https://gitlab.com/gitlab-org/gitlab/-/issues/375583
2626
https://gitlab.com/gitlab-org/gitlab/issues/375583
27+
https://gitlab.com/gitlab-org/gitlab/-/issues/
2728
https://gitlab.com/gitlab-org/gitlab/issues/
29+
https://gitlab.com/gitlab-org/gitlab/-/issues
2830
https://gitlab.com/gitlab-org/gitlab/issues
2931
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/84669
3032
https://gitlab.com/gitlab-org/gitlab/-/pipelines/511894707

tests/test_build/gitlab_links.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
<a class="gitlab reference external" href="https://gitlab.com/gitlab-org/gitlab/issues/375583">
1616
gitlab-org/gitlab/issues/375583
1717
</a>
18+
<a class="gitlab reference external" href="https://gitlab.com/gitlab-org/gitlab/-/issues/">
19+
gitlab-org/gitlab/issues
20+
</a>
1821
<a class="gitlab reference external" href="https://gitlab.com/gitlab-org/gitlab/issues/">
1922
gitlab-org/gitlab/issues/
2023
</a>
24+
<a class="gitlab reference external" href="https://gitlab.com/gitlab-org/gitlab/-/issues">
25+
gitlab-org/gitlab/issues
26+
</a>
2127
<a class="gitlab reference external" href="https://gitlab.com/gitlab-org/gitlab/issues">
2228
gitlab-org/gitlab/issues
2329
</a>

tests/test_short_url.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""Shortening url tests."""
2+
3+
from urllib.parse import urlparse
4+
5+
import pytest
6+
from pydata_sphinx_theme.short_link import ShortenLinkTransform
7+
8+
9+
class Mock:
10+
"""mock object."""
11+
12+
pass
13+
14+
15+
@pytest.mark.parametrize(
16+
"platform,url,expected",
17+
[
18+
# TODO, I belive this is wrong as both github.com and github.com/github
19+
# shorten to just github.
20+
("github", "https://github.com", "github"),
21+
("github", "https://github.com/github", "github"),
22+
("github", "https://github.com/pydata", "pydata"),
23+
(
24+
"github",
25+
"https://github.com/pydata/pydata-sphinx-theme",
26+
"pydata/pydata-sphinx-theme",
27+
),
28+
(
29+
"github",
30+
"https://github.com/pydata/pydata-sphinx-theme/pull/1012",
31+
"pydata/pydata-sphinx-theme#1012",
32+
),
33+
# TODO, I belive this is wrong as both orgs/pydata/projects/2 and pydata/projects/issue/2
34+
# shorten to the same
35+
("github", "https://github.com/orgs/pydata/projects/2", "pydata/projects#2"),
36+
("github", "https://github.com/pydata/projects/pull/2", "pydata/projects#2"),
37+
# issues and pulls are athe same, so it's ok to normalise to the same here
38+
("github", "https://github.com/pydata/projects/issues/2", "pydata/projects#2"),
39+
# Gitlab
40+
("gitlab", "https://gitlab.com/tezos/tezos/-/issues", "tezos/tezos/issues"),
41+
("gitlab", "https://gitlab.com/tezos/tezos/issues", "tezos/tezos/issues"),
42+
(
43+
"gitlab",
44+
"https://gitlab.com/gitlab-org/gitlab/-/issues/375583",
45+
"gitlab-org/gitlab#375583",
46+
),
47+
(
48+
# TODO, non canonical url, discuss if should maybe be shortened to
49+
# gitlab-org/gitlab#375583
50+
"gitlab",
51+
"https://gitlab.com/gitlab-org/gitlab/issues/375583",
52+
"gitlab-org/gitlab/issues/375583",
53+
),
54+
(
55+
"gitlab",
56+
"https://gitlab.com/gitlab-org/gitlab/-/issues/",
57+
"gitlab-org/gitlab/issues",
58+
),
59+
(
60+
# TODO, non canonical url, discuss if should maybe be shortened to
61+
# gitlab-org/gitlab/issues (no trailing slash)
62+
"gitlab",
63+
"https://gitlab.com/gitlab-org/gitlab/issues/",
64+
"gitlab-org/gitlab/issues/",
65+
),
66+
(
67+
"gitlab",
68+
"https://gitlab.com/gitlab-org/gitlab/-/issues",
69+
"gitlab-org/gitlab/issues",
70+
),
71+
(
72+
"gitlab",
73+
"https://gitlab.com/gitlab-org/gitlab/issues",
74+
"gitlab-org/gitlab/issues",
75+
),
76+
(
77+
"gitlab",
78+
"https://gitlab.com/gitlab-org/gitlab/-/merge_requests/84669",
79+
"gitlab-org/gitlab!84669",
80+
),
81+
(
82+
"gitlab",
83+
"https://gitlab.com/gitlab-org/gitlab/-/pipelines/511894707",
84+
"gitlab-org/gitlab/-/pipelines/511894707",
85+
),
86+
(
87+
"gitlab",
88+
"https://gitlab.com/gitlab-com/gl-infra/production/-/issues/6788",
89+
"gitlab-com/gl-infra/production#6788",
90+
),
91+
],
92+
)
93+
def test_shorten(platform, url, expected):
94+
"""Unit test for url shortening.
95+
96+
Usually you also want a build test in `test_build.py`
97+
"""
98+
document = Mock()
99+
document.settings = Mock()
100+
document.settings.language_code = "en"
101+
document.reporter = None
102+
103+
sl = ShortenLinkTransform(document)
104+
sl.platform = platform
105+
106+
URI = urlparse(url)
107+
108+
assert sl.parse_url(URI) == expected

0 commit comments

Comments
 (0)