Skip to content

18136 Allow Script running within a Branch #18137

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/configuration/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,11 @@ The time zone NetBox will use when dealing with dates and times. It is recommend
Default: True

Enables language translation for the user interface. (This parameter maps to Django's [USE_I18N](https://docs.djangoproject.com/en/stable/ref/settings/#std-setting-USE_I18N) setting.)

---

## BRANCHING_BACKEND

Default: None

The dotted path to the desired branching class. If using [netboxlabs-netbox-branching](https://github.com/netboxlabs/netbox-branching) set this to `netbox_branching.backends.BranchingBackend`
21 changes: 19 additions & 2 deletions netbox/extras/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import traceback
from contextlib import nullcontext

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import transaction
from django.utils.module_loading import import_string
from django.utils.translation import gettext as _

from core.signals import clear_events
Expand Down Expand Up @@ -100,5 +103,19 @@ def run(self, data, request=None, commit=True, **kwargs):

# Execute the script. If commit is True, wrap it with the event_tracking context manager to ensure we process
# change logging, event rules, etc.
with event_tracking(request) if commit else nullcontext():
self.run_script(script, request, data, commit)
branch = None
if settings.BRANCHING_BACKEND:
try:
branching_cls = import_string(settings.BRANCHING_BACKEND)
except AttributeError:
logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
message = _("Failed to import configured BRANCHING_BACKEND: ") + settings.BRANCHING_BACKEND
logger.error(message)
raise ImproperlyConfigured(message)

branching_backend = branching_cls()
branch = branching_backend.get_active_branch(request)

with branching_backend.activate_branch(branch) if branch else nullcontext():
with event_tracking(request) if commit else nullcontext():
self.run_script(script, request, data, commit)
1 change: 1 addition & 0 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {})
TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
TRANSLATION_ENABLED = getattr(configuration, 'TRANSLATION_ENABLED', True)
BRANCHING_BACKEND = getattr(configuration, 'BRANCHING_BACKEND', None)

# Load any dynamic configuration parameters which have been hard-coded in the configuration file
for param in CONFIG_PARAMS:
Expand Down
Loading