~aleteoryx/muditaos

ref: sign_test muditaos/tools/jira-version-update.py -rwxr-xr-x 4.4 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months ago
                                                                                
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
#!/usr/bin/env python3
'''
Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
'''

from git.repo import Repo
from os import getcwd
from jira import JIRA
import os
import re
import sys


release_base_regex = re.compile(r'^release-(\d+).(\d+).(\d+)$')
rc_regex = re.compile(release_base_regex.pattern[:-1] + r'-rc(\d+)$')
release_regex = re.compile(release_base_regex.pattern[:-1] + r'(-internal)?$')
commit_regex = re.compile(r'^\[((EGD|BH|CP)-\d+)\] .*$')


class Version(object):
    def __init__(self, major, minor, subminor, internal=False):
        self.major = major
        self.minor = minor
        self.subminor = subminor
        self.internal = internal

    @staticmethod
    def from_tag(tag):
        match = release_regex.match(tag)
        if match is None:
            raise RuntimeError("Invalid version tag " + tag)

        internal = match.lastindex == 4 and match.group(4) == "-internal"

        return Version(int(match.group(1)), int(match.group(2)), int(match.group(3)), internal)

    def __str__(self):
        s = "{}.{}.{}".format(self.major, self.minor, self.subminor)
        if self.internal:
            s += "-internal"
        return s

    def __gt__(self, other):
        if self.major == other.major:
            if self.minor == other.minor:
                return self.subminor > other.subminor
            else:
                return self.minor > other.minor
        else:
            return self.major > other.major

    def is_next(self, other):
        if self.major == other.major:
            if self.minor == other.minor:
                return self.subminor - other.subminor == 1
            else:
                return self.minor - other.minor == 1 and self.subminor == 1
        else:
            return self.major - other.major == 1 and self.minor == 0 and self.subminor == 0

    def tag(self):
        return "release-" + str(self)


def jira_connect():
    juser = os.environ.get('JIRA_USER')
    jtoken = os.environ.get('JIRA_TOKEN')
    jserver = "https://appnroll.atlassian.net"
    options = {'server': jserver}
    return JIRA(options, basic_auth=(juser, jtoken))


def main(args):
    if len(args) == 2:
        repo = Repo(args[1])
    else:
        repo = Repo(getcwd())

    # get all versions
    versions = [Version.from_tag(str(t))
                for t in repo.tags if release_regex.match(str(t))]
    versions.sort()
    current_version = versions[-1]

    # make new version looking for the last RC version
    rc = [t for t in repo.tags if rc_regex.match(str(t))][-1]
    match = rc_regex.match(str(rc))
    if match is None:
        print("No RC version")
        return 1
    new_version = Version(int(match.group(1)), int(
        match.group(2)), int(match.group(3)))

    print("Current version : " + str(current_version))
    print("Next version    : " + str(new_version))

    commits = [c for c in repo.iter_commits(
        rev=current_version.tag() + ".." + str(rc))]

    issues = []
    for c in commits:
        match = commit_regex.match(c.summary)
        if match is None:
            print("Skipping commit: " + c.summary)
            continue
        issue = match.group(1)
        if issue in issues:
            print("Skipping duplicate issue: " + issue)
            continue

        issues.append(issue)

    issues.sort()

    # jira
    jira = jira_connect()
    egd = jira.project('EGD')
    sversions = [str(v) for v in jira.project_versions(egd)]
    if str(new_version) in sversions:
        print("JIRA version found: " + str(new_version))
    else:
        print("Creating JIRA version: " + str(new_version))
        jira.create_version(str(new_version), egd)

    for issueName in issues:
        print("Updating version for: " + issueName)
        try:
            issue = jira.issue(issueName)
        except:
            print("Failed to fetch version")
            continue
        fix_versions = [str(v) for v in issue.fields.fixVersions]
        if str(new_version) in fix_versions:
            print(str(issue) + " alredy in version " + str(new_version))
            continue

        update_versions = []
        for v in fix_versions:
            update_versions.append({'name': v})
        update_versions.append({'name': str(new_version)})
        try:
            issue.update(fields={'fixVersions': update_versions})
        except:
            print("Failed to update issue")

    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv))