forked from linode/manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_changelog.py
131 lines (102 loc) · 4.04 KB
/
generate_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
import subprocess
import re
import sys
RELEASE=sys.argv[1]
DATE=sys.argv[2]
ORIGIN=sys.argv[3]
START_INSERT=5
NOT_INCLUDED_IN_LOG = []
TEST_KEYWORDS = ['test', 'script', 'storybook', 'e2e']
BREAKING_KEYWORDS = ['break', 'deprecate']
CHANGED_KEYWORDS = ['update', 'change']
FIXED_KEYWORDS = ['fix', 'repair', 'bug']
def incrementLine():
global START_INSERT
START_INSERT+=1
return START_INSERT
def generateJQLQuery(ticket_list):
last_ticket=len(ticket_list) - 1
jql_query='key in('
for i,ticket in enumerate(ticket_list):
clean_ticket=ticket.strip().replace(':','')
if( i == last_ticket ):
jql_query+=clean_ticket+')'
else:
jql_query+=clean_ticket+','
print('~~~~~~~~NOT INCLUDED IN CHANGELOG~~~~~~~')
for i,commit in enumerate(NOT_INCLUDED_IN_LOG):
print(commit)
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print('~~~~~~~~TICKETS IN RELEASE~~~~~~~~')
print(jql_query.replace(' ','-'))
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
def checkKeyWords(list_keywords, commit):
for word in list_keywords:
if(word in commit):
return True
return False
# Given a commit message from a merged Pull Request, remove the PR ID.
# Example: "My first commit (#1)" -> "My first commit"
def remove_pull_request_id(commit_message):
regexp = "\(#\d+\)"
return re.sub(regexp, '', commit_message).strip()
def generateChangeLog(release, date, origin):
git_log_command = ["git", "log", "--no-merges", "--oneline", "--pretty='%s'", "{}/master...HEAD".format(origin)]
commits = subprocess.check_output(git_log_command, subprocess.STDOUT).decode('utf-8').split('\n')
# Strip the first and last characters of each line, which are single quotes.
commits = [c[1:-1] for c in commits]
breaking=[]
added=[]
changed=[]
fixed=[]
jql_query=[]
for commit in commits:
commit = remove_pull_request_id(commit)
jira_key_regex=re.match('M3-\d{4}', commit)
if (jira_key_regex is not None):
jira_key=jira_key_regex.group(0)
jql_query.append(jira_key)
commit.lstrip(jira_key)
if(checkKeyWords(TEST_KEYWORDS, commit.lower())):
NOT_INCLUDED_IN_LOG.append(commit)
continue
if(checkKeyWords(BREAKING_KEYWORDS, commit.lower())):
breaking.append(commit)
continue
if(checkKeyWords(CHANGED_KEYWORDS, commit.lower())):
changed.append(commit)
continue
if(checkKeyWords(FIXED_KEYWORDS, commit.lower())):
fixed.append(commit)
continue
added.append(commit)
generateJQLQuery(jql_query)
read_change_log=open('CHANGELOG.md', 'r')
change_log_lines=read_change_log.readlines()
read_change_log.close()
change_log_lines.insert(START_INSERT,'\n')
change_log_lines.insert(incrementLine(),'## [%s] - %s\n'%(release,date))
change_log_lines.insert(incrementLine(),'\n')
if( breaking ):
change_log_lines.insert(incrementLine(),'### Breaking:\n')
for commit in breaking:
change_log_lines.insert(incrementLine(),'- %s\n'%(commit))
change_log_lines.insert(incrementLine(),'\n')
if( added ):
change_log_lines.insert(incrementLine(),'### Added:\n')
for commit in added:
change_log_lines.insert(incrementLine(),'- %s\n'%(commit))
change_log_lines.insert(incrementLine(),'\n')
if( changed ):
change_log_lines.insert(incrementLine(),'### Changed:\n')
for commit in changed:
change_log_lines.insert(incrementLine(),'- %s\n'%(commit))
change_log_lines.insert(incrementLine(),'\n')
if( fixed ):
change_log_lines.insert(incrementLine(),'### Fixed:\n')
for commit in fixed:
change_log_lines.insert(incrementLine(),'- %s\n'%(commit))
change_log_lines.insert(incrementLine(),'\n')
write_change_log=open('CHANGELOG.md', 'w')
write_change_log.writelines(change_log_lines)
generateChangeLog(RELEASE, DATE, ORIGIN)