1313from github .GitRelease import GitRelease
1414from pydantic import BaseModel , ConfigDict
1515
16- from .__main__ import compare_bo4e_versions_iteratively
16+ from .__main__ import compare_bo4e_versions
1717
1818logger = logging .getLogger (__name__ )
1919
@@ -24,10 +24,12 @@ class Version(BaseModel):
2424 A class to represent a BO4E version number.
2525 """
2626
27- version_pattern_with_rc : ClassVar [re .Pattern ] = re .compile (
27+ version_pattern_with_rc : ClassVar [re .Pattern [ str ] ] = re .compile (
2828 r"^v(?P<major>\d{6})\.(?P<functional>\d+)\.(?P<technical>\d+)(?:-rc(?P<candidate>\d+))?$"
2929 )
30- version_pattern : ClassVar [re .Pattern ] = re .compile (r"^v(?P<major>\d{6})\.(?P<functional>\d+)\.(?P<technical>\d+)$" )
30+ version_pattern : ClassVar [re .Pattern [str ]] = re .compile (
31+ r"^v(?P<major>\d{6})\.(?P<functional>\d+)\.(?P<technical>\d+)$"
32+ )
3133
3234 major : int
3335 functional : int
@@ -97,21 +99,21 @@ def bumped_candidate(self, other: "Version") -> bool:
9799 Return False if major, functional or technical bump is detected.
98100 Raises ValueError if one of the versions is not a candidate version.
99101 """
100- if not self .is_candidate () or not other .is_candidate () :
102+ if self .candidate is not None or other .candidate is not None :
101103 raise ValueError ("Cannot compare candidate versions if one of them is not a candidate." )
102104 return not self .bumped_technical (other ) and self .candidate > other .candidate
103105
104- def __lt__ (self , other ):
106+ def __gt__ (self , other : "Version" ):
105107 if not isinstance (other , Version ):
106108 return NotImplemented
107109 return (
108- self .major < other .major
109- or self .functional < other .functional
110- or self .technical < other .technical
111- or (self .is_candidate () and (not other .is_candidate () or self .candidate < other .candidate ))
110+ self .major > other .major
111+ or self .functional > other .functional
112+ or self .technical > other .technical
113+ or (self .is_candidate () and (not other .is_candidate () or self .candidate > other .candidate ))
112114 )
113115
114- def __eq__ (self , other ):
116+ def __eq__ (self , other : "Version" ):
115117 if not isinstance (other , Version ):
116118 return NotImplemented
117119 return (
@@ -133,8 +135,17 @@ def get_latest_release(gh_token: str | None = None) -> GitRelease:
133135 repo = get_source_repo (gh_token )
134136 latest_release = repo .get_latest_release ()
135137 # Ensure that the latest release is on main branch
136- if latest_release .target_commitish != "main" :
137- raise ValueError (f"Fatal Error: Latest release { latest_release .tag_name } is not on main branch." )
138+ commit_id = subprocess .check_output (f"git rev-parse tags/{ latest_release .tag_name } ~0" ).decode ().strip ()
139+ branches_containing_commit = [
140+ line .lstrip ("*" ).strip ()
141+ for line in subprocess .check_output (f"git branch --contains { commit_id } " ).decode ().splitlines ()
142+ ]
143+ if "main" not in branches_containing_commit :
144+ raise ValueError (
145+ f"Fatal Error: Latest release { latest_release .tag_name } is not on main branch "
146+ f"(branches { branches_containing_commit } contain commit { commit_id } of the "
147+ f"release { latest_release .tag_name } )"
148+ )
138149 return latest_release
139150
140151
@@ -202,10 +213,9 @@ def compare_work_tree_with_latest_version(gh_version: str, gh_token: str | None
202213 logger .info ("Major version bump detected. No further checks needed." )
203214 return
204215 logger .info ("Comparing versions iteratively: %s -> %s" , version_behind , version_ahead )
205- changes_iterables = compare_bo4e_versions_iteratively ([version_behind .tag ], version_ahead .tag , gh_token = gh_token )
206- assert len (changes_iterables ) == 1 , "Internal error: Expected exactly one comparison"
216+ changes = list (compare_bo4e_versions (version_behind .tag , version_ahead .tag , gh_token = gh_token , from_local = True ))
207217 logger .info ("Check if functional or technical release bump is needed" )
208- functional_changes = any (changes_iterables [ version_behind . tag , version_ahead . tag ] )
218+ functional_changes = any (changes )
209219 logger .info ("%s release bump is needed" , "Functional" if functional_changes else "Technical" )
210220
211221 if not functional_changes and version_ahead .bumped_functional (version_behind ):
@@ -242,3 +252,7 @@ def compare_work_tree_with_latest_version_cli(gh_version: str, gh_token: str | N
242252if __name__ == "__main__" :
243253 # pylint: disable=no-value-for-parameter
244254 compare_work_tree_with_latest_version_cli ()
255+
256+
257+ def test_compare_work_tree_with_latest_version ():
258+ compare_work_tree_with_latest_version ("v202401.1.2-rc3" , gh_token = None )
0 commit comments