/
release.py.j2
198 lines (152 loc) · 6.26 KB
/
release.py.j2
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
{% include 'header.j2' %}
import argparse
import json
import os
import textwrap
from collections import defaultdict
from pathlib import Path
from git import Repo
from redminelib import Redmine
from redminelib.exceptions import ResourceAttrError
REDMINE_URL = "https://pulp.plan.io"
REDMINE_QUERY_URL = f"{REDMINE_URL}/issues?set_filter=1&status_id=*&issue_id="
def validate_redmine_data(redmine_query_url, redmine_issues):
"""Validate redmine milestone."""
redmine = Redmine("https://pulp.plan.io")
project_set = set()
stats = defaultdict(list)
milestone_url = "\n[noissue]"
milestone_id = None
for issue in redmine_issues:
redmine_issue = redmine.issue.get(int(issue))
project_name = redmine_issue.project.name
project_set.update([project_name])
stats[f"project_{project_name.lower().replace(' ', '_')}"].append(issue)
status = redmine_issue.status.name
if "CLOSE" not in status and status != "MODIFIED":
stats["status_not_modified"].append(issue)
try:
milestone = redmine_issue.fixed_version.name
milestone_id = redmine_issue.fixed_version.id
stats[f"milestone_{milestone}"].append(issue)
except ResourceAttrError:
stats["without_milestone"].append(issue)
if milestone_id is not None:
milestone_url = f"Redmine Milestone: {REDMINE_URL}/versions/{milestone_id}.json\n[noissue]"
print(f"\n\nRedmine stats: {json.dumps(stats, indent=2)}")
error_messages = []
if stats.get("status_not_modified"):
error_messages.append(f"One or more issues are not MODIFIED {stats['status_not_modified']}")
if stats.get("without_milestone"):
error_messages.append(
f"One or more issues are not associated with a milestone {stats['without_milestone']}"
)
if len(project_set) > 1:
error_messages.append(f"Issues with different projects - {project_set}")
if error_messages:
error_messages.append(f"Verify at {redmine_query_url}")
raise RuntimeError("\n".join(error_messages))
return milestone_url
release_path = os.path.dirname(os.path.abspath(__file__))
plugin_path = release_path.split("/.ci")[0]
plugin_name = "{{ plugin_snake }}"
version = None
with open(f"{plugin_path}/setup.py") as fp:
for line in fp.readlines():
if "version=" in line:
version = line.split('"')[1]
if not version:
raise RuntimeError("Could not detect existing version ... aborting.")
release_version = version.replace(".dev", "")
issues_to_close = []
for filename in Path(f"{plugin_path}/CHANGES").rglob("*"):
if filename.stem.isdigit():
issue = filename.stem
issue_url = f"{REDMINE_URL}/issues/{issue}.json"
issues_to_close.append(issue)
issues = ",".join(issues_to_close)
redmine_final_query = f"{REDMINE_QUERY_URL}{issues}"
milestone_url = validate_redmine_data(redmine_final_query, issues_to_close)
helper = textwrap.dedent(
"""\
Start the release process.
Example:
setup.py on plugin before script:
version="2.0.dev"
requirements = ["pulpcore>=3.4"]
$ python .ci/scripts/release.py minor 4.0 4.1
setup.py on plugin after script:
version="2.1.dev"
requirements = ["pulpcore>=4.0,<4.1"]
"""
)
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=helper)
parser.add_argument(
"release_type",
type=str,
help="Whether the release should be major, minor or patch.",
)
parser.add_argument(
"--lower", type=str, required=False, help="Lower bound of pulpcore requirement."
)
parser.add_argument(
"--upper", type=str, required=False, help="Upper bound of pulpcore requirement."
)
args = parser.parse_args()
release_type = args.release_type
if "pulpcore" not in release_path:
lower_pulpcore_version = args.lower
upper_pulpcore_version = args.upper
print("\n\nHave you checked the output of: $towncrier --version x.y.z --draft")
print(f"\n\nRepo path: {plugin_path}")
repo = Repo(plugin_path)
git = repo.git
git.checkout("HEAD", b=f"release_{release_version}")
# First commit: changelog
os.system(f"towncrier --yes --version {release_version}")
git.add("CHANGES.rst")
git.add("CHANGES/*")
git.commit("-m", f"Building changelog for {release_version}\n\n[noissue]")
# Second commit: release version
with open(f"{plugin_path}/requirements.txt", "rt") as setup_file:
setup_lines = setup_file.readlines()
with open(f"{plugin_path}/requirements.txt", "wt") as setup_file:
for line in setup_lines:
if "pulpcore" in line and "pulpcore" not in release_path:
line = f"pulpcore>={lower_pulpcore_version},<{upper_pulpcore_version}\n"
setup_file.write(line)
os.system("bump2version release --allow-dirty")
git.add(f"{plugin_path}/{plugin_name}/*")
git.add(f"{plugin_path}/docs/conf.py")
git.add(f"{plugin_path}/setup.py")
git.add(f"{plugin_path}/requirements.txt")
git.add(f"{plugin_path}/.bumpversion.cfg")
git.commit(
"-m",
f"Release {release_version}\n\nRedmine Query: {redmine_final_query}\n{milestone_url}",
)
sha = repo.head.object.hexsha
short_sha = git.rev_parse(sha, short=7)
# Third commit: bump to .dev
with open(f"{plugin_path}/requirements.txt", "wt") as setup_file:
for line in setup_lines:
if "pulpcore" in line and "pulpcore" not in release_path and release_type != "patch":
line = f"pulpcore>={lower_pulpcore_version}\n"
setup_file.write(line)
os.system(f"bump2version {release_type} --allow-dirty")
new_dev_version = None
with open(f"{plugin_path}/setup.py") as fp:
for line in fp.readlines():
if "version=" in line:
new_dev_version = line.split('"')[1]
if not new_dev_version:
raise RuntimeError("Could not detect new dev version ... aborting.")
git.add(f"{plugin_path}/{plugin_name}/*")
git.add(f"{plugin_path}/docs/conf.py")
git.add(f"{plugin_path}/setup.py")
git.add(f"{plugin_path}/requirements.txt")
git.add(f"{plugin_path}/.bumpversion.cfg")
git.commit("-m", f"Bump to {new_dev_version}\n\n[noissue]")
print(f"\n\nRedmine query of issues to close:\n{redmine_final_query}")
print(f"Release commit == {short_sha}")
print(f"All changes were committed on branch: release_{release_version}")