Skip to content

Commit

Permalink
Add Config Option to Exclude Pull Requests That Matches Labels
Browse files Browse the repository at this point in the history
  • Loading branch information
saadmk11 committed Aug 24, 2022
1 parent 33ddb9a commit cac3cc4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 40 deletions.
83 changes: 43 additions & 40 deletions scripts/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,56 +164,59 @@ def parse_changelog(self, file_type: str) -> str:
changelog_string = f"{header}\n{'=' * len(header)}\n\n"

group_config = self.config.group_config
exclude_labels = self.config.exclude_labels

if group_config:
for config in group_config:
if not group_config:
# If group config does not exist then append it without and groups
changelog_string += "".join(
[self._get_changelog_line(file_type, item) for item in new_changes]
)
return changelog_string

if len(new_changes) == 0:
break
for config in group_config:

items_string = ""
if len(new_changes) == 0:
break

pull_request_list = copy.deepcopy(new_changes)
items_string = ""

for pull_request in pull_request_list:
# check if the pull request label matches with
# any label of the config
if any(
label in pull_request["labels"] for label in config["labels"]
):
items_string += self._get_changelog_line(
file_type, pull_request
)
# remove the item so that one item
# does not match multiple groups
new_changes.remove(pull_request)

if items_string:
if file_type == MARKDOWN_FILE:
changelog_string += f"\n#### {config['title']}\n\n"
else:
changelog_string += (
f"\n{config['title']}\n {'-' * len(config['title'])}\n\n"
)
changelog_string += items_string

if new_changes and self.config.include_unlabeled_changes:
# if they do not match any user provided group
# Add items in `unlabeled group` group
pull_request_list = copy.deepcopy(new_changes)

for pull_request in pull_request_list:
# check if the pull request label matches with
# any label of the `exclude_labels` list
if any(label in pull_request["labels"] for label in exclude_labels):
# if it matches then remove it from the list
new_changes.remove(pull_request)
continue

# check if the pull request label matches with
# any label of the config
if any(label in pull_request["labels"] for label in config["labels"]):
items_string += self._get_changelog_line(file_type, pull_request)
# remove the item so that one item
# does not match multiple groups
new_changes.remove(pull_request)

if items_string:
if file_type == MARKDOWN_FILE:
changelog_string += (
f"\n#### {self.config.unlabeled_group_title}\n\n"
)
changelog_string += f"\n#### {config['title']}\n\n"
else:
changelog_string += (
f"\n{self.config.unlabeled_group_title}\n"
f"{'-' * len(self.config.unlabeled_group_title)}\n\n"
f"\n{config['title']}\n {'-' * len(config['title'])}\n\n"
)
changelog_string += "".join(
[self._get_changelog_line(file_type, item) for item in new_changes]
changelog_string += items_string

if new_changes and self.config.include_unlabeled_changes:
# if they do not match any user provided group
# Add items in `unlabeled group` group
if file_type == MARKDOWN_FILE:
changelog_string += f"\n#### {self.config.unlabeled_group_title}\n\n"
else:
changelog_string += (
f"\n{self.config.unlabeled_group_title}\n"
f"{'-' * len(self.config.unlabeled_group_title)}\n\n"
)
else:
# If group config does not exist then append it without and groups
changelog_string += "".join(
[self._get_changelog_line(file_type, item) for item in new_changes]
)
Expand Down
10 changes: 10 additions & 0 deletions scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Configuration(NamedTuple):
)
changelog_type: str = PULL_REQUEST
group_config: list[dict[str, str | list[str]]] = []
exclude_labels: list[str] = []
include_unlabeled_changes: bool = True
unlabeled_group_title: str = "Other Changes"
changelog_filename: str = f"CHANGELOG.{MARKDOWN_FILE}"
Expand Down Expand Up @@ -334,6 +335,15 @@ def clean_github_token(cls, value: Any) -> str | None:
gha_utils.notice("`github_token` was not provided as an input.")
return None

@classmethod
def clean_exclude_labels(cls, value: Any) -> list[str] | None:
"""clean exclude_labels item configuration option"""
if value and isinstance(value, list):
return value
else:
gha_utils.notice("`exclude_labels` was not provided as an input.")
return []

@classmethod
def clean_group_config(cls, value: Any) -> list[dict[str, Any]] | None:
"""clean group_config configuration option"""
Expand Down
10 changes: 10 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,13 @@ def test_clean_group_config_item(self, gha_utils):
Configuration._clean_group_config_item({"title": "test", "labels": None})
)
self.assertIsNone(Configuration._clean_group_config_item({"title": "test"}))

def test_clean_exclude_labels(self, gha_utils):
exclude_labels = ["skip-changelog", "dependabot"]

self.assertEqual(
Configuration.clean_exclude_labels(exclude_labels), exclude_labels
)

self.assertEqual(Configuration.clean_exclude_labels("test"), [])
self.assertEqual(Configuration.clean_exclude_labels({"title": "test"}), [])

0 comments on commit cac3cc4

Please sign in to comment.