Commit abc14d51 authored by Francesco Witz's avatar Francesco Witz
Browse files

feat: changelog section generation and file writing

parent 0fa35f42
import os
import re
import json
from datetime import datetime
from git_scrapper import Commit
class File():
......@@ -31,10 +33,65 @@ class File():
def write_chlog(self, newlines: list[str]):
""" Method that writes the new lines in the chlog file"""
pass
if os.path.exists(self.path):
with open(self.path, 'w') as file:
file.writelines(newlines)
def write_changes(file: File, commits: list[Commit]):
def write_changes(project_path: str, file: File, commits: list[Commit]):
""" Method that write the given commits by adding a new section to the changelog file """
# Getting released version
os.chdir(project_path)
with open("chlog.json") as chlog_file:
chlog = json.load(chlog_file)
version: str = chlog["version"]
# Creating title (version + date)
section: str = ["## [" + version[1:] + "] - " + datetime.today().isoformat().split('T')[0] + "\n"]
# Sorting commits by types
# Creating sections (title + unnumbered list)
\ No newline at end of file
added = []
changed = []
removed = []
security = []
doc = []
for commit in commits:
if commit.get_type() == "feat":
added.append(commit.chlog_format())
elif commit.get_type() == "fix":
changed.append(commit.chlog_format())
elif commit.get_type() == "removed":
removed.append(commit.chlog_format())
elif commit.get_type() == "security":
security.append(commit.chlog_format())
elif commit.get_type() == "doc":
doc.append(commit.chlog_format())
# Creating sections (title + unnumbered list)
if added:
section.append("### Added")
for e in added:
section.append("- " + e)
section.append("\n")
if changed:
section.append("### Changed")
for e in changed:
section.append("- " + e)
section.append("\n")
if removed:
section.append("### Removed")
for e in removed:
section.append("- " + e)
section.append("\n")
if security:
section.append("### Security")
for e in security:
section.append("- " + e)
section.append("\n")
if doc:
section.append("### Doc")
for e in doc:
section.append("- " + e)
section.append("\n")
# Finally writting everything to file
lines = file.read_chlog()
insert_index = file.get_where_to_write()
lines.insert(insert_index, section)
file.write_chlog(lines)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment