diff --git a/.env b/.env new file mode 100644 index 0000000..8303ced --- /dev/null +++ b/.env @@ -0,0 +1 @@ +VERSION=0.0.1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ccc2c50 --- /dev/null +++ b/.gitignore @@ -0,0 +1,483 @@ +# Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,pycharm+all,intellij+all,emacs,vim,macos,windows,linux +# Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode,pycharm+all,intellij+all,emacs,vim,macos,windows,linux + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data + + +### Intellij+all ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij+all Patch ### +# Ignore everything but code style settings and run configurations +# that are supposed to be shared within teams. + +.idea/* + +!.idea/codeStyles +!.idea/runConfigurations + +### Linux ### + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### macOS ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### macOS Patch ### +# iCloud generated files +*.icloud + +### PyCharm+all ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff + +# AWS User-specific + +# Generated files + +# Sensitive or high-churn files + +# Gradle + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake + +# Mongo Explorer plugin + +# File-based project format + +# IntelliJ + +# mpeltonen/sbt-idea plugin + +# JIRA plugin + +# Cursive Clojure plugin + +# SonarLint plugin + +# Crashlytics plugin (for Android Studio and IntelliJ) + +# Editor-based Rest Client + +# Android studio 3.1+ serialized cache file + +### PyCharm+all Patch ### +# Ignore everything but code style settings and run configurations +# that are supposed to be shared within teams. + + + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + +# ruff +.ruff_cache/ + +# LSP config files +pyrightconfig.json + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/python,visualstudiocode,pycharm+all,intellij+all,emacs,vim,macos,windows,linux +ChatAI + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c9bc1e6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +Copyright (c) 2024, Rybakov Viktor All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/LICENSE_RU b/LICENSE_RU new file mode 100644 index 0000000..48c6cac --- /dev/null +++ b/LICENSE_RU @@ -0,0 +1,8 @@ +Copyright (c) 2024, Рыбаков Виктор + +Разрешается повторное распространение и использование как в виде исходного кода, так и в двоичной форме, с изменениями или без, при соблюдении следующих условий: + +При повторном распространении исходного кода должно оставаться указанное выше уведомление об авторском праве, этот список условий и последующий отказ от гарантий. +При повторном распространении двоичного кода должна сохраняться указанная выше информация об авторском праве, этот список условий и последующий отказ от гарантий в документации и/или в других материалах, поставляемых при распространении. +Имена авторов и соавторов не могут быть использованы в качестве поддержки или продвижения продуктов, основанных на этом ПО без предварительного письменного разрешения. +ЭТА ПРОГРАММА ПРЕДОСТАВЛЕНА ВЛАДЕЛЬЦАМИ АВТОРСКИХ ПРАВ И/ИЛИ ДРУГИМИ СТОРОНАМИ «КАК ОНА ЕСТЬ» БЕЗ КАКОГО-ЛИБО ВИДА ГАРАНТИЙ, ВЫРАЖЕННЫХ ЯВНО ИЛИ ПОДРАЗУМЕВАЕМЫХ, ВКЛЮЧАЯ, НО НЕ ОГРАНИЧИВАЯСЬ ИМИ, ПОДРАЗУМЕВАЕМЫЕ ГАРАНТИИ КОММЕРЧЕСКОЙ ЦЕННОСТИ И ПРИГОДНОСТИ ДЛЯ КОНКРЕТНОЙ ЦЕЛИ. НИ В КОЕМ СЛУЧАЕ НИ ОДИН ВЛАДЕЛЕЦ АВТОРСКИХ ПРАВ И НИ ОДНО ДРУГОЕ ЛИЦО, КОТОРОЕ МОЖЕТ ИЗМЕНЯТЬ И/ИЛИ ПОВТОРНО РАСПРОСТРАНЯТЬ ПРОГРАММУ, КАК БЫЛО СКАЗАНО ВЫШЕ, НЕ НЕСЁТ ОТВЕТСТВЕННОСТИ, ВКЛЮЧАЯ ЛЮБЫЕ ОБЩИЕ, СЛУЧАЙНЫЕ, СПЕЦИАЛЬНЫЕ ИЛИ ПОСЛЕДОВАВШИЕ УБЫТКИ, ВСЛЕДСТВИЕ ИСПОЛЬЗОВАНИЯ ИЛИ НЕВОЗМОЖНОСТИ ИСПОЛЬЗОВАНИЯ ПРОГРАММЫ (ВКЛЮЧАЯ, НО НЕ ОГРАНИЧИВАЯСЬ ПОТЕРЕЙ ДАННЫХ, ИЛИ ДАННЫМИ, СТАВШИМИ НЕПРАВИЛЬНЫМИ, ИЛИ ПОТЕРЯМИ, ПРИНЕСЕННЫМИ ИЗ-ЗА ВАС ИЛИ ТРЕТЬИХ ЛИЦ, ИЛИ ОТКАЗОМ ПРОГРАММЫ РАБОТАТЬ СОВМЕСТНО С ДРУГИМИ ПРОГРАММАМИ), ДАЖЕ ЕСЛИ ТАКОЙ ВЛАДЕЛЕЦ ИЛИ ДРУГОЕ ЛИЦО БЫЛИ ИЗВЕЩЕНЫ О ВОЗМОЖНОСТИ ТАКИХ УБЫТКОВ. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..02e6168 --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +# README.md + +--- + +## Changelog Generator Tool + +This Python script is a changelog generator that creates a detailed changelog for a project based on git commit history. It identifies version changes, associates commits with issues from an issue tracker (currently supports Redmine), and generates a markdown file summarizing these changes. + +### Features + +- Supports different version containers like Maven (`pom.xml`), NPM (`package.json`), and environment files. +- Identifies commits associated with version changes. +- Extracts issue information from commit messages for Redmine. +- Generates a changelog in markdown format. + +### Prerequisites + +- Python 3 +- Git repository +- Redmine issue tracker (for advanced commit information) + +### Setup + +Ensure Python 3 is installed on your system. + +### Usage + +1. Clone your git repository locally, if you haven't already. +2. Navigate to the root directory of this script. +3. Run the script via the command line, adjusting the arguments as needed. + +#### Sample Command + +```shell +python easy_changelog.py --issue-tracker-url YOUR_REDMINE_URL --issue-tracker-token YOUR_API_KEY --version-control-system-commit-url YOUR_VCS_COMMIT_BASE_URL -C PATH_TO_YOUR_GIT_REPO +``` +Replace placeholders (`YOUR_REDMINE_URL`, `YOUR_API_KEY`, `YOUR_VCS_COMMIT_BASE_URL`, `PATH_TO_YOUR_GIT_REPO`) with applicable values. + +### Command Line Arguments + +- `--issue-tracker-type`: Optional. Issue tracker type. Currently, only `redmine` is supported. +- `--issue-tracker-url`: Required. Base URL of your issue tracker. +- `--issue-tracker-token`: Required. API key or token for the issue tracker. +- `--version-control-system-commit-url`: Required. Base URL used in the changelog to link commits. +- `-C` / `--repository-path`: Optional. Path to the git directory. If not provided, the current working directory is used by default. +- `--version-container-type`: Optional. The type of version container (`maven`, `npm`, `env`). Default is `maven`. +- `--version-container-path`: Optional. Path to the version file relative to the repository root. Default is `pom.xml` for Maven projects. +- `-o` / `--output-file`: Optional. Path to the output changelog file. Default is `CHANGELOG.md`. + +### Output + +The script generates a `CHANGELOG.md` file (or another specified file) in the root directory of this script, documenting all notable changes in the format recommended by [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). diff --git a/easy_changelog.py b/easy_changelog.py new file mode 100644 index 0000000..c5cca97 --- /dev/null +++ b/easy_changelog.py @@ -0,0 +1,486 @@ +#!/usr/bin/env python3 + +from http.client import HTTPResponse, HTTPSConnection +import json +import re +import sys +import subprocess +import shlex +from typing import Any +import xml.etree.ElementTree as ET +from collections import defaultdict +import argparse +from enum import Enum + + +class VersionContainer(Enum): + """`VersionContainer` + + An Enum representing different types of version containers. + - Attributes: `maven`, `npm`, `env` which are the types of version containers. + """ + + maven = "maven" + npm = "npm" + env = "env" + + def __str__(self): + return self.value + + +class Commit: + """`Commit` + + A class representing a single git commit. + - Properties: `sha`, `title`, `date`, `version`, `issue`, `issue_title`, `tracker`. + - Parameters: + - `sha`: A string representing the commit hash. + - `title`: Commit title. + - `date`: Commit date. + - `version`: Version related to the commit. + - `issue` (Optional): The issue ID related to this commit if any. + - `issue_title` (Optional): The title of the issue linked to this commit. + - `tracker` (Optional): The tracker system used for the linked issue. + """ + + sha: str + title: str + date: str + version: str + issue: str + issue_title: str + tracker: str + + def __init__( + self, + sha: str, + title: str, + date: str, + version: str, + issue: str = None, + issue_title: str = None, + tracker: str = None, + ): + self.sha = sha + self.title = title + self.date = date + self.version = version + self.issue = issue + self.issue_title = issue_title + self.tracker = tracker + + +def git_log_array( + git_dir: str, file: str = None, from_sha: str = None, to_sha: str = None +) -> list[str]: + """`git_log_array` + + Returns an array of strings obtained from git log command. + - Parameters: + - `git_dir`: Directory of the git repository. + - `file` (Optional): Specific file to track history. + - `from_sha` (Optional): Starting commit hash. + - `to_sha` (Optional): Ending commit hash. + - Returns: A list of strings, each representing a git commit log entry. + """ + cmd = [ + "git", + "-C", + git_dir, + "log", + "--pretty=format:%h%n%s%n%d%ci", + "-s", + "-z", + "--reverse", + "--no-merges", + ] + if from_sha and to_sha: + cmd.extend([f"{from_sha}..{to_sha}"]) + elif from_sha: + cmd.extend([f"{from_sha}"]) + if file: + cmd.extend(["--", file]) + print(f"[CMD]: {shlex.join(cmd)}") + result = subprocess.run(cmd, capture_output=True, text=True) + if result.returncode != 0: + print(f"[ERROR]: {result.stderr}") + exit(result.returncode) + git_log_history = result.stdout.split("\0") + return git_log_history + + +def git_show(git_dir: str, sha: str, version_file: str = "pom.xml") -> tuple[str, str]: + """`git_show` + + Shows the content of a version file at a specific commit. + - Parameters: + - `git_dir`: Directory of the git repository. + - `sha`: Commit hash. + - `version_file` (Optional): File containing the version, default is `pom.xml`. + - Returns: A tuple `(error, result)` where `error` is any error message, `result` is the file content at the given commit. + """ + cmd = ["git", "-C", git_dir, "show", f"{sha}:{version_file}"] + print(f"[CMD]: {shlex.join(cmd)}") + result = subprocess.run(cmd, capture_output=True, text=True) + if result.returncode != 0: + return (result.stderr, None) + return (None, result.stdout) + + +def parse_version_maven(file_content: str) -> str: + """`parse_version_maven` + + Parses the version from a Maven pom.xml file content. + - Parameters: + - `file_content`: Content of the pom.xml file. + - Returns: The version string if found, otherwise empty string. + """ + pre = "{http://maven.apache.org/POM/4.0.0}" + root = ET.ElementTree(ET.fromstring(file_content)) + result = "" + xpath = f"./{pre}version" + result = root.findtext(xpath) + return result + + +def parse_version_npm(file_content: str) -> str: + """`parse_version_npm` + + Parses the version from a npm package.json file content. + - Parameters: + - `file_content`: Content of the package.json file. + - Returns: The version string. + """ + file_json = json.loads(file_content) + return file_json["version"] + + +def parse_version_env(file_content: str, version_key: str = "VERSION") -> str: + """`parse_version_env` + + Parses the version from an environment file content. + - Parameters: + - `file_content`: Content of the env file. + - `version_key` (Optional): The key for the version variable, default is `VERSION`. + - Returns: The version string if found, otherwise `None`. + """ + env_vars = [] # or dict {} + for line in file_content: + if line.startswith("#") or not line.strip(): + continue + key, value = line.replace("export ", "", 1).strip().split("=", 1) + if key == version_key: + return value + env_vars.append(key) + print( + f"[WARN] cannot parse version. Found keys: {env_vars}. Expected: {version_key}" + ) + return None + + +def parse_issue_redmine(title: str) -> str: + """`parse_issue_redmine` + + Extracts an issue ID from a commit title assuming it mentions a Redmine issue. + - Parameters: + - `title`: The commit title. + - Returns: The issue ID as a string, or `None` if not found. + """ + result = re.search("#\d+", title) + if result: + return result.group()[1:] + return None + + +def fill_commits_info_stub( + commits: list[Commit], issue_tool_url: str, issue_tool_api_key: str +) -> None: + """`fill_commits_info_stub` + + A stub function to set tracker and issue title to "STUB" for demonstration. + - Parameters: + - `commits`: A list of `Commit` objects. + - `issue_tool_url`: The base URL of the issue tracking tool. + - `issue_tool_api_key`: API key for the issue tracking tool.""" + for c in commits: + if c.issue: + c.tracker = "STUB" + c.issue_title = "STUB" + + +def fill_commits_info_redmine( + commits: list[Commit], + issue_tool_url: str, + issue_tool_api_key: str, + chunk_size: int = 100, +) -> None: + """`fill_commits_info_redmine` + + Extracts detailed information about Redmine issues and updates the commits list. + - Parameters: + - `commits`: A list of `Commit` objects. + - `issue_tool_url`: The base URL of the Redmine instance. + - `issue_tool_api_key`: API key for Redmine. + - `chunk_size` (Optional): The batch size for fetching issues in bulk. + """ + batch: list[str] = [] + unique_issues: set[str] = set() + for i in range(len(commits)): + if len(unique_issues) >= chunk_size or i == len(commits) - 1: + fill_commits_info_redmine_batch(batch, issue_tool_url, issue_tool_api_key) + batch = [] + unique_issues = set() + elif commits[i].issue: + batch.append(commits[i]) + unique_issues.add(commits[i].issue) + + +def fill_commits_info_redmine_batch( + commits: list[Commit], issue_tool_url: str, issue_tool_api_key: str +) -> None: + """`fill_commits_info_redmine_batch` + + Helper function that processes information for a batch of commits with known issues from Redmine. + - Parameters: + - `commits`: A list of `Commit` objects for a specific batch. + - `issue_tool_url`: The base URL of the Redmine instance. + - `issue_tool_api_key`: API key for Redmine. + """ + commits_with_known_issues = list(filter(lambda c: c.issue, commits)) + commit_groups_by_issues = defaultdict(list) + for c in commits_with_known_issues: + commit_groups_by_issues[c.issue].append(c) + + request_url: str = f"/issues.json?issue_id={','.join(set(map(lambda i: i.issue, commits_with_known_issues)))}&limit={len(commit_groups_by_issues.keys())}&status_id=*" + conn: HTTPSConnection = HTTPSConnection(issue_tool_url) + print(f"[REQUEST]: curl https://{issue_tool_url}{request_url}") + conn.request( + "GET", + request_url, + headers={"X-Redmine-API-Key": issue_tool_api_key}, + ) + response: HTTPResponse = conn.getresponse() + result: str = response.read() + conn.close() + if response.status != 200: + print( + f"[WARN]: Error while getting issue info from redmine: STATUS: {response.status} REASON: {response.reason} BODY: {response.read()}" + ) + result_json = json.loads(result) + for issue in result_json["issues"]: + for commit in commit_groups_by_issues[str(issue["id"])]: + commit.tracker = issue["tracker"]["name"] + commit.issue_title = issue["subject"] + + +def sort_inside_versions(commits: list[Commit]) -> None: + """`sort_inside_versions` + + Sorts commits within each version group primarily by tracker, issue, and date. + - Parameters: + - `commits`: A list of `Commit` objects. + """ + version: str = commits[0].version + version_begin_idx: int = 0 + for i in range(len(commits)): + if commits[i].version != version: + version_end_idx: int = i if i == len(commits) - 1 else i - 1 + commits[version_begin_idx:version_end_idx] = sorted( + commits[version_begin_idx:version_end_idx], + key=lambda c: ( + c.tracker + "." + c.issue + "." + c.date + if c.tracker + else chr(sys.maxunicode) + ), + ) + version_begin_idx = i + version = commits[i].version + + +def filterout_none_issue(commits: list[Commit]) -> list[Commit]: + """`filterout_none_issue` + + Filters out commits without issue information. + - Parameters: + - `commits`: A list of `Commit` objects. + - Returns: A list of `Commit` objects that have issue information.""" + return filter(lambda c: c.tracker and c.issue, commits) + + +def build_changelog( + commits: list[Commit], + issue_tool_url: str, + version_control_commit_url: str, +) -> str: + """`build_changelog` + + Builds a comprehensive changelog from a list of detailed commit objects. + - Parameters: + - `commits`: A list of `Commit` objects, ideally with detailed issue information. + - `issue_tool_url`: Base URL for the issue tracker to link issues. + - `version_control_commit_url`: Base URL for linking to specific commits. + - Returns: A string containing the formatted changelog. + """ + commits_reversed = list(reversed(commits)) + changelog: str = """ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +""" + version, tracker, issue = ["", "", ""] + mod_count: int = 0 + version_date = "" + for i in range(len(commits_reversed)): + version_date = max(commits_reversed[i].date[:10], version_date) + print( + f"[INFO] building changelog: {int((i/len(commits_reversed))*100)}%", + end="\r", + ) + expected_mod_count = mod_count + if version != commits_reversed[i].version or mod_count != expected_mod_count: + changelog = changelog.replace("%version_date%", version_date) + version_date = "" + changelog += "\n\n" + version = commits_reversed[i].version + mod_count += 1 + changelog += f"""## [{version.replace("-SNAPSHOT", "")}] - %version_date%""" + if tracker != commits_reversed[i].tracker or mod_count != expected_mod_count: + changelog += "\n\n" + tracker = commits_reversed[i].tracker + mod_count += 1 + changelog += f"### {tracker}" + if issue != commits_reversed[i].issue or mod_count != expected_mod_count: + changelog += "\n\n" + issue = commits_reversed[i].issue + mod_count += 1 + changelog += f"- [#{issue}](https://{issue_tool_url}/issue/{issue}) {commits_reversed[i].issue_title} commits: " + + changelog += f" [{commits_reversed[i].sha}]({version_control_commit_url}{commits_reversed[i].sha})" + changelog = changelog.replace("%version_date%", version_date) + return changelog + + +def find_version_container_changes( + git_directory: str, version_container: str, version_parser +) -> list[Commit]: + """`find_version_container_changes` + + Identifies commits where a version change occurs based on a version file. + - Parameters: + - `git_directory`: Path to the git repository. + - `version_container`: Name/type of the version file. + - `version_parser`: A function to parse the version from the file content. + - Returns: A list of `Commit` objects representing version changes. + """ + previous_version: str = "" + version_container_changes: list[Commit] = [] + for commit_record in git_log_array(git_directory, version_container): + sha, title, date = commit_record.split("\n", 2) + error, result = git_show(git_directory, sha) + if not error: + version: str = version_parser(result) + # TODO: make error message condition independent from system locale + elif "exists on disk, but not in" in error: + version: str = "0.0" + else: + print(f"[ERROR]: {error}") + exit(1) + if version != previous_version: + version_container_changes.append(Commit(sha, title, date, version)) + previous_version = version + print( + f"[INFO]: found versions {list(map(lambda v: v.version, version_container_changes))}" + ) + return version_container_changes + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + # parser.add_argument("--issue-parser", required=True,) + parser.add_argument( + "--issue-tracker-type", + required=False, + help="Issue tracker type. Only redmine is supported now", + ) + parser.add_argument( + "--issue-tracker-url", required=True, help="Issue tracker base address" + ) + parser.add_argument( + "--issue-tracker-token", required=True, help="Issue tracker token or api key" + ) + parser.add_argument( + "--version-control-system-commit-url", + required=True, + help="Commit url used in changelog", + ) + parser.add_argument( + "-C", + "--repository-path", + required=False, + help="Path to git directory. Current workong directory used by default.", + default="", + ) + parser.add_argument( + "--version-container-type", + required=False, + help="Version container. Some bill of materials or properties file that contains application version. " + "Name of this file and it's structure should stay same along all git log history.", + type=VersionContainer, + choices=list(VersionContainer), + default=VersionContainer.maven, + ) + parser.add_argument( + "-o", + "--output-file", + required=False, + help="Target file", + default="CHANGELOG.md", + ) + parser.add_argument("--version-container-path", required=False, default="pom.xml") + # parser.add_argument("--log-level", required=False,) ## DEBUG -> CMD -> INFO -> WARN -> ERROR + # parser.add_argument("--version-filter", required=False,) + # parser.add_argument("--yanked-filter", required=False,) + args = parser.parse_args() + + version_container_parser: dict[VersionContainer, Any] = { + VersionContainer.maven: parse_version_maven, + VersionContainer.npm: parse_version_npm, + VersionContainer.env: parse_version_env, + } + + version_container_changes = find_version_container_changes( + args.repository_path, + args.version_container_path, + version_container_parser.get(args.version_container_type), + ) + + detailed_commits: list[Commit] = [] + for i in range(len(version_container_changes) - 1): + for commit_record in git_log_array( + args.repository_path, + from_sha=version_container_changes[i].sha, + to_sha=version_container_changes[i + 1].sha, + )[:-1]: + sha, title, date = commit_record.split("\n", 2) + issue: str = parse_issue_redmine(title) + detailed_commits.append( + Commit(sha, title, date, version_container_changes[i].version, issue) + ) + + fill_commits_info_redmine( + detailed_commits, args.issue_tracker_url, args.issue_tracker_token + ) + + sort_inside_versions(detailed_commits) + + filtered_commits = list(filterout_none_issue(detailed_commits)) + + changelog = build_changelog( + filtered_commits, args.issue_tracker_url, args.version_control_system_commit_url + ) + + with open(args.output_file, "w") as f: + f.write(changelog)