File: release.py

package info (click to toggle)
bird3 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,600 kB
  • sloc: ansic: 85,199; sh: 3,807; perl: 3,484; lex: 976; python: 726; makefile: 527; xml: 520; sed: 13
file content (366 lines) | stat: -rwxr-xr-x 12,924 bytes parent folder | download
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/python3

import jinja2
import logging
import os
import pathlib
import requests
import subprocess
import sys

logging.basicConfig(format='%(levelname)# 8s | %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

class CommandError(Exception):
    def __str__(self):
        return f"""Command {self.args[0].args} failed with code {self.args[0].returncode}.
        {self.args[0].stdout.decode()}
        {self.args[0].stderr.decode()}"""

def cmd(*args):
    result = subprocess.run(args, capture_output=True)
    if result.returncode != 0:
        raise CommandError(result)

    return result.stdout.decode().split("\n")

class ReleaseException(Exception):
    pass

class Version:
    def __init__(self, *args):
        if len(args) == 3:
            for a in args:
                assert(isinstance(a, int))
            
            self.major, self.minor, self.patch = tuple(args)

        else:
            assert(len(args) == 1)
            s = args[0].split('.')
            if len(s) > 3:
                raise ReleaseException(f"Weird version: {value} (too many dots)")

            try:
                self.major = int(s[0])
                self.minor = int(s[1])
            except Exception as e:
                raise ReleaseException(f"Weird version: {value}") from e

            try:
                self.patch = int(s[2])
            except IndexError:
                self.patch = 0
            except Exception as e:
                raise ReleaseException(f"Weird version: {value}") from e

        if self.major == 2 and self.patch == 0:
            self.branch = "master"
        elif self.major == 3 and self.patch == 0:
            self.branch = "thread-next"
        else:
            self.branch = f"stable-v{self.major}.{self.minor}"


    def __eq__(self, other):
        return self.major == other.major and self.minor == other.minor and self.patch == other.patch

    def __repr__(self):
        return f"Version({str(self)})"

    def __str__(self):
        if self.major == 2 and self.patch == 0:
            return f"{self.major}.{self.minor}"
        else:
            return f"{self.major}.{self.minor}.{self.patch}"

    def next_patch(self):
        return Version(self.major, self.minor, self.patch + 1)

    def next_minor(self):
        return Version(self.major, self.minor + 1, 0)

    def next_major(self):
        return Version(self.major + 1, 0, 0)

    def template_data(self):
        return {
                "version": str(self),
                "branch": {
                    "name": self.branch,
                    "url": "https://gitlab.nic.cz/labs/bird/-/commits/" + self.branch,
                    },
                "milestone": Milestone(self).template_data(),
                }

class Milestone:
    seen = {}

    def __new__(cls, version):
        try:
            return cls.seen[str(version)]
        except KeyError:
            logger.debug(f"Milestone v{version} not yet queried")
            return super(Milestone, cls).__new__(cls)

    def __init__(self, version):
        try:
            assert(Milestone.seen[str(version)] == self)
            return
        except KeyError:
            pass

        milestone = gitlab.get(f"milestones?title=v{version}")
        if len(milestone) == 0:
            milestone = gitlab.post(f"milestones", json={
                "title": f"v{version}",
                "description": f"Collection of issues intended to be resolved in release {version}",
                })
            logger.debug(f"Gitlab replied: {milestone}")
            logger.info(f"Created milestone v{version}: {milestone['web_url']}")
            self.gitlab_id = milestone['id']
            self.local_id = milestone['iid']
            self.url = milestone['web_url']
            self.name = milestone['title']
            Milestone.seen[str(version)] = self

        elif len(milestone) == 1:
            logger.info(f"Milestone v{version} already exists: {milestone[0]['web_url']}")
            self.gitlab_id = milestone[0]['id']
            self.local_id = milestone[0]['iid']
            self.url = milestone[0]['web_url']
            self.name = milestone[0]['title']
            Milestone.seen[str(version)] = self
        else:
            raise ReleaseException(f"Too many milestones of name v{version}: {milestone}")

    def template_data(self):
        return {
                "name": self.name,
                "url": self.url,
                "local_id": self.local_id,
                "gitlab_id": self.gitlab_id,
                }

# A singleton class accessing the current git state
class GitState:
    def __init__(self):
        # Normalize where we are
        self.toplevel = pathlib.Path(sys.argv[0]).parent.parent.absolute()
        os.chdir(self.toplevel)

        with open("VERSION", "r") as f:
            self.version = Version(f.read().strip())

        try:
            gitbranch = [ x[3:] for x in cmd("git", "status", "-bs") if x.startswith("## ") ][0]
        except Exception as e:
            raise ReleaseException(f"Git status is broken, are you even inside a repo?") from e

        if "(no branch)" in gitbranch:
            raise ReleaseException(f"Not on any branch, I refuse to release.")

        if "..." not in gitbranch and " " not in gitbranch:
            raise ReleaseException(f"Detected branch {gitbranch} but not tracking any remote. I refuse to release.")

        try:
            locbranch, remref = gitbranch.split("...")
            remote, rembranch = remref.split("/")
            remuri, _ = cmd("git", "remote", "get-url", remote)
        except Exception as e:
            raise ReleaseException(f"This does not look like a regular branch, git status says: {gitbranch}") from e

        if \
                "https" not in remuri and "git@" not in remuri \
                or "gitlab.nic.cz" not in remuri \
                or "labs/bird" not in remuri \
                or "office" in remuri:
                    raise ReleaseException(f"Current branch is {locbranch}, tracking {rembranch} at {remote} but the appropriate uri is kinda sus: {remuri}")


        if locbranch != rembranch:
            raise ReleaseException(f"Hey sis, your local branch {locbranch} tracks remote branch {rembranch} at {remote}. Go and fix that mess.")

        self.remote = remote
        self.branch = locbranch

    def __str__(self):
        return f"GitState(toplevel={self.toplevel},branch={self.branch},version={self.version})"

    def token(self):
        try:
            return self._token
        except AttributeError:
            try:
                self._token, _ = cmd("git", "config", "gitlab.token")
                return self._token
            except CommandError as e:
                raise ReleaseException(f"To use gitlab API, you need a token. Add one in \"Settings → Access Tokens\" and call \"git config set --local gitlab.token=<token>\".") from e


class GitlabException(Exception):
    def __str__(self):
        return f"Gitlab request {self.args[0]} failed with {self.args[1].status_code}"

# A singleton class providing raw Gitlab API
class Gitlab:
    stem = "https://gitlab.nic.cz/api/v4/projects/labs%2Fbird/"
    def get(self, uri):
        response = requests.get(self.stem + uri, headers={"PRIVATE-TOKEN": git.token()})
        if not response.ok:
            raise GitlabException(self.stem + uri, response)

        return response.json()

    def post(self, uri, **kwargs):
        response = requests.post(self.stem + uri, headers={"PRIVATE-TOKEN": git.token()}, **kwargs)
        if not response.ok:
            raise GitlabException({ "uri": self.stem + uri, **kwargs }, response)

        return response.json()

class Templater:
    def __init__(self):
        self.j2env = jinja2.Environment(loader=jinja2.FileSystemLoader("."))

    def process(self, tpath, **data):
        te = self.j2env.get_template(tpath)
        return te.render(**data)

# A singleton class doing the release
class Release:
    def __new__(cls, *args, **kwargs):
        if cls != Release:
            return super(Release, cls).__new__(cls)

        version = None
        if git.branch == "master":
            cls = MinorRelease
            assert(git.version.major == 2)
            version = git.version.next_minor()

        elif git.branch == "thread-next":
            cls = MinorRelease
            assert(git.version.major == 3)
            version = git.version.next_minor()

        elif git.branch == f"stable-v{git.version.major}.{git.version.minor}":
            cls = PatchRelease
            version = git.version.next_patch()

        elif git.branch.startswith("release-v"):
            bv = git.branch[9:]
            nmi = git.version.next_minor()
            npa = git.version.next_patch()

            if Version(bv) == git.version:
                version = git.version
                cls = MinorRelease if git.version.patch == 0 else PatchRelease
            elif Version(bv) == nmi:
                version = nmi
                cls = MinorRelease
            elif Version(bv) == npa:
                version = npa
                cls = PatchRelease
            else:
                raise ReleaseException(f"Release branch {git.branch} incongruent with its VERSION {git.version}")
        else:
            raise ReleaseException(f"I have no idea what to release from branch {git.branch}")

        obj = cls.__new__(cls)
        obj.version = version
        return obj

    def __init__(self):
        logger.info(f"Releasing {self.kind} version {self.version} from branch {git.branch}")
        super().__init__()

    def issue(self):
        issue = gitlab.get(f"issues?labels=release-checklist&state=opened&milestone=v{self.version}")
        if len(issue) == 0:
            logger.info(f"Release issue does not exist yet, creating")
#            print({
            issue = gitlab.post(f"issues", json={
                "title": f"BIRD {self.version} release ({ self.kind })",
                "labels": f"release-{self.kind},release-checklist",
                "milestone_id": Milestone(self.version).gitlab_id,
                "description": templater.process(
                    "tools/release-issue.md.j2",
                    **(self.issue_template_data()),
                    kind=self.kind,
                    )
                })
            logger.info(f"Check the release issue #{issue['iid']} at {issue['web_url']}")

        elif len(issue) == 1:
            logger.info(f"Release issue #{issue[0]['iid']} already exists: {issue[0]['web_url']}")
        else:
            raise ReleaseException(f"Too many release issues for version {version}: {[ i['web_url'] for i in issue].join(', ')}")


    def create_branch(self):
        name = f"release-v{self.version}"
        logger.info(f"Creating branch {name}")
        try:
            cmd("git", "checkout", "-b", name)
        except CommandError as e:
            raise ReleaseException(f"Failed to create branch {name}") from e

    def run(self):
        # Check commit history
        try:
            assert(cmd("tools/git-check-commits") == [""])
        except Exception as e:
            raise ReleaseException("Commit structure unsuitable for release!") from e

# Not creating release branch, maybe later
#        if not git.branch.startswith("release-v"):
#            self.create_branch()

        # Assure 
        self.milestones()
        self.issue()


# Subclasses to define things where Minor and Patch release actually differ
class MinorRelease(Release):
    kind = "minor"

    def milestones(self):
        Milestone(self.version) # The version we are currently releasing
        Milestone(self.version.next_minor()) # What didn't make it
        Milestone(self.version.next_patch()) # Fixes of this version

    def issue_template_data(self):
        return {
                "this": self.version.template_data(),
                "next": self.version.next_minor().template_data(),
                "patch": self.version.next_patch().template_data(),
                }

class PatchRelease(Release):
    kind = "patch"

    def milestones(self):
        Milestone(self.version) # The version we are currently releasing
        Milestone(self.version.next_minor()) # What actually isn't a bug worth patchfixing
        Milestone(self.version.next_patch()) # Fixes of this version

    def issue_template_data(self):
        return {
                "this": self.version.template_data(),
                "next": self.version.next_patch().template_data(),
                "main": self.version.next_minor().template_data(),
                }

# Do the release preparation
try:
    git = GitState()
    gitlab = Gitlab()
    templater = Templater()
    release = Release()
    release.run()
except ReleaseException as e:
    logger.error(e, exc_info=True)
except Exception:
    logger.exception("Fatal error", exc_info=True)