|
| 1 | +import json |
| 2 | +from typing import Dict |
| 3 | + |
| 4 | +from scripts.release.build.build_scenario import BuildScenario |
| 5 | +from scripts.release.constants import DEFAULT_REPOSITORY_PATH, DEFAULT_CHANGELOG_PATH, RELEASE_INITIAL_VERSION_ENV_VAR, \ |
| 6 | + get_initial_version, get_initial_commit_sha |
| 7 | + |
| 8 | + |
| 9 | +class ImageInfo(dict): |
| 10 | + def __init__(self, repository: str, platforms: list[str], version: str): |
| 11 | + super().__init__() |
| 12 | + self.repository = repository |
| 13 | + self.platforms = platforms |
| 14 | + self.version = version |
| 15 | + |
| 16 | + def to_json(self): |
| 17 | + return {"repository": self.repository, "platforms": self.platforms, "version": self.version} |
| 18 | + |
| 19 | + |
| 20 | +class BinaryInfo(dict): |
| 21 | + def __init__(self, s3_store: str, platforms: list[str], version: str): |
| 22 | + super().__init__() |
| 23 | + self.s3_store = s3_store |
| 24 | + self.platforms = platforms |
| 25 | + self.version = version |
| 26 | + |
| 27 | + def to_json(self): |
| 28 | + return {"platforms": self.platforms, "version": self.version} |
| 29 | + |
| 30 | + |
| 31 | +class HelmChartInfo(dict): |
| 32 | + def __init__(self, repository: str, version: str): |
| 33 | + super().__init__() |
| 34 | + self.repository = repository |
| 35 | + self.version = version |
| 36 | + |
| 37 | + def to_json(self): |
| 38 | + return {"repository": self.repository, "version": self.version} |
| 39 | + |
| 40 | + |
| 41 | +class BuildInfo(dict): |
| 42 | + def __init__( |
| 43 | + self, images: Dict[str, ImageInfo], binaries: Dict[str, BinaryInfo], helm_charts: Dict[str, HelmChartInfo] |
| 44 | + ): |
| 45 | + super().__init__() |
| 46 | + self.images = images |
| 47 | + self.binaries = binaries |
| 48 | + self.helm_charts = helm_charts |
| 49 | + |
| 50 | + def __dict__(self): |
| 51 | + return { |
| 52 | + "images": {name: images.__dict__ for name, images in self.images.items()}, |
| 53 | + "binaries": {name: bin.__dict__ for name, bin in self.binaries.items()}, |
| 54 | + "helm-charts": {name: chart.__dict__ for name, chart in self.helm_charts.items()}, |
| 55 | + } |
| 56 | + |
| 57 | + def to_json(self): |
| 58 | + return { |
| 59 | + "images": {name: images.to_json() for name, images in self.images.items()}, |
| 60 | + "binaries": {name: bin.to_json() for name, bin in self.binaries.items()}, |
| 61 | + "helm-charts": {name: chart.to_json() for name, chart in self.helm_charts.items()}, |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +def load_build_info(scenario: BuildScenario, |
| 66 | + repository_path: str = DEFAULT_REPOSITORY_PATH, |
| 67 | + changelog_sub_path: str = DEFAULT_CHANGELOG_PATH, |
| 68 | + initial_commit_sha: str = None, |
| 69 | + initial_version: str = None) -> BuildInfo: |
| 70 | + f""" |
| 71 | + Load build information based on the specified scenario. |
| 72 | +
|
| 73 | + :param scenario: BuildScenario enum value indicating the build scenario (e.g., PATCH, STAGING, RELEASE). |
| 74 | + :param repository_path: Path to the Git repository. Default is the current directory `{DEFAULT_REPOSITORY_PATH}`. |
| 75 | + :param changelog_sub_path: Path to the changelog directory relative to the repository root. Default is '{DEFAULT_CHANGELOG_PATH}'. |
| 76 | + :param initial_commit_sha: SHA of the initial commit to start from if no previous version tag is found. If not provided, it will be determined based on `{RELEASE_INITIAL_VERSION_ENV_VAR} environment variable. |
| 77 | + :param initial_version: Initial version to use if no previous version tag is found. If not provided, it will be determined based on `{RELEASE_INITIAL_VERSION_ENV_VAR}` environment variable. |
| 78 | + :return: BuildInfo object containing images, binaries, and helm charts information for specified scenario. |
| 79 | + """ |
| 80 | + |
| 81 | + if not initial_commit_sha: |
| 82 | + initial_commit_sha = get_initial_commit_sha() |
| 83 | + if not initial_version: |
| 84 | + initial_version = get_initial_version() |
| 85 | + |
| 86 | + version = scenario.get_version(repository_path, changelog_sub_path, initial_commit_sha, initial_version) |
| 87 | + |
| 88 | + with open("build_info.json", "r") as f: |
| 89 | + build_info = json.load(f) |
| 90 | + |
| 91 | + images = {} |
| 92 | + for name, env_data in build_info["images"].items(): |
| 93 | + data = env_data[scenario] |
| 94 | + # Only update the image_version if it is not already set in the build_info.json file |
| 95 | + image_version = data.get("version") |
| 96 | + if not image_version: |
| 97 | + image_version = version |
| 98 | + |
| 99 | + images[name] = ImageInfo(repository=data["repository"], platforms=data["platforms"], version=image_version) |
| 100 | + |
| 101 | + binaries = {} |
| 102 | + for name, env_data in build_info["binaries"].items(): |
| 103 | + data = env_data[scenario] |
| 104 | + binaries[name] = BinaryInfo(s3_store=data["s3-store"], platforms=data["platforms"], version=version) |
| 105 | + |
| 106 | + helm_charts = {} |
| 107 | + for name, env_data in build_info["helm-charts"].items(): |
| 108 | + data = env_data[scenario] |
| 109 | + helm_charts[name] = HelmChartInfo(repository=data["repository"], version=version) |
| 110 | + |
| 111 | + return BuildInfo(images=images, binaries=binaries, helm_charts=helm_charts) |
0 commit comments