From 976946a1f62622ff2f297d9a3222d0c827b90e8c Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Thu, 5 Aug 2021 13:55:31 +0200 Subject: [PATCH] tools: Add update_changelog.py helper to tools directory This new helper script takes a branch name as input, then fetches `CHANGELOG.md` from that branch in the upstream mixxxdj/mixxx repo, converts the changelog into sphinx's ReStructured Text format and writes it to `source/chapters/appendix/version_history.py`. This makes keeping the manual repository changelog in sync with the code repository changelog much easier and will allow automation in the future. --- requirements.txt | 2 + tools/update_changelog.py | 77 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100755 tools/update_changelog.py diff --git a/requirements.txt b/requirements.txt index a129f0c1af..a840e65813 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,9 @@ sphinx sphinx-intl graphviz +m2r2 markupsafe +requests transifex-client sphinxcontrib-svg2pdfconverter sphinx-rtd-theme==0.5.2 diff --git a/tools/update_changelog.py b/tools/update_changelog.py new file mode 100755 index 0000000000..9f7c5bb705 --- /dev/null +++ b/tools/update_changelog.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import argparse +import os +import re + +import m2r2 +import requests + + +TEMPLATE = """ +.. This is a generated file. Do not edit it manually, because it is updated + automatically via tools/update_changelog.py. + +.. include:: /shortcuts.rstext + +.. _appendix-version-history: + +{content} + +.. seealso:: For an overview of previous versions, `take a look + `_ at the timeline. +""" + + +def fetch_changelog(branch): + r = requests.get( + "https://raw.githubusercontent.com/mixxxdj/mixxx/" + f"{branch}/CHANGELOG.md" + ) + r.raise_for_status() + return r.text + + +def changelog_to_rst(changelog): + # Replace headline with "Version History" + changelog = re.sub( + "^# Changelog$", + "# Version History", + changelog, + flags=re.MULTILINE | re.IGNORECASE, + ) + + # Ensure that all launchpad issues are hyperlinks + changelog = re.sub( + r"(?!\[)\blp:?(?P\d+)\b(?!\])", + r"[lp:\g](https://bugs.launchpad.net/mixxx/+bug/\g)", + changelog, + ) + + # Ensure that all GitHub PRs are hyperlinks + changelog = re.sub( + r"(?!\[)#\b(?P\d+)\b(?!\])", + r"[#\g](https://github.com/mixxxdj/mixxx/pull/\g)", + changelog, + ) + + return TEMPLATE.lstrip().format(content=m2r2.convert(changelog)) + + +def main(argv=None): + parser = argparse.ArgumentParser() + parser.add_argument("-b", "--branch", required=True) + args = parser.parse_args(argv) + + changelog = fetch_changelog(args.branch) + changelog = changelog_to_rst(changelog) + + path = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "../source/chapters/appendix/version_history.rst", + ) + with open(path, mode="w") as fp: + fp.write(changelog) + + +if __name__ == "__main__": + main()