|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +import bb.tinfoil |
| 5 | +import git |
| 6 | + |
| 7 | +# TODO: add minor version numbers once we have them |
| 8 | +RE_VERSION_TAG = "v(?P<year>[0-9][0-9])\.(?P<month>[0-9][0-9])" |
| 9 | +RE_VERSION_BB = "(?P<year>[0-9][0-9])\.(?P<month>[0-9][0-9])(?P<dev>$|\+dev)" |
| 10 | + |
| 11 | + |
| 12 | +def bb_variables(names): |
| 13 | + ret = dict() |
| 14 | + |
| 15 | + with bb.tinfoil.Tinfoil(tracking=True, setup_logging=True) as tinfoil: |
| 16 | + tinfoil.prepare(quiet=2, config_only=True) |
| 17 | + d = tinfoil.config_data |
| 18 | + |
| 19 | + for name in names: |
| 20 | + ret[name] = d.getVar(name) |
| 21 | + |
| 22 | + return ret |
| 23 | + |
| 24 | + |
| 25 | +def git_prev_tag(): |
| 26 | + repo = git.Repo() |
| 27 | + |
| 28 | + current_commit_is_tagged = True |
| 29 | + |
| 30 | + for commit in repo.iter_commits(): |
| 31 | + for tag in repo.tags: |
| 32 | + if tag.commit == commit: |
| 33 | + return (tag, current_commit_is_tagged) |
| 34 | + |
| 35 | + current_commit_is_tagged = False |
| 36 | + |
| 37 | + |
| 38 | +def check_version(distro_version): |
| 39 | + prev_tag, commit_is_tagged = git_prev_tag() |
| 40 | + |
| 41 | + print(f"Checking tag {prev_tag.name} against version {distro_version}") |
| 42 | + |
| 43 | + version_tag = re.fullmatch(RE_VERSION_TAG, prev_tag.name) |
| 44 | + version_tag_numeric = int(version_tag["year"]) * 100 + int(version_tag["month"]) |
| 45 | + |
| 46 | + version_bb = re.fullmatch(RE_VERSION_BB, distro_version) |
| 47 | + version_bb_numeric = int(version_bb["year"]) * 100 + int(version_bb["month"]) |
| 48 | + |
| 49 | + if commit_is_tagged: |
| 50 | + # The version in a tagged commit must match the version in the tag's name. |
| 51 | + assert version_bb["dev"] == "" |
| 52 | + assert version_tag_numeric == version_bb_numeric |
| 53 | + |
| 54 | + elif version_bb["dev"] == "": |
| 55 | + # Release candidates already have the next release version set, |
| 56 | + # but there must not be a tag with the same or a newer version number |
| 57 | + # in the current commits history. |
| 58 | + assert version_bb_numeric > version_tag_numeric |
| 59 | + |
| 60 | + else: |
| 61 | + # Non release candidate versions should have the previous tagged |
| 62 | + # version number plus the +dev suffix set. |
| 63 | + assert version_bb_numeric == version_tag_numeric |
| 64 | + |
| 65 | + |
| 66 | +def check_codename(codename): |
| 67 | + base_ref = os.environ.get("GITHUB_BASE_REF") |
| 68 | + ref = os.environ.get("GITHUB_REF") |
| 69 | + |
| 70 | + if base_ref is not None: |
| 71 | + print(f"Checking codename {codename} against pull request into {base_ref}") |
| 72 | + assert codename == f"tacos-{base_ref}" |
| 73 | + elif ref is not None: |
| 74 | + print(f"Checking codename {codename} against branch {ref}") |
| 75 | + assert codename == f"tacos-{ref}" |
| 76 | + else: |
| 77 | + print("Running outside of GitHub CI. Skipping codename check") |
| 78 | + return |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + variables = bb_variables(("DISTRO_VERSION", "DISTRO_CODENAME")) |
| 83 | + |
| 84 | + check_version(variables["DISTRO_VERSION"]) |
| 85 | + check_codename(variables["DISTRO_CODENAME"]) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + main() |
0 commit comments