|
| 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