Skip to content

Commit

Permalink
fix: fixing a bug that confuses types (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamakou108 authored Jun 4, 2023
1 parent d6898ae commit bbb7151
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 45 deletions.
26 changes: 9 additions & 17 deletions depwatch/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from requests import HTTPError
from requests.models import Response
from typing import cast
from depwatch.exception import DepwatchException

from depwatch.history import DeploymentHistory

Expand All @@ -14,7 +15,7 @@ def __get_workflow(id):
return ci.get_workflow(id)
except HTTPError as e:
if cast(Response, e.response).status_code == 404:
return []
raise DepwatchException("The workflow is not found")
else:
raise e

Expand All @@ -31,27 +32,18 @@ def __is_succeeded_workflows(workflow) -> bool:
)

for id in workflow_ids:
workflows = __get_workflow(id)
succeeded_workflows = filter(__is_succeeded_workflows, workflows)

first_succeeded_workflow = None
for w in succeeded_workflows:
if first_succeeded_workflow is None:
first_succeeded_workflow = w
continue

if datetime.fromisoformat(w.get("stopped_at")) < datetime.fromisoformat(
first_succeeded_workflow.get("stopped_at")
):
first_succeeded_workflow = w
try:
workflow = __get_workflow(id)
except DepwatchException:
continue

if first_succeeded_workflow is None:
if not __is_succeeded_workflows(workflow):
continue

histories.append(
DeploymentHistory(
first_succeeded_workflow.get("id"),
datetime.fromisoformat(first_succeeded_workflow.get("stopped_at")),
workflow.get("id"),
datetime.fromisoformat(workflow.get("stopped_at")),
)
)

Expand Down
48 changes: 20 additions & 28 deletions tests/test_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@ class TestDeployment:
def test_get_deployment_history(self, mock_Api: Mock):
mock_ci = MagicMock()
mock_ci.get_workflow.side_effect = [
[
{
"id": "40020909-fd66-4657-b3e4-8872627507d2",
"status": "success",
"stopped_at": "2023-01-01T00:00:00.000Z",
},
],
[
{
"id": "84abef06-1fa2-48ec-875e-382e9b7add78",
"status": "success",
"stopped_at": "2023-01-02T00:00:00.000Z",
},
],
{
"id": "40020909-fd66-4657-b3e4-8872627507d2",
"status": "success",
"stopped_at": "2023-01-01T00:00:00.000Z",
},
{
"id": "84abef06-1fa2-48ec-875e-382e9b7add78",
"status": "success",
"stopped_at": "2023-01-02T00:00:00.000Z",
},
]
mock_Api.return_value = mock_ci

Expand Down Expand Up @@ -64,13 +60,11 @@ def test_get_deployment_history_when_the_workflow_has_not_been_completed_then_th
self, mock_Api: Mock
):
mock_ci = MagicMock()
mock_ci.get_workflow.return_value = [
{
"id": "40020909-fd66-4657-b3e4-8872627507d2",
"status": "success",
"stopped_at": None,
},
]
mock_ci.get_workflow.return_value = {
"id": "40020909-fd66-4657-b3e4-8872627507d2",
"status": "success",
"stopped_at": None,
}
mock_Api.return_value = mock_ci

workflow_ids = [
Expand All @@ -85,13 +79,11 @@ def test_get_deployment_history_when_the_workflow_is_not_successful_then_the_ite
self, mock_Api: Mock
):
mock_ci = MagicMock()
mock_ci.get_workflow.return_value = [
{
"id": "40020909-fd66-4657-b3e4-8872627507d2",
"status": "failed",
"stopped_at": "2023-01-01T00:00:00.000Z",
},
]
mock_ci.get_workflow.return_value = {
"id": "40020909-fd66-4657-b3e4-8872627507d2",
"status": "failed",
"stopped_at": "2023-01-01T00:00:00.000Z",
}
mock_Api.return_value = mock_ci

workflow_ids = [
Expand Down

0 comments on commit bbb7151

Please sign in to comment.