Skip to content

Commit 1012f0c

Browse files
committed
RepoVersionInfo: Add a method to find the last tag
This method gets the last tag if we are at a tag or the last tag matching the current branch major and minor (if any) if we are at a branch. Useful for calculating minimal dependencies. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 8cdca51 commit 1012f0c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/frequenz/repo/config/version.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,27 @@ def branches(self) -> dict[str, BranchVersion]:
326326
"""
327327
return self._branches
328328

329+
def find_last_tag(self) -> semver.Version | None:
330+
"""Find the last tag.
331+
332+
Returns:
333+
If we are at a tag, return the [current
334+
tag][frequenz.repo.config.version.RepoVersionInfo.current_tag]. If we
335+
are at a branch, return the last tag matching the branch major and minor
336+
(if any). If there are no matching tags, return `None`.
337+
"""
338+
if self._current_tag:
339+
return self._current_tag
340+
branch = self.current_branch
341+
if branch is None:
342+
return None
343+
tags = [t for t in self._tags.values() if t.major == branch.major]
344+
if branch.minor is not None:
345+
tags = [t for t in tags if t.minor == branch.minor]
346+
if not tags:
347+
return None
348+
return max(tags)
349+
329350
def find_next_minor_for_major_branch(self) -> int | None:
330351
"""Find the next minor version for the current major branch.
331352

tests/test_version.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class _Expected: # pylint: disable=too-many-instance-attributes
176176
is_tag: bool = False
177177
is_tag_last_minor_for_major: bool = False
178178
is_tag_latest: bool = False
179+
last_tag: semver.Version | None = None
179180
# Branch
180181
current_branch: BranchVersion | None = None
181182
is_branch: bool = False
@@ -212,6 +213,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
212213
current_tag=semver.Version(1, 0, 1),
213214
is_tag=True,
214215
is_tag_last_minor_for_major=True,
216+
last_tag=semver.Version(1, 0, 1),
215217
),
216218
),
217219
_TestCase(
@@ -223,6 +225,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
223225
current_tag=semver.Version(1, 1, 0),
224226
is_tag=True,
225227
is_tag_last_minor_for_major=True,
228+
last_tag=semver.Version(1, 1, 0),
226229
),
227230
),
228231
_TestCase(
@@ -234,6 +237,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
234237
current_tag=semver.Version(2, 0, 1),
235238
is_tag=True,
236239
is_tag_last_minor_for_major=False,
240+
last_tag=semver.Version(2, 0, 1),
237241
),
238242
),
239243
_TestCase(
@@ -247,6 +251,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
247251
is_tag=True,
248252
is_tag_last_minor_for_major=True,
249253
is_tag_latest=True,
254+
last_tag=semver.Version(3, 0, 0),
250255
),
251256
),
252257
_TestCase(
@@ -259,6 +264,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
259264
is_tag=True,
260265
is_tag_last_minor_for_major=True,
261266
is_tag_latest=True,
267+
last_tag=semver.Version(2, 1, 0),
262268
),
263269
),
264270
_TestCase(
@@ -272,6 +278,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
272278
is_tag=True,
273279
is_tag_last_minor_for_major=True,
274280
is_tag_latest=True,
281+
last_tag=semver.Version(2, 1, 1),
275282
),
276283
),
277284
_TestCase(
@@ -283,6 +290,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
283290
ref_name="v1.0.2-alpha.1",
284291
current_tag=semver.Version(1, 0, 2, "alpha.1"),
285292
is_tag=True,
293+
last_tag=semver.Version(1, 0, 2, "alpha.1"),
286294
),
287295
),
288296
_TestCase(
@@ -295,6 +303,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
295303
current_tag=semver.Version(1, 1, 0, "beta"),
296304
is_tag=True,
297305
is_tag_last_minor_for_major=True,
306+
last_tag=semver.Version(1, 1, 0, "beta"),
298307
),
299308
),
300309
_TestCase(
@@ -308,6 +317,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
308317
is_tag=True,
309318
is_tag_last_minor_for_major=True,
310319
is_tag_latest=True,
320+
last_tag=semver.Version(3, 0, 0, "rc1"),
311321
),
312322
),
313323
_TestCase(
@@ -321,6 +331,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
321331
is_tag=True,
322332
is_tag_last_minor_for_major=True,
323333
is_tag_latest=True,
334+
last_tag=semver.Version(2, 1, 0, "alpha.1"),
324335
),
325336
),
326337
_TestCase(
@@ -334,6 +345,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
334345
is_tag=True,
335346
is_tag_last_minor_for_major=True,
336347
is_tag_latest=True,
348+
last_tag=semver.Version(2, 0, 1, "rc1"),
337349
),
338350
),
339351
_TestCase(
@@ -355,6 +367,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
355367
current_branch=BranchVersion(major=1, minor=None, name="v1.x.x"),
356368
is_branch=True,
357369
next_minor_for_major_branch=1,
370+
last_tag=semver.Version(1, 1, 0, "rc"),
358371
),
359372
),
360373
_TestCase(
@@ -365,6 +378,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
365378
ref_name="v1.0.x",
366379
current_branch=BranchVersion(major=1, minor=0, name="v1.0.x"),
367380
is_branch=True,
381+
last_tag=semver.Version(1, 0, 2, "alpha.1"),
368382
),
369383
),
370384
_TestCase(
@@ -377,6 +391,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
377391
current_tag=semver.Version(1, 0, 0),
378392
is_tag=True,
379393
is_tag_last_minor_for_major=True,
394+
last_tag=semver.Version(1, 0, 0),
380395
),
381396
),
382397
_TestCase(
@@ -389,6 +404,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
389404
current_tag=semver.Version(1, 0, 2),
390405
is_tag=True,
391406
is_tag_last_minor_for_major=True,
407+
last_tag=semver.Version(1, 0, 2),
392408
),
393409
),
394410
_TestCase(
@@ -400,6 +416,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
400416
ref_name="v2.0.1",
401417
current_tag=semver.Version(2, 0, 1),
402418
is_tag=True,
419+
last_tag=semver.Version(2, 0, 1),
403420
),
404421
),
405422
_TestCase(
@@ -413,6 +430,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
413430
current_tag=semver.Version(1, 0, 0),
414431
is_tag=True,
415432
is_tag_last_minor_for_major=True,
433+
last_tag=semver.Version(1, 0, 0),
416434
),
417435
),
418436
_TestCase(
@@ -437,6 +455,7 @@ class _TestCase: # pylint: disable=too-many-instance-attributes
437455
current_branch=BranchVersion(major=1, minor=None, name="v1.x.x"),
438456
is_branch=True,
439457
next_minor_for_major_branch=1,
458+
last_tag=semver.Version(1, 1, 0, "rc"),
440459
),
441460
),
442461
_TestCase(
@@ -488,3 +507,9 @@ def test_repo_version(
488507
)
489508
assert repo_version_info.is_tag_latest() == case.expected.is_tag_latest
490509
assert repo_version_info.is_branch_latest() == case.expected.is_branch_latest
510+
if case.expected.last_tag is None:
511+
assert repo_version_info.find_last_tag() is None
512+
else:
513+
last_tag = repo_version_info.find_last_tag()
514+
assert last_tag is not None
515+
assert last_tag == case.expected.last_tag

0 commit comments

Comments
 (0)