-
Notifications
You must be signed in to change notification settings - Fork 194
/
release.py
382 lines (334 loc) · 13.6 KB
/
release.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
'''
Make sure you follow the repos from this page: https://app.circleci.com/projects/project-dashboard/github/reactioncommerce/
'''
import subprocess
import semantic_version
import os
from collections import defaultdict
from git import Repo
import fileinput
import time
from shutil import copyfile
import sys
import requests
from github import Github
import re
CORE_MEMBERS = ['[email protected]', '[email protected]']
QA = 'manueldelreal'
repos = [
'reaction-admin',
'reaction',
'example-storefront',
]
previousReleaseVersion = {
'reaction-admin': '',
'reaction': '',
'example-storefront': '',
}
dockerRepoDict = {
'reaction-admin': 'admin',
'reaction': 'reaction',
'example-storefront': 'example-storefront',
}
repoVersions = {}
# list of commits across all repos
allCommits = []
class cd:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
def line_prepender(filename, line):
with open(filename, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(line + content)
def getBump(commits):
level = 'patch'
for author, commit, pullNumber in commits:
if commit.lower().startswith('feat'):
## its minor if not already major
if level != 'major':
level = 'minor'
if 'BREAKING CHANGE:' in commit:
return 'major'
return level
def getVersion(prevVersion, commits):
if prevVersion.prerelease:
# only increment the build version
nextVersion = semantic_version.Version(major=prevVersion.major, minor=prevVersion.minor, patch=prevVersion.patch, prerelease=(prevVersion.prerelease[0], str(int(prevVersion.prerelease[1]) + 1) ))
else:
bump = getBump(commits)
if bump == 'minor':
nextVersion = prevVersion.next_minor()
elif bump == 'major':
nextVersion = prevVersion.next_major()
else:
nextVersion = prevVersion.next_patch()
return str(nextVersion)
def generateChangelog(commits, version, bump, prevVersion, repo):
categoriesOrder = {
'Feature': 0,
'Fixes': 0,
'Refactors': 0,
'Tests': 0,
'Chore': 0,
'Docs': 0,
'Style': 0,
'Performance': 0,
}
categoriesDict = {
'feat': 'Feature',
'fix': 'Fixes',
'docs': 'Docs',
'style': 'Style',
'refactor': 'Refactors',
'perf': 'Performance',
'test': 'Tests',
'chore': 'Chore',
}
changelogDict = defaultdict(str)
contributors = set([])
for author, commit, pullNumber in commits:
authorEmail = author.email.lower()
if not (authorEmail.endswith('@mailchimp.com') or authorEmail in CORE_MEMBERS or 'dependabot' in authorEmail):
contributors.add(author.name)
key = commit.split(':')[0].lower()
if '(' in key:
# if the key has a scope
key = key.split('(')[0]
if key not in categoriesDict.keys():
# anything that is not in above is a fix
key = 'fix'
changelogDict[key] += '\n - ' + commit.split('\n')[0] + f' [#{pullNumber}](https://github.com/reactioncommerce/{repo}/pull/{pullNumber})'
changelogDoc = sorted(map(lambda item: (categoriesOrder[categoriesDict[item[0]]], f'\n\n## {categoriesDict[item[0]]}\n{item[1]}'), changelogDict.items()))
changelogDoc = ''.join([v for k, v in changelogDoc])
if contributors:
changelogDoc += f'\n\n## Contributors\n\n Special thanks to {", ".join(contributors)} for contributing to the release!'
return f'# v{version}\n\n{repo} v{version} adds {bump} features or bug fixes and contains no breaking changes since v{str(prevVersion)}.{changelogDoc}\n\n'
def getLatestVersion():
# Get latest relase tag name
process = subprocess.Popen(['git', 'describe', '--abbrev=0', '--tags'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
tag, stderr = process.communicate()
tag = tag.decode("utf-8").strip('\n')
version = semantic_version.Version(tag[1:])
if stderr:
print(stderr)
print("Error getting tag")
return version
def prepareTrunk(branch='trunk'):
# Stashing the changes
print("## Stashing the changes")
process = subprocess.Popen('git stash',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
# Change branch to trunk
print(f"## Running git checkout {branch}")
process = subprocess.Popen(['git', 'checkout', branch],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
# Pull the latest changes
print("## Running git pull")
process = subprocess.Popen(['git', 'pull', 'origin'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
def restoreTrunk():
# unstashing the changes
print("## Un-stashing the changes")
process = subprocess.Popen('git stash apply',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
def createPR(repo, version, files, changelogDoc, reviewer='reactioncommerce/oc', branch='trunk'):
# create new brach
print("## Creating release branch locally")
process = subprocess.Popen(f'git checkout -b release-next-v{version}',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
if 'fatal' in str(stderr):
exit()
# staging files
print("## Staging files")
process = subprocess.Popen(f'git add {" ".join(files)}',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
if 'fatal' in str(stderr):
exit()
# commiting files
print("## Comitting files")
process = subprocess.Popen(f'git commit -m "ci: v{version}" --signoff',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
# pushing the branch to github
print("## Pushing the branch to github")
process = subprocess.Popen(f'git push origin HEAD',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
print(f'gh pr create --title "Release v{version}" --base {branch} --body {changelogDoc} --reviewer {reviewer}')
# Making PR on github
print("## Making PR on github")
process = subprocess.Popen(f'gh pr create --title "Release v{version}" --base {branch} --body "{changelogDoc}" --reviewer {reviewer}',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
return str(stdout).strip('\n')
def getCommits(repo, prevVersion):
gitRepo = Repo()
user = Github(os.environ['GITHUB_TOKEN_RELEASE'])
repo = user.get_repo(f'reactioncommerce/{repo}')
bareCommits = list(gitRepo.iter_commits(f'HEAD...v'+str(prevVersion)))
commits = []
for index, c in enumerate(bareCommits):
try:
commit = repo.get_commit(sha=str(c))
except:
continue
commitPulls = commit.get_pulls()
pullNumber = 0
for pull in commitPulls:
pullNumber = pull.number
if not pullNumber:
continue
message = '\n'.join(filter(lambda msg: 'Signed-off-by:' not in msg, c.message.strip('\n').strip('\t').split('\n')))
if message.startswith("Merge "):
# these are generate when PRs are merged.
pass
else:
commits.append((c.author, message, pullNumber))
return commits
def createRelease(version, changelogDoc):
print("## Creating release on github")
process = subprocess.Popen(f'gh release create v{version} -d -t v{version} -n "{changelogDoc}"',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
print(stdout)
print(stderr)
def getCurrentRepoVersion():
print(os.getcwd())
for line in fileinput.FileInput('config.mk'):
res = re.search("^https://github.com/reactioncommerce/.*,",line)
if res:
lineArr = line.split(",")
previousReleaseVersion[lineArr[-2]]=lineArr[-1].replace('\\','').replace('v','').strip()
def prepareRepos():
getCurrentRepoVersion()
for repo in repos:
# Go inside the repo
with cd(repo):
prepareTrunk()
latestVersion = getLatestVersion()
print("Latest version of {0} is {1}".format(repo,latestVersion))
# Get commits
commits = getCommits(repo, previousReleaseVersion[repo])
if not commits:
continue
allCommits.extend(commits)
repoVersions[repo] = (str(previousReleaseVersion[repo]), str(latestVersion))
def updateDevPlatform():
print("Starting Reaction Development Platform update.")
prepareTrunk()
prevVersion = getLatestVersion()
version = getVersion(prevVersion, allCommits)
bump = getBump(allCommits)
print("Next version of reaction-dev-platform is", version)
allRepos = list(repoVersions.items()) + [('reaction-development-platform', (prevVersion, version))]
# add dev platform to all repos
for repo, (prevVersion, ver) in allRepos:
print(f'Updating {repo} from {prevVersion} to {ver}')
with fileinput.FileInput('README.md', inplace=True) as file:
for line in file:
print(line.replace(f'[`{prevVersion}`](https://github.com/reactioncommerce/{repo}/tree/v{prevVersion})', f'[`{ver}`](https://github.com/reactioncommerce/{repo}/tree/v{ver})'), end='')
releases = ", ".join(map(lambda repoVersionTuple: f'[{repoVersionTuple[0]} v{repoVersionTuple[1][1]}](https://github.com/reactioncommerce/{repoVersionTuple[0]}/releases/tag/v{repoVersionTuple[1][1]})', repoVersions.items()))
changelogDoc = f'# v{str(ver)}\nThis release is coordinated with the release of {releases} to keep the `reaction-development-platform` up-to-date with the latest version of all our development platform projects.\n'
# Send changelog to the file
print("## Writing changelog to file")
line_prepender('CHANGELOG.md', changelogDoc)
destConfig = f'./config/reaction-oss/reaction-v{str(version)}.mk'
print("## Copying config file")
copyfile(f'./config/reaction-oss/reaction-v{str(prevVersion)}.mk', destConfig)
## replacing version number at the top of the file
with fileinput.FileInput(destConfig, inplace=True) as file:
for line in file:
if "Reaction OSS" in line:
print(f'### Reaction OSS v{version}')
else:
print(line, end='')
for repo, (prevVersion, ver) in repoVersions.items():
with fileinput.FileInput(destConfig, inplace=True) as file:
for line in file:
print(line.replace(f'{repo},v{str(prevVersion)}', f'{repo},v{str(ver)}'), end='')
# updating the config file
print("## updating config file")
for repo, (prevVersion, ver) in repoVersions.items():
with fileinput.FileInput('config.mk', inplace=True) as file:
for line in file:
print(line.replace(f'{repo},v{str(prevVersion)}', f'{repo},v{str(ver)}'), end='')
files = ['README.md', 'CHANGELOG.md', 'config.mk', destConfig]
createPR('reaction-development-platform', version, files, changelogDoc, QA)
restoreTrunk()
return version
def prerequisite():
# Current directory should be reaction-development-platform
pwd = os.getcwd()
if not pwd.endswith('reaction-development-platform'):
print("Error: Please run the script from reaction-development-platform")
sys.exit()
process = subprocess.Popen('gh --version',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
if stderr:
print("Error: github command line not installed. Please install gh")
sys.exit()
process = subprocess.Popen('gh auth status',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
if "Logged in" not in str(stderr):
print(stderr)
print("Error: Please login into github using command line")
sys.exit()
if not os.environ['CIRCLE_TOKEN']:
print("Error: Please export yout CircleCI key as CIRCLE_TOKEN")
sys.exit()
if not os.environ['GITHUB_TOKEN_RELEASE']:
print("Error: Please export yout GithubKey key as GITHUB_TOKEN_RELEASE")
sys.exit()
def main():
prerequisite()
prepareRepos()
devVersion = updateDevPlatform()
if __name__ == "__main__":
main()