From 1118139db8fd43104c6d5874187a6df00492a481 Mon Sep 17 00:00:00 2001 From: Ondrej Ezr Date: Wed, 30 Aug 2023 20:02:06 +0200 Subject: [PATCH] Allow link in changelog scope --- src/rhenvision_changelog/changelog.py | 21 ++++++++++++--- tests/test_changelog.py | 39 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 tests/test_changelog.py diff --git a/src/rhenvision_changelog/changelog.py b/src/rhenvision_changelog/changelog.py index 4777ee0..209cd7e 100644 --- a/src/rhenvision_changelog/changelog.py +++ b/src/rhenvision_changelog/changelog.py @@ -7,9 +7,10 @@ from git_changelog import templates from git_changelog.build import Changelog from git_changelog.providers import GitHub, RefDef, RefRe +from git_changelog.commit import AngularConvention, Commit RefRe.BBB = r"(?:^|[\s,]|\(|/)" # blank or bracket before -RefRe.BBA = r"(?:[\s,]|$)|\)" # blank or bracket after +RefRe.BBA = r"(?:[\s,]|\)|$)" # blank or bracket after RefRe.JIRA_ISSUE = r"(?PHMS[A-Z]*)-(?P[1-9]\d*)" # forces the HMS jira project prefix for now class GitHubJiraProvider(GitHub): @@ -30,8 +31,22 @@ def build_ref_url(self, ref_type: str, match_dict: Dict[str, str]) -> str: # no match_dict["jira_url"] = self.jira_url if not match_dict.get("jira_project"): match_dict["jira_project"] = self.jira_project + return super().build_ref_url(ref_type, match_dict) +class AngularJiraConvention(AngularConvention): + """A class to enhance scope by an Jira issue URL.""" + + def parse_commit(self, commit: Commit): + result = super().parse_commit(commit) + scope = result["scope"] + if isinstance(scope, str) and "issues" in commit.text_refs: + for issue in commit.text_refs["issues"]: + if issue.ref in scope or scope in issue.ref: + result["scope"] = "[{}]({})".format(scope, issue.url) + + return result + class ProvisioningChangelog(Changelog): """A class to represent a Provisioning common project changelog.""" @@ -46,9 +61,9 @@ def __init__(self, repository: str, jira_url: str, jira_project: str, limit=None namespace, project = "/".join(split[3:-1]), split[-1] provider = GitHubJiraProvider(namespace, project, jira_url, jira_project) - super().__init__(repository, provider=provider, convention="angular", parse_provider_refs=True) + super().__init__(repository, provider=provider, convention=AngularJiraConvention, parse_provider_refs=True) - def get_log(self) -> str: + def get_log(self): """Get the `git log` output possibly limited by limit passed to constructor. Returns: diff --git a/tests/test_changelog.py b/tests/test_changelog.py new file mode 100644 index 0000000..788ef83 --- /dev/null +++ b/tests/test_changelog.py @@ -0,0 +1,39 @@ +"""Test changelog parses Jira issues properly""" + +from rhenvision_changelog.changelog import ProvisioningChangelog +from rhenvision_changelog.commit_check import check_commit + +jira_project = "HMS" +commit_head = """a5bc1e0f00aed28102579d14d651a27d5dded4a4 +John Doe +jdoe@test.com +1669736056 +John Doe +jdoe@test.com +1669736056 +HEAD -> branch, origin/branch +""" + +class StubbedChangelog(ProvisioningChangelog): + def __init__(self, commit_message): + self.commit_message = commit_message + super().__init__('.', 'https://jira.test.com', jira_project) + + def get_log(self) -> str: + """Get stubbed `git log` output defined by stub passed to constructor. + + Returns: + The stubbed `git log` in a particular format. + """ + return commit_head + self.commit_message + "\n\n"+self.MARKER+"\n" + + def get_remote_url(self) -> str: + return "https://github.com/RHEnVision/provisioning" + + + +def test_jira_issue_parsing() -> int: + changelog = StubbedChangelog("feat(HMS-123): Subject") + issue = changelog.commits[0].text_refs["issues"][0] + print(issue) + assert changelog.commits[0].convention["scope"] == "[HMS-123](https://jira.test.com/browse/HMS-123)"