-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Automatically add dates to release notes #8001
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4a4b59
Sphinx extension to add dates to release notes
hugovk 3500334
Fetch tags on Read the Docs
hugovk ccf1efb
Use subprocess.DEVNULL
radarhere c0678ed
Merge pull request #116 from radarhere/dater
hugovk bc35bf0
Use split instead of datetime
radarhere 9392906
Merge pull request #117 from radarhere/dater
hugovk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """ | ||
| Sphinx extension to add timestamps to release notes based on Git versions. | ||
|
|
||
| Based on https://github.com/jaraco/rst.linker, with thanks to Jason R. Coombs. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import datetime as dt | ||
| import re | ||
| import subprocess | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sphinx.application import Sphinx | ||
|
|
||
| DOC_NAME_REGEX = re.compile(r"releasenotes/\d+\.\d+\.\d+") | ||
| VERSION_TITLE_REGEX = re.compile(r"^(\d+\.\d+\.\d+)\n-+\n") | ||
|
|
||
|
|
||
| def get_date_for(git_version: str) -> dt.datetime | None: | ||
| cmd = ["git", "log", "-1", "--format=%ai", git_version] | ||
| try: | ||
| out = subprocess.check_output( | ||
| cmd, stderr=subprocess.DEVNULL, text=True, encoding="utf-8" | ||
| ) | ||
| ts = out.strip() | ||
| return dt.datetime.fromisoformat(ts) | ||
| except subprocess.CalledProcessError: | ||
| return None | ||
|
|
||
|
|
||
| def add_date(app: Sphinx, doc_name: str, source: list[str]) -> None: | ||
| if DOC_NAME_REGEX.match(doc_name) and (m := VERSION_TITLE_REGEX.match(source[0])): | ||
| old_title = m.group(1) | ||
|
|
||
| if tag_datetime := get_date_for(old_title): | ||
| new_title = f"{old_title} ({tag_datetime:%Y-%m-%d})" | ||
| else: | ||
| new_title = f"{old_title} (unreleased)" | ||
|
|
||
| new_underline = "-" * len(new_title) | ||
|
|
||
| result = source[0].replace(m.group(0), f"{new_title}\n{new_underline}\n", 1) | ||
| source[0] = result | ||
|
|
||
|
|
||
| def setup(app: Sphinx) -> dict[str, bool]: | ||
| app.connect("source-read", add_date) | ||
| return {"parallel_read_safe": True} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are both
textandencodingused? From testing locally and from the docs, I would think only one is necessary.https://docs.python.org/3/library/subprocess.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny story - originally, only
text=Truewas used. Then later, when PEP 597 introduce encoding warnings when the encoding wasn't specified, indicated to include the encoding. Eventually, the need forencoding=will be once again unnecessary (python 3.14 maybe), so we'll want to fall back totext=, but until then, both are there. Maybetext=could be removed in the interim, but I'm kind-of inclined to leave it there as it's the primary signal.