diff --git a/src/rhenvision_changelog/changelog.py b/src/rhenvision_changelog/changelog.py index 4777ee0..bdeceac 100644 --- a/src/rhenvision_changelog/changelog.py +++ b/src/rhenvision_changelog/changelog.py @@ -7,6 +7,7 @@ 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 @@ -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) -> dict[str, str | bool]: + 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: + result["scope"] = "[{}]({})".format(scope, issue.url) + + return result + class ProvisioningChangelog(Changelog): """A class to represent a Provisioning common project changelog.""" @@ -46,7 +61,7 @@ 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: """Get the `git log` output possibly limited by limit passed to constructor. diff --git a/tests/test_changelog.py b/tests/test_changelog.py new file mode 100644 index 0000000..d8791dc --- /dev/null +++ b/tests/test_changelog.py @@ -0,0 +1,37 @@ +"""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") + assert changelog.commits[0].convention["scope"] == "[HMS-123](https://jira.test.com/browse/HMS-123)"