Skip to content

Commit

Permalink
Closes #129: Introduce pre-event signals for branch actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Sep 17, 2024
1 parent a841eb1 commit 63d8f2c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
35 changes: 25 additions & 10 deletions netbox_branching/models/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ def sync(self, user, commit=True):
if not self.ready:
raise Exception(f"Branch {self} is not ready to sync")

# Emit pre-sync signal
pre_sync.send(sender=self.__class__, branch=self, user=user)

# Retrieve unsynced changes before we update the Branch's status
if changes := self.get_unsynced_changes().order_by('time'):
logger.info(f"Found {len(changes)} changes to sync")
Expand Down Expand Up @@ -288,8 +291,8 @@ def sync(self, user, commit=True):
logger.debug(f"Recording branch event: {BranchEventTypeChoices.SYNCED}")
BranchEvent.objects.create(branch=self, user=user, type=BranchEventTypeChoices.SYNCED)

# Emit branch_synced signal
branch_synced.send(sender=self.__class__, branch=self, user=user)
# Emit post-sync signal
post_sync.send(sender=self.__class__, branch=self, user=user)

logger.info('Syncing completed')

Expand All @@ -306,6 +309,9 @@ def merge(self, user, commit=True):
if not self.ready:
raise Exception(f"Branch {self} is not ready to merge")

# Emit pre-merge signal
pre_merge.send(sender=self.__class__, branch=self, user=user)

# Retrieve staged changes before we update the Branch's status
if changes := self.get_unmerged_changes().order_by('time'):
logger.info(f"Found {len(changes)} changes to merge")
Expand Down Expand Up @@ -354,8 +360,8 @@ def merge(self, user, commit=True):
logger.debug(f"Recording branch event: {BranchEventTypeChoices.MERGED}")
BranchEvent.objects.create(branch=self, user=user, type=BranchEventTypeChoices.MERGED)

# Emit branch_merged signal
branch_merged.send(sender=self.__class__, branch=self, user=user)
# Emit post-merge signal
post_merge.send(sender=self.__class__, branch=self, user=user)

logger.info('Merging completed')

Expand All @@ -375,6 +381,9 @@ def revert(self, user, commit=True):
if not self.merged:
raise Exception(f"Only merged branches can be reverted.")

# Emit pre-revert signal
pre_revert.send(sender=self.__class__, branch=self, user=user)

# Retrieve applied changes before we update the Branch's status
if changes := self.get_changes().order_by('-time'):
logger.info(f"Found {len(changes)} changes to revert")
Expand Down Expand Up @@ -423,8 +432,8 @@ def revert(self, user, commit=True):
logger.debug(f"Recording branch event: {BranchEventTypeChoices.REVERTED}")
BranchEvent.objects.create(branch=self, user=user, type=BranchEventTypeChoices.REVERTED)

# Emit branch_reverted signal
branch_reverted.send(sender=self.__class__, branch=self, user=user)
# Emit post-revert signal
post_revert.send(sender=self.__class__, branch=self, user=user)

logger.info('Reversion completed')

Expand All @@ -440,6 +449,9 @@ def provision(self, user):
logger = logging.getLogger('netbox_branching.branch.provision')
logger.info(f'Provisioning branch {self} ({self.schema_name})')

# Emit pre-provision signal
pre_provision.send(sender=self.__class__, branch=self, user=user)

# Update Branch status
Branch.objects.filter(pk=self.pk).update(status=BranchStatusChoices.PROVISIONING)

Expand Down Expand Up @@ -516,8 +528,8 @@ def provision(self, user):

raise e

# Emit branch_provisioned signal
branch_provisioned.send(sender=self.__class__, branch=self, user=user)
# Emit post-provision signal
post_provision.send(sender=self.__class__, branch=self, user=user)

logger.info('Provisioning completed')

Expand Down Expand Up @@ -545,15 +557,18 @@ def deprovision(self):
logger = logging.getLogger('netbox_branching.branch.provision')
logger.info(f'Deprovisioning branch {self} ({self.schema_name})')

# Emit pre-deprovision signal
pre_deprovision.send(sender=self.__class__, branch=self)

with connection.cursor() as cursor:
# Delete the schema and all its tables
logger.debug(f'Deleting schema {self.schema_name}')
cursor.execute(
f"DROP SCHEMA IF EXISTS {self.schema_name} CASCADE"
)

# Emit branch_deprovisioned signal
branch_deprovisioned.send(sender=self.__class__, branch=self)
# Emit post-deprovision signal
post_deprovision.send(sender=self.__class__, branch=self)

logger.info('Deprovisioning completed')

Expand Down
10 changes: 5 additions & 5 deletions netbox_branching/signal_receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ def handle_branch_event(event_type, branch, user=None, **kwargs):
)


branch_provisioned.connect(partial(handle_branch_event, event_type=BRANCH_PROVISIONED))
branch_synced.connect(partial(handle_branch_event, event_type=BRANCH_SYNCED))
branch_merged.connect(partial(handle_branch_event, event_type=BRANCH_MERGED))
branch_reverted.connect(partial(handle_branch_event, event_type=BRANCH_REVERTED))
branch_deprovisioned.connect(partial(handle_branch_event, event_type=BRANCH_DEPROVISIONED))
post_provision.connect(partial(handle_branch_event, event_type=BRANCH_PROVISIONED))
post_deprovision.connect(partial(handle_branch_event, event_type=BRANCH_DEPROVISIONED))
post_sync.connect(partial(handle_branch_event, event_type=BRANCH_SYNCED))
post_merge.connect(partial(handle_branch_event, event_type=BRANCH_MERGED))
post_revert.connect(partial(handle_branch_event, event_type=BRANCH_REVERTED))


@receiver(pre_delete, sender=Branch)
Expand Down
42 changes: 22 additions & 20 deletions netbox_branching/signals.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
from django.dispatch import Signal

from .events import *

__all__ = (
'branch_deprovisioned',
'branch_merged',
'branch_provisioned',
'branch_reverted',
'branch_synced',
'post_deprovision',
'post_merge',
'post_provision',
'post_revert',
'post_sync',
'pre_deprovision',
'pre_merge',
'pre_provision',
'pre_revert',
'pre_sync',
)

# Pre-event signals
pre_provision = Signal()
pre_deprovision = Signal()
pre_sync = Signal()
pre_merge = Signal()
pre_revert = Signal()

branch_provisioned = Signal()
branch_deprovisioned = Signal()
branch_synced = Signal()
branch_merged = Signal()
branch_reverted = Signal()

branch_signals = {
branch_provisioned: BRANCH_PROVISIONED,
branch_deprovisioned: BRANCH_DEPROVISIONED,
branch_synced: BRANCH_SYNCED,
branch_merged: BRANCH_MERGED,
branch_reverted: BRANCH_REVERTED,
}
# Post-event signals
post_provision = Signal()
post_deprovision = Signal()
post_sync = Signal()
post_merge = Signal()
post_revert = Signal()

0 comments on commit 63d8f2c

Please sign in to comment.