|
| 1 | +# --------------------------------- |
| 2 | +# _____ __ __ ______ _ |
| 3 | +# / ___// /_ ___ / /________ ____ / ____/__ ______________ ______(_) |
| 4 | +# \__ \/ __ \/ _ \/ / ___/ __ \/ __ \ / /_ / _ \/ ___/ ___/ __ `/ ___/ / |
| 5 | +# ___/ / / / / __/ (__ ) /_/ / / / / / __/ / __/ / / / / /_/ / / / / |
| 6 | +# /____/_/ /_/\___/_/____/\____/_/ /_/ /_/ \___/_/ /_/ \__,_/_/ /_/ |
| 7 | +# --------------------------------- |
| 8 | +# Currency Conversion API - Changelog generation and version update |
| 9 | +# --------------------------------- |
| 10 | +# This Python script performs the following tasks: |
| 11 | +# |
| 12 | +# - Obtains the list of Git commits in the format: hash, date, author, and message. |
| 13 | +# - Defines and increments the project version according to the commits. |
| 14 | +# - Updates the 'CHANGELOG.md' file with a detailed log of all changes. |
| 15 | +# - Updates the '.env' file with the new project version. |
| 16 | +# - Ignores commits containing the keywords "AUTO_COMMIT" and "AUTO_CHANGELOG". |
| 17 | +# |
| 18 | +# Versions are incremented based on valid commits, and the changelog is generated |
| 19 | +# in reverse chronological order. |
| 20 | +# --------------------------------- |
| 21 | + |
| 22 | +import subprocess |
| 23 | +import os |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +# Function to read properties from a .properties file without section headers |
| 27 | +def read_properties(filepath): |
| 28 | + properties = {} |
| 29 | + with open(filepath, 'r', encoding='utf-8') as f: |
| 30 | + for line in f: |
| 31 | + line = line.strip() |
| 32 | + if not line or line.startswith('#') or line.startswith('!'): |
| 33 | + continue |
| 34 | + if '=' in line: |
| 35 | + key, value = line.split('=', 1) |
| 36 | + properties[key.strip()] = value.strip() |
| 37 | + return properties |
| 38 | + |
| 39 | +# Define the project directory as two levels up from the current script directory |
| 40 | +project_dir = Path(__file__).resolve().parent.parent |
| 41 | + |
| 42 | +# Load properties from project.properties |
| 43 | +properties_path = project_dir / 'project.properties' |
| 44 | +properties = read_properties(properties_path) |
| 45 | + |
| 46 | +# Extract user, repo, and i18n from properties |
| 47 | +user = properties['github.repo.user'] |
| 48 | +repo = properties['github.repo.name'] |
| 49 | +i18n = properties['i18n'] |
| 50 | + |
| 51 | +# Load i18n properties |
| 52 | +i18n_properties_path = project_dir / 'i18n' / f'{i18n}.properties' |
| 53 | +i18n_properties = read_properties(i18n_properties_path) |
| 54 | + |
| 55 | +# Execute Git command to get the list of commits |
| 56 | +git_command = ['git', 'log', '--reverse', '--pretty=format:%h - %ad - %an - %s', '--date=format:%d/%m/%Y %H:%M'] |
| 57 | +process = subprocess.Popen(git_command, stdout=subprocess.PIPE, text=True, cwd=project_dir) |
| 58 | +commits = process.stdout.readlines() |
| 59 | + |
| 60 | +# Initial version |
| 61 | +major, minor, patch = 0, 1, 0 |
| 62 | + |
| 63 | +# Function to increment version |
| 64 | +def increment_version(major, minor, patch): |
| 65 | + if patch < 9: |
| 66 | + patch += 1 |
| 67 | + else: |
| 68 | + patch = 0 |
| 69 | + if minor < 9: |
| 70 | + minor += 1 |
| 71 | + else: |
| 72 | + minor = 0 |
| 73 | + major += 1 |
| 74 | + return major, minor, patch |
| 75 | + |
| 76 | +# Prepare the changelog |
| 77 | +changelog_entries = [] |
| 78 | +last_version, last_date, last_author, last_commit_url = None, None, None, None |
| 79 | + |
| 80 | +for commit in commits: |
| 81 | + parts = commit.strip().split(' - ') |
| 82 | + commit_hash, date, author, message = parts[0], parts[1] + "h", parts[2], parts[3] |
| 83 | + commit_url = f"https://github.com/{user}/{repo}/commit/{commit_hash}" |
| 84 | + last_commit_url = commit_url |
| 85 | + |
| 86 | + # Terms to ignore in commit messages |
| 87 | + ignore_terms = ["AUTO_COMMIT", "AUTO_CHANGELOG"] |
| 88 | + |
| 89 | + # Ignore specified terms in commit messages |
| 90 | + if any(term in message for term in ignore_terms): |
| 91 | + continue |
| 92 | + |
| 93 | + version = f"[{major}.{minor}.{patch}]({commit_url})" |
| 94 | + last_version = f"{major}.{minor}.{patch}" |
| 95 | + last_date, last_author = date, author |
| 96 | + |
| 97 | + changelog_entries.append(f"{version} - {date} - {author}\n- {message}\n\n---\n") |
| 98 | + major, minor, patch = increment_version(major, minor, patch) |
| 99 | + |
| 100 | +# Generate the changelog content |
| 101 | +changelog_header = f"# {i18n_properties['project.title']} - {i18n_properties['label.project.change.log']}\n\n**{i18n_properties['label.current.version']}:** [{last_version}]({last_commit_url}) ({last_author}) - {last_date}\n\n---\n" |
| 102 | +changelog_content = changelog_header + "\n".join(reversed(changelog_entries)) |
| 103 | + |
| 104 | +# Write the changelog to the file |
| 105 | +changelog_path = project_dir / 'CHANGELOG.md' |
| 106 | +changelog_path.write_text(changelog_content, encoding='utf-8') |
| 107 | + |
| 108 | +print("File 'CHANGELOG.md' generated successfully.") |
| 109 | + |
| 110 | +# Update the .env file with the new version |
| 111 | +env_path = project_dir / '.env' |
| 112 | +env_content = f"PROJECT_VERSION={last_version}-SNAPSHOT\n" |
| 113 | +env_path.write_text(env_content, encoding='utf-8') |
| 114 | + |
| 115 | +print("File '.env' updated with the new project version.") |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
0 commit comments