-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix substituting regex with empty string #1953
Merged
Merged
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7133ca9
Add type hints to regex sub strategy
JCZuurmond 841aa76
Move temporary name down
JCZuurmond dc9f02e
Reorder init
JCZuurmond f8d61ec
Add type hint for generate migrated groups
JCZuurmond 0b2dc40
Add test to replace regex with empty string
JCZuurmond 3ab908f
Remove duplicate mock
JCZuurmond beeb1ba
Test substitute groups with empty string
JCZuurmond dec6b3a
Check for regexes to be not none
JCZuurmond 5ef5790
Make type hints stricter
JCZuurmond f30c1fc
Add return type hint
JCZuurmond ee61d12
Move temporary name down
JCZuurmond 297eb46
Allow empty substitution value on ConfigureGroups
JCZuurmond bf4f07d
Separate group and substitute question
JCZuurmond ae30f18
Compile regex pattern in strategy init
JCZuurmond 407fd0d
Compile substitute string
JCZuurmond 842d363
Rename variable
JCZuurmond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,7 +167,7 @@ def __init__( | |
self.include_group_names = include_group_names | ||
|
||
@abstractmethod | ||
def generate_migrated_groups(self): | ||
def generate_migrated_groups(self) -> Iterable[MigratedGroup]: | ||
raise NotImplementedError | ||
|
||
def get_filtered_groups(self): | ||
|
@@ -208,9 +208,9 @@ def __init__( | |
self, | ||
workspace_groups_in_workspace, | ||
account_groups_in_account, | ||
/, | ||
renamed_groups_prefix, | ||
include_group_names=None, | ||
*, | ||
renamed_groups_prefix: str, | ||
include_group_names: list[str] | None, | ||
): | ||
super().__init__( | ||
workspace_groups_in_workspace, | ||
|
@@ -219,16 +219,16 @@ def __init__( | |
renamed_groups_prefix=renamed_groups_prefix, | ||
) | ||
|
||
def generate_migrated_groups(self): | ||
def generate_migrated_groups(self) -> Iterable[MigratedGroup]: | ||
workspace_groups = self.get_filtered_groups() | ||
for group in workspace_groups.values(): | ||
temporary_name = f"{self.renamed_groups_prefix}{group.display_name}" | ||
account_group = self.account_groups_in_account.get(group.display_name) | ||
if not account_group: | ||
logger.info( | ||
f"Couldn't find a matching account group for {group.display_name} group using name matching" | ||
) | ||
continue | ||
temporary_name = f"{self.renamed_groups_prefix}{group.display_name}" | ||
yield MigratedGroup( | ||
id_in_workspace=group.id, | ||
name_in_workspace=group.display_name, | ||
|
@@ -246,9 +246,9 @@ def __init__( | |
self, | ||
workspace_groups_in_workspace, | ||
account_groups_in_account, | ||
/, | ||
renamed_groups_prefix, | ||
include_group_names=None, | ||
*, | ||
renamed_groups_prefix: str, | ||
include_group_names: list[str] | None, | ||
): | ||
super().__init__( | ||
workspace_groups_in_workspace, | ||
|
@@ -257,15 +257,15 @@ def __init__( | |
renamed_groups_prefix=renamed_groups_prefix, | ||
) | ||
|
||
def generate_migrated_groups(self): | ||
def generate_migrated_groups(self) -> Iterable[MigratedGroup]: | ||
workspace_groups = self.get_filtered_groups() | ||
account_groups_by_id = {group.external_id: group for group in self.account_groups_in_account.values()} | ||
for group in workspace_groups.values(): | ||
temporary_name = f"{self.renamed_groups_prefix}{group.display_name}" | ||
account_group = account_groups_by_id.get(group.external_id) | ||
if not account_group: | ||
logger.info(f"Couldn't find a matching account group for {group.display_name} group with external_id") | ||
continue | ||
temporary_name = f"{self.renamed_groups_prefix}{group.display_name}" | ||
yield MigratedGroup( | ||
id_in_workspace=group.id, | ||
name_in_workspace=group.display_name, | ||
|
@@ -281,27 +281,26 @@ def generate_migrated_groups(self): | |
class RegexSubStrategy(GroupMigrationStrategy): | ||
def __init__( | ||
self, | ||
workspace_groups_in_workspace, | ||
account_groups_in_account, | ||
/, | ||
renamed_groups_prefix, | ||
include_group_names=None, | ||
workspace_group_regex: str | None = None, | ||
workspace_group_replace: str | None = None, | ||
workspace_groups_in_workspace: dict[str, Group], | ||
account_groups_in_account: dict[str, Group], | ||
*, | ||
renamed_groups_prefix: str, | ||
include_group_names: list[str] | None, | ||
workspace_group_regex: str, | ||
workspace_group_replace: str, | ||
): | ||
super().__init__( | ||
workspace_groups_in_workspace, | ||
account_groups_in_account, | ||
include_group_names=include_group_names, | ||
renamed_groups_prefix=renamed_groups_prefix, | ||
) | ||
self.workspace_group_replace = workspace_group_replace | ||
self.workspace_group_regex = workspace_group_regex | ||
self.workspace_group_replace = workspace_group_replace | ||
|
||
def generate_migrated_groups(self): | ||
def generate_migrated_groups(self) -> Iterable[MigratedGroup]: | ||
workspace_groups = self.get_filtered_groups() | ||
for group in workspace_groups.values(): | ||
temporary_name = f"{self.renamed_groups_prefix}{group.display_name}" | ||
name_in_account = self._safe_sub( | ||
group.display_name, self.workspace_group_regex, self.workspace_group_replace | ||
) | ||
|
@@ -311,6 +310,7 @@ def generate_migrated_groups(self): | |
f"Couldn't find a matching account group for {group.display_name} group with regex substitution" | ||
) | ||
continue | ||
temporary_name = f"{self.renamed_groups_prefix}{group.display_name}" | ||
yield MigratedGroup( | ||
id_in_workspace=group.id, | ||
name_in_workspace=group.display_name, | ||
|
@@ -328,11 +328,11 @@ def __init__( | |
self, | ||
workspace_groups_in_workspace, | ||
account_groups_in_account, | ||
/, | ||
renamed_groups_prefix, | ||
include_group_names=None, | ||
workspace_group_regex: str | None = None, | ||
account_group_regex: str | None = None, | ||
*, | ||
renamed_groups_prefix: str, | ||
include_group_names: list[str] | None, | ||
workspace_group_regex: str, | ||
account_group_regex: str, | ||
): | ||
super().__init__( | ||
workspace_groups_in_workspace, | ||
|
@@ -343,7 +343,7 @@ def __init__( | |
self.account_group_regex = account_group_regex | ||
self.workspace_group_regex = workspace_group_regex | ||
|
||
def generate_migrated_groups(self): | ||
def generate_migrated_groups(self) -> Iterable[MigratedGroup]: | ||
workspace_groups_by_match = { | ||
self._safe_match(group_name, self.workspace_group_regex): group | ||
for group_name, group in self.get_filtered_groups().items() | ||
|
@@ -353,13 +353,13 @@ def generate_migrated_groups(self): | |
for group_name, group in self.account_groups_in_account.items() | ||
} | ||
for group_match, ws_group in workspace_groups_by_match.items(): | ||
temporary_name = f"{self.renamed_groups_prefix}{ws_group.display_name}" | ||
account_group = account_groups_by_match.get(group_match) | ||
if not account_group: | ||
logger.info( | ||
f"Couldn't find a matching account group for {ws_group.display_name} group with regex matching" | ||
) | ||
continue | ||
temporary_name = f"{self.renamed_groups_prefix}{ws_group.display_name}" | ||
yield MigratedGroup( | ||
id_in_workspace=ws_group.id, | ||
name_in_workspace=ws_group.display_name, | ||
|
@@ -672,7 +672,7 @@ def _reflect_account_group_to_workspace(self, account_group_id: str): | |
def _get_strategy( | ||
self, workspace_groups_in_workspace: dict[str, Group], account_groups_in_account: dict[str, Group] | ||
) -> GroupMigrationStrategy: | ||
if self._workspace_group_regex and self._workspace_group_replace: | ||
if self._workspace_group_regex is not None and self._workspace_group_replace is not None: | ||
return RegexSubStrategy( | ||
workspace_groups_in_workspace, | ||
account_groups_in_account, | ||
|
@@ -681,7 +681,7 @@ def _get_strategy( | |
workspace_group_regex=self._workspace_group_regex, | ||
workspace_group_replace=self._workspace_group_replace, | ||
) | ||
if self._workspace_group_regex and self._account_group_regex: | ||
if self._workspace_group_regex is not None and self._account_group_regex is not None: | ||
return RegexMatchStrategy( | ||
workspace_groups_in_workspace, | ||
account_groups_in_account, | ||
|
@@ -756,7 +756,7 @@ def _configure_substitution(self): | |
if not match_value: | ||
return False | ||
sub_value = self._ask_for_group("Enter the substitution value") | ||
if not sub_value: | ||
if sub_value is None: | ||
return False | ||
self.workspace_group_regex = match_value | ||
self.workspace_group_replace = sub_value | ||
|
@@ -788,8 +788,8 @@ def _configure_external(self): | |
return True | ||
|
||
@staticmethod | ||
def _is_valid_group_str(group_str: str): | ||
return group_str and not re.search(r"[\s#,+ \\<>;]", group_str) | ||
def _is_valid_group_str(group_str: str | None) -> bool: | ||
return group_str is not None and not re.search(r"[\s#,+ \\<>;]", group_str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please pre-compile as a class-level property. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
@staticmethod | ||
def _validate_regex(regex_input: str) -> bool: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the caller is allowed to pass these in as strings (instead of compiled patterns) I'd really like to compile them here during isn't initialisation. This isn't (just) a performance thing: a) it catches problems with regex syntax (a common problem) during initialisation rather than later when we use it for the first time; and b) any time a string is converted to its domain type lots of things just become simpler (IDE completions, type checking, etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, note that I kept the public API the same