Skip to content

Commit

Permalink
- Fix Review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpojer committed Nov 25, 2024
1 parent d779f69 commit 3a77a9e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
33 changes: 13 additions & 20 deletions release_notes_generator/action_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ def get_row_format_issue() -> str:
Get the issue row format for the release notes.
"""
if ActionInputs._row_format_issue is None:
ActionInputs._row_format_issue = ActionInputs._clean_row_format_invalid_keywords(
get_action_input(ROW_FORMAT_ISSUE, "{number} _{title}_ in {pull-requests}").strip()
ActionInputs._row_format_issue = ActionInputs._detect_row_format_invalid_keywords(
get_action_input(ROW_FORMAT_ISSUE, "{number} _{title}_ in {pull-requests}").strip(), clean=True
)
return ActionInputs._row_format_issue

Expand All @@ -176,8 +176,8 @@ def get_row_format_pr() -> str:
Get the pr row format for the release notes.
"""
if ActionInputs._row_format_pr is None:
ActionInputs._row_format_pr = ActionInputs._clean_row_format_invalid_keywords(
get_action_input(ROW_FORMAT_PR, "{number} _{title}_").strip()
ActionInputs._row_format_pr = ActionInputs._detect_row_format_invalid_keywords(
get_action_input(ROW_FORMAT_PR, "{number} _{title}_").strip(), clean=True
)
return ActionInputs._row_format_pr

Expand Down Expand Up @@ -267,31 +267,24 @@ def validate_inputs():
logger.debug("Print empty chapters: %s", print_empty_chapters)

@staticmethod
def _detect_row_format_invalid_keywords(row_format: str, row_type: str = "Issue") -> None:
def _detect_row_format_invalid_keywords(row_format: str, row_type: str = "Issue", clean: bool = False) -> str:
"""
Detects invalid keywords in the row format.
@param row_format: The row format to be checked for invalid keywords.
@param row_type: The type of row format. Default is "Issue".
@return: None
@return: If clean is True, the cleaned row format. Otherwise, the original row format.
"""
keywords_in_braces = re.findall(r"\{(.*?)\}", row_format)
invalid_keywords = [keyword for keyword in keywords_in_braces if keyword not in SUPPORTED_ROW_FORMAT_KEYS]
cleaned_row_format = row_format
for invalid_keyword in invalid_keywords:
logger.error(
f"Invalid `{invalid_keyword}` detected in `{row_type}` row format keyword(s) found: {', '.join(invalid_keywords)}. Will be removed from string."
"Invalid `{}` detected in `{}` row format keyword(s) found: {}. Will be removed from string.".format(
invalid_keyword, row_type, ", ".join(invalid_keywords)
)
)
if clean:
cleaned_row_format = cleaned_row_format.replace(f"{{{invalid_keyword}}}", "")

@staticmethod
def _clean_row_format_invalid_keywords(row_format: str) -> str:
"""
Detects and clean invalid keywords in the row format.
@param row_format: The row format to be checked for invalid keywords.
@return: The cleaned row format.
"""
keywords_in_braces = re.findall(r"\{(.*?)\}", row_format)
invalid_keywords = [keyword for keyword in keywords_in_braces if keyword not in SUPPORTED_ROW_FORMAT_KEYS]
for invalid_keyword in invalid_keywords:
row_format = row_format.replace(f"{{{invalid_keyword}}}", "")
return row_format
return cleaned_row_format
3 changes: 0 additions & 3 deletions release_notes_generator/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@
"""

import logging
import re

from typing import Optional

from github.GitRelease import GitRelease
from github.Repository import Repository

from release_notes_generator.utils.constants import SUPPORTED_ROW_FORMAT_KEYS

logger = logging.getLogger(__name__)


Expand Down
12 changes: 7 additions & 5 deletions tests/test_action_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import logging

import pytest
from coverage.files import actual_path
from scipy.stats.contingency import expected_freq

from release_notes_generator.action_inputs import ActionInputs

Expand Down Expand Up @@ -183,14 +185,14 @@ def test_detect_row_format_invalid_keywords_with_invalid_keywords(caplog):


def test_clean_row_format_invalid_keywords_no_keywords():
row_format = "{number} _{title}_ in {pull-requests}"
cleaned_format = ActionInputs._clean_row_format_invalid_keywords(row_format)
assert cleaned_format == row_format
expected_row_format = "{number} _{title}_ in {pull-requests}"
actual_format = ActionInputs._detect_row_format_invalid_keywords(expected_row_format, clean=True)
assert expected_row_format == actual_format


def test_clean_row_format_invalid_keywords_nested_braces():
row_format = "{number} _{title}_ in {pull-requests} {invalid_key} {another_invalid}"
expected_format = "{number} _{title}_ in {pull-requests} "
cleaned_format = ActionInputs._clean_row_format_invalid_keywords(row_format)
assert cleaned_format == expected_format
actual_format = ActionInputs._detect_row_format_invalid_keywords(row_format, clean=True)
assert expected_format == actual_format

2 changes: 1 addition & 1 deletion tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from release_notes_generator.action_inputs import ActionInputs

from release_notes_generator.utils.utils import get_change_url


Expand Down

0 comments on commit 3a77a9e

Please sign in to comment.