Skip to content

Commit bc3b64e

Browse files
committed
Fetch the repo_url from the mkdocs.yaml
With this, there is no need for customization unless you want to define your own macros. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 30cc92a commit bc3b64e

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/frequenz/repo/config/mkdocs/mkdocstrings_macros.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
version of the repository. It is built using the information from `version`. Also
4343
only available if the rigth environment variables are set, and if the resulting
4444
version is a tag (will be empty for branches). If you want to get the version
45-
requirement for a branch, you need to provide a repository URL, please read the
46-
[Advanced usage] section for more details.
45+
requirement for a branch, you need to provide a `repo_url` variable in the
46+
`mkdocs.yaml` file or do a custom setup. Please read the [Customized usage]
47+
section for more details.
4748
4849
4950
# Customized usage
@@ -71,7 +72,8 @@
7172
7273
You also need to make sure to call the function at the end, after you define your own
7374
variables, filters and macros. You can optionally pass a `repo_url` in this case so the
74-
`version_requirement` variable can work when the current version is a branch.
75+
`version_requirement` variable can work when the current version is a branch. If a
76+
`repo_url` variable is present in the `mkdocs.yml` file, it will be used as the default.
7577
7678
Here is an example of how to do it:
7779
@@ -82,7 +84,7 @@ def define_env(env: macros.MacrosPlugin) -> None:
8284
env.variables.my_var = "Example"
8385
8486
# This hook needs to be done at the end of the `define_env` function.
85-
hook_env_with_everything(env, "git+https://your-repo-url")
87+
hook_env_with_everything(env, "https://your-repo-url")
8688
```
8789
8890
# Advanced usage
@@ -154,16 +156,25 @@ def add_version_variables(
154156
version of the repository. It is built using the information from `version`. Also
155157
only available if the rigth environment variables are set, and if the resulting
156158
version is a tag (will be empty for branches). If you want to get the version
157-
requirement for a branch, you need to provide a `repo_url`.
159+
requirement for a branch, you need to provide a `repo_url` or a `repo_url`
160+
config in the `mkdocs.yml` file.
158161
159162
Args:
160163
env: The environment to add the variables to.
161164
repo_url: The URL of the repository to use in the `version_requirement`
162-
variable. Only needed if you want to use the `version_requirement` variable
163-
for branches.
165+
variable. If `None` the `config.repo_url` mkdocs variable will be used. Only
166+
needed if you want to use the `version_requirement` variable for branches.
164167
"""
165168
env.variables["version"] = None
166169
env.variables["version_requirement"] = ""
170+
171+
if repo_url is None:
172+
repo_url = env.variables.get("config", {}).get("repo_url")
173+
if repo_url is None:
174+
_logger.warning(
175+
"No repo_url provided, can't build the 'version_requirement' variable"
176+
)
177+
167178
try:
168179
version_info = get_repo_version_info()
169180
except Exception as exc: # pylint: disable=broad-except
@@ -173,13 +184,9 @@ def add_version_variables(
173184
if version_info.current_tag:
174185
env.variables["version_requirement"] = f" == {version_info.current_tag}"
175186
elif version_info.current_branch:
176-
if repo_url is None:
177-
_logger.warning(
178-
"No repo_url provided, can't build the 'version_requirement' variable"
179-
)
180-
else:
187+
if repo_url is not None:
181188
env.variables["version_requirement"] = (
182-
f" @ {repo_url}@{version_info.current_branch}"
189+
f" @ git+{repo_url}@{version_info.current_branch}"
183190
)
184191

185192

@@ -230,8 +237,8 @@ def hook_env_with_everything(
230237
Args:
231238
env: The environment to hook.
232239
repo_url: The URL of the repository to use in the `version_requirement`
233-
variable. Only needed if you want to use the `version_requirement` variable
234-
for branches.
240+
variable. If `None` the `config.repo_url` mkdocs variable will be used. Only
241+
needed if you want to use the `version_requirement` variable for branches.
235242
"""
236243
env.variables.code_annotation_marker = CODE_ANNOTATION_MARKER
237244
add_version_variables(env, repo_url=repo_url)

0 commit comments

Comments
 (0)