Skip to content

Commit

Permalink
πŸ› Detect if there's a second level header after the release content, …
Browse files Browse the repository at this point in the history
…to support the first change in a README with a last section for a license (#59)
  • Loading branch information
tiangolo committed Nov 4, 2023
1 parent e13de0f commit 2f6ba35
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ You can configure:
* `latest_changes_file`: The file to modify with the latest changes. For example: `./docs/latest-changes.rst`.
* `latest_changes_header`: The header to look for before adding a new message. for example: `# CHANGELOG`.
* `template_file`: A custom Jinja2 template file to use to generate the message, you could use this to generate a different message or to use a different format, for example, HTML instead of the default Markdown.
* `end_regex`: A RegEx string that marks the end of this release, so it normally matches the start of the header of the next release section, normally the same header level as `latest_changes_header`, so, if the `latest_changes_header` is `### Latest Changes`, the content for the next release below is probably something like `### 0.2.0`, then the `end_regex` should be `^### `. This is used to limit the content updated as this will read the existing sub sections and possibly update them using the labels configuration and the labels in the PR.
* `end_regex`: A RegEx string that marks the end of this release, so it normally matches the start of the header of the next release section, normally the same header level as `latest_changes_header`, so, if the `latest_changes_header` is `### Latest Changes`, the content for the next release below is probably something like `### 0.2.0`, then the `end_regex` should be `^### `. This is used to limit the content updated as this will read the existing sub sections and possibly update them using the labels configuration and the labels in the PR. By default it is `(^### .*)|(^## .*)` to detect a possible next header, e.g. for the license.
* `debug_logs`: Set to `'true'` to show logs with the current settings.
* `labels`: A JSON array of JSON objects with a `label` that you would put in each PR and the `header` that would be used in the release notes. See the example below.
* `label_header_prefix`: A prefix to put before each label's header. This is also used to detect where the next label header starts. By default it is `#### `, so the headers will look like `#### Features`.
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ inputs:
required: false
default: /app/latest_changes/latest-changes.jinja2
end_regex:
description: A RegEx string that marks the end of this release, so it normally matches the start of the header of the next release section, normally the same header level as `latest_changes_header`, so, if the `latest_changes_header` is `### Latest Changes`, the content for the next release below is probably something like `### 0.2.0`, then the `end_regex` should be `^### `.
default: '^### '
description: A RegEx string that marks the end of this release, so it normally matches the start of the header of the next release section, normally the same header level as `latest_changes_header`, so, if the `latest_changes_header` is `### Latest Changes`, the content for the next release below is probably something like `### 0.2.0`, then the `end_regex` should be `^### `. By default it is `(^### .*)|(^## .*)` to detect a possible next header, e.g. for the license.
default: '(^### .*)|(^## .*)'
required: false
debug_logs:
description: Use debug=True to enable more logging, useful to see the object shape for custom Jinja2 templates
Expand Down
2 changes: 1 addition & 1 deletion latest_changes/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Settings(BaseSettings):
input_latest_changes_file: Path = Path("README.md")
input_latest_changes_header: str = "### Latest Changes"
input_template_file: Path = Path(__file__).parent / "latest-changes.jinja2"
input_end_regex: str = "^### "
input_end_regex: str = "(^### .*)|(^## .*)"
input_debug_logs: Optional[bool] = False
input_labels: List[Section] = [
Section(label="breaking", header="Breaking Changes"),
Expand Down
111 changes: 111 additions & 0 deletions tests/test_generate_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,3 +1045,114 @@ def test_multiple_header_sections_label():
)
+ "\n"
)


def test_first_change_with_extra_header():
raw_content = """
## Release Notes
### Latest Changes
## License
Released under the MIT License.
"""

content = inspect.cleandoc(raw_content)
settings = Settings(
github_repository="tiangolo/latest-changes",
github_event_path="event.json",
input_token="secret",
)
pr = TemplateDataPR(
title="Demo PR",
number=42,
html_url="https://example.com/pr/42",
user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"),
)
new_content = generate_content(
content=content, settings=settings, pr=pr, labels=["feature"]
)
assert (
new_content
== inspect.cleandoc(
"""
## Release Notes
### Latest Changes
#### Features
* Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo).
## License
Released under the MIT License.
"""
)
+ "\n"
)


def test_first_release_existing_content_with_extra_header():
raw_content = """
## Release Note
### Latest Changes
* πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo).
#### Features
* πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo).
#### Docs
* πŸ“ Add docs. PR [#43](https://github.com/tiangolo/latest-changes/pull/43) by [@tiangolo](https://github.com/tiangolo).
## License
Released under the MIT License.
"""

content = inspect.cleandoc(raw_content)
settings = Settings(
github_repository="tiangolo/latest-changes",
github_event_path="event.json",
input_token="secret",
)
pr = TemplateDataPR(
title="Demo PR",
number=42,
html_url="https://example.com/pr/42",
user=TemplateDataUser(login="tiangolo", html_url="https://github.com/tiangolo"),
)
new_content = generate_content(
content=content, settings=settings, pr=pr, labels=["feature"]
)
assert (
new_content
== inspect.cleandoc(
"""
## Release Note
### Latest Changes
* πŸ”₯ Remove config. PR [#47](https://github.com/tiangolo/latest-changes/pull/47) by [@tiangolo](https://github.com/tiangolo).
#### Features
* Demo PR. PR [#42](https://example.com/pr/42) by [@tiangolo](https://github.com/tiangolo).
* πŸš€ Publish amd64 and arm64 versions. PR [#46](https://github.com/tiangolo/latest-changes/pull/46) by [@tiangolo](https://github.com/tiangolo).
#### Docs
* πŸ“ Add docs. PR [#43](https://github.com/tiangolo/latest-changes/pull/43) by [@tiangolo](https://github.com/tiangolo).
## License
Released under the MIT License.
"""
)
+ "\n"
)

0 comments on commit 2f6ba35

Please sign in to comment.