-
Notifications
You must be signed in to change notification settings - Fork 309
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
PagerDuty Migrator: Add filtering capabilities and fix user notification rule preservation #5454
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5d633e0
Add filtering capabilities and fix notification policy preservation
joeyorlando 102cc25
Add filtering implementation and tests
joeyorlando ed6f93d
Fix notification rules preservation and add tests
joeyorlando a6e4cac
wip
joeyorlando 45381f8
wip
joeyorlando 1d04a75
linting
joeyorlando b5381f8
wip
joeyorlando 020c865
update docs
joeyorlando 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
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
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
125 changes: 125 additions & 0 deletions
125
tools/migrators/lib/tests/pagerduty/test_notification_rules.py
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import os | ||
from unittest.mock import call, patch | ||
|
||
# Mock environment variables before importing any modules that use them | ||
os.environ["MIGRATING_FROM"] = "pagerduty" | ||
os.environ["ONCALL_API_TOKEN"] = "test-token" | ||
os.environ["ONCALL_API_URL"] = "http://test" | ||
os.environ["PAGERDUTY_API_TOKEN"] = "test-pd-token" | ||
os.environ["MODE"] = "plan" | ||
|
||
from lib.pagerduty.resources.notification_rules import migrate_notification_rules | ||
|
||
|
||
class TestNotificationRulesMigration: | ||
def setup_method(self): | ||
self.pd_user = { | ||
"id": "U1", | ||
"name": "Test User", | ||
"email": "[email protected]", | ||
"notification_rules": [{ | ||
"id": "PD1", | ||
"urgency": "high", | ||
"start_delay_in_minutes": 0, | ||
"contact_method": { | ||
"type": "email_contact_method" | ||
} | ||
}] | ||
} | ||
self.oncall_user = { | ||
"id": "OC1", | ||
"email": "[email protected]", | ||
"notification_rules": [] | ||
} | ||
self.pd_user["oncall_user"] = self.oncall_user | ||
|
||
@patch("lib.pagerduty.resources.notification_rules.PRESERVE_EXISTING_NOTIFICATION_POLICIES", True) | ||
@patch("lib.pagerduty.resources.notification_rules.OnCallAPIClient") | ||
def test_existing_notification_policies_are_preserved(self, MockOnCallAPIClient): | ||
# Setup user with existing notification rules | ||
self.oncall_user["notification_rules"] = [{"id": "NR1"}] | ||
|
||
# Run migration | ||
migrate_notification_rules(self.pd_user) | ||
|
||
# Verify no notification rules were migrated | ||
MockOnCallAPIClient.create.assert_not_called() | ||
|
||
@patch("lib.pagerduty.resources.notification_rules.PRESERVE_EXISTING_NOTIFICATION_POLICIES", True) | ||
@patch("lib.pagerduty.resources.notification_rules.OnCallAPIClient") | ||
def test_notification_policies_migrated_when_none_exist(self, MockOnCallAPIClient): | ||
# Run migration | ||
migrate_notification_rules(self.pd_user) | ||
|
||
# Verify notification rules were migrated for both important and non-important cases | ||
expected_calls = [ | ||
call("personal_notification_rules", { | ||
"user_id": "OC1", | ||
"type": "notify_by_email", | ||
"important": False | ||
}), | ||
call("personal_notification_rules", { | ||
"user_id": "OC1", | ||
"type": "notify_by_email", | ||
"important": True | ||
}) | ||
] | ||
MockOnCallAPIClient.create.assert_has_calls(expected_calls) | ||
|
||
@patch("lib.pagerduty.resources.notification_rules.PRESERVE_EXISTING_NOTIFICATION_POLICIES", False) | ||
@patch("lib.pagerduty.resources.notification_rules.OnCallAPIClient") | ||
def test_existing_notification_policies_are_replaced_when_preserve_is_false(self, MockOnCallAPIClient): | ||
# Setup user with existing notification rules | ||
self.oncall_user["notification_rules"] = [ | ||
{"id": "NR1", "important": False}, | ||
{"id": "NR2", "important": True} | ||
] | ||
|
||
# Run migration | ||
migrate_notification_rules(self.pd_user) | ||
|
||
# Verify old rules were deleted | ||
expected_delete_calls = [ | ||
call("personal_notification_rules/NR1"), | ||
call("personal_notification_rules/NR2") | ||
] | ||
MockOnCallAPIClient.delete.assert_has_calls(expected_delete_calls, any_order=True) | ||
|
||
# Verify new rules were created | ||
expected_create_calls = [ | ||
call("personal_notification_rules", { | ||
"user_id": "OC1", | ||
"type": "notify_by_email", | ||
"important": False | ||
}), | ||
call("personal_notification_rules", { | ||
"user_id": "OC1", | ||
"type": "notify_by_email", | ||
"important": True | ||
}) | ||
] | ||
MockOnCallAPIClient.create.assert_has_calls(expected_create_calls) | ||
|
||
@patch("lib.pagerduty.resources.notification_rules.PRESERVE_EXISTING_NOTIFICATION_POLICIES", False) | ||
@patch("lib.pagerduty.resources.notification_rules.OnCallAPIClient") | ||
def test_notification_policies_migrated_when_none_exist_and_preserve_is_false(self, MockOnCallAPIClient): | ||
# Run migration | ||
migrate_notification_rules(self.pd_user) | ||
|
||
# Verify no rules were deleted (since none existed) | ||
MockOnCallAPIClient.delete.assert_not_called() | ||
|
||
# Verify new rules were created | ||
expected_create_calls = [ | ||
call("personal_notification_rules", { | ||
"user_id": "OC1", | ||
"type": "notify_by_email", | ||
"important": False | ||
}), | ||
call("personal_notification_rules", { | ||
"user_id": "OC1", | ||
"type": "notify_by_email", | ||
"important": True | ||
}) | ||
] | ||
MockOnCallAPIClient.create.assert_has_calls(expected_create_calls) |
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.
there was a request to map Amazon CloudWatch Integration in PagerDuty to our Amazon SNS Integration (apparently these are essentially the same thing?)