forked from proofpoint/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangelog.py
executable file
·88 lines (63 loc) · 2.42 KB
/
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
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
"""
changelog.py helps generate the CHANGELOG.md message for a particular release.
"""
import argparse
import subprocess
import shlex
import re
def run(cmd, *args, **kwargs):
return subprocess.check_output(shlex.split(cmd), *args, **kwargs)
def get_commit_ids(from_commit, to_commit):
cmd = "git log --format=%H --no-merges {from_commit}..{to_commit}"
commit_ids = run(cmd.format(from_commit=from_commit,
to_commit=to_commit)).splitlines()
return commit_ids
def get_commit_message(commit_id):
cmd = "git log --format=%B --max-count=1 {commit_id}".format(
commit_id=commit_id)
return run(cmd)
def fixes_issue_id(commit_message):
match = re.search(r"Fixes #(\d+)", commit_message)
if match:
return match.group(1)
def get_subject(commit_message):
return commit_message.splitlines()[0]
def get_changelog_message(commit_message, repo_url):
issue_id = fixes_issue_id(commit_message)
if issue_id:
subject = get_subject(commit_message)
return "Fixes", "{subject} ([#{issue_id}]({repo_url}/issues/{issue_id}))".format(subject=subject, issue_id=issue_id, repo_url=repo_url)
return None, get_subject(commit_message)
def get_latest_tag():
cmd = "git describe --tags --first-parent"
return run(cmd).split('-')[0]
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--repo_url", default="https://github.com/open-policy-agent/opa")
parser.add_argument("from_version", nargs="?",
default=get_latest_tag(), help="start of changes")
parser.add_argument("to_commit", nargs="?",
default="HEAD", help="end of changes")
return parser.parse_args()
def main():
args = parse_args()
changelog = {}
for commit_id in get_commit_ids(args.from_version, args.to_commit):
commit_message = get_commit_message(commit_id)
group, line = get_changelog_message(commit_message, args.repo_url)
changelog.setdefault(group, []).append(line)
if "Fixes" in changelog:
print "### Fixes"
print ""
for line in sorted(changelog["Fixes"]):
print "- {}".format(line)
print ""
if None in changelog:
print "### Miscellaneous"
print ""
for line in sorted(changelog[None]):
print "- {}".format(line)
if __name__ == "__main__":
main()