|
| 1 | +from argparse import ArgumentParser |
| 2 | +import logging |
| 3 | +from pathlib import Path |
| 4 | +import sys |
| 5 | + |
| 6 | +import ruamel.yaml |
| 7 | +yaml = ruamel.yaml.YAML() |
| 8 | +# Preserve document layout |
| 9 | +yaml.block_seq_indent = 2 |
| 10 | +yaml.explicit_start = True |
| 11 | +yaml.preserve_quotes = True |
| 12 | + |
| 13 | +try: |
| 14 | + import argcomplete |
| 15 | +except ImportError: |
| 16 | + argcomplete = None |
| 17 | + |
| 18 | + |
| 19 | +logging.basicConfig(format="%(levelname)-10s%(message)s", level=logging.INFO) |
| 20 | + |
| 21 | + |
| 22 | +# What level of version to apply for a given change type |
| 23 | +RULES = { |
| 24 | + "major": [ |
| 25 | + "major_changes", |
| 26 | + "breaking_changes", |
| 27 | + "removed_features", |
| 28 | + ], |
| 29 | + "minor": [ |
| 30 | + "minor_changes", |
| 31 | + "deprecated_features", |
| 32 | + ], |
| 33 | + "patch": [ |
| 34 | + "bugfixes", |
| 35 | + "security_fixes", |
| 36 | + "trivial", |
| 37 | + ], |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +def get_last_version(path) -> str: |
| 42 | + changelog_path = path / "changelogs" / "changelog.yaml" |
| 43 | + changelog = yaml.load(changelog_path) |
| 44 | + return max(changelog["releases"].keys()) |
| 45 | + |
| 46 | + |
| 47 | +def update_version(path: Path, version: str) -> str: |
| 48 | + version_parts = [int(v) for v in version.split(".")] |
| 49 | + fragment_path = path / "changelogs" / "fragments" |
| 50 | + |
| 51 | + types = {key: False for key in RULES} |
| 52 | + if fragment_path.exists() and fragment_path.is_dir(): |
| 53 | + for file in fragment_path.iterdir(): |
| 54 | + fragment = yaml.load(file) |
| 55 | + if fragment: |
| 56 | + for level, headings in RULES.items(): |
| 57 | + for heading in headings: |
| 58 | + if heading in fragment: |
| 59 | + types[level] = True |
| 60 | + |
| 61 | + # Bump version accordingly |
| 62 | + if types["major"]: |
| 63 | + version_parts = version_parts[0] + 1, 0, 0 |
| 64 | + elif types["minor"]: |
| 65 | + version_parts[1:] = version_parts[1] + 1, 0 |
| 66 | + elif types["patch"]: |
| 67 | + version_parts[2] += 1 |
| 68 | + new_version = ".".join(str(v) for v in version_parts) |
| 69 | + |
| 70 | + if new_version != version: |
| 71 | + new_version += "-dev" |
| 72 | + |
| 73 | + return new_version |
| 74 | + |
| 75 | + |
| 76 | +def update_galaxy(path: Path, new_version: str) -> bool: |
| 77 | + galaxy_path = path / "galaxy.yml" |
| 78 | + if galaxy_path.exists(): |
| 79 | + galaxy = yaml.load(galaxy_path) |
| 80 | + else: |
| 81 | + logging.error("Unable to find galaxy.yml in %s", path) |
| 82 | + sys.exit(2) |
| 83 | + |
| 84 | + logging.info("Current version in galaxy.yml is %s", galaxy["version"]) |
| 85 | + if galaxy["version"] != new_version: |
| 86 | + logging.info("Updating version string in galaxy.yml") |
| 87 | + galaxy["version"] = new_version |
| 88 | + yaml.dump(galaxy, galaxy_path) |
| 89 | + return True |
| 90 | + return False |
| 91 | + |
| 92 | + |
| 93 | +def main() -> None: |
| 94 | + parser = ArgumentParser() |
| 95 | + parser.add_argument( |
| 96 | + "-p", |
| 97 | + "--path", |
| 98 | + help="The path to the collection (ie ./ansible.netcommon", |
| 99 | + required=True, |
| 100 | + ) |
| 101 | + |
| 102 | + if argcomplete: |
| 103 | + argcomplete.autocomplete(parser) |
| 104 | + |
| 105 | + args = parser.parse_args() |
| 106 | + path = Path(args.path).absolute() |
| 107 | + |
| 108 | + version = get_last_version(path) |
| 109 | + logging.info("Detected collection version is %s", version) |
| 110 | + |
| 111 | + new_version = update_version(path, version) |
| 112 | + logging.info("Updated collection version is %s", new_version) |
| 113 | + |
| 114 | + changed = update_galaxy(path, new_version) |
| 115 | + sys.exit(int(changed)) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + main() |
0 commit comments