9
VERSION_VALIDATOR = re.compile(r"^[0-9]+\.[0-9]+$")
12
class ReadmeAuth(requests.auth.AuthBase):
13
def __call__(self, r):
14
r.headers["authorization"] = f"Basic {readme_token()}"
19
api_key = os.getenv("RDME_API_KEY", None)
21
raise Exception("RDME_API_KEY env var is not set")
23
api_key = f"{api_key}:"
24
return base64.b64encode(api_key.encode("utf-8")).decode("utf-8")
29
Return all versions currently published in Readme.io.
31
url = "https://dash.readme.com/api/v1/version"
32
res = requests.get(url, auth=ReadmeAuth(), timeout=30)
33
res.raise_for_status()
34
return [v["version"] for v in res.json()]
37
def create_new_unstable(current, new):
39
Create new version by copying current.
41
:param current: Existing current unstable version
42
:param new: Non existing new unstable version
44
url = "https://dash.readme.com/api/v1/version/"
45
payload = {"is_beta": False, "version": new, "from": current, "is_hidden": False, "is_stable": False}
46
res = requests.post(url, json=payload, auth=ReadmeAuth(), timeout=30)
47
res.raise_for_status()
50
def promote_unstable_to_stable(unstable, stable):
52
Rename the current unstable to stable and set it as stable.
54
:param unstable: Existing unstable version
55
:param stable: Non existing new stable version
57
url = f"https://dash.readme.com/api/v1/version/{unstable}"
58
payload = {"is_beta": False, "version": stable, "from": unstable, "is_hidden": False, "is_stable": True}
59
res = requests.put(url, json=payload, auth=ReadmeAuth(), timeout=30)
60
res.raise_for_status()
63
def calculate_new_unstable(version):
64
# version must be formatted like so <major>.<minor>
65
major, minor = version.split(".")
66
return f"{major}.{int(minor) + 1}-unstable"
69
if __name__ == "__main__":
70
parser = argparse.ArgumentParser()
72
"-v", "--new-version", help="The new minor version that is being released (e.g. 1.9).", required=True
74
args = parser.parse_args()
76
if VERSION_VALIDATOR.match(args.new_version) is None:
77
sys.exit("Version must be formatted like so <major>.<minor>")
79
# This two are the version that we must have published in the end
80
new_stable = f"{args.new_version}"
81
new_unstable = calculate_new_unstable(args.new_version)
83
versions = get_versions()
84
new_stable_is_published = new_stable in versions
85
new_unstable_is_published = new_unstable in versions
87
if new_stable_is_published and new_unstable_is_published:
88
# If both versions are published there's nothing to do.
90
print(f"Both new version {new_stable} and {new_unstable} are already published.")
92
elif new_stable_is_published or new_unstable_is_published:
93
# Either new stable or unstable is already published, it's to risky to
94
# proceed so we abort the publishing process.
95
sys.exit(f"Either version {new_stable} or {new_unstable} are already published. Too risky to proceed.")
97
# This version must exist since it's the one we're trying to promote
99
current_unstable = f"{new_stable}-unstable"
101
if current_unstable not in versions:
102
sys.exit(f"Can't find version {current_unstable} to promote to {new_stable}")
104
# First we create new unstable from the currently existing one
105
create_new_unstable(current_unstable, new_unstable)
106
# Then we promote the current unstable to stable since it's the one being published
107
promote_unstable_to_stable(current_unstable, new_stable)