From a876fdc799b89938f6ae3d559b645992bb4d7478 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Mon, 14 Aug 2023 09:10:31 +0200 Subject: [PATCH] add error handling for release_id parsing --- .../application/release_mixin_services.py | 8 +++++-- .../infrastructure/infrastructure.py | 6 +++--- .../test_release_mixin_services.py | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/python/ddadevops/application/release_mixin_services.py b/src/main/python/ddadevops/application/release_mixin_services.py index 2b094a8..546d93e 100644 --- a/src/main/python/ddadevops/application/release_mixin_services.py +++ b/src/main/python/ddadevops/application/release_mixin_services.py @@ -62,7 +62,7 @@ class ReleaseService: self.git_api.push_follow_tags() def publish_artifacts(self, release: Release): - release_response = json.loads( + release_id = self.__parse_forgejo_release_id__( self.artifact_deployment_api.create_forgejo_release( release.forgejo_release_api_endpoint(), release.version.to_string(), @@ -73,12 +73,16 @@ class ReleaseService: self.artifact_deployment_api.calculate_checksums(artifact_path) self.artifact_deployment_api.add_asset_to_release( "todo", - release_response["id"], + release_id, "todo", "todo", release.release_artifact_token, ) + def __parse_forgejo_release_id__(self, release_response: str) -> int: + parsed = json.loads(release_response) + return parsed["id"] + def __set_version_and_commit__( self, version: Version, build_file_ids: List[str], message: str ): diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index cd5f946..d86e029 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -218,13 +218,13 @@ class ArtifactDeploymentApi: def __init__(self): self.execution_api = ExecutionApi() - def create_forgejo_release(self, target_url: str, tag: str, token: str): + def create_forgejo_release(self, api_endpoint_url: str, tag: str, token: str): return self.execution_api.execute_secure( - f'curl -X "POST" "{target_url}" ' + f'curl -X "POST" "{api_endpoint_url}" ' + '-H "accept: application/json" -H "Content-Type: application/json" ' + f'-d "{{ "body": "Provides files for release {tag} Attention: The "Source Code"-files below are not up-to-date!", "tag_name": "{tag}"}}" ' # noqa: E501 + f'-H "Authorization: token {token}"', - sanitized_command=f'curl -X "POST" "{target_url}" ' + sanitized_command=f'curl -X "POST" "{api_endpoint_url}" ' + '-H "accept: application/json" -H "Content-Type: application/json" ' + f'-d "{{ "body": "Provides files for release {tag} Attention: The "Source Code"-files below are not up-to-date!", "tag_name": "{tag}"}}" ', ) # noqa: E501 diff --git a/src/test/python/application/test_release_mixin_services.py b/src/test/python/application/test_release_mixin_services.py index 8e1d786..56f9ddf 100644 --- a/src/test/python/application/test_release_mixin_services.py +++ b/src/test/python/application/test_release_mixin_services.py @@ -40,3 +40,24 @@ def test_sould_publish_artifacts(): release = devops.mixins[MixinType.RELEASE] sut.publish_artifacts(release) assert 2345 == mock.add_asset_to_release_id + +def test_sould_throw_exception_if_there_was_an_error_in_publish_artifacts(): + devops = build_devops( + { + "release_artifacts": ["target/art"], + "release_artifact_server_url": "http://repo.test/", + "release_organisation": "orga", + "release_repository_name": "repo", + } + ) + release = devops.mixins[MixinType.RELEASE] + + with pytest.raises(Exception): + mock = ArtifactDeploymentApiMock(release='') + sut = ReleaseService(GitApiMock(), mock, BuildFileRepositoryMock()) + sut.publish_artifacts(release) + + with pytest.raises(Exception): + mock = ArtifactDeploymentApiMock(release='{"message": "there was an error", "url":"some-url"}') + sut = ReleaseService(GitApiMock(), mock, BuildFileRepositoryMock()) + sut.publish_artifacts(release)