Skip to content

Commit 0994d18

Browse files
committed
Added CMD function to create release_info.json
1 parent aeafcfb commit 0994d18

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

scripts/release/release_info.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import argparse
12
import json
3+
import pathlib
24
from typing import Dict
35

46
from scripts.release.version import (
@@ -93,3 +95,34 @@ def create_release_info_json(version: str) -> str:
9395
build_info = load_build_info(Environment.PROD, version)
9496

9597
return json.dumps(build_info.to_json(), indent=2)
98+
99+
100+
if __name__ == "__main__":
101+
parser = argparse.ArgumentParser(
102+
description="Create relevant release artifacts information in JSON format.",
103+
formatter_class=argparse.RawTextHelpFormatter,
104+
)
105+
parser.add_argument(
106+
"-v",
107+
"--version",
108+
metavar="",
109+
action="store",
110+
type=str,
111+
help=f"Version to use for this release.",
112+
)
113+
parser.add_argument(
114+
"--output",
115+
"-o",
116+
metavar="",
117+
type=pathlib.Path,
118+
help="Path to save the release information file. If not provided, prints to stdout.",
119+
)
120+
args = parser.parse_args()
121+
122+
release_info = create_release_info_json(args.version)
123+
124+
if args.output is not None:
125+
with open(args.output, "w") as file:
126+
file.write(release_info)
127+
else:
128+
print(release_info)

scripts/release/version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
get_changelog_entries,
1212
)
1313

14+
COMMIT_SHA_LENGTH = 8
1415

1516
class Environment(StrEnum):
1617
DEV = "dev"
@@ -30,7 +31,7 @@ def get_version_for_environment(env: Environment, initial_commit_sha: str | None
3031
raise ValueError(f"BUILD_ID environment variable is not set for {env} environment")
3132
return build_id
3233
case Environment.STAGING:
33-
return repo.head.object.hexsha
34+
return repo.head.object.hexsha[:COMMIT_SHA_LENGTH]
3435
case Environment.PROD:
3536
return calculate_next_version(repo, changelog_sub_path, initial_commit_sha, initial_version)
3637

scripts/release/version_test.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
1+
import os
12
import unittest
23

3-
from scripts.release.changelog import ChangeKind
4-
from scripts.release.version import increment_previous_version
4+
from git import Repo
5+
6+
from scripts.release.changelog import DEFAULT_INITIAL_GIT_TAG_VERSION, ChangeKind
7+
from scripts.release.version import (
8+
Environment,
9+
get_version_for_environment,
10+
increment_previous_version,
11+
)
12+
13+
14+
class TestGetVersionForEnvironment:
15+
16+
def test_dev_environment(self, git_repo: Repo):
17+
os.environ["BUILD_ID"] = "688364423f9b6c00072b3556"
18+
expected_version = os.environ["BUILD_ID"]
19+
20+
version = get_version_for_environment(
21+
env=Environment.DEV,
22+
initial_commit_sha=None,
23+
initial_version=DEFAULT_INITIAL_GIT_TAG_VERSION,
24+
repository_path=git_repo.working_dir,
25+
)
26+
27+
assert version == expected_version
28+
29+
def test_staging_environment(self, git_repo: Repo):
30+
initial_commit = list(git_repo.iter_commits(reverse=True))[4]
31+
git_repo.git.checkout(initial_commit)
32+
expected_version = initial_commit.hexsha[:8]
33+
34+
version = get_version_for_environment(
35+
env=Environment.STAGING,
36+
initial_commit_sha=None,
37+
initial_version=DEFAULT_INITIAL_GIT_TAG_VERSION,
38+
repository_path=git_repo.working_dir,
39+
)
40+
41+
assert version == expected_version
42+
43+
def test_prod_environment(self, git_repo: Repo):
44+
git_repo.git.checkout("1.2.0")
45+
46+
version = get_version_for_environment(
47+
env=Environment.PROD,
48+
initial_commit_sha=None,
49+
initial_version=DEFAULT_INITIAL_GIT_TAG_VERSION,
50+
repository_path=git_repo.working_dir,
51+
)
52+
53+
assert version == "1.2.0"
554

655

756
class TestCalculateNextReleaseVersion(unittest.TestCase):

0 commit comments

Comments
 (0)