-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtasks.py
More file actions
220 lines (168 loc) · 5.44 KB
/
Copy pathtasks.py
File metadata and controls
220 lines (168 loc) · 5.44 KB
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
"""
SPDX-FileCopyrightText: 2022 Aurélien Gâteau <mail@agateau.com>
SPDX-License-Identifier: GPL-3.0-or-later
A set of tasks to simplify the release process. See docs/release-check-list.md
for details.
"""
import json
import os
import re
import shutil
import sys
import time
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import List
from invoke import run, task
ARTIFACTS_DIR = Path("artifacts")
MAIN_BRANCH = "main"
def get_version():
return os.environ["VERSION"]
def erun(*args, **kwargs):
"""Like run, but with echo on"""
kwargs["echo"] = True
return run(*args, **kwargs)
def cerun(c, *args, **kwargs):
"""Like Context.run, but with echo on"""
kwargs["echo"] = True
return c.run(*args, **kwargs)
def ask(msg: str) -> str:
"""Show a message, wait for input and returns it"""
print(msg, end=" ")
return input()
def is_ok(msg: str) -> bool:
"""Show a message, append (y/n) and return True if user select y or Y"""
answer = ask(f"{msg} (y/n)").lower()
return answer == "y"
@task(help={"skip_changelog": "Add skip-changelog label"})
def create_pr(c, skip_changelog=False):
"""Create a pull-request and mark it as auto-mergeable"""
cmd = "gh pr create --fill"
if skip_changelog:
cmd += " --label skip-changelog"
result = cerun(c, cmd, warn=True)
if not result:
sys.exit(1)
cerun(c, "gh pr merge --auto -dm")
@task
def update_version(c):
version = get_version()
path = Path("Cargo.toml")
text = path.read_text()
text, count = re.subn(
r"^version = .*", f'version = "{version}"', text, flags=re.MULTILINE
)
assert count == 0 or count == 1
path.write_text(text)
@task
def prepare_release(c):
version = get_version()
run(f"gh issue list -m {version}", pty=True)
run("gh pr list", pty=True)
if not is_ok("Continue?"):
sys.exit(1)
erun(f"git checkout {MAIN_BRANCH}")
erun("git pull")
erun("git status -s")
if not is_ok("Continue?"):
sys.exit(1)
prepare_release2(c)
@task
def prepare_release2(c):
version = get_version()
erun("git checkout -b prep-release")
update_version(c)
erun(f"changie batch {version}")
print(f"Review/edit changelog (.changes/{version}.md)")
if not is_ok("Looks good?"):
sys.exit(1)
erun("changie merge")
print("Review CHANGELOG.md")
if not is_ok("Looks good?"):
sys.exit(1)
prepare_release3(c)
@task
def prepare_release3(c):
version = get_version()
# Rebuild to ensure Cargo.lock is updated
erun("cargo build")
erun("git add Cargo.toml Cargo.lock CHANGELOG.md .changes")
erun(f"git commit -m 'Prepare {version}'")
prepare_release4(c)
@task
def prepare_release4(c):
erun("cargo publish --dry-run")
erun("git push -u origin prep-release")
create_pr(c)
@task
def tag(c):
version = get_version()
erun(f"git checkout {MAIN_BRANCH}")
erun("git pull")
changes_file = Path(".changes") / f"{version}.md"
if not changes_file.exists():
print(f"{changes_file} does not exist, check previous PR has been merged")
sys.exit(1)
if not is_ok("Create tag?"):
sys.exit(1)
erun(f"git tag -a {version} -m 'Releasing version {version}'")
erun("git push")
erun("git push --tags")
def get_artifact_list() -> List[Path]:
assert ARTIFACTS_DIR.exists()
return list(ARTIFACTS_DIR.rglob("*.tar.gz")) + list(ARTIFACTS_DIR.rglob("*.zip"))
@task
def wait_for_tag_run(c) -> int:
version = get_version()
while True:
proc = run(f"gh run list -b {version} --json databaseId,status")
results = json.loads(proc.stdout)
assert isinstance(results, list)
assert len(results) == 1
run_id = results[0]["databaseId"]
status = results[0]["status"]
print(f"tag run ID: {run_id}, status: {status}")
if status == "completed":
return run_id
print("Waiting 30s")
time.sleep(30)
@task
def download_artifacts(c):
run_id = wait_for_tag_run(c)
if ARTIFACTS_DIR.exists():
shutil.rmtree(ARTIFACTS_DIR)
ARTIFACTS_DIR.mkdir()
erun(f"gh run download {run_id} --dir {ARTIFACTS_DIR}", pty=True)
def prepare_release_notes(version_md: Path) -> str:
"""
Take the content of $VERSION.md and return it ready to use as GitHub release notes:
- Remove header
- Turn all h3 into h2
"""
content = re.sub("^## .*", "", version_md.read_text())
content = re.sub("^### ", "## ", content, flags=re.MULTILINE)
return content.strip() + "\n"
@task
def publish(c):
version = get_version()
files_str = " ".join(str(x) for x in get_artifact_list())
print("Creating GitHub release")
with NamedTemporaryFile() as tmp_file:
content = prepare_release_notes(Path(".changes") / f"{version}.md")
tmp_file.write(content.encode("utf-8"))
tmp_file.flush()
erun(f"gh release create {version} -F{tmp_file.name} {files_str}")
print("Publishing on crates.io")
erun("cargo publish")
@task
def update_store(c):
version = get_version()
with c.cd("../clyde-store"):
cerun(c, "git checkout main")
cerun(c, "git pull")
cerun(c, "git checkout -b update-clyde")
cerun(c, "clydetools fetch clyde.yaml", pty=True)
cerun(c, "git add clyde.yaml")
cerun(c, f"git commit -m 'Update clyde to {version}'")
cerun(c, "git push -u origin update-clyde")
create_pr(c)