Skip to content

Commit b67e605

Browse files
authored
Support self hosted git repo
1 parent 1694015 commit b67e605

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

README.rst

+8
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ Extension options (``conf.py``)
150150
provided, the build will still pass but the changelog will not be built, and
151151
a link to the ``changelog-url`` will be displayed (if provided).
152152

153+
- ``sphinx_github_changelog_root_repo`` (optional): Root url to the repository,
154+
defaults to "https://github.com/". Useful if you're using a self-hosted GitHub
155+
instance.
156+
157+
- ``sphinx_github_changelog_graphql_url`` (optional): Url to graphql api, defaults
158+
to "https://api.github.com/graphql". Useful if you're using a self-hosted GitHub
159+
instance.
160+
153161
.. _ReadTheDocs: https://readthedocs.org/
154162

155163
Directive

sphinx_github_changelog/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ def setup(app):
1919
app.add_config_value(
2020
name=token_name, default=os.environ.get(token_name.upper()), rebuild="html"
2121
)
22+
root_repo_name = "sphinx_github_changelog_root_repo"
23+
app.add_config_value(
24+
name=root_repo_name,
25+
default=os.environ.get(root_repo_name.upper()),
26+
rebuild="html",
27+
)
28+
graphql_url_name = "sphinx_github_changelog_graphql_url"
29+
app.add_config_value(
30+
name=graphql_url_name,
31+
default=os.environ.get(graphql_url_name.upper()),
32+
rebuild="html",
33+
)
34+
2235
app.add_directive("changelog", changelog.ChangelogDirective)
2336

2437
return {

sphinx_github_changelog/changelog.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,28 @@ def run(self) -> List[nodes.Node]:
3131
config = self.state.document.settings.env.config
3232
try:
3333
return compute_changelog(
34-
token=config.sphinx_github_changelog_token, options=self.options
34+
token=config.sphinx_github_changelog_token,
35+
options=self.options,
36+
root_url=config.sphinx_github_changelog_root_repo,
37+
graphql_url=config.sphinx_github_changelog_graphql_url,
3538
)
3639
except ChangelogError as exc:
3740
raise self.error(str(exc))
3841

3942

4043
def compute_changelog(
41-
token: Optional[str], options: Dict[str, str]
44+
token: Optional[str],
45+
options: Dict[str, str],
46+
root_url: Optional[str] = None,
47+
graphql_url: Optional[str] = None,
4248
) -> List[nodes.Node]:
4349
if not token:
4450
return no_token(changelog_url=options.get("changelog-url"))
4551

46-
owner_repo = extract_github_repo_name(url=options["github"])
47-
releases = extract_releases(owner_repo=owner_repo, token=token)
52+
owner_repo = extract_github_repo_name(url=options["github"], root_url=root_url)
53+
releases = extract_releases(
54+
owner_repo=owner_repo, token=token, graphql_url=graphql_url
55+
)
4856

4957
pypi_name = extract_pypi_package_name(url=options.get("pypi"))
5058

@@ -72,14 +80,19 @@ def no_token(changelog_url: Optional[str]) -> List[nodes.Node]:
7280
return result
7381

7482

75-
def extract_github_repo_name(url: str) -> str:
83+
def extract_github_repo_name(url: str, root_url: Optional[str] = None) -> str:
7684
stripped_url = url.rstrip("/")
77-
prefix, postfix = "https://github.com/", "/releases"
85+
prefix, postfix = (
86+
root_url if root_url is not None else "https://github.com/",
87+
"/releases",
88+
)
89+
if not prefix.endswith("/"):
90+
prefix += "/"
7891
url_is_correct = stripped_url.startswith(prefix) and stripped_url.endswith(postfix)
7992
if not url_is_correct:
8093
raise ChangelogError(
8194
"Changelog needs a Github releases URL "
82-
f"(https://github.com/:owner/:repo/releases). Received {url}"
95+
f"({prefix}:owner/:repo/releases). Received {url}"
8396
)
8497

8598
return stripped_url[len(prefix) : -len(postfix)]
@@ -142,7 +155,9 @@ def node_for_release(
142155
return section
143156

144157

145-
def extract_releases(owner_repo: str, token: str) -> Iterable[Dict[str, Any]]:
158+
def extract_releases(
159+
owner_repo: str, token: str, graphql_url: Optional[str] = None
160+
) -> Iterable[Dict[str, Any]]:
146161
# Necessary for GraphQL
147162
owner, repo = owner_repo.split("/")
148163
query = """
@@ -161,7 +176,7 @@ def extract_releases(owner_repo: str, token: str) -> Iterable[Dict[str, Any]]:
161176
)
162177
full_query = {"query": query.replace("\n", "")}
163178

164-
url = "https://api.github.com/graphql"
179+
url = "https://api.github.com/graphql" if graphql_url is None else graphql_url
165180

166181
try:
167182
result = github_call(url=url, query=full_query, token=token)

tests/unit/test_changelog.py

+34
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ def test_extract_github_repo_name_error():
109109
changelog.extract_github_repo_name("https://example.com")
110110

111111

112+
@pytest.mark.parametrize(
113+
"url",
114+
[
115+
"https://git.privaterepo.com/a/b/releases",
116+
"https://git.privaterepo.com/a/b/releases/",
117+
],
118+
)
119+
def test_extract_github_repo_different_root_url(url):
120+
with pytest.raises(
121+
changelog.ChangelogError, match="^Changelog needs a Github releases URL"
122+
):
123+
changelog.extract_github_repo_name(url)
124+
125+
assert (
126+
changelog.extract_github_repo_name(url, "https://git.privaterepo.com/") == "a/b"
127+
)
128+
assert (
129+
changelog.extract_github_repo_name(url, "https://git.privaterepo.com") == "a/b"
130+
)
131+
132+
112133
@pytest.mark.parametrize(
113134
"url", ["https://pypi.org/project/a", "https://pypi.org/project/a/"]
114135
)
@@ -180,6 +201,19 @@ def test_extract_releases(github_payload, release_dict, mocker):
180201
]
181202

182203

204+
def test_extract_releases_custom_graphql_url(github_payload, release_dict, mocker):
205+
mocker.patch(
206+
"sphinx_github_changelog.changelog.github_call", return_value=github_payload
207+
)
208+
assert changelog.extract_releases(
209+
owner_repo="a/b",
210+
token="token",
211+
graphql_url="https://git.privaterepo.com/graphql",
212+
) == [
213+
release_dict,
214+
]
215+
216+
183217
def test_extract_releases_remove_none(github_payload, release_dict, mocker):
184218
mocker.patch(
185219
"sphinx_github_changelog.changelog.github_call",

0 commit comments

Comments
 (0)