forked from conan-io/conan-vs-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_rc.py
286 lines (232 loc) · 11.4 KB
/
make_rc.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
#!/usr/bin/env python
import sys
import os
import re
import textwrap
from functools import partial
from datetime import date
try:
from github import Github, RateLimitExceededException
except ImportError:
sys.stderr.write("Install 'pip install PyGithub'")
sys.exit(1)
me = os.path.dirname(__file__)
source_pattern = re.compile(r'\s*public const string Version = \"(?P<v>[\d\.]+)\";', re.MULTILINE)
source_extension_cs = os.path.join(me, "Conan.VisualStudio", "source.extension.cs")
vsixmanifest_pattern = re.compile(r'\s+<Identity .*Version=\"(?P<v>[\d\.]+)\".*', re.MULTILINE)
source_vsixmanifest_cs = os.path.join(me, "Conan.VisualStudio", "source.extension.vsixmanifest")
# Get the github client
def get_github():
github_token = os.environ.get("GITHUB_TOKEN")
if not github_token:
sys.stderr.write("Please, provide a read-only token to access Github using environment variable 'GITHUB_TOKEN'\n")
# Find matching milestone
g = Github(github_token)
return g
# Get the github repository
def get_github_repository():
g = get_github()
return g.get_repo('conan-io/conan-vs-extension')
repo = get_github_repository()
def get_current_version():
v_source_extension = None
v_source_manifest = None
# Get version from 'source.extension.cs'
for line in open(source_extension_cs, "r").readlines():
m = source_pattern.match(line)
if m:
v_source_extension = m.group("v")
# Get version from 'source.extension.vsixmanifest'
for line in open(source_vsixmanifest_cs, "r").readlines():
m = vsixmanifest_pattern.match(line)
if m:
v_source_manifest = m.group("v")
assert v_source_extension == v_source_manifest, "Versions in {!r} and {!r} are different:" \
" {!r} != {!r}".format(source_extension_cs, source_vsixmanifest_cs, v_source_extension, v_source_manifest)
return v_source_extension
def set_current_version(version):
v_source_extension = None
v_source_manifest = None
def replace_closure(subgroup, replacement, m):
if m.group(subgroup) not in [None, '']:
start = m.start(subgroup)
end = m.end(subgroup)
return m.group(0)[:start] + replacement + m.group(0)[end:]
# Substitute version in 'source.extension.cs'
lines = []
for line in open(source_extension_cs, "r").readlines():
line_sub = source_pattern.sub(partial(replace_closure, "v", version), line)
lines.append(line_sub)
with open(source_extension_cs, "w") as f:
f.write("".join(lines))
# Substitute version in 'source.extension.vsixmanifest'
lines = []
for line in open(source_vsixmanifest_cs, "r").readlines():
line_sub = vsixmanifest_pattern.sub(partial(replace_closure, "v", version), line)
lines.append(line_sub)
with open(source_vsixmanifest_cs, "w") as f:
f.write("".join(lines))
def write_changelog(version, prs):
changelog = os.path.join(me, "CHANGELOG.md")
prs = [pr for pr in prs if "[skip changelog]" not in pr.title]
version_content = ["- {} ([#{}]({}))\n".format(pr.title, pr.number, pr.html_url) for pr in prs]
sys.stdout.write("*"*20)
sys.stdout.write("\n{}".format(''.join(version_content)))
sys.stdout.write("*"*20)
sys.stdout.write("\n\n")
if not query_yes_no("This is the list of items that will be added to the CHANGELOG"):
sys.stdout.write("Exit!")
sys.exit(1)
new_content = []
changelog_found = False
changelog_added = False
version_pattern = re.compile("## [\d\.]+")
for line in open(changelog, "r").readlines():
if changelog_added:
pass
elif not changelog_found:
changelog_found = bool(line.strip() == "# Changelog")
else:
if version_pattern.match(line):
# Add before new content
new_content.append("## {}\n\n".format(version))
new_content.append("**{}**\n\n".format(date.today().strftime('%Y-%m-%d')))
new_content += version_content
new_content.append("\n\n")
changelog_added = True
new_content.append(line)
with open(changelog, "w") as f:
f.write("".join(new_content))
def get_git_current_branch():
return os.popen('git rev-parse --abbrev-ref HEAD').read().strip()
def get_git_is_clean():
return len(os.popen('git status --untracked-files=no --porcelain').read().strip()) == 0
def query_yes_no(question, default="yes"):
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' (or 'y' or 'n').\n")
def work_on_release(next_release):
open_milestones = repo.get_milestones(state='open')
for milestone in open_milestones:
if str(milestone.title) == next_release:
# Gather pull requests
prs = [it for it in repo.get_pulls(state="all") if it.milestone == milestone]
sys.stdout.write("Found {} pull request for this milestone:\n".format(len(prs)))
for p in prs:
status = "[!]" if p.state != "closed" else ""
sys.stdout.write("\t {}\t#{} {}\n".format(status, p.number, p.title))
# Gather issues
issues = [it for it in repo.get_issues(milestone=milestone, state="all")]
sys.stdout.write("Found {} issues for this milestone:\n".format(len(issues)))
for issue in issues:
status = "[!]" if issue.state != "closed" else ""
sys.stdout.write("\t {}\t#{} {}\n".format(status, issue.number, issue.title))
# Any open PR or issue?
if any([p.state != "closed" for p in prs]) or any([issue.state != "closed" for issue in issues]):
sys.stderr.write("Close all PRs and issues belonging to the milestone before making the release")
return
# Checkout the release branch and commit the changes
os.system('git checkout -b release/{}'.format(next_release))
# Modify the working directory
set_current_version(next_release)
prs = [pr for pr in prs if pr.merged]
write_changelog(next_release, prs)
if query_yes_no("Commit and push to 'conan' repository"):
os.system("git add CHANGELOG.md")
os.system("git add Conan.VisualStudio/source.extension.cs")
os.system("git add Conan.VisualStudio/source.extension.vsixmanifest")
os.system('git commit -m "Preparing release {}"'.format(next_release))
os.system('git push --set-upstream conan release/{}'.format(next_release))
sys.stdout.write("Now create PR to 'master' and PR back to 'dev'")
pr = repo.create_pull(title="Release {}".format(next_release),
head="release/{}".format(next_release),
base="master",
body=textwrap.dedent("""
Release {}
Manual checking:
- [ ] VS 2019
- [ ] VS 2017
- [ ] General usability
After merging, don't forget to create the tag and merge back 'master' into 'dev'.
""".format(next_release)))
# TOO DANGEROUS: a simple click on 'update with dev' will make a commit to 'master'
#repo.create_pull(title="Merge back release branch {}".format(next_release),
# head="master", # So we get also the merge commit from 'master'
# base="dev",
# body="Merging back changes from release branch {}. Don't merge before #{}".format(next_release, pr.number))
else:
sys.stdout.write("You will need to commit and push yourself, and to create the PRs")
break
else:
sys.stderr.write("No milestone matching version {!r}. Open milestones found were '{}'\n".format(next_release, "', '".join([it.title for it in open_milestones])))
def guess_next_release(current_release, head_branch):
major, minor, patch = map(int, current_release.split("."))
open_milestones = repo.get_milestones(state='open')
# Look into open milestones with PRs already merged to 'head_branch' branch
ml_to_consider = []
closed_prs = repo.get_pulls(state="closed")
for milestone in open_milestones:
prs = [it for it in closed_prs if it.milestone == milestone]
merged_prs = [it for it in prs if it.merged and it.base.ref==head_branch]
if merged_prs:
ml_to_consider.append((milestone, merged_prs))
# If no PR
if not ml_to_consider:
sys.stderr.write("Cannot find any milestone suitable for the operation\n")
# If we have more than one, we should warn the user
if len(ml_to_consider) > 1:
sys.stderr.write("There are several open milestones with merged PRs into '{}' branch."
" Cannot decide which one to use for next release. Please,"
" reorganize milestones.\n".format(head_branch))
for ml, prs in ml_to_consider:
sys.stdout.write("Milestone: {}\n".format(milestone.title))
for it in prs:
sys.stdout.write("\t#{} {}\n".format(it.number, it.title))
sys.exit(1)
next_milestone, _ = ml_to_consider[0]
# Check it is a valid release and it is greater than the current one
next_major, next_minor, next_patch = map(int, next_milestone.title.split('.'))
assert (next_major >= major) or \
(next_major == major and next_minor >= minor) or \
(next_major == major and next_minor == minor and next_patch >= patch), "{} < {}!!".format(next_milestone.title, current_release)
return next_milestone.title
def main():
current_branch = get_git_current_branch()
if current_branch != "dev":
sys.stderr.write("Move to the 'dev' branch to work with this tool. You are in '{}'\n".format(current_branch))
sys.exit(1)
if not get_git_is_clean():
sys.stderr.write("Current branch is not clean\n")
sys.exit(1)
v = get_current_version()
sys.stdout.write("Current version is {!r}\n".format(v))
next_release = guess_next_release(v, current_branch)
if query_yes_no("Next version will be {!r}".format(next_release)):
work_on_release(next_release)
else:
sys.stdout.write("Sorry, I cannot help you then...")
if __name__ == "__main__":
try:
main()
except RateLimitExceededException:
sys.stderr.write("Rate limit!")
g = get_github()
r = g.get_rate_limit()
sys.stdout.write(" limit: {}".format(r.core.limit))
sys.stdout.write(" remaining: {}".format(r.core.remaining))