Skip to content

Commit aadd873

Browse files
Merge pull request #28 from brainelectronics/feature/update-changelog-in-dry-run
Dry run option to print latest changelog entry
2 parents e56b641 + ab2bcfc commit aadd873

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

.snippets/27.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Dry run option to print latest changelog entry
2+
<!--
3+
type: feature
4+
scope: all
5+
affected: all
6+
-->
7+
8+
Use `--dry-run` with the `changelog` subparser to print the latest changelog entry as JSON instead of updating the changelog file.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ version entries and version bumps multiple times otherwise.*
9898
changelog-generator changelog changelog.md --snippets=.snippets [--in-place]
9999
```
100100

101+
To just get the latest changelog entry without updating or generating the
102+
changelog, use `--dry-run` to print the latest snippet content in JSON format.
103+
104+
```json
105+
{
106+
"version": "1.5.0",
107+
"timestamp": "2024-10-12T13:36:46+02:00",
108+
"meta": {
109+
"type": "feature",
110+
"scope": [
111+
"all"
112+
],
113+
"affected": [
114+
"all"
115+
]
116+
},
117+
"content": "\n\nUse `--dry-run` with the `changelog` subparser to print the latest changelog entry as JSON instead of updating the changelog file.\n",
118+
"version_reference": "https://github.com/brainelectronics/snippets2changelog/tree/1.5.0"
119+
}
120+
```
121+
101122
### Parse
102123

103124
Parse an existing snippet file and return the data as JSON without indentation

snippets2changelog/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ def parse_args(argv: Union[Sequence[str], None] = None) -> Args:
7575
action='store_true',
7676
help="Skip snippets with scope set as 'internal'",
7777
)
78+
parser_changelog.add_argument(
79+
"--dry-run",
80+
action='store_true',
81+
help="Print latest changelog entry as JSON instead of updating the changelog file",
82+
)
7883

7984
parser_create = subparsers.add_parser(
8085
"create",
@@ -125,7 +130,7 @@ def fn_info(_args: Args) -> None:
125130

126131
def fn_changelog(args: Args) -> None:
127132
cc = ChangelogCreator(changelog=args.changelog, snippets_folder=args.snippets, update_in_place=args.in_place, skip_internal=args.no_internal, verbosity=args.verbose)
128-
cc.update_changelog()
133+
cc.update_changelog(dry_run=args.dry_run)
129134

130135

131136
def fn_create(args: Args) -> None:

snippets2changelog/creator.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
"""Snippets generator"""
44

5+
import json
56
import logging
67
from pathlib import Path
78
from typing import Dict, Iterator
@@ -89,11 +90,13 @@ def __init__(self, changelog: Path, snippets_folder: Path, update_in_place: boo
8990

9091
self._skip_internal = skip_internal
9192

92-
def update_changelog(self) -> None:
93+
def update_changelog(self, dry_run: bool = False) -> None:
9394
new_changelog_content = ""
9495
# create a "prolog" and an "epilog", with the new content in between
9596
existing_changelog_content = read_file(path=self._changelog, parse="read").split(self._version_line)
9697

98+
latest_changelog_entry = {}
99+
97100
for commit, file_name in self.snippets():
98101
self._logger.debug(f"Parsing {file_name}")
99102
self.parse(file_name=file_name)
@@ -123,12 +126,18 @@ def update_changelog(self) -> None:
123126
"content": snippet_content["details"],
124127
"version_reference": f"https://github.com/brainelectronics/snippets2changelog/tree/{self.semver_data}",
125128
}
129+
latest_changelog_entry = changelog_entry_content
126130
self._logger.debug(f"changelog_entry_content: {changelog_entry_content}")
127131

128132
changelog_entry = self._env.get_template("changelog_part.md.template").render(changelog_entry_content)
129133
self._logger.debug(f"rendered changelog_entry: \n{changelog_entry}")
130134
new_changelog_content = changelog_entry + new_changelog_content
131135

136+
if dry_run:
137+
latest_changelog_entry['version'] = str(latest_changelog_entry['version'])
138+
print(json.dumps(latest_changelog_entry))
139+
return
140+
132141
rendered_changelog = self._env.get_template("changelog.md.template").render({"prolog": existing_changelog_content[0], "new": new_changelog_content, "existing": self._version_line + existing_changelog_content[1]})
133142
rendered_changelog_path = Path(f"{self._changelog}")
134143
if not self._update_in_place:

0 commit comments

Comments
 (0)