diff --git a/README.md b/README.md index 635f821..9396bc1 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) 🎉 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