From 730521ace38ca0c342688b507686f93f646a21dc Mon Sep 17 00:00:00 2001 From: Jonas Scharpf Date: Fri, 4 Oct 2024 11:26:10 +0200 Subject: [PATCH] add meta data parsing, close #28 --- .snippets/28.md | 11 ++++++ README.md | 2 +- examples/changelog.json | 47 +++++++++++++++++++++++- requirements-deploy.txt | 2 +- src/changelog2version/extract_version.py | 27 +++++++++++++- src/changelog2version/update_version.py | 1 + tests/data/valid/changelog_with_meta.md | 32 ++++++++++++++++ tests/test_extract_version.py | 17 +++++++-- 8 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 .snippets/28.md create mode 100644 tests/data/valid/changelog_with_meta.md diff --git a/.snippets/28.md b/.snippets/28.md new file mode 100644 index 0000000..3cb71c4 --- /dev/null +++ b/.snippets/28.md @@ -0,0 +1,11 @@ +## Add meta data parsing + + +Add parser for meta data comment in changelog entry. The parsed data is available via the `meta_data` property of `ExtractVersion` after running `parse_changelog_completely` and is added to the `changelog.json` file. See #28 + +- bump `snippets2changelog` to 1.3.0 to have the snippets meta data added to the changelog entries +- update `examples/changelog.json` to contain meta data of latest changelog entry diff --git a/README.md b/README.md index 9d0dd26..18a8904 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ changelog2version \ ``` ```json -{"info": {"version": "0.7.0", "description": "### Added\n- Changelog parsed as JSON contains a new key `description` like the PyPi package JSON info, compare to `https://pypi.org/pypi/changelog2version/json`, with the description/content of the latest change, see #19, relates to #18\n- Increase unittest coverage above 95%\n\n### Changed\n- Line breaks are no longer used in this changelog for enumerations\n- Issues are referenced as `#123` instead of `[#123][ref-issue-123]` to avoid explicit references at the bottom or some other location in the file\n- Output of `changelog2version` call with `--print` but without `--debug` option is JSON compatible\n"}, "releases": {"0.7.0": [{"upload_time": "2022-11-11"}], "0.6.0": [{"upload_time": "2022-10-26"}], "0.5.0": [{"upload_time": "2022-10-20"}], "0.4.0": [{"upload_time": "2022-08-07"}], "0.3.0": [{"upload_time": "2022-08-05"}], "0.2.0": [{"upload_time": "2022-08-03"}], "0.1.1": [{"upload_time": "2022-07-31"}], "0.1.0": [{"upload_time": "2022-07-31"}]}} +{"info":{"version":"0.12.0","description":"\n\nAdd parser for meta data comment in changelog entry. The parsed data is available via the `meta_data` property of `ExtractVersion` after running `parse_changelog_completely` and is added to the `changelog.json` file. See #28\n\n- bump `snippets2changelog` to 1.3.0 to have the snippets meta data added to the changelog entries\n\n[0.12.0]: https://github.com/brainelectronics/snippets2changelog/tree/0.12.0\n","meta":{"type":"feature","scope":["all"],"affected":["all"]}},"releases":{"0.12.0":[{"upload_time":"2024-10-04T11:26:10"}],"0.11.0":[{"upload_time":"2024-10-04T10:53:11"}],"0.10.1":[{"upload_time":"2024-10-02"}],"0.10.0":[{"upload_time":"2023-07-08"}],"0.9.0":[{"upload_time":"2022-11-12"}],"0.8.0":[{"upload_time":"2022-11-11"}],"0.7.0":[{"upload_time":"2022-11-11"}],"0.6.0":[{"upload_time":"2022-10-26"}],"0.5.0":[{"upload_time":"2022-10-20"}],"0.4.0":[{"upload_time":"2022-08-07"}],"0.3.0":[{"upload_time":"2022-08-05"}],"0.2.0":[{"upload_time":"2022-08-03"}],"0.1.1":[{"upload_time":"2022-07-31"}],"0.1.0":[{"upload_time":"2022-07-31"}]}} ``` To get the latest version and description in the console as environment diff --git a/examples/changelog.json b/examples/changelog.json index 00f6074..765cdfc 100644 --- a/examples/changelog.json +++ b/examples/changelog.json @@ -1,8 +1,53 @@ { "info": { - "version": "0.6.0" + "version": "0.12.0", + "description": "\n\nAdd parser for meta data comment in changelog entry. The parsed data is available via the `meta_data` property of `ExtractVersion` after running `parse_changelog_completely` and is added to the `changelog.json` file. See #28\n\n- bump `snippets2changelog` to 1.3.0 to have the snippets meta data added to the changelog entries\n\n[0.12.0]: https://github.com/brainelectronics/snippets2changelog/tree/0.12.0\n", + "meta": { + "type": "feature", + "scope": [ + "all" + ], + "affected": [ + "all" + ] + } }, "releases": { + "0.12.0": [ + { + "upload_time": "2024-10-04T11:26:10" + } + ], + "0.11.0": [ + { + "upload_time": "2024-10-04T10:53:11" + } + ], + "0.10.1": [ + { + "upload_time": "2024-10-02" + } + ], + "0.10.0": [ + { + "upload_time": "2023-07-08" + } + ], + "0.9.0": [ + { + "upload_time": "2022-11-12" + } + ], + "0.8.0": [ + { + "upload_time": "2022-11-11" + } + ], + "0.7.0": [ + { + "upload_time": "2022-11-11" + } + ], "0.6.0": [ { "upload_time": "2022-10-26" diff --git a/requirements-deploy.txt b/requirements-deploy.txt index 643e30d..7d27db2 100644 --- a/requirements-deploy.txt +++ b/requirements-deploy.txt @@ -2,4 +2,4 @@ # Avoid fixed versions # # to upload package to PyPi or other package hosts twine>=5.1.1,<6 -snippets2changelog>=1.2.1,<2 +snippets2changelog>=1.3.0,<2 diff --git a/src/changelog2version/extract_version.py b/src/changelog2version/extract_version.py index 7ec0229..9a464fb 100644 --- a/src/changelog2version/extract_version.py +++ b/src/changelog2version/extract_version.py @@ -6,11 +6,12 @@ from this line """ +import json import logging import re from pathlib import Path from sys import stdout -from typing import List, Optional +from typing import Dict, List, Optional from semver import VersionInfo @@ -34,6 +35,7 @@ def __init__(self, logger: Optional[logging.Logger] = None): self._logger = logger self._semver_data = VersionInfo(*(0, 0, 0)) self._latest_description_lines = [] + self._meta_data = {} self._semver_line_regex = ( r"^(?P0|[1-9]\d*)\." # major version part @@ -185,6 +187,16 @@ def latest_description(self) -> str: """ return '\n'.join(self.latest_description_lines) + @property + def meta_data(self) -> Dict[str, str]: + """ + Get latest meta data of the parsed changelog + + :returns: Latest meta data + :rtype: str + """ + return self._meta_data + @staticmethod def _create_logger(logger_name: str = None) -> logging.Logger: """ @@ -275,6 +287,8 @@ def parse_changelog_completely(self, # the version line itself is also included, ignore it self._latest_description_lines = latest_description_lines[1:] + self.parse_meta_comment() + return release_version_lines def parse_semver_line_date(self, release_version_line: str) -> str: @@ -346,3 +360,14 @@ def parse_semver_line(self, release_version_line: str) -> str: format(release_version_line)) return semver_string + + def parse_meta_comment(self) -> None: + """Find and parse meta comment line of all parsed description lines""" + for line in self.latest_description_lines: + # try to extract any comment with "meta =" + match = re.search(r"()", line) + + if match and len(match.groups()) == 3: + self._meta_data = json.loads(match.groups()[1].replace("'", "\"")) + self._logger.debug("Latest Meta Data: '{}'".format(self._meta_data)) + break diff --git a/src/changelog2version/update_version.py b/src/changelog2version/update_version.py index c19d700..9f98567 100644 --- a/src/changelog2version/update_version.py +++ b/src/changelog2version/update_version.py @@ -301,6 +301,7 @@ def main(): 'info': { 'version': semver_string, 'description': version_extractor.latest_description, + 'meta': version_extractor.meta_data, }, 'releases': release_infos } diff --git a/tests/data/valid/changelog_with_meta.md b/tests/data/valid/changelog_with_meta.md new file mode 100644 index 0000000..e6528ae --- /dev/null +++ b/tests/data/valid/changelog_with_meta.md @@ -0,0 +1,32 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + + + + +## Released +## [1.3.0] - 2022-10-26 + + +### Added +- Something fixed + +## [1.2.3] - 2022-07-31 +### Fixed +- Something fixed + + +[1.3.0]: https://github.com/brainelectronics/changelog2version/tree/1.3.0 +[1.2.3]: https://github.com/brainelectronics/changelog2version/tree/1.2.3 diff --git a/tests/test_extract_version.py b/tests/test_extract_version.py index 6886f41..5ceee6e 100644 --- a/tests/test_extract_version.py +++ b/tests/test_extract_version.py @@ -125,7 +125,8 @@ def test_parse_changelog_file(self, ( "changelog_with_date.md", ["## [1.3.0] - 2022-10-26", "## [1.2.3] - 2022-07-31"], - "### Added\n- Something fixed\n" + "### Added\n- Something fixed\n", + {} ), ( "changelog_with_date_and_time.md", @@ -133,13 +134,21 @@ def test_parse_changelog_file(self, "## [94.0.0] - 2022-10-26 23:59:01", "## [93.10.1] - 2022-07-31 12:34:56" ], - "### Added\n- Something fixed\n" + "### Added\n- Something fixed\n", + {} + ), + ( + "changelog_with_meta.md", + ["## [1.3.0] - 2022-10-26", "## [1.2.3] - 2022-07-31"], + "### Added\n- Something fixed\n", + {'type': 'feature', 'scope': ['all'], 'affected': ['all']} ), ) def test_parse_changelog_completely_file(self, file_name: str, expectation: List[str], - expected_description: str + expected_description: str, + expected_meta_data: Dict[str, str], ) -> None: """Test parse_changelog""" changelog = self._here / 'data' / 'valid' / file_name @@ -155,7 +164,9 @@ def test_parse_changelog_completely_file(self, # test extracted description self.assertIsInstance(self.ev.latest_description, str) + self.assertIsInstance(self.ev.meta_data, dict) self.assertEqual(self.ev.latest_description, expected_description) + self.assertEqual(self.ev.meta_data, expected_meta_data) self.assertTrue(all(isinstance(ele, str) for ele in self.ev.latest_description_lines)) self.assertEqual(len(self.ev.latest_description_lines), 3)