-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_changelog.py
195 lines (163 loc) · 7.35 KB
/
process_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import json
import argparse
import re
from collections import OrderedDict
global parser
def parseArguments():
parser = argparse.ArgumentParser(description="Read commits and determine the next release number along with creating the changelog.")
parser.add_argument("release", metavar="last-release", type=str,
help="the last release value")
parser.add_argument("commit", metavar="commit-filename", type=str,
help="name of the file containing the summary & body of each commit")
parser.add_argument("output", metavar="changelog-output", type=str,
help="json-ready file to be sent to github for releasing the next version")
parser.add_argument('--verbose', '-v', action='count', default = 0,
help="up to 2 levels of verbosity")
args = parser.parse_args()
return args.release, args.commit, args.output, args.verbose
def analyzeCommits(filename):
logs = []
summary = OrderedDict(
type = [],
scope = [],
summary = [],
body = [],
breakingchange = [],
size = 0
)
# example of a element stored in the json file
# {
# "commit-hash": "11d07df4ce627d98bd30eb1e37c27ac9515c75ff",
# "abbreviated-commit-hash": "11d07df",
# "author-name": "Robert Lucian CHIRIAC",
# "author-email": "[email protected]",
# "author-date": "Sat, 27 Jan 2018 22:33:37 +0200",
# "subject": "@fix(automation): patch versions aren't released",
# "sanitized-subject-line": "fix-automation-patch-versions-aren-t-released",
# "body": "Nothing else to add. Fixes #24.",
# "commit-notes": ""
# }
with open(filename) as filelog:
changelog = json.load(filelog)
for index in range(len(changelog)):
subject = changelog[index]["subject"].lstrip().rstrip()
body = changelog[index]["body"].replace("\n", "").lstrip().rstrip()
# pattern sample: "@fix(automation): patch versions aren't released"
pattern_subject = re.compile("^(@)(\w*)([(])(\w*)([):]{2})(\s)(.*)$")
components_subject = pattern_subject.findall(subject)
if len(components_subject) > 0:
# it means we found the pattern in the subject of the commit message
# and take out the tuple out of the list
components_subject = components_subject[0]
summary["type"].append(components_subject[1])
summary["scope"].append(components_subject[3])
summary["summary"].append(components_subject[6])
summary["size"] += 1
else:
continue
breakingchange_and_others = re.compile("^(.*)((?i)(close|fixes|closes))(.*)(BREAKING CHANGE:)( ?)(.*)")
body_components = breakingchange_and_others.findall(body)
if len(body_components) > 0:
# we have a breaking change here and a footer mentioned fixed issues
body_components = body_components[0]
summary["body"].append(body_components[0])
summary["breakingchange"].append(body_components[6])
continue
breakingchange = re.compile("^(.*)(BREAKING CHANGE:)( ?)(.*)")
body_components = breakingchange.findall(body)
if len(body_components) > 0:
# we have a breaking change and that's it
body_components = body_components[0]
summary["body"].append(body_components[0])
summary["breakingchange"].append(body_components[3])
continue
footer = re.compile("^(.*)((?i)(close|fixes|closes))(.*)")
body_components = footer.findall(body)
if len(body_components) > 0:
# there's a footer and we also have a body message
body_components = body_components[0]
summary["body"].append(body_components[0])
summary["breakingchange"].append("")
else:
# no footer - just body message
summary["body"].append(body)
summary["breakingchange"].append("")
return summary
def nextRelease(last_release, commits):
# getting rid of the 'v' letter in front of the version
last_release = last_release[1:]
major, minor, patch = map(lambda x: int(x), last_release.split('.'))
breakingchange_list = list(filter(lambda x: len(x) > 0, commits["breakingchange"]))
if len(breakingchange_list) > 0:
major += 1
minor = 0
patch = 0
elif "feature" in commits["type"]:
minor += 1
patch = 0
elif len(commits["type"]) > 0:
patch += 1
return "v{}.{}.{}".format(major, minor, patch)
def makeChangelog(commits):
apparitions = OrderedDict()
apparitions["feature"] = []
apparitions["fix"] = []
apparitions["breakingchange"] = []
apparitions["docs"] = []
apparitions["refactor"] = []
apparitions["test"] = []
apparitions["chore"] = []
size = commits["size"]
for index in range(size):
apparitions[commits["type"][index]].append(index)
breakingchange_apparitions = list(enumerate(commits["breakingchange"]))
for supposed_occurrence in breakingchange_apparitions:
if len(supposed_occurrence[1]) > 0:
apparitions["breakingchange"].append(supposed_occurrence[0])
changelog = ""
for key, value in apparitions.items():
if len(value) > 0:
if key != "breakingchange":
changelog += "### {}\n\n".format(key.title())
for index_item in value:
changelog += "* **{}**: {} ".format(commits["scope"][index_item],
commits["summary"][index_item])
if len(commits["body"][index_item].strip(" .,:;!?")) > 0:
changelog += "**->** {}\n".format(commits["body"][index_item])
else:
changelog += "\n"
else:
changelog += "### Breaking Change\n"
for index_item in value:
changelog += "* {}\n".format(commits["breakingchange"][index_item])
changelog += "\n"
return changelog
def main():
last_release, commit_filename, output_filename, verbosity = parseArguments()
commits = analyzeCommits(commit_filename)
new_release = nextRelease(last_release, commits)
if last_release != new_release:
changelog = makeChangelog(commits)
if verbosity == 0:
print(new_release)
elif verbosity == 1:
print("old release: {}".format(last_release))
print("new release: {}".format(new_release))
if verbosity == 2:
print("commits organized: ")
print(json.dumps(commits, indent=2, sort_keys=False))
print("changelog: ")
print(changelog)
github_json_data = dict(
tag_name = new_release,
target_commitish = "master",
name = new_release,
body = changelog,
draft = False,
prerelease = False
)
github_data_json_dump = json.dumps(github_json_data, indent=2, sort_keys=False)
with open(output_filename, "w") as f:
f.write(github_data_json_dump)
if __name__ == "__main__":
main()