Skip to content

Commit

Permalink
Merge pull request #16 from neuropoly/aj/03-prepend_to_existing_chang…
Browse files Browse the repository at this point in the history
…elog

Support prepending to existing changelog
  • Loading branch information
Drulex authored Oct 29, 2020
2 parents b745334 + 558525c commit 6c27f88
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <GITHUB_USER/REPO_NAME>. Example: neuropoly/spinalcordtoolbox
repo-url Repository url in the format <GITHUB_USER/REPO_NAME>. 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) 🎉
43 changes: 39 additions & 4 deletions changelog/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6c27f88

Please sign in to comment.