From b9f7c2f1113abd548f8c6a9f0343afa79b5ec306 Mon Sep 17 00:00:00 2001 From: Alexandru Jora Date: Thu, 29 Oct 2020 09:39:54 -0400 Subject: [PATCH 1/3] changelog: support prepending to existing changelog file --- changelog/changelog.py | 43 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/changelog/changelog.py b/changelog/changelog.py index daf67cb..7e9f443 100755 --- a/changelog/changelog.py +++ b/changelog/changelog.py @@ -188,6 +188,16 @@ def get_parser(): help="Logging level (eg. INFO, see Python logging docs)", ) + optional.add_argument("--update", + action='store_true', + help="Update an existing changelog file by prepending to it.", + ) + + optional.add_argument("--name", + type=str, + default='CHANGES.md', + help="Existing changelog file to use.", + ) return parser @@ -229,10 +239,35 @@ def main(): for diff in diff_pr: logger.warning('Pull request not labeled: %s', diff) - filename = f"{user}_{repo}_changelog.{milestone['number']}.md" - with io.open(filename, "w") as changelog: - changelog.write('\n'.join(lines)) - logger.info(f"Changelog saved in {filename}") + if args.update: + filename = args.name + if not os.path.exists(filename): + raise IOError(f"The provided changelog file: {filename} does not exist!") + + with io.open(filename, 'r') as f: + original = f.readlines() + + backup = f"{filename}.bak" + os.rename(filename, backup) + + with io.open(filename, 'w') as changelog: + # re-use first line from existing file since it most likely contains the title + changelog.write(original[0] + '\n') + + # write current changelog + changelog.write('\n'.join(lines)) + + # write back rest of changelog + changelog.writelines(original) + + logger.info(f"Backup created: {backup}") + + else: + filename = f"{user}_{repo}_changelog.{milestone['number']}.md" + with io.open(filename, "w") as changelog: + changelog.write('\n'.join(lines)) + + logger.info(f"Changelog written into {filename}") # provides customization to changelog for some repos From b1370cee77366efb165d3be47254e103e1325dea Mon Sep 17 00:00:00 2001 From: Alexandru Jora Date: Thu, 29 Oct 2020 09:40:03 -0400 Subject: [PATCH 2/3] Update README --- README.md | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 635f821..7074e9e 100644 --- a/README.md +++ b/README.md @@ -18,20 +18,39 @@ pip install -e . Then you can use changelog from anywhere: ```` -usage: changelog.py [-h] [--log-level LOG_LEVEL] repo-url +usage: changelog.py [-h] [--log-level LOG_LEVEL] [--update] [--name NAME] repo-url Changelog generator script required arguments: - repo-url Repository url in the format . Example: neuropoly/spinalcordtoolbox + repo-url Repository url in the format . Example: neuropoly/spinalcordtoolbox optional arguments: - -h, --help show this help message and exit - --log-level LOG_LEVEL Logging level (eg. INFO, see Python logging docs) + -h, --help show this help message and exit + --log-level LOG_LEVEL Logging level (eg. INFO, see Python logging docs) + --update Update an existing changelog file by prepending current changelog to it. + --name NAME Existing changelog file to use (by default use CHANGES.md). + + ```` +### Examples +``` +# create a new changelog file [user]_[repo]_changelog.[tagId].md for spinalcordtoolbox +changelog neuropoly/spinalcordtoolbox + +# prepend to an existing CHANGES.md file (default) +changelog neuropoly/spinalcordtoolbox --update + +# prepend to an existing CUSTOM_CHANGELOG.md +changelog neuropoly/spinalcordtoolbox --update --name CUSTOM_CHANGELOG.md + +# run in debug +changelog neuropoly/spinalcordtoolbox --log-level DEBUG +``` + To use a Github Personal Access Token (https://github.com/settings/tokens) simply export the token string via the `GITHUB_TOKEN` environment variable. -The script will produce a `[user]_[repo]_changelog.[tagId].md` file with changelog contents. The contents can then be verified manually and copied over to the respective project `CHANGES.md` file. +Unless --update is passed, the script will produce a `[user]_[repo]_changelog.[tagId].md` file with changelog contents. Contributions are welcome (via a fork of the repository and pull request) 🎉 From 558525c08ef79d26ac78dc83f3d0d02bbf082381 Mon Sep 17 00:00:00 2001 From: Alexandru Jora Date: Thu, 29 Oct 2020 09:45:56 -0400 Subject: [PATCH 3/3] formatting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7074e9e..9396bc1 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,6 @@ changelog neuropoly/spinalcordtoolbox --log-level DEBUG To use a Github Personal Access Token (https://github.com/settings/tokens) simply export the token string via the `GITHUB_TOKEN` environment variable. -Unless --update is passed, the script will produce a `[user]_[repo]_changelog.[tagId].md` file with changelog contents. +Unless `--update` is passed, the script will produce a `[user]_[repo]_changelog.[tagId].md` file with changelog contents. Contributions are welcome (via a fork of the repository and pull request) 🎉