Skip to content

Fix monorepo compatibility #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ dev-dependencies = [
"mkdocs-gen-files>=0.5.0",
"mkdocs-git-authors-plugin>=0.9.2",
"mkdocs-material>=9.6.7",
"mkdocs-monorepo-plugin>=1.1.0",
"mkdocs-static-i18n>=1.3.0",
"pytest>=8.3.5",
"pytest-cov>=5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/mkdocs_git_revision_date_localized_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.4.0"
__version__ = "1.4.1"
24 changes: 15 additions & 9 deletions src/mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from mkdocs_git_revision_date_localized_plugin.util import Util
from mkdocs_git_revision_date_localized_plugin.exclude import exclude

from typing import Any, Dict
from typing import Any, Dict, Optional
from collections import OrderedDict

from packaging.version import Version
Expand Down Expand Up @@ -145,12 +145,15 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
return config


def parallel_compute_commit_timestamps(self, files, docs_dir, is_first_commit=False):
def parallel_compute_commit_timestamps(self, files, original_source: Optional[Dict] = None, is_first_commit=False):
pool = multiprocessing.Pool(processes=min(10, multiprocessing.cpu_count()))
results = []
for file in files:
if file.is_documentation_page():
abs_src_path = os.path.join(docs_dir, file.src_uri)
abs_src_path = file.abs_src_path
# Support plugins like monorep that might have moved the files from the original source that is under git
if original_source and abs_src_path in original_source:
abs_src_path = original_source[abs_src_path]
result = pool.apply_async(
self.util.get_git_commit_timestamp, args=(abs_src_path, is_first_commit)
)
Expand All @@ -170,14 +173,17 @@ def on_files(self, files: Files, config: MkDocsConfig):
"""
if not self.config.get("enabled") or not self.config.get("enable_parallel_processing"):
return
# Some plugins like TechDocs/monorepo copies docs_dir to a tmp dir and we need the real git path.
real_docs_dir = os.path.join(
os.path.dirname(config["config_file_path"]), "docs"
)

# Support monorepo/techdocs, which copies the docs_dir to a temporary directory
if "monorepo" in config.get('plugins', {}):
original_source = config.get('plugins').get('monorepo').merger.files_source_dir
else:
original_source = None

if not self.last_revision_commits:
self.parallel_compute_commit_timestamps(files=files, docs_dir=real_docs_dir, is_first_commit=False)
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=False)
if not self.created_commits:
self.parallel_compute_commit_timestamps(files=files, docs_dir=real_docs_dir, is_first_commit=True)
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=True)


def on_page_markdown(self, markdown: str, page: Page, config: config_options.Config, files, **kwargs) -> str:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/monorepo/components/a/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Docs for A
4 changes: 4 additions & 0 deletions tests/fixtures/monorepo/components/a/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
site_name: Component A

nav:
- Home: "README.md"
1 change: 1 addition & 0 deletions tests/fixtures/monorepo/components/b/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Docs for B
4 changes: 4 additions & 0 deletions tests/fixtures/monorepo/components/b/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
site_name: Component B

nav:
- Home: "README.md"
1 change: 1 addition & 0 deletions tests/fixtures/monorepo/components/c/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Docs for C
4 changes: 4 additions & 0 deletions tests/fixtures/monorepo/components/c/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
site_name: Component C

nav:
- Home: "README.md"
3 changes: 3 additions & 0 deletions tests/fixtures/monorepo/components/d/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Component D

This will not be included
1 change: 1 addition & 0 deletions tests/fixtures/monorepo/components/d/e/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Docs for E
4 changes: 4 additions & 0 deletions tests/fixtures/monorepo/components/d/e/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
site_name: Component E

nav:
- Home: "README.md"
1 change: 1 addition & 0 deletions tests/fixtures/monorepo/components/randomfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
File present to test that it is successfully ignored by wildcard include.
10 changes: 10 additions & 0 deletions tests/fixtures/monorepo/docs/authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Authentication

This explains how to authenticate.

## Authorization Header

To authenticate, simply pass in your access token in the form of a `Authorization: Bearer {access_token}` header in your HTTP request to our API.

- Markdown source: `sample-docs/docs/authentication.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/authentication/>
12 changes: 12 additions & 0 deletions tests/fixtures/monorepo/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Welcome to our Developer Platform

This contains the documentation for our Developer Platform.

## API

- [v1 API Reference](./versions/v1/reference.md)
- [v2 API Reference](./versions/v2/reference.md)
- [v3 API Reference](./versions/v3/reference.md)

- Markdown source: `sample-docs/docs/index.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/>
18 changes: 18 additions & 0 deletions tests/fixtures/monorepo/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
site_name: Cats API

nav:
- Intro: 'index.md'
- Authentication: 'authentication.md'
- Components: '*include ./components/*/mkdocs.yml'
- API:
- v1: '!include ./v1/mkdocs.yml'
- v2: '!include ./v2/mkdocs.yml'
- v3: '!include ./v3/mkdocs.yml'

theme:
name: material

plugins:
- search
- git-revision-date-localized
- monorepo
6 changes: 6 additions & 0 deletions tests/fixtures/monorepo/v1/docs/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

This contains the changelog for the v1 API.

- Markdown source: `sample-docs/v1/docs/changelog.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v1/changelog/>
18 changes: 18 additions & 0 deletions tests/fixtures/monorepo/v1/docs/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# v1 API reference

This contains the v1 API reference.

## GET /v1/cats

Returns a list of cats.

## GET /v1/search

Parameters:

- `q` = A string containing the search query for a cat name.

Returns a list of cats that match the search query.

- Markdown source: `sample-docs/v1/docs/reference.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v1/reference/>
5 changes: 5 additions & 0 deletions tests/fixtures/monorepo/v1/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
site_name: versions/v1

nav:
- Reference: "reference.md"
- Changelog: "changelog.md"
6 changes: 6 additions & 0 deletions tests/fixtures/monorepo/v2/docs/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

This contains the changelog for the v2 API.

- Markdown source: `sample-docs/v2/docs/changelog.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v2/changelog/>
6 changes: 6 additions & 0 deletions tests/fixtures/monorepo/v2/docs/migrating.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Migration Guide to v2

This contains the migration guide to the v2 API from the v1 API.

- Markdown source: `sample-docs/v2/docs/migrating.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v2/migrating/>
26 changes: 26 additions & 0 deletions tests/fixtures/monorepo/v2/docs/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# v2 API reference

This contains the v2 API reference.

## GET /v2/cats

Returns a list of all cats.

## GET /v2/breeds

Returns a list of all breeds of cats.

## GET /v2/breeds/:breed_name/cats

Returns a list of all cats under the `:breed_name` breed.

## GET /v2/search

Parameters:

- `q` = A string containing the search query for a cat name or breed.

Returns a list of cats that match the search query.

- Markdown source: `sample-docs/v2/docs/reference.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v2/reference/>
6 changes: 6 additions & 0 deletions tests/fixtures/monorepo/v2/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
site_name: versions/v2

nav:
- Migrating to v2: "migrating.md"
- Reference: "reference.md"
- Changelog: "changelog.md"
6 changes: 6 additions & 0 deletions tests/fixtures/monorepo/v3/docs/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

This contains the changelog for the v3 API.

- Markdown source: `sample-docs/v3/docs/changelog.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v3/changelog/>
6 changes: 6 additions & 0 deletions tests/fixtures/monorepo/v3/docs/migrating.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Migration Guide to v3

This contains the migration guide to the v3 API from the v2 API.

- Markdown source: `sample-docs/v3/docs/migrating.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v3/migrating/>
30 changes: 30 additions & 0 deletions tests/fixtures/monorepo/v3/docs/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# v3 API reference

This contains the v3 API reference.

## GET /v3/cats

Returns a list of all cats.

## GET /v3/breeds

Returns a list of all breeds of cats.

## GET /v3/breeds/:breed_name/cats

Returns a list of all cats under the `:breed_name` breed.

## GET /v3/search

Parameters:

- `q` = A string containing the search query for a cat name or breed.

Returns a list of cats that match the search query.

- Markdown source: `sample-docs/v3/docs/reference.md`
- Permalink: <https://backstage.github.io/mkdocs-monorepo-plugin/monorepo-example/versions/v3/reference/>

## GET /v3/cat/:name/lives_left

Returns how many lives the cat named `:name` still has left.
3 changes: 3 additions & 0 deletions tests/fixtures/monorepo/v3/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
site_name: versions/v3

docs_dir: ./docs
40 changes: 30 additions & 10 deletions tests/test_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
if "gen-files" in mkdocs_yml_path:
shutil.copyfile(str(Path(mkdocs_yml_path).parent / "gen_pages.py"), str(testproject_path / "gen_pages.py"))

# Copy monorepo files
if "monorepo" in mkdocs_yml_path:
shutil.copytree("tests/fixtures/monorepo", str(testproject_path), dirs_exist_ok=True)

return testproject_path


Expand Down Expand Up @@ -313,27 +317,27 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):


MKDOCS_FILES = [
'basic_project/mkdocs_theme_no_locale.yml',
'basic_project/mkdocs.yml',
'basic_project/mkdocs_theme_timeago_locale.yml',
'basic_project/mkdocs_creation_date.yml',
'basic_project/mkdocs_custom_type.yml',
'basic_project/mkdocs_datetime.yml',
'basic_project/mkdocs_exclude.yml',
'basic_project/mkdocs_fallback_to_build_date.yml',
'basic_project/mkdocs_locale.yml',
'basic_project/mkdocs_meta.yml',
'basic_project/mkdocs_plugin_locale.yml',
'basic_project/mkdocs_with_override.yml',
'basic_project/mkdocs.yml',
'basic_project/mkdocs_theme_timeago_locale.yml',
'basic_project/mkdocs_theme_language.yml',
'basic_project/mkdocs_theme_locale_and_language.yml',
'basic_project/mkdocs_theme_locale_disabled.yml',
'basic_project/mkdocs_theme_timeago.yml',
'basic_project/mkdocs_theme_locale.yml',
'basic_project/mkdocs_theme_no_locale.yml',
'basic_project/mkdocs_theme_timeago_override.yml',
'basic_project/mkdocs_theme_timeago_instant.yml',
'basic_project/mkdocs_creation_date.yml',
'basic_project/mkdocs_timeago_locale.yml',
'basic_project/mkdocs_timeago.yml',
'basic_project/mkdocs_fallback_to_build_date.yml',
'basic_project/mkdocs_locale.yml',
'basic_project/mkdocs_exclude.yml',
'basic_project/mkdocs_meta.yml',
'basic_project/mkdocs_custom_type.yml',
'basic_project/mkdocs_with_override.yml',
# 'i18n/mkdocs.yml'
]

Expand Down Expand Up @@ -704,3 +708,19 @@ def test_ignored_commits(tmp_path):
page_with_tag = testproject_path / "site/page_with_tag/index.html"
contents = page_with_tag.read_text(encoding="utf8")
assert "May 4, 2018" in contents



def test_monorepo_compat(tmp_path):
testproject_path = setup_clean_mkdocs_folder(
"tests/fixtures/monorepo/mkdocs.yml", tmp_path
)
repo = setup_commit_history(testproject_path)
result = build_docs_setup(testproject_path)

# author = "Test Person <[email protected]>"
# with working_directory(testproject_path):
# repo.git.add(".")
# repo.git.commit(message="add all", author=author, date="1500854705")

assert result.exit_code == 0, f"'mkdocs build' command failed with:\n\n{result.stdout}"
Loading
Loading