Skip to content

Commit 38a84a0

Browse files
committed
Test fixes
1 parent 50dce8b commit 38a84a0

File tree

5 files changed

+79
-19
lines changed

5 files changed

+79
-19
lines changed

scripts/release/build/build_info.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Dict
33

44
from scripts.release.build.build_scenario import BuildScenario
5-
from scripts.release.constants import DEFAULT_REPOSITORY_PATH
5+
from scripts.release.constants import DEFAULT_REPOSITORY_PATH, DEFAULT_CHANGELOG_PATH, RELEASE_INITIAL_VERSION_ENV_VAR
66

77

88
class ImageInfo(dict):
@@ -61,8 +61,23 @@ def to_json(self):
6161
}
6262

6363

64-
def load_build_info(scenario: BuildScenario, repository_path: str = DEFAULT_REPOSITORY_PATH) -> BuildInfo:
65-
version = scenario.get_version(repository_path)
64+
def load_build_info(scenario: BuildScenario,
65+
repository_path: str = DEFAULT_REPOSITORY_PATH,
66+
changelog_sub_path: str = DEFAULT_CHANGELOG_PATH,
67+
initial_commit_sha: str = None,
68+
initial_version: str = None) -> BuildInfo:
69+
f"""
70+
Load build information based on the specified scenario.
71+
72+
:param scenario: BuildScenario enum value indicating the build scenario (e.g., PATCH, STAGING, RELEASE).
73+
:param repository_path: Path to the Git repository. Default is the current directory `{DEFAULT_REPOSITORY_PATH}`.
74+
:param changelog_sub_path: Path to the changelog directory relative to the repository root. Default is '{DEFAULT_CHANGELOG_PATH}'.
75+
: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.
76+
: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.
77+
:return: BuildInfo object containing images, binaries, and helm charts information for specified scenario.
78+
"""
79+
80+
version = scenario.get_version(repository_path, changelog_sub_path, initial_commit_sha, initial_version)
6681

6782
with open("build_info.json", "r") as f:
6883
build_info = json.load(f)
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import os
22
from enum import StrEnum
3+
34
from git import Repo
45

5-
from scripts.release.constants import get_initial_commit_sha, get_initial_version, DEFAULT_CHANGELOG_PATH, \
6-
DEFAULT_REPOSITORY_PATH
76
from scripts.release.version import calculate_next_version
87

98
COMMIT_SHA_LENGTH = 8
@@ -14,20 +13,19 @@ class BuildScenario(StrEnum):
1413
PATCH = "patch" # CI build for a patch/pull request
1514
STAGING = "staging" # CI build from a merge to the master
1615

17-
def get_version(self, repository_path: str) -> str:
16+
def get_version(self, repository_path: str, changelog_sub_path: str, initial_commit_sha: str = None,
17+
initial_version: str = None) -> str:
18+
repo = Repo(repository_path)
19+
1820
match self:
1921
case BuildScenario.PATCH:
2022
build_id = os.environ["BUILD_ID"]
2123
if not build_id:
2224
raise ValueError(f"BUILD_ID environment variable is not set for `{self}` build scenario")
2325
return build_id
2426
case BuildScenario.STAGING:
25-
repo = Repo(repository_path)
2627
return repo.head.object.hexsha[:COMMIT_SHA_LENGTH]
2728
case BuildScenario.RELEASE:
28-
repo = Repo(repository_path)
29-
initial_commit_sha = get_initial_commit_sha()
30-
initial_version = get_initial_version()
31-
return calculate_next_version(repo, DEFAULT_CHANGELOG_PATH, initial_commit_sha, initial_version)
29+
return calculate_next_version(repo, changelog_sub_path, initial_commit_sha, initial_version)
3230

3331
raise ValueError(f"Unknown build scenario: {self}")

scripts/release/build/build_scenario_test.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from git import Repo
44

55
from scripts.release.build.build_scenario import BuildScenario
6+
from scripts.release.constants import DEFAULT_CHANGELOG_PATH
67

78

89
class TestGetVersionForBuildScenario:
@@ -11,7 +12,8 @@ def test_patch_build_scenario(self, git_repo: Repo):
1112
os.environ["BUILD_ID"] = "688364423f9b6c00072b3556"
1213
expected_version = os.environ["BUILD_ID"]
1314

14-
version = BuildScenario.PATCH.get_version(repository_path=git_repo.working_dir)
15+
version = BuildScenario.PATCH.get_version(repository_path=git_repo.working_dir,
16+
changelog_sub_path=DEFAULT_CHANGELOG_PATH)
1517

1618
assert version == expected_version
1719

@@ -20,13 +22,15 @@ def test_staging_build_scenario(self, git_repo: Repo):
2022
git_repo.git.checkout(initial_commit)
2123
expected_version = initial_commit.hexsha[:8]
2224

23-
version = BuildScenario.STAGING.get_version(repository_path=git_repo.working_dir)
25+
version = BuildScenario.STAGING.get_version(repository_path=git_repo.working_dir,
26+
changelog_sub_path=DEFAULT_CHANGELOG_PATH)
2427

2528
assert version == expected_version
2629

2730
def test_release_build_scenario(self, git_repo: Repo):
2831
git_repo.git.checkout("1.2.0")
2932

30-
version = BuildScenario.RELEASE.get_version(repository_path=git_repo.working_dir)
33+
version = BuildScenario.RELEASE.get_version(repository_path=git_repo.working_dir,
34+
changelog_sub_path=DEFAULT_CHANGELOG_PATH)
3135

3236
assert version == "1.2.0"

scripts/release/release_info.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@
44

55
from scripts.release.build.build_info import load_build_info
66
from scripts.release.build.build_scenario import BuildScenario
7-
from scripts.release.constants import DEFAULT_REPOSITORY_PATH
7+
from scripts.release.constants import (
8+
DEFAULT_CHANGELOG_PATH,
9+
DEFAULT_RELEASE_INITIAL_VERSION,
10+
DEFAULT_REPOSITORY_PATH,
11+
)
812

913

10-
def create_release_info_json(repository_path: str) -> str:
11-
build_info = load_build_info(BuildScenario.RELEASE, repository_path)
14+
def create_release_info_json(
15+
repository_path: str, changelog_sub_path: str, initial_commit_sha: str = None, initial_version: str = None
16+
) -> str:
17+
build_info = load_build_info(
18+
scenario=BuildScenario.RELEASE,
19+
repository_path=repository_path,
20+
changelog_sub_path=changelog_sub_path,
21+
initial_commit_sha=initial_commit_sha,
22+
initial_version=initial_version,
23+
)
1224

1325
return json.dumps(build_info.to_json(), indent=2)
1426

@@ -27,6 +39,32 @@ def create_release_info_json(repository_path: str) -> str:
2739
type=pathlib.Path,
2840
help="Path to the Git repository. Default is the current directory '.'",
2941
)
42+
parser.add_argument(
43+
"-c",
44+
"--changelog-path",
45+
default=DEFAULT_CHANGELOG_PATH,
46+
metavar="",
47+
action="store",
48+
type=str,
49+
help=f"Path to the changelog directory relative to a current working directory. Default is '{DEFAULT_CHANGELOG_PATH}'",
50+
)
51+
parser.add_argument(
52+
"-s",
53+
"--initial-commit-sha",
54+
metavar="",
55+
action="store",
56+
type=str,
57+
help="SHA of the initial commit to start from if no previous version tag is found.",
58+
)
59+
parser.add_argument(
60+
"-v",
61+
"--initial-version",
62+
default=DEFAULT_RELEASE_INITIAL_VERSION,
63+
metavar="",
64+
action="store",
65+
type=str,
66+
help=f"Version to use if no previous version tag is found. Default is '{DEFAULT_RELEASE_INITIAL_VERSION}'",
67+
)
3068
parser.add_argument(
3169
"--output",
3270
"-o",
@@ -36,7 +74,9 @@ def create_release_info_json(repository_path: str) -> str:
3674
)
3775
args = parser.parse_args()
3876

39-
release_info = create_release_info_json(args.path)
77+
release_info = create_release_info_json(
78+
args.path, args.changelog_path, args.initial_commit_sha, args.initial_version
79+
)
4080

4181
if args.output is not None:
4282
with open(args.output, "w") as file:

scripts/release/release_info_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from git import Repo
44

5+
from scripts.release.constants import DEFAULT_CHANGELOG_PATH
56
from scripts.release.release_info import create_release_info_json
67

78

@@ -57,6 +58,8 @@ def test_create_release_info_json(
5758
"helm-charts": {"mongodb-kubernetes": {"repository": "quay.io/mongodb/helm-charts", "version": "1.0.0"}},
5859
}
5960
expected_release_info_json = json.dumps(expected_json, indent=2)
60-
release_info_json = create_release_info_json(git_repo.working_dir)
61+
release_info_json = create_release_info_json(
62+
repository_path=git_repo.working_dir, changelog_sub_path=DEFAULT_CHANGELOG_PATH
63+
)
6164

6265
assert release_info_json == expected_release_info_json

0 commit comments

Comments
 (0)