Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow link in changelog scope #8

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/rhenvision_changelog/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"(?P<jira_project>HMS[A-Z]*)-(?P<ref>[1-9]\d*)" # forces the HMS jira project prefix for now

class GitHubJiraProvider(GitHub):
Expand All @@ -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."""
Expand All @@ -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:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_changelog.py
Original file line number Diff line number Diff line change
@@ -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
[email protected]
1669736056
John Doe
[email protected]
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)"