Skip to content

Commit

Permalink
Merge cfebf11 into e499cd4
Browse files Browse the repository at this point in the history
  • Loading branch information
brainelectronics authored Oct 4, 2024
2 parents e499cd4 + cfebf11 commit 2c60f70
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 9 deletions.
11 changes: 11 additions & 0 deletions .snippets/28.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Add meta data parsing
<!--
type: feature
scope: all
affected: all
-->

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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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":"<!-- meta = {'type': 'feature', 'scope': ['all'], 'affected': ['all']} -->\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
Expand Down
47 changes: 46 additions & 1 deletion examples/changelog.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
{
"info": {
"version": "0.6.0"
"version": "0.12.0",
"description": "<!-- meta = {'type': 'feature', 'scope': ['all'], 'affected': ['all']} -->\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"
Expand Down
2 changes: 1 addition & 1 deletion requirements-deploy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 26 additions & 1 deletion src/changelog2version/extract_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"^(?P<major>0|[1-9]\d*)\." # major version part
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"(<!--\smeta\s=\s)(.*?)(\s-->)", 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
1 change: 1 addition & 0 deletions src/changelog2version/update_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ def main():
'info': {
'version': semver_string,
'description': version_extractor.latest_description,
'meta': version_extractor.meta_data,
},
'releases': release_infos
}
Expand Down
32 changes: 32 additions & 0 deletions tests/data/valid/changelog_with_meta.md
Original file line number Diff line number Diff line change
@@ -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).

<!--
## [x.y.z] - yyyy-mm-dd
### Added
### Changed
### Removed
### Fixed
-->
<!--
RegEx for release version from file
r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}"
-->

## Released
## [1.3.0] - 2022-10-26
<!-- meta = {'type': 'feature', 'scope': ['all'], 'affected': ['all']} -->

### Added
- Something fixed

## [1.2.3] - 2022-07-31
### Fixed
- Something fixed

<!-- Links -->
[1.3.0]: https://github.com/brainelectronics/changelog2version/tree/1.3.0
[1.2.3]: https://github.com/brainelectronics/changelog2version/tree/1.2.3
21 changes: 16 additions & 5 deletions tests/test_extract_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
from pathlib import Path
from sys import stdout
from typing import List
from typing import Dict, List
from unittest.mock import mock_open, patch

from changelog2version.extract_version import (ExtractVersion,
Expand Down Expand Up @@ -125,21 +125,30 @@ 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",
[
"## [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"],
"<!-- meta = {'type': 'feature', 'scope': ['all'], 'affected': ['all']} -->\n\n### 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: Dict[str, str],
) -> None:
"""Test parse_changelog"""
changelog = self._here / 'data' / 'valid' / file_name
Expand All @@ -155,10 +164,12 @@ 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)
self.assertTrue(all(isinstance(ele, str)
for ele in self.ev.latest_description_lines))
self.assertEqual(len(self.ev.latest_description_lines), 3)
self.assertTrue(len(self.ev.latest_description_lines) in [3, 5])

@params(
# valid semver release version lines
Expand Down

0 comments on commit 2c60f70

Please sign in to comment.