Files
git-flow/src/git_flow/changelog.py
T

61 lines
1.6 KiB
Python

from datetime import datetime
from os.path import isfile
from git_flow.git import Commit
class Changelog:
FILENAME = "CHANGELOG.md"
COMMIT_MESSAGE = "chore: bump version and update CHANGELOG.md [skip ci]"
def update(self, title: str, commits: list[Commit]):
self._prepend(self.generate_entry(title, commits))
def generate_header(self, title):
now = datetime.now()
return f"""## {title} - {now.strftime("%F")}
"""
def generate_content(self, commits: list[Commit]):
content = ""
for commit in commits:
message = commit.subject
email = commit.email
username = email[: email.index("@")] if "@" in email else email
description = message
if ":" in message:
index = message.index(":")
commit_type = message[:index]
commit_message = message[index + 1 :]
description = f"**{commit_type}**: {commit_message}"
content += f"- {description} [[{username}](mailto:{email})] ({commit.sha})\n"
return content
def generate_footer(self):
return """
---
"""
def generate_entry(self, title: str, commits: list[Commit]):
return (
self.generate_header(title)
+ self.generate_content(commits)
+ self.generate_footer()
)
def _prepend(self, content: str):
if isfile(self.FILENAME):
with open(self.FILENAME, "r") as f:
content += f.read()
with open(self.FILENAME, "w") as f:
f.write(content)