Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit ecae01d

Browse files
authored
feat: migrate @group annotations (#1158)
Closes #1154. ### Summary of Changes Migration of `@group` annotations through a mapping from old api elements to new api elements for the method and the parameters of the annotated method in apiv1. If there is a mapping for a method from apiv1 to apiv2 and at least two parameters of the method in apiv2 and of the parameters mentioned in the annotation are mapped together, a `@group` annotation will be created. Additionally, the group name will be changed if the number of grouped parameters in the new annotated function is not equal with the number of grouped parameters in the apiv1 function. Otherwise, mark the mapped apiv2 elements with a `@todo` annotation that include possible grouping parameters. ### Testing Instructions run the migrate command or view and run the `test_migration.py` file Co-authored-by: Aclrian <[email protected]>
1 parent 17b48bd commit ecae01d

File tree

6 files changed

+665
-5
lines changed

6 files changed

+665
-5
lines changed

package-parser/package_parser/processing/migration/_migrate.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
migrate_boundary_annotation,
1010
migrate_called_after_annotation,
1111
migrate_enum_annotation,
12+
migrate_group_annotation,
1213
migrate_move_annotation,
1314
migrate_remove_annotation,
1415
migrate_rename_annotation,
@@ -56,6 +57,14 @@ def migrate_annotations(
5657
for annotation in migrate_enum_annotation(enum_annotation, mapping):
5758
migrated_annotation_store.add_annotation(annotation)
5859

60+
for group_annotation in annotationsv1.groupAnnotations:
61+
mapping = _get_mapping_from_annotation(group_annotation, mappings)
62+
if mapping is not None:
63+
for annotation in migrate_group_annotation(
64+
group_annotation, mapping, mappings
65+
):
66+
migrated_annotation_store.add_annotation(annotation)
67+
5968
for move_annotation in annotationsv1.moveAnnotations:
6069
mapping = _get_mapping_from_annotation(move_annotation, mappings)
6170
if mapping is not None:

package-parser/package_parser/processing/migration/annotations/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ._migrate_boundary_annotation import migrate_boundary_annotation
44
from ._migrate_called_after_annotation import migrate_called_after_annotation
55
from ._migrate_enum_annotation import migrate_enum_annotation
6+
from ._migrate_group_annotation import migrate_group_annotation
67
from ._migrate_move_annotation import migrate_move_annotation
78
from ._migrate_remove_annotation import migrate_remove_annotation
89
from ._migrate_rename_annotation import migrate_rename_annotation

package-parser/package_parser/processing/migration/annotations/_get_migration_text.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def get_migration_text(
112112
migrate_text += (
113113
" and the possible replacements (" + _list_api_elements(functions) + ")"
114114
)
115+
116+
parameters = [
117+
parameter
118+
for parameter in additional_information
119+
if isinstance(parameter, Parameter)
120+
]
121+
if len(parameters) > 0:
122+
migrate_text += (
123+
" and the possible replacements ("
124+
+ _list_api_elements(parameters)
125+
+ ")"
126+
)
127+
115128
return migrate_text
116129

117130

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
from copy import deepcopy
2+
3+
from package_parser.processing.annotations.model import (
4+
AbstractAnnotation,
5+
EnumReviewResult,
6+
GroupAnnotation,
7+
TodoAnnotation,
8+
)
9+
from package_parser.processing.api.model import Attribute, Function, Parameter, Result
10+
from package_parser.processing.migration.model import Mapping
11+
12+
from ._constants import migration_author
13+
from ._get_migration_text import get_migration_text
14+
15+
16+
def migrate_group_annotation(
17+
annotation: GroupAnnotation, mapping: Mapping, mappings: list[Mapping]
18+
) -> list[AbstractAnnotation]:
19+
group_annotation = deepcopy(annotation)
20+
authors = group_annotation.authors
21+
authors.append(migration_author)
22+
group_annotation.authors = authors
23+
24+
migrated_annotations: list[AbstractAnnotation] = []
25+
26+
for functionv2 in mapping.get_apiv2_elements():
27+
if isinstance(functionv2, (Attribute, Result)):
28+
continue
29+
if not isinstance(functionv2, Function):
30+
migrated_annotations.append(
31+
TodoAnnotation(
32+
target=functionv2.id,
33+
authors=authors,
34+
reviewers=group_annotation.reviewers,
35+
comment=group_annotation.comment,
36+
reviewResult=group_annotation.reviewResult,
37+
newTodo=get_migration_text(group_annotation, mapping),
38+
)
39+
)
40+
else:
41+
parameter_replacements = _get_mappings_for_grouped_parameters(
42+
group_annotation, mappings, functionv2
43+
)
44+
grouped_parameters: list[Parameter] = []
45+
name_modifier = ""
46+
47+
for parameter_list in parameter_replacements:
48+
if len(parameter_list) == 0:
49+
name_modifier = "0" + name_modifier
50+
else:
51+
grouped_parameters.extend(parameter_list)
52+
if len(parameter_list) == 1:
53+
name_modifier = "1" + name_modifier
54+
else:
55+
name_modifier = "0" + name_modifier
56+
57+
remove_duplicates_and_preserve_order = [
58+
i
59+
for n, i in enumerate(grouped_parameters)
60+
if i not in grouped_parameters[:n]
61+
]
62+
grouped_parameters = remove_duplicates_and_preserve_order
63+
64+
group_name = group_annotation.groupName
65+
review_result = EnumReviewResult.NONE
66+
migrate_text = get_migration_text(group_annotation, mapping)
67+
68+
if len(grouped_parameters) < 2 < len(group_annotation.parameters):
69+
migrated_annotations.append(
70+
TodoAnnotation(
71+
target=functionv2.id,
72+
authors=authors,
73+
reviewers=group_annotation.reviewers,
74+
comment=group_annotation.comment,
75+
reviewResult=group_annotation.reviewResult,
76+
newTodo=get_migration_text(
77+
group_annotation,
78+
mapping,
79+
additional_information=grouped_parameters,
80+
),
81+
)
82+
)
83+
continue
84+
85+
if len(grouped_parameters) != len(group_annotation.parameters):
86+
group_name += str(int(name_modifier, base=2))
87+
review_result = EnumReviewResult.UNSURE
88+
if len(group_annotation.comment) != 0:
89+
migrate_text = group_annotation.comment + "\n" + migrate_text
90+
91+
migrated_annotations.append(
92+
GroupAnnotation(
93+
target=functionv2.id,
94+
authors=authors,
95+
reviewers=group_annotation.reviewers,
96+
comment=(
97+
migrate_text
98+
if review_result is EnumReviewResult.UNSURE
99+
else group_annotation.comment
100+
),
101+
reviewResult=review_result,
102+
groupName=group_name,
103+
parameters=[parameter.name for parameter in grouped_parameters],
104+
)
105+
)
106+
107+
return migrated_annotations
108+
109+
110+
def _get_mappings_for_grouped_parameters(
111+
group_annotation: GroupAnnotation, mappings: list[Mapping], functionv2: Function
112+
) -> list[list[Parameter]]:
113+
parameter_ids = [
114+
group_annotation.target + "/" + parameter_name
115+
for parameter_name in group_annotation.parameters
116+
]
117+
118+
matched_parameters: list[list[Parameter]] = []
119+
for parameter_id in parameter_ids:
120+
for mapping in mappings:
121+
for parameterv1 in mapping.get_apiv1_elements():
122+
if (
123+
isinstance(parameterv1, Parameter)
124+
and parameterv1.id == parameter_id
125+
):
126+
mapped_parameters: list[Parameter] = []
127+
for parameterv2 in mapping.get_apiv2_elements():
128+
if isinstance(
129+
parameterv2, Parameter
130+
) and parameterv2.id.startswith(functionv2.id + "/"):
131+
mapped_parameters.append(parameterv2)
132+
matched_parameters.append(mapped_parameters)
133+
break
134+
return matched_parameters

0 commit comments

Comments
 (0)