Skip to content

Commit b1090c9

Browse files
trigger release and test
1 parent 2703738 commit b1090c9

File tree

3 files changed

+128
-71
lines changed

3 files changed

+128
-71
lines changed

doc/user_guide/how_to_release.rst

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,19 @@ How to Release?
44
Creating a Release
55
++++++++++++++++++
66

7-
1. Set a variable named **TAG** with the appropriate version numbers:
8-
9-
.. code-block:: shell
10-
11-
TAG="<major>.<minor>.<patch>"
12-
137
#. Prepare the project for a new release:
148

159
.. code-block:: shell
1610
17-
nox -s release:prepare -- "${TAG}"
11+
nox -s release:prepare -- [-h] [-v | --version VERSION] [-t | --type {major,minor,patch}]
1812
1913
#. Merge your **Pull Request** to the **default branch**
20-
#. Switch to the **default branch**:
2114

22-
.. code-block:: shell
23-
24-
git checkout $(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
25-
26-
#. Update branch:
15+
#. Trigger the release:
2716

2817
.. code-block:: shell
2918
30-
git pull
31-
32-
#. Create a new tag in your local repo:
33-
34-
.. code-block:: shell
35-
36-
git tag "${TAG}"
37-
38-
#. Push the repo to remote:
39-
40-
.. code-block:: shell
41-
42-
git push origin "${TAG}"
43-
44-
.. hint::
45-
46-
GitHub workflow **.github/workflows/cd.yml** reacts on this tag and starts the release process
19+
nox -s release:trigger -- [-h] [-v | --version VERSION] [-t | --type {major,minor,patch}]
4720
4821
What to do if the release failed?
4922
+++++++++++++++++++++++++++++++++

exasol/toolbox/nox/_release.py

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,53 @@ def _version_control(
143143
if not has_release_version and has_release_type:
144144
return _type_release(release_type=args.type, old_version=old_version)
145145

146-
session.error("error in _version_control")
146+
session.error("version not allowed with type")
147+
148+
149+
class ReleaseError(Exception):
150+
"""Error during trigger release"""
151+
152+
153+
def _trigger_release() -> Version:
154+
def run(*args: str):
155+
try:
156+
return subprocess.run(
157+
args, capture_output=True, text=True, check=True
158+
).stdout
159+
except subprocess.CalledProcessError as ex:
160+
raise ReleaseError(f"failed to execute command {args}") from ex
161+
162+
branches = run("git", "remote", "show", "origin")
163+
if not (default_branch := re.search(r"HEAD branch: (\S+)", branches)):
164+
raise ReleaseError("default branch could not be found")
165+
default_branch = default_branch.group(1)
166+
167+
run("git", "checkout", default_branch)
168+
run("git", "pull")
169+
170+
release_version = Version.from_poetry()
171+
print(f"release version: {release_version}")
172+
173+
if re.search(rf"{release_version}", run("git", "tag", "--list")):
174+
raise ReleaseError(f"tag {release_version} already exists")
175+
if re.search(rf"{release_version}", run("gh", "release", "list")):
176+
raise ReleaseError(f"release {release_version} already exists")
177+
178+
run("git", "tag", str(release_version))
179+
run("git", "push", "origin", str(release_version))
180+
return release_version
181+
182+
183+
def _trigger_release_test():
184+
def run(*args: str):
185+
return subprocess.run(args, capture_output=True, text=True, check=True).stdout
186+
187+
v = Version.from_poetry()
188+
run("echo", "test 1")
189+
run("echo", "test 2")
190+
run("echo", "test 3")
191+
run("echo", "test 4")
192+
run("echo", "test 5")
147193

148194

149195
@nox.session(name="release:prepare", python=False)
@@ -155,6 +201,7 @@ def prepare_release(session: Session, python=False) -> None:
155201
args = parser.parse_args(session.posargs)
156202

157203
new_version = _version_control(session, args)
204+
print(f"release version: {new_version}")
158205

159206
if not args.no_branch and not args.no_add:
160207
session.run("git", "switch", "-c", f"release/prepare-{new_version}")
@@ -199,42 +246,5 @@ def prepare_release(session: Session, python=False) -> None:
199246

200247

201248
@nox.session(name="release:trigger", python=False)
202-
def release(session: Session) -> None:
203-
204-
parser = argparse.ArgumentParser(
205-
prog="nox -s release:experimental",
206-
usage="nox -s release:experimental -- [-h] [-v | --version VERSION] [-t | --type {major,minor,patch}]",
207-
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
208-
)
209-
group = parser.add_mutually_exclusive_group(required=True)
210-
group.add_argument(
211-
"-v",
212-
"--version",
213-
type=cli.version,
214-
help="A version string of the following format:" '"NUMBER.NUMBER.NUMBER"',
215-
default=argparse.SUPPRESS,
216-
)
217-
group.add_argument(
218-
"-t",
219-
"--type",
220-
type=ReleaseTypes,
221-
help="specifies which type of upgrade is to be performed",
222-
choices=list(ReleaseTypes),
223-
default=argparse.SUPPRESS,
224-
)
225-
226-
args = parser.parse_args(session.posargs)
227-
228-
new_version = _version_control(session, args)
229-
print(str(new_version))
230-
231-
result = subprocess.run(["git", "remote", "show", "origin"], capture_output=True)
232-
match = re.search(r"HEAD branch: (\S+)", result.stdout.decode("utf-8"))
233-
if not match:
234-
session.error("Default branch could not be found")
235-
default_branch = match.group(1) if match else None
236-
subprocess.run(["git", "checkout", default_branch])
237-
subprocess.run(["git", "pull"])
238-
subprocess.run(["git", "tag", str(new_version)])
239-
subprocess.run(["git", "push", "origin", str(new_version)])
240-
pass
249+
def trigger_release(session: Session) -> None:
250+
print(_trigger_release())

test/unit/release_test.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import subprocess
22
from datetime import datetime
33
from inspect import cleandoc
4-
from unittest.mock import patch
4+
from subprocess import CalledProcessError
5+
from unittest.mock import (
6+
MagicMock,
7+
patch,
8+
)
59

610
import pytest
711

812
from exasol.toolbox.nox._release import (
13+
ReleaseError,
914
ReleaseTypes,
15+
_trigger_release,
1016
_type_release,
1117
)
1218
from exasol.toolbox.release import (
@@ -161,3 +167,71 @@ def test_type_release(rtype, old, expected):
161167
actual = _type_release(ReleaseTypes(rtype), Version.from_string(old))
162168
expected = Version.from_string(expected)
163169
assert actual == expected
170+
171+
172+
@pytest.mark.parametrize(
173+
"cmdline, expected",
174+
[
175+
(
176+
("git", "remote", "show", "origin"),
177+
CalledProcessError,
178+
),
179+
(
180+
("git", "remote", "show", "origin"),
181+
"test\nHEAD: \ntest",
182+
),
183+
(
184+
("git", "checkout", "main"),
185+
CalledProcessError,
186+
),
187+
(
188+
("git", "pull"),
189+
CalledProcessError,
190+
),
191+
(
192+
("git", "tag", "--list"),
193+
CalledProcessError,
194+
),
195+
(
196+
("git", "tag", "--list"),
197+
"0.1.0\n0.2.0\n0.3.0",
198+
),
199+
(
200+
("gh", "release", "list"),
201+
CalledProcessError,
202+
),
203+
(
204+
("gh", "release", "list"),
205+
"0.1.0\n0.2.0\n0.3.0",
206+
),
207+
(
208+
("git", "tag", "0.3.0"),
209+
CalledProcessError,
210+
),
211+
(
212+
("git", "push", "origin", "0.3.0"),
213+
CalledProcessError,
214+
),
215+
],
216+
)
217+
@patch("exasol.toolbox.nox._release.Version.from_poetry", return_value="0.3.0")
218+
def test_x1(mock, cmdline, expected):
219+
def valide_result(args):
220+
if args == ("git", "remote", "show", "origin"):
221+
return "test\nHEAD branch: main\ntest"
222+
return ""
223+
def simulate_fail(args, **kwargs):
224+
print("_______________")
225+
print(args)
226+
if args != cmdline:
227+
print(valide_result(args))
228+
return MagicMock(returncode=0, stdout=valide_result(args))
229+
if expected == CalledProcessError:
230+
raise CalledProcessError(returncode=1, cmd=cmdline)
231+
else:
232+
return MagicMock(returncode=0, stdout=expected)
233+
234+
with patch("subprocess.run", side_effect=simulate_fail):
235+
with pytest.raises(ReleaseError) as ex:
236+
_trigger_release()
237+
print(ex.value)

0 commit comments

Comments
 (0)