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

Commit a9684ea

Browse files
authored
feat: migrate @todo annotations (#1141)
Closes #1124. ### Summary of Changes - add migration for todo annotations to another version, with the possibilities: - If the annotation is mapped to only one api element, the annotation will be migrated as expected. - If the annotation is mapped from one api element to multiple api elements, todo annotation will be created for all api elements in the image for the mapping. - If the annotation is mapped from multiple api element to multiple api elements, a unsure todo annotation will be created for all api elements in the image for the mapping. Maybe inspect some details for the target of the annotation. E.g., its type ### Testing Instructions run the migrate command or view and run the test_migration.py file
1 parent eddb597 commit a9684ea

File tree

8 files changed

+355
-37
lines changed

8 files changed

+355
-37
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from package_parser.processing.migration.annotations import (
99
migrate_enum_annotation,
1010
migrate_rename_annotation,
11+
migrate_todo_annotation,
1112
)
1213
from package_parser.processing.migration.model import Mapping
1314

@@ -42,4 +43,10 @@ def migrate_annotations(
4243
for annotation in migrate_rename_annotation(rename_annotation, mapping):
4344
migrated_annotation_store.add_annotation(annotation)
4445

46+
for todo_annotation in annotationsv1.todoAnnotations:
47+
mapping = _get_mapping_from_annotation(todo_annotation, mappings)
48+
if mapping is not None:
49+
for annotation in migrate_todo_annotation(todo_annotation, mapping):
50+
migrated_annotation_store.add_annotation(annotation)
51+
4552
return migrated_annotation_store
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from ._constants import migration_author
22
from ._migrate_enum_annotation import migrate_enum_annotation
33
from ._migrate_rename_annotation import migrate_rename_annotation
4+
from ._migrate_todo_annotation import migrate_todo_annotation

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ def migrate_enum_annotation(
9595
return [enum_annotation]
9696
return [
9797
TodoAnnotation(
98-
parameter.id, authors, [], "", EnumReviewResult.NONE, migrate_text
98+
parameter.id,
99+
authors,
100+
enum_annotation.reviewers,
101+
enum_annotation.comment,
102+
EnumReviewResult.NONE,
103+
migrate_text,
99104
)
100105
]
101106

@@ -113,8 +118,8 @@ def migrate_enum_annotation(
113118
EnumAnnotation(
114119
parameter.id,
115120
authors,
116-
[],
117-
"",
121+
enum_annotation.reviewers,
122+
enum_annotation.comment,
118123
EnumReviewResult.NONE,
119124
enum_annotation.enumName,
120125
enum_annotation.pairs,
@@ -127,8 +132,8 @@ def migrate_enum_annotation(
127132
TodoAnnotation(
128133
parameter.id,
129134
authors,
130-
[],
131-
"",
135+
enum_annotation.reviewers,
136+
enum_annotation.comment,
132137
EnumReviewResult.UNSURE,
133138
migrate_text,
134139
)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ def migrate_rename_annotation(
5858
return [rename_annotation]
5959
todo_annotations.append(
6060
TodoAnnotation(
61-
element.id, authors, [], "", EnumReviewResult.NONE, migrate_text
61+
element.id,
62+
authors,
63+
rename_annotation.reviewers,
64+
rename_annotation.comment,
65+
EnumReviewResult.NONE,
66+
migrate_text,
6267
)
6368
)
6469
return todo_annotations
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from copy import deepcopy
2+
3+
from package_parser.processing.annotations.model import (
4+
AbstractAnnotation,
5+
EnumReviewResult,
6+
TodoAnnotation,
7+
)
8+
from package_parser.processing.api.model import Attribute, Result
9+
from package_parser.processing.migration.model import (
10+
ManyToOneMapping,
11+
Mapping,
12+
OneToOneMapping,
13+
)
14+
15+
from ._constants import migration_author
16+
17+
18+
def migrate_todo_annotation(
19+
todo_annotation: TodoAnnotation, mapping: Mapping
20+
) -> list[AbstractAnnotation]:
21+
todo_annotation = deepcopy(todo_annotation)
22+
authors = todo_annotation.authors
23+
authors.append(migration_author)
24+
todo_annotation.authors = authors
25+
26+
if isinstance(mapping, (ManyToOneMapping, OneToOneMapping)):
27+
element = mapping.get_apiv2_elements()[0]
28+
if isinstance(element, (Attribute, Result)):
29+
return []
30+
todo_annotation.target = element.id
31+
return [todo_annotation]
32+
33+
migrate_text = (
34+
"The @Todo Annotation with the todo '"
35+
+ todo_annotation.newTodo
36+
+ "' from the previous version was at '"
37+
+ todo_annotation.target
38+
+ "' and the possible alternatives in the new version of the api are: "
39+
+ ", ".join(
40+
map(lambda api_element: api_element.name, mapping.get_apiv2_elements())
41+
)
42+
)
43+
44+
annotated_apiv1_element = None
45+
for element in mapping.get_apiv1_elements():
46+
if not isinstance(element, (Attribute, Result)) and element.id:
47+
annotated_apiv1_element = element
48+
49+
todo_annotations: list[AbstractAnnotation] = []
50+
for element in mapping.get_apiv2_elements():
51+
if isinstance(element, type(annotated_apiv1_element)) and not isinstance(
52+
element, (Attribute, Result)
53+
):
54+
todo_annotations.append(
55+
TodoAnnotation(
56+
element.id,
57+
authors,
58+
todo_annotation.reviewers,
59+
todo_annotation.comment,
60+
EnumReviewResult.NONE,
61+
todo_annotation.newTodo,
62+
)
63+
)
64+
elif not isinstance(element, (Attribute, Result)):
65+
todo_annotations.append(
66+
TodoAnnotation(
67+
element.id,
68+
authors,
69+
todo_annotation.reviewers,
70+
todo_annotation.comment,
71+
EnumReviewResult.UNSURE,
72+
migrate_text,
73+
)
74+
)
75+
return todo_annotations

package-parser/tests/processing/migration/annotations/test_rename_migration.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@ def migrate_rename_annotation_data_one_to_one_mapping() -> Tuple[
4343
documentation=ParameterDocumentation("", "", ""),
4444
)
4545
mappings = OneToOneMapping(1.0, parameterv1, parameterv2)
46-
annotationsv1 = RenameAnnotation(
46+
annotationv1 = RenameAnnotation(
4747
target="test/test.Test_",
4848
authors=["testauthor"],
4949
reviewers=[],
5050
comment="",
5151
reviewResult=EnumReviewResult.NONE,
5252
newName="TestE",
5353
)
54-
annotationsv2 = RenameAnnotation(
54+
annotationv2 = RenameAnnotation(
5555
target="test/test.TestB",
5656
authors=["testauthor", migration_author],
5757
reviewers=[],
5858
comment="",
5959
reviewResult=EnumReviewResult.NONE,
6060
newName="TestE",
6161
)
62-
return mappings, annotationsv1, [annotationsv2]
62+
return mappings, annotationv1, [annotationv2]
6363

6464

6565
def migrate_rename_annotation_data_one_to_many_mapping__with_changed_new_name() -> Tuple[
@@ -68,50 +68,50 @@ def migrate_rename_annotation_data_one_to_many_mapping__with_changed_new_name()
6868
list[AbstractAnnotation],
6969
]:
7070
parameterv1 = Parameter(
71-
id_="test/test.Test",
71+
id_="test/test.rename.Test",
7272
name="Test",
73-
qname="test.Test",
73+
qname="test.rename.Test",
7474
default_value=None,
7575
assigned_by=ParameterAssignment.POSITION_OR_NAME,
7676
is_public=True,
7777
documentation=ParameterDocumentation("", "", ""),
7878
)
7979
parameterv2_a = Parameter(
80-
id_="test/test.TestA",
80+
id_="test/test.rename.TestA",
8181
name="TestA",
82-
qname="test.TestA",
82+
qname="test.rename.TestA",
8383
default_value=None,
8484
assigned_by=ParameterAssignment.POSITION_OR_NAME,
8585
is_public=True,
8686
documentation=ParameterDocumentation("", "", ""),
8787
)
8888
parameterv2_b = Parameter(
89-
id_="test/test.TestB",
89+
id_="test/test.rename.TestB",
9090
name="TestB",
91-
qname="test.TestB",
91+
qname="test.rename.TestB",
9292
default_value=None,
9393
assigned_by=ParameterAssignment.POSITION_OR_NAME,
9494
is_public=True,
9595
documentation=ParameterDocumentation("", "", ""),
9696
)
9797
mappings = OneToManyMapping(1.0, parameterv1, [parameterv2_a, parameterv2_b])
98-
annotationsv1 = RenameAnnotation(
99-
target="test/test.Test",
98+
annotationv1 = RenameAnnotation(
99+
target="test/test.rename.Test",
100100
authors=["testauthor"],
101101
reviewers=[],
102102
comment="",
103103
reviewResult=EnumReviewResult.NONE,
104104
newName="TestA",
105105
)
106-
annotationsv2 = RenameAnnotation(
107-
target="test/test.TestA",
106+
annotationv2 = RenameAnnotation(
107+
target="test/test.rename.TestA",
108108
authors=["testauthor", migration_author],
109109
reviewers=[],
110-
comment="The @Rename Annotation with the new name 'TestA' from the previous version was at 'test/test.Test' and the possible alternatives in the new version of the api are: TestA, TestB",
110+
comment="The @Rename Annotation with the new name 'TestA' from the previous version was at 'test/test.rename.Test' and the possible alternatives in the new version of the api are: TestA, TestB",
111111
reviewResult=EnumReviewResult.UNSURE,
112112
newName="TestA",
113113
)
114-
return mappings, annotationsv1, [annotationsv2]
114+
return mappings, annotationv1, [annotationv2]
115115

116116

117117
def migrate_rename_annotation_data_one_to_many_mapping() -> Tuple[
@@ -120,59 +120,59 @@ def migrate_rename_annotation_data_one_to_many_mapping() -> Tuple[
120120
list[AbstractAnnotation],
121121
]:
122122
parameterv1 = Parameter(
123-
id_="test/test.Test",
123+
id_="test/test.rename.Test",
124124
name="Test",
125-
qname="test.Test",
125+
qname="test.rename.Test",
126126
default_value=None,
127127
assigned_by=ParameterAssignment.POSITION_OR_NAME,
128128
is_public=True,
129129
documentation=ParameterDocumentation("", "", ""),
130130
)
131131
parameterv2_a = Parameter(
132-
id_="test/test.TestA",
132+
id_="test/test.rename.TestA",
133133
name="TestA",
134-
qname="test.TestA",
134+
qname="test.rename.TestA",
135135
default_value=None,
136136
assigned_by=ParameterAssignment.POSITION_OR_NAME,
137137
is_public=True,
138138
documentation=ParameterDocumentation("", "", ""),
139139
)
140140
parameterv2_b = Parameter(
141-
id_="test/test.TestB",
141+
id_="test/test.rename.TestB",
142142
name="TestB",
143-
qname="test.TestB",
143+
qname="test.rename.TestB",
144144
default_value=None,
145145
assigned_by=ParameterAssignment.POSITION_OR_NAME,
146146
is_public=True,
147147
documentation=ParameterDocumentation("", "", ""),
148148
)
149149
mappings = OneToManyMapping(1.0, parameterv1, [parameterv2_a, parameterv2_b])
150-
annotationsv1 = RenameAnnotation(
151-
target="test/test.Test",
150+
annotationv1 = RenameAnnotation(
151+
target="test/test.rename.Test",
152152
authors=["testauthor"],
153153
reviewers=[],
154154
comment="",
155155
reviewResult=EnumReviewResult.NONE,
156156
newName="TestZ",
157157
)
158-
annotationsv2_a = TodoAnnotation(
159-
target="test/test.TestA",
158+
annotationv2_a = TodoAnnotation(
159+
target="test/test.rename.TestA",
160160
authors=["testauthor", migration_author],
161161
reviewers=[],
162162
comment="",
163163
reviewResult=EnumReviewResult.NONE,
164-
newTodo="The @Rename Annotation with the new name 'TestZ' from the previous version was at 'test/test.Test' and the possible alternatives in the new version of the api are: TestA, TestB",
164+
newTodo="The @Rename Annotation with the new name 'TestZ' from the previous version was at 'test/test.rename.Test' and the possible alternatives in the new version of the api are: TestA, TestB",
165165
)
166-
annotationsv2_b = TodoAnnotation(
167-
target="test/test.TestB",
166+
annotationv2_b = TodoAnnotation(
167+
target="test/test.rename.TestB",
168168
authors=["testauthor", migration_author],
169169
reviewers=[],
170170
comment="",
171171
reviewResult=EnumReviewResult.NONE,
172-
newTodo="The @Rename Annotation with the new name 'TestZ' from the previous version was at 'test/test.Test' and the possible alternatives in the new version of the api are: TestA, TestB",
172+
newTodo="The @Rename Annotation with the new name 'TestZ' from the previous version was at 'test/test.rename.Test' and the possible alternatives in the new version of the api are: TestA, TestB",
173173
)
174174
return (
175175
mappings,
176-
annotationsv1,
177-
[annotationsv2_a, annotationsv2_b],
176+
annotationv1,
177+
[annotationv2_a, annotationv2_b],
178178
)

0 commit comments

Comments
 (0)