Skip to content

Commit e23c904

Browse files
committed
Changed replacement of version in Github Actions
1 parent 45ec6fb commit e23c904

File tree

3 files changed

+64
-25
lines changed

3 files changed

+64
-25
lines changed

exasol/toolbox/tools/replace_version.py

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
from dataclasses import dataclass
33
from enum import Enum
44
from pathlib import Path
5+
from typing import (
6+
Callable,
7+
Optional,
8+
)
59

10+
from exasol.toolbox.util.version import Version
611

7-
def update_github_yml(template: Path, version: str) -> None:
12+
13+
def update_github_yml(template: Path, version: Version) -> None:
814
"""Updates versions in GitHub workflows and actions"""
915
with open(template, encoding="utf-8") as file:
1016
content = file.readlines()
@@ -19,10 +25,7 @@ def update_github_yml(template: Path, version: str) -> None:
1925
class Pattern:
2026
match_pattern: str
2127
break_pattern: str
22-
23-
@property
24-
def version_pattern(self) -> str:
25-
return r"[0-9]+\.[0-9]+\.[0-9]+"
28+
version_pattern: str
2629

2730
@property
2831
def full_pattern(self) -> str:
@@ -36,24 +39,58 @@ def replace_version(self, line: str, version: str) -> str:
3639
)
3740

3841

39-
class ToolboxPattern(Enum):
40-
github = Pattern(
41-
match_pattern="exasol/python-toolbox/.github/[^/]+/[^/]+",
42-
break_pattern="@",
42+
def full_version_modifier(version: Version) -> str:
43+
return str(version)
44+
45+
46+
def major_version_modifier(version: Version) -> str:
47+
return f"v{version.major}"
48+
49+
50+
class GithubActionsReplacement:
51+
def __init__(
52+
self, pattern: Pattern, version_string_modifier: Callable[[Version], str]
53+
) -> None:
54+
self.pattern = pattern
55+
self.version_string_modifier = version_string_modifier
56+
57+
def replace_version(self, line: str, version: Version) -> Optional[str]:
58+
match = re.search(self.pattern.full_pattern, line)
59+
if match:
60+
return self.pattern.replace_version(
61+
line=line, version=self.version_string_modifier(version)
62+
)
63+
return None
64+
65+
66+
class Replacements(Enum):
67+
github = GithubActionsReplacement(
68+
pattern=Pattern(
69+
match_pattern="exasol/python-toolbox/.github/[^/]+/[^/]+",
70+
break_pattern="@",
71+
version_pattern=r"[0-9]+\.[0-9]+\.[0-9]+",
72+
),
73+
version_string_modifier=major_version_modifier,
4374
)
44-
pypi = Pattern(
45-
match_pattern="exasol-toolbox",
46-
break_pattern="==",
75+
76+
pypi = GithubActionsReplacement(
77+
pattern=Pattern(
78+
match_pattern="exasol-toolbox",
79+
break_pattern="==",
80+
version_pattern=r"[0-9]+\.[0-9]+\.[0-9]+",
81+
),
82+
version_string_modifier=full_version_modifier,
4783
)
4884

4985

50-
def _update_line_with_version(line: str, version: str) -> str:
51-
for pattern in ToolboxPattern:
52-
match = re.search(pattern.value.full_pattern, line)
53-
if match:
54-
return pattern.value.replace_version(line=line, version=version)
86+
def _update_line_with_version(line: str, version: Version) -> str:
87+
for replacement in Replacements:
88+
if replaced_line := replacement.value.replace_version(
89+
line=line, version=version
90+
):
91+
return replaced_line
5592
return line
5693

5794

58-
def update_versions(lines: list[str], version: str) -> list[str]:
95+
def update_versions(lines: list[str], version: Version) -> list[str]:
5996
return [_update_line_with_version(line=line, version=version) for line in lines]

noxconfig.py

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

99
from exasol.toolbox.nox.plugin import hookimpl
1010
from exasol.toolbox.tools.replace_version import update_github_yml
11+
from exasol.toolbox.util.version import Version
1112

1213

1314
class UpdateTemplates:
@@ -25,7 +26,7 @@ def actions(self) -> list[Path]:
2526
return [f for f in gh_actions.rglob("*") if f.is_file()]
2627

2728
@hookimpl
28-
def prepare_release_update_version(self, session, config, version):
29+
def prepare_release_update_version(self, session, config, version: Version) -> None:
2930
for workflow in self.template_workflows:
3031
update_github_yml(workflow, version)
3132

test/unit/replace_version_test.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
_update_line_with_version,
55
update_versions,
66
)
7+
from exasol.toolbox.util.version import Version
78

89

910
@pytest.mark.parametrize(
1011
"line,expected",
1112
[
1213
pytest.param(
13-
"exasol/python-toolbox/.github/actions/python-environment@1.0.0",
14-
"exasol/python-toolbox/.github/actions/python-environment@2.0.0",
14+
"exasol/python-toolbox/.github/actions/python-environment@v1",
15+
"exasol/python-toolbox/.github/actions/python-environment@v2",
1516
id="github_action",
1617
),
1718
pytest.param(
@@ -27,16 +28,16 @@
2728
],
2829
)
2930
def test_update_line_with_version(line: str, expected: str):
30-
actual = _update_line_with_version(line=line, version="2.0.0")
31+
actual = _update_line_with_version(line=line, version=Version(2, 0, 0))
3132
assert actual == expected
3233

3334

3435
@pytest.mark.parametrize(
3536
"line_to_change, expected",
3637
[
3738
pytest.param(
38-
"exasol/python-toolbox/.github/actions/python-environment@1.0.0",
39-
"exasol/python-toolbox/.github/actions/python-environment@2.0.0",
39+
"exasol/python-toolbox/.github/actions/python-environment@v1",
40+
"exasol/python-toolbox/.github/actions/python-environment@v2",
4041
id="github_action",
4142
),
4243
pytest.param(
@@ -55,5 +56,5 @@ def test_update_versions(line_to_change, expected):
5556
lines = dummy_lines + [line_to_change]
5657
expected_lines = dummy_lines + [expected]
5758

58-
actual = update_versions(lines=lines, version="2.0.0")
59+
actual = update_versions(lines=lines, version=Version(2, 0, 0))
5960
assert actual == expected_lines

0 commit comments

Comments
 (0)