-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_release_to_changelog.py
executable file
·79 lines (63 loc) · 2.04 KB
/
github_release_to_changelog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""
Generates a Markdown changelog from GitHub release notes
See:
- http://keepachangelog.com/en/0.3.0/
- https://developer.github.com/v3/
"""
import json
from argparse import ArgumentParser
import requests
CHANGELOG_HEADER = """# Change Log
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
"""
GITHUB_RELEASE_ENDPOINT = 'https://api.github.com/repos/{user}/{repo}/releases'
def generate_changelog(releases):
"""Generates a changelog from GitHub release notes"""
changelog = ""
for release in releases:
changelog += "\n## [{tag}]({url}) - {date}\n{body}\n".format(
tag=release['tag_name'],
url=release['html_url'],
date=release['published_at'].split('T')[0],
body=release['body'].replace('\r\n', '\n').replace('##', '###')
)
return changelog
def main():
"""Main entrypoint"""
parser = ArgumentParser()
g_source = parser.add_mutually_exclusive_group()
g_source.add_argument(
'-j',
'--json-file',
help="JSON file containing GitHub release data"
)
g_source.add_argument(
'-r',
'--gh-repo',
help="GitHub repository, e.g.: user/repository"
)
parser.add_argument(
'-o',
'--output',
default='CHANGELOG.md',
help="CHANGELOG file"
)
args = parser.parse_args()
if args.json_file:
with open(args.json_file) as f_json:
releases = json.loads(f_json.read())
elif args.gh_repo:
user, repo = args.gh_repo.split('/')
response = requests.get(
GITHUB_RELEASE_ENDPOINT.format(user=user, repo=repo)
)
response.raise_for_status()
releases = response.json()
with open(args.output, 'w') as f_output:
f_output.write(CHANGELOG_HEADER)
f_output.write(generate_changelog(releases))
if __name__ == '__main__':
main()