From cf9d6f0eb02091081758db215c5282c22445edc9 Mon Sep 17 00:00:00 2001 From: Marnik Bercx Date: Wed, 20 Dec 2023 19:06:48 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Devops:=20Add=20`update=5Fchange?= =?UTF-8?q?log.py`=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/update_changelog.py | 87 +++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 .github/workflows/update_changelog.py diff --git a/.github/workflows/update_changelog.py b/.github/workflows/update_changelog.py new file mode 100644 index 0000000..fdea23c --- /dev/null +++ b/.github/workflows/update_changelog.py @@ -0,0 +1,87 @@ +#!/bin/bash +# -*- coding: utf-8 -*- +"""Script for updating the `CHANGELOG.md` based on the commits since the latest release tag.""" +import re +import subprocess +from pathlib import Path + +DEFAULT_CHANGELOG_SECTIONS = """ +### ‼️ Breaking changes + + +### ✨ New features + + +### 🗑️ Deprecations + + +### 👌 Improvements + + +### 🐛 Bug fixes + + +### 📚 Documentation + + +### 🔧 Maintenance + + +### ⬆️ Update dependencies + + +### ♻️ Refactor + +""" + + +def update_changelog(): + """Update the `CHANGELOG.md` for a first draft of the release.""" + + print("🔍 Checking the current version number") + current_changelog = Path("CHANGELOG.md").read_text(encoding="utf-8") + + from aiida_submission_controller import __version__ + + if str(__version__) in current_changelog: + print("🛑 Current version already in `CHANGELOG.md`. Skipping...") + return + + print("⬆️ Found updated version number, adapting `CHANGELOG.md`.") + tags = subprocess.run(["git", "tag", "--sort=v:refname"], capture_output=True, check=True, encoding="utf-8").stdout + latest_tag = re.findall(r"(v\d\.\d\.\d)\n", tags)[-1] + + print(f"🔄 Comparing with latest tag `{latest_tag}`.") + commits = subprocess.run( + ["git", "log", "--pretty=format:'%h|%H|%s'", f"{latest_tag}..origin/main"], + capture_output=True, + check=True, + encoding="utf-8", + ).stdout + + pr_pattern = re.compile(r"\(\S(?P\d+)\)") + + changelog_message = f"## v{__version__}\n" + DEFAULT_CHANGELOG_SECTIONS + + for commit in commits.splitlines(): + # Remove the PR number from the commit message + pr_match = pr_pattern.search(commit) + + if pr_match is not None: + pr_number = pr_match.groupdict()["pr_number"] + commit = commit.replace(rf"(#{pr_number})", "") + + # Add the commit hash (short) to link to the changelog + commit = commit.strip("'") + hash_short, hash_long, message = commit.split("|", maxsplit=2) + message += f" [[{hash_short}](https://github.com/aiidateam/aiida-submission-controller/commit/{hash_long})]" + changelog_message += f"\n* {message}" + + with Path("CHANGELOG.md").open("w", encoding="utf8") as handle: + handle.write(changelog_message + "\n\n" + current_changelog) + + print("🚀 Success! Finalise the `CHANGELOG.md` and let's get this baby released.") + + +if __name__ == "__main__": + update_changelog()