|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +"""Generate a changelog from our commit log.""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import datetime |
| 23 | +import sys |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +import dotenv |
| 27 | +import pygit2 |
| 28 | + |
| 29 | +from . import title_check |
| 30 | + |
| 31 | + |
| 32 | +def display(*args, **kwargs): |
| 33 | + print(*args, file=sys.stderr, **kwargs) |
| 34 | + |
| 35 | + |
| 36 | +def get_commit(repo: pygit2.Repository, rev: str) -> pygit2.Oid: |
| 37 | + try: |
| 38 | + return repo.lookup_reference_dwim(rev).target |
| 39 | + except KeyError: |
| 40 | + return repo[rev].id |
| 41 | + |
| 42 | + |
| 43 | +def list_commits( |
| 44 | + repo: pygit2.Repository, from_rev: str, to_rev: str |
| 45 | +) -> list[title_check.Commit]: |
| 46 | + root = Path(repo.workdir) |
| 47 | + from_commit = get_commit(repo, from_rev) |
| 48 | + to_commit = get_commit(repo, to_rev) |
| 49 | + walker = repo.walk(to_commit, pygit2.GIT_SORT_TIME) |
| 50 | + walker.hide(from_commit) |
| 51 | + commits = [] |
| 52 | + for commit in walker: |
| 53 | + title = commit.message.strip().split("\n")[0] |
| 54 | + commits.append(title_check.matches_commit_format(root, title)) |
| 55 | + return commits |
| 56 | + |
| 57 | + |
| 58 | +def format_commit(commit: title_check.Commit) -> str: |
| 59 | + components = "" |
| 60 | + warning = "" |
| 61 | + if commit.components: |
| 62 | + components = f"**{', '.join(commit.components)}**: " |
| 63 | + if commit.breaking_change: |
| 64 | + warning = "⚠️ " |
| 65 | + return f"{warning}{components}{commit.subject}" |
| 66 | + |
| 67 | + |
| 68 | +def format_section(title: str, commits: list[title_check.Commit]) -> list[str]: |
| 69 | + if not commits: |
| 70 | + return [] |
| 71 | + |
| 72 | + lines = [f"### {title}", ""] |
| 73 | + commits.sort(key=lambda commit: (commit.components, commit.subject)) |
| 74 | + lines.extend(f"- {format_commit(commit)}" for commit in commits) |
| 75 | + lines.append("") |
| 76 | + return lines |
| 77 | + |
| 78 | + |
| 79 | +def format_changelog( |
| 80 | + title: str, release: dict[str, str], commits: list[title_check.Commit] |
| 81 | +) -> str: |
| 82 | + date = datetime.date.today().strftime("%Y-%m-%d") |
| 83 | + lines = [ |
| 84 | + f"## {title} ({date})", |
| 85 | + "", |
| 86 | + "### Versions", |
| 87 | + "", |
| 88 | + f"- C/C++/GLib/Go/Python/Ruby: {release['VERSION_NATIVE']}", |
| 89 | + f"- C#: {release['VERSION_CSHARP']}", |
| 90 | + f"- Java: {release['VERSION_JAVA']}", |
| 91 | + f"- R: {release['VERSION_R']}", |
| 92 | + f"- Rust: {release['VERSION_RUST']}", |
| 93 | + "", |
| 94 | + ] |
| 95 | + |
| 96 | + breaking = [commit for commit in commits if commit.breaking_change] |
| 97 | + lines.extend(format_section("Breaking Changes", breaking)) |
| 98 | + |
| 99 | + feat = [commit for commit in commits if commit.category == "feat"] |
| 100 | + lines.extend(format_section("New Features", feat)) |
| 101 | + |
| 102 | + fix = [commit for commit in commits if commit.category == "fix"] |
| 103 | + lines.extend(format_section("Bugfixes", fix)) |
| 104 | + |
| 105 | + docs = [commit for commit in commits if commit.category == "docs"] |
| 106 | + lines.extend(format_section("Documentation Improvements", docs)) |
| 107 | + |
| 108 | + perf = [commit for commit in commits if commit.category == "perf"] |
| 109 | + lines.extend(format_section("Performance Improvements", perf)) |
| 110 | + |
| 111 | + return "\n".join(lines) |
| 112 | + |
| 113 | + |
| 114 | +def main(): |
| 115 | + parser = argparse.ArgumentParser(description=__doc__) |
| 116 | + parser.add_argument("from_rev", help="The start revision.") |
| 117 | + parser.add_argument("to_rev", help="The end revision.") |
| 118 | + parser.add_argument("--name", required=True, help="The name of the release.") |
| 119 | + |
| 120 | + args = parser.parse_args() |
| 121 | + |
| 122 | + repo_root = Path(__file__).parent.parent.parent.resolve() |
| 123 | + release = dotenv.dotenv_values(repo_root / "dev/release/versions.env") |
| 124 | + display("Opening repository at", repo_root) |
| 125 | + repo = pygit2.Repository(repo_root) |
| 126 | + |
| 127 | + commits = list_commits(repo, args.from_rev, args.to_rev) |
| 128 | + changelog = format_changelog(args.name, release, commits) |
| 129 | + print(changelog) |
| 130 | + |
| 131 | + return 0 |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + sys.exit(main()) |
0 commit comments