haystack

Форк
0
/
release_docs.py 
107 строк · 3.7 Кб
1
import os
2
import re
3
import sys
4
import base64
5
import argparse
6
import requests
7

8

9
VERSION_VALIDATOR = re.compile(r"^[0-9]+\.[0-9]+$")
10

11

12
class ReadmeAuth(requests.auth.AuthBase):
13
    def __call__(self, r):
14
        r.headers["authorization"] = f"Basic {readme_token()}"
15
        return r
16

17

18
def readme_token():
19
    api_key = os.getenv("RDME_API_KEY", None)
20
    if not api_key:
21
        raise Exception("RDME_API_KEY env var is not set")
22

23
    api_key = f"{api_key}:"
24
    return base64.b64encode(api_key.encode("utf-8")).decode("utf-8")
25

26

27
def get_versions():
28
    """
29
    Return all versions currently published in Readme.io.
30
    """
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()]
35

36

37
def create_new_unstable(current, new):
38
    """
39
    Create new version by copying current.
40

41
    :param current: Existing current unstable version
42
    :param new: Non existing new unstable version
43
    """
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()
48

49

50
def promote_unstable_to_stable(unstable, stable):
51
    """
52
    Rename the current unstable to stable and set it as stable.
53

54
    :param unstable: Existing unstable version
55
    :param stable: Non existing new stable version
56
    """
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()
61

62

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"
67

68

69
if __name__ == "__main__":
70
    parser = argparse.ArgumentParser()
71
    parser.add_argument(
72
        "-v", "--new-version", help="The new minor version that is being released (e.g. 1.9).", required=True
73
    )
74
    args = parser.parse_args()
75

76
    if VERSION_VALIDATOR.match(args.new_version) is None:
77
        sys.exit("Version must be formatted like so <major>.<minor>")
78

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)
82

83
    versions = get_versions()
84
    new_stable_is_published = new_stable in versions
85
    new_unstable_is_published = new_unstable in versions
86

87
    if new_stable_is_published and new_unstable_is_published:
88
        # If both versions are published there's nothing to do.
89
        # We fail gracefully.
90
        print(f"Both new version {new_stable} and {new_unstable} are already published.")
91
        sys.exit(0)
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.")
96

97
    # This version must exist since it's the one we're trying to promote
98
    # to stable.
99
    current_unstable = f"{new_stable}-unstable"
100

101
    if current_unstable not in versions:
102
        sys.exit(f"Can't find version {current_unstable} to promote to {new_stable}")
103

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)
108

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.