Skip to content

Commit f62463d

Browse files
authored
feat(LAB-3664): add method for list all available steps in a project (#1896)
1 parent ef3a703 commit f62463d

File tree

6 files changed

+100
-4
lines changed

6 files changed

+100
-4
lines changed

src/kili/adapters/kili_api_gateway/project_workflow/mappers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ def project_input_mapper(data: ProjectWorkflowDataKiliAPIGatewayInput) -> Dict:
1010
return {
1111
"enforceStepSeparation": data.enforce_step_separation,
1212
}
13+
14+
15+
def step_data_mapper(data: Dict) -> Dict:
16+
"""Build the GraphQL StepData variable to be sent in an operation."""
17+
return {
18+
"id": data["id"],
19+
"name": data["name"],
20+
"type": data["type"],
21+
}

src/kili/adapters/kili_api_gateway/project_workflow/operations.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,18 @@ def get_update_project_workflow_mutation(fragment: str) -> str:
1010
}}
1111
}}
1212
"""
13+
14+
15+
GQL_GET_STEPS = """
16+
query getSteps($where: ProjectWhere!, $first: PageSize!, $skip: Int!) {
17+
data: projects(where: $where, first: $first, skip: $skip) {
18+
id
19+
steps {
20+
id
21+
name
22+
type
23+
isActivated
24+
}
25+
}
26+
}
27+
"""

src/kili/adapters/kili_api_gateway/project_workflow/operations_mixin.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
"""Mixin extending Kili API Gateway class with Projects related operations."""
22

3-
from typing import Dict
3+
from typing import Dict, List
44

55
from kili.adapters.kili_api_gateway.base import BaseOperationMixin
66
from kili.adapters.kili_api_gateway.helpers.queries import (
77
fragment_builder,
88
)
99
from kili.domain.project import ProjectId
10+
from kili.exceptions import NotFound
1011

11-
from .mappers import project_input_mapper
12+
from .mappers import project_input_mapper, step_data_mapper
1213
from .operations import (
14+
GQL_GET_STEPS,
1315
get_update_project_workflow_mutation,
1416
)
1517
from .types import ProjectWorkflowDataKiliAPIGatewayInput
@@ -35,3 +37,25 @@ def update_project_workflow(
3537
variables = {"input": project_workflow_input}
3638
result = self.graphql_client.execute(mutation, variables)
3739
return result["data"]
40+
41+
def get_steps(
42+
self,
43+
project_id: ProjectId,
44+
) -> List[Dict]:
45+
"""Get steps in a project workflow."""
46+
variables = {"where": {"id": project_id}, "first": 1, "skip": 0}
47+
result = self.graphql_client.execute(GQL_GET_STEPS, variables)
48+
project = result["data"]
49+
50+
if len(project) == 0:
51+
raise NotFound(f"project ID: {project_id}. The project does not exist.")
52+
53+
steps = project[0].get("steps")
54+
55+
if len(steps) == 0:
56+
raise NotFound(
57+
f"project ID: {project_id}. The workflow v2 is not activated on this project."
58+
)
59+
steps_mapper = step_data_mapper(steps)
60+
61+
return [step for step in steps_mapper if step.get("isActivated") is True]

src/kili/presentation/client/project_workflow.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Client presentation methods for project workflow."""
22

3-
from typing import Any, Dict, Optional
3+
from typing import Any, Dict, List, Optional
44

55
from typeguard import typechecked
66

@@ -35,3 +35,20 @@ def update_project_workflow(
3535
project_id=ProjectId(project_id),
3636
enforce_step_separation=enforce_step_separation,
3737
)
38+
39+
@typechecked
40+
def get_steps(
41+
self,
42+
project_id: str,
43+
) -> List[Dict[str, Any]]:
44+
"""Get steps in a project workflow.
45+
46+
Args:
47+
project_id: Id of the project.
48+
49+
Returns:
50+
A dict with the steps of the project workflow.
51+
"""
52+
return ProjectWorkflowUseCases(self.kili_api_gateway).get_steps(
53+
project_id=ProjectId(project_id),
54+
)

src/kili/use_cases/project_workflow/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Project use cases."""
22

3-
from typing import Dict, Optional
3+
from typing import Dict, List, Optional
44

55
from kili.adapters.kili_api_gateway.project_workflow.types import (
66
ProjectWorkflowDataKiliAPIGatewayInput,
@@ -23,3 +23,10 @@ def update_project_workflow(
2323
)
2424

2525
return self._kili_api_gateway.update_project_workflow(project_id, project_workflow_data)
26+
27+
def get_steps(
28+
self,
29+
project_id: ProjectId,
30+
) -> List[Dict[str, object]]:
31+
"""Get steps in a project workflow."""
32+
return self._kili_api_gateway.get_steps(project_id)

tests/integration/presentation/test_project_workflow.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
get_update_project_workflow_mutation,
66
)
77
from kili.presentation.client.project_workflow import ProjectWorkflowClientMethods
8+
from kili.use_cases.project_workflow import ProjectWorkflowUseCases
89

910

1011
def test_when_updating_project_workflow_then_it_returns_updated_project_workflow(
@@ -27,3 +28,26 @@ def test_when_updating_project_workflow_then_it_returns_updated_project_workflow
2728
"input": {"projectId": "fake_proj_id", "enforceStepSeparation": False},
2829
},
2930
)
31+
32+
33+
def test_when_getting_steps_then_it_returns_steps(
34+
mocker: pytest_mock.MockerFixture,
35+
):
36+
mocker.patch.object(
37+
ProjectWorkflowUseCases,
38+
"get_steps",
39+
return_value=[{"id": "step_id", "name": "step_name", "type": "step_type"}],
40+
)
41+
kili = ProjectWorkflowClientMethods()
42+
kili.kili_api_gateway = KiliAPIGateway(
43+
graphql_client=mocker.MagicMock(), http_client=mocker.MagicMock()
44+
)
45+
# Given
46+
project_id = "fake_proj_id"
47+
48+
# When
49+
steps = kili.get_steps(project_id)
50+
51+
# Then
52+
53+
assert steps == [{"id": "step_id", "name": "step_name", "type": "step_type"}]

0 commit comments

Comments
 (0)