From 01f044f5fa3a2570095019c6d285deb19a8a9911 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 14 Nov 2024 15:45:51 -0500 Subject: [PATCH 1/8] Clean up GitHub issue templates --- .github/ISSUE_TEMPLATE/01-feature_request.yaml | 14 +++++++------- .github/ISSUE_TEMPLATE/02-bug_report.yaml | 7 +++---- .../ISSUE_TEMPLATE/03-documentation_change.yaml | 2 +- .github/ISSUE_TEMPLATE/04-housekeeping.yaml | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/01-feature_request.yaml b/.github/ISSUE_TEMPLATE/01-feature_request.yaml index 6c594b7..1bff827 100644 --- a/.github/ISSUE_TEMPLATE/01-feature_request.yaml +++ b/.github/ISSUE_TEMPLATE/01-feature_request.yaml @@ -1,6 +1,6 @@ --- name: ✨ Feature Request -description: Propose a new NetBox feature or enhancement +description: Propose a new feature or enhancement labels: ["type: feature"] body: - type: input @@ -13,21 +13,21 @@ body: attributes: label: Proposed functionality description: > - Describe in detail the new feature or behavior you are proposing. Include any specific changes - to work flows, data models, and/or the user interface. The more detail you provide here, the - greater chance your proposal has of being discussed. + Describe in detail the new feature or behavior you are proposing. Include any specific changes to work flows, + data models, and/or the user interface. The more detail you provide here, the greater chance your proposal has + of being discussed. validations: required: true - type: textarea attributes: label: Use case description: > - Explain how adding this functionality would benefit users. What need does it address? + Explain how adding this functionality would benefit users. What specific need(s) does it address? validations: required: true - type: textarea attributes: label: External dependencies description: > - List any new dependencies on external libraries or services that this new feature would - introduce. For example, does the proposal require the installation of a new Python package? + List any new dependencies on external libraries or services that this new feature would introduce. For example, + does the proposal require the installation of a new Python package? diff --git a/.github/ISSUE_TEMPLATE/02-bug_report.yaml b/.github/ISSUE_TEMPLATE/02-bug_report.yaml index a085f62..7117388 100644 --- a/.github/ISSUE_TEMPLATE/02-bug_report.yaml +++ b/.github/ISSUE_TEMPLATE/02-bug_report.yaml @@ -25,9 +25,8 @@ body: attributes: label: Steps to Reproduce description: > - Describe in detail the exact steps that someone else can take to reproduce - this bug. A numbered list of discrete steps is strongly preferred. Remember - to capture the creation of any objects which must exist to reproduce the + Describe in detail the exact steps that someone else can take to reproduce this bug. A numbered list of discrete + steps is strongly preferred. Remember to capture the creation of any objects which must exist to reproduce the behavior. placeholder: | 1. Click on "create widget" @@ -45,7 +44,7 @@ body: - type: textarea attributes: label: Observed Behavior - description: What happened instead? + description: What happened instead? Be sure to include any error messages. placeholder: A TypeError exception was raised validations: required: true diff --git a/.github/ISSUE_TEMPLATE/03-documentation_change.yaml b/.github/ISSUE_TEMPLATE/03-documentation_change.yaml index 9f7968e..f9489e5 100644 --- a/.github/ISSUE_TEMPLATE/03-documentation_change.yaml +++ b/.github/ISSUE_TEMPLATE/03-documentation_change.yaml @@ -18,6 +18,6 @@ body: - type: textarea attributes: label: Proposed Changes - description: Describe the proposed changes and why they are necessary. + description: Describe the proposed changes and explain why they are necessary. validations: required: true diff --git a/.github/ISSUE_TEMPLATE/04-housekeeping.yaml b/.github/ISSUE_TEMPLATE/04-housekeeping.yaml index cec1500..c8b3de5 100644 --- a/.github/ISSUE_TEMPLATE/04-housekeeping.yaml +++ b/.github/ISSUE_TEMPLATE/04-housekeeping.yaml @@ -1,6 +1,6 @@ --- name: 🏡 Housekeeping -description: A change pertaining to the codebase itself (developers only) +description: An internal change pertaining to the codebase itself labels: ["type: housekeeping"] body: - type: textarea From f37185103837f196973837b8d6606200cf902150 Mon Sep 17 00:00:00 2001 From: bctiemann Date: Wed, 11 Dec 2024 16:04:40 -0500 Subject: [PATCH 2/8] Fixes: #133 - Add tests for remaining config parameters (#180) * Add config tests * Add newline --- netbox_branching/tests/test_config.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 netbox_branching/tests/test_config.py diff --git a/netbox_branching/tests/test_config.py b/netbox_branching/tests/test_config.py new file mode 100644 index 0000000..b78f152 --- /dev/null +++ b/netbox_branching/tests/test_config.py @@ -0,0 +1,27 @@ +from django.test import TransactionTestCase, override_settings + +from netbox.registry import registry +from netbox_branching.models import Branch +from netbox_branching.utilities import register_models + + +class ConfigTestCase(TransactionTestCase): + serialized_rollback = True + + @override_settings(PLUGINS_CONFIG={ + 'netbox_branching': { + 'exempt_models': ['ipam.prefix'], + } + }) + def test_exempt_models(self): + register_models() + self.assertNotIn('prefix', registry['model_features']['branching']['ipam']) + + @override_settings(PLUGINS_CONFIG={ + 'netbox_branching': { + 'schema_prefix': 'dummy_', + } + }) + def test_schema_prefix(self): + branch = Branch(name='Branch 5') + self.assertEqual(branch.schema_name, f'dummy_{branch.schema_id}') From ef86578e9ae4a66424a996d9078cfa654ea6578f Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Tue, 3 Dec 2024 15:51:25 -0800 Subject: [PATCH 3/8] 178 Fix Tags display on branch list view --- netbox_branching/tables/tables.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/netbox_branching/tables/tables.py b/netbox_branching/tables/tables.py index f1df9e6..b1165b3 100644 --- a/netbox_branching/tables/tables.py +++ b/netbox_branching/tables/tables.py @@ -65,6 +65,9 @@ class BranchTable(NetBoxTable): schema_id = tables.TemplateColumn( template_code='{{ value }}' ) + tags = columns.TagColumn( + url_name='plugins:netbox_branching:branch_list' + ) class Meta(NetBoxTable.Meta): model = Branch From 68f8925ef9c7dd8b44391e7b880e7bc0a98a6ec5 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 30 Jan 2025 10:21:15 -0500 Subject: [PATCH 4/8] Closes #206: Rename URL path for branch bulk import --- netbox_branching/navigation.py | 2 +- netbox_branching/urls.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox_branching/navigation.py b/netbox_branching/navigation.py index c773814..fa48de5 100644 --- a/netbox_branching/navigation.py +++ b/netbox_branching/navigation.py @@ -11,7 +11,7 @@ link_text=_('Branches'), buttons=( PluginMenuButton('plugins:netbox_branching:branch_add', _('Add'), 'mdi mdi-plus-thick'), - PluginMenuButton('plugins:netbox_branching:branch_import', _('Import'), 'mdi mdi-upload'), + PluginMenuButton('plugins:netbox_branching:branch_bulk_import', _('Import'), 'mdi mdi-upload'), ) ), PluginMenuItem( diff --git a/netbox_branching/urls.py b/netbox_branching/urls.py index 55d2308..790c2e2 100644 --- a/netbox_branching/urls.py +++ b/netbox_branching/urls.py @@ -7,7 +7,7 @@ # Branches path('branches/', views.BranchListView.as_view(), name='branch_list'), path('branches/add/', views.BranchEditView.as_view(), name='branch_add'), - path('branches/import/', views.BranchBulkImportView.as_view(), name='branch_import'), + path('branches/import/', views.BranchBulkImportView.as_view(), name='branch_bulk_import'), path('branches/edit/', views.BranchBulkEditView.as_view(), name='branch_bulk_edit'), path('branches/delete/', views.BranchBulkDeleteView.as_view(), name='branch_bulk_delete'), path('branches//', include(get_model_urls('netbox_branching', 'branch'))), From ba7e62fb6a213b1e34a1ba5b20930f59f046755a Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Thu, 30 Jan 2025 11:21:41 -0800 Subject: [PATCH 5/8] 148 Allow NetBox Scripts to run when a Branch is selected (#184) * 148 add message to script run form in NetBox * 148 Add BranchingBackend to support running scripts * 148 Add BranchingBackend to support running scripts * 148 Add BranchingBackend to support running scripts * 148 Fix instance check * 148 change to use script context managers * 148 remove incorrect checkin * 148 add missing file * 148 cleanup * Adapt for dynamic registration of request processors in NetBox * Remove activate_branch() from middleware to avoid duplicate call * Require NetBox >= v4.1.9 * Move Script alert to a separate template extension --------- Co-authored-by: Jeremy Stretch --- netbox_branching/__init__.py | 2 +- netbox_branching/middleware.py | 46 ++-------------- netbox_branching/template_content.py | 21 ++++++- .../netbox_branching/inc/script_alert.html | 8 +++ netbox_branching/utilities.py | 55 ++++++++++++++++++- 5 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 netbox_branching/templates/netbox_branching/inc/script_alert.html diff --git a/netbox_branching/__init__.py b/netbox_branching/__init__.py index 7ba11ca..32d1c2b 100644 --- a/netbox_branching/__init__.py +++ b/netbox_branching/__init__.py @@ -11,7 +11,7 @@ class AppConfig(PluginConfig): description = 'A git-like branching implementation for NetBox' version = '0.5.2' base_url = 'branching' - min_version = '4.1' + min_version = '4.1.9' middleware = [ 'netbox_branching.middleware.BranchMiddleware' ] diff --git a/netbox_branching/middleware.py b/netbox_branching/middleware.py index a79e994..c339365 100644 --- a/netbox_branching/middleware.py +++ b/netbox_branching/middleware.py @@ -1,14 +1,8 @@ -from django.contrib import messages from django.core.exceptions import ObjectDoesNotExist from django.http import HttpResponseBadRequest -from django.urls import reverse -from utilities.api import is_api_request - -from .choices import BranchStatusChoices -from .constants import COOKIE_NAME, BRANCH_HEADER, QUERY_PARAM -from .models import Branch -from .utilities import activate_branch, is_api_request +from .constants import COOKIE_NAME, QUERY_PARAM +from .utilities import activate_branch, is_api_request, get_active_branch __all__ = ( 'BranchMiddleware', @@ -24,12 +18,11 @@ def __call__(self, request): # Set/clear the active Branch on the request try: - branch = self.get_active_branch(request) + branch = get_active_branch(request) except ObjectDoesNotExist: return HttpResponseBadRequest("Invalid branch identifier") - with activate_branch(branch): - response = self.get_response(request) + response = self.get_response(request) # Set/clear the branch cookie (for non-API requests) if not is_api_request(request): @@ -39,34 +32,3 @@ def __call__(self, request): response.delete_cookie(COOKIE_NAME) return response - - @staticmethod - def get_active_branch(request): - """ - Return the active Branch (if any). - """ - # The active Branch may be specified by HTTP header for REST & GraphQL API requests. - if is_api_request(request) and BRANCH_HEADER in request.headers: - branch = Branch.objects.get(schema_id=request.headers.get(BRANCH_HEADER)) - if not branch.ready: - return HttpResponseBadRequest(f"Branch {branch} is not ready for use (status: {branch.status})") - return branch - - # Branch activated/deactivated by URL query parameter - elif QUERY_PARAM in request.GET: - if schema_id := request.GET.get(QUERY_PARAM): - branch = Branch.objects.get(schema_id=schema_id) - if branch.ready: - messages.success(request, f"Activated branch {branch}") - return branch - else: - messages.error(request, f"Branch {branch} is not ready for use (status: {branch.status})") - return None - else: - messages.success(request, f"Deactivated branch") - request.COOKIES.pop(COOKIE_NAME, None) # Delete cookie if set - return None - - # Branch set by cookie - elif schema_id := request.COOKIES.get(COOKIE_NAME): - return Branch.objects.filter(schema_id=schema_id, status=BranchStatusChoices.READY).first() diff --git a/netbox_branching/template_content.py b/netbox_branching/template_content.py index 063a9df..b4993a0 100644 --- a/netbox_branching/template_content.py +++ b/netbox_branching/template_content.py @@ -1,6 +1,6 @@ from django.contrib.contenttypes.models import ContentType -from netbox.plugins import PluginTemplateExtension +from netbox.plugins import PluginTemplateExtension from .choices import BranchStatusChoices from .contextvars import active_branch from .models import Branch, ChangeDiff @@ -8,6 +8,8 @@ __all__ = ( 'BranchNotification', 'BranchSelector', + 'ScriptNotification', + 'ShareButton', 'template_extensions', ) @@ -34,6 +36,7 @@ class BranchNotification(PluginTemplateExtension): def alerts(self): if not (instance := self.context['object']): return '' + ct = ContentType.objects.get_for_model(instance) relevant_changes = ChangeDiff.objects.filter( object_type=ct, @@ -51,4 +54,18 @@ def alerts(self): }) -template_extensions = [BranchSelector, ShareButton, BranchNotification] +class ScriptNotification(PluginTemplateExtension): + models = ['extras.script'] + + def alerts(self): + return self.render('netbox_branching/inc/script_alert.html', extra_context={ + 'active_branch': active_branch.get(), + }) + + +template_extensions = ( + BranchSelector, + BranchNotification, + ScriptNotification, + ShareButton, +) diff --git a/netbox_branching/templates/netbox_branching/inc/script_alert.html b/netbox_branching/templates/netbox_branching/inc/script_alert.html new file mode 100644 index 0000000..002e3aa --- /dev/null +++ b/netbox_branching/templates/netbox_branching/inc/script_alert.html @@ -0,0 +1,8 @@ +{% load i18n %} +{% if active_branch %} + +{% endif %} diff --git a/netbox_branching/utilities.py b/netbox_branching/utilities.py index d7293d0..56fbc6e 100644 --- a/netbox_branching/utilities.py +++ b/netbox_branching/utilities.py @@ -1,23 +1,29 @@ import datetime import logging from collections import defaultdict -from contextlib import contextmanager +from contextlib import contextmanager, nullcontext from dataclasses import dataclass +from django.contrib import messages from django.db.models import ForeignKey, ManyToManyField +from django.http import HttpResponseBadRequest from django.urls import reverse from netbox.plugins import get_plugin_config from netbox.registry import registry -from .constants import EXEMPT_MODELS, INCLUDE_MODELS +from netbox.utils import register_request_processor +from .choices import BranchStatusChoices +from .constants import BRANCH_HEADER, COOKIE_NAME, EXEMPT_MODELS, INCLUDE_MODELS, QUERY_PARAM from .contextvars import active_branch __all__ = ( 'ChangeSummary', 'DynamicSchemaDict', 'ListHandler', + 'ActiveBranchContextManager', 'activate_branch', 'deactivate_branch', + 'get_active_branch', 'get_branchable_object_types', 'get_tables_to_replicate', 'is_api_request', @@ -209,4 +215,49 @@ def is_api_request(request): """ Returns True if the given request is a REST or GraphQL API request. """ + if not hasattr(request, 'path_info'): + return False + return request.path_info.startswith(reverse('api-root')) or request.path_info.startswith(reverse('graphql')) + + +def get_active_branch(request): + """ + Return the active Branch (if any). + """ + # The active Branch may be specified by HTTP header for REST & GraphQL API requests. + from .models import Branch + if is_api_request(request) and BRANCH_HEADER in request.headers: + branch = Branch.objects.get(schema_id=request.headers.get(BRANCH_HEADER)) + if not branch.ready: + return HttpResponseBadRequest(f"Branch {branch} is not ready for use (status: {branch.status})") + return branch + + # Branch activated/deactivated by URL query parameter + elif QUERY_PARAM in request.GET: + if schema_id := request.GET.get(QUERY_PARAM): + branch = Branch.objects.get(schema_id=schema_id) + if branch.ready: + messages.success(request, f"Activated branch {branch}") + return branch + else: + messages.error(request, f"Branch {branch} is not ready for use (status: {branch.status})") + return None + else: + messages.success(request, f"Deactivated branch") + request.COOKIES.pop(COOKIE_NAME, None) # Delete cookie if set + return None + + # Branch set by cookie + elif schema_id := request.COOKIES.get(COOKIE_NAME): + return Branch.objects.filter(schema_id=schema_id, status=BranchStatusChoices.READY).first() + + +@register_request_processor +def ActiveBranchContextManager(request): + """ + Activate a branch if indicated by the request. + """ + if branch := get_active_branch(request): + return activate_branch(branch) + return nullcontext() From 0a983919dc3f91ebc7871b2cb0360c6318532dbd Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 30 Jan 2025 13:24:54 -0500 Subject: [PATCH 6/8] Fixes #87: Allow branch to be created/delete while other branch is active --- netbox_branching/models/branches.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/netbox_branching/models/branches.py b/netbox_branching/models/branches.py index be5712d..5cc2a2a 100644 --- a/netbox_branching/models/branches.py +++ b/netbox_branching/models/branches.py @@ -159,9 +159,6 @@ def save(self, provision=True, *args, **kwargs): _provision = provision and self.pk is None - if active_branch.get(): - raise AbortRequest(_("Cannot create or modify a branch while a branch is active.")) - super().save(*args, **kwargs) if _provision: @@ -173,8 +170,8 @@ def save(self, provision=True, *args, **kwargs): ) def delete(self, *args, **kwargs): - if active_branch.get(): - raise AbortRequest(_("Cannot delete a branch while a branch is active.")) + if active_branch.get() == self: + raise AbortRequest(_("The active branch cannot be deleted.")) # Deprovision the schema self.deprovision() From f5d40a38da23159c63a23af570f600cd61c10ebb Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 4 Feb 2025 10:33:01 -0500 Subject: [PATCH 7/8] Closes #209: Prevent stale branches from being synced --- docs/using-branches/syncing-merging.md | 3 +++ netbox_branching/models/branches.py | 16 +++++++++++++++- netbox_branching/tables/tables.py | 16 ++++++++++++---- .../templates/netbox_branching/branch.html | 17 +++++++++++++++-- .../netbox_branching/buttons/branch_sync.html | 2 +- netbox_branching/tests/test_branches.py | 17 +++++++++++++++++ 6 files changed, 63 insertions(+), 8 deletions(-) diff --git a/docs/using-branches/syncing-merging.md b/docs/using-branches/syncing-merging.md index af3c8f9..3c7d0cd 100644 --- a/docs/using-branches/syncing-merging.md +++ b/docs/using-branches/syncing-merging.md @@ -6,6 +6,9 @@ Synchronizing a branch replicates all recent changes from main into the branch. To synchronize a branch, click the "Sync" button. (If this button is not visible, verify that the branch status shows "ready" and that you have permission to synchronize the branch.) +!!! warning + A branch must be synchronized frequently enough to avoid exceeding NetBox's configured [changelog retention period](https://netboxlabs.com/docs/netbox/en/stable/configuration/miscellaneous/#changelog_retention) (which defaults to 90 days). This is to protect against data loss when replicating changes from main. A branch whose `last_sync` time exceeds the configured retention window can no longer be synced. + While a branch is being synchronized, its status will show "synchronizing." !!! tip diff --git a/netbox_branching/models/branches.py b/netbox_branching/models/branches.py index 5cc2a2a..6ace090 100644 --- a/netbox_branching/models/branches.py +++ b/netbox_branching/models/branches.py @@ -1,6 +1,7 @@ import logging import random import string +from datetime import timedelta from functools import cached_property, partial from django.conf import settings @@ -15,6 +16,7 @@ from django.utils.translation import gettext_lazy as _ from core.models import ObjectChange as ObjectChange_ +from netbox.config import get_config from netbox.context import current_request from netbox.context_managers import event_tracking from netbox.models import PrimaryModel @@ -121,7 +123,7 @@ def schema_name(self): def connection_name(self): return f'schema_{self.schema_name}' - @cached_property + @property def synced_time(self): return self.last_sync or self.created @@ -240,6 +242,16 @@ def get_event_history(self): last_time = event.time return history + @property + def is_stale(self): + """ + Indicates whether the branch is too far out of date to be synced. + """ + if not (changelog_retention := get_config().CHANGELOG_RETENTION): + # Changelog retention is disabled + return False + return self.synced_time < timezone.now() - timedelta(days=changelog_retention) + def sync(self, user, commit=True): """ Apply changes from the main schema onto the Branch's schema. @@ -249,6 +261,8 @@ def sync(self, user, commit=True): if not self.ready: raise Exception(f"Branch {self} is not ready to sync") + if self.is_stale: + raise Exception(f"Branch {self} is stale and can no longer be synced") # Emit pre-sync signal pre_sync.send(sender=self.__class__, branch=self, user=user) diff --git a/netbox_branching/tables/tables.py b/netbox_branching/tables/tables.py index b1165b3..8d90a59 100644 --- a/netbox_branching/tables/tables.py +++ b/netbox_branching/tables/tables.py @@ -56,8 +56,16 @@ class BranchTable(NetBoxTable): verbose_name=_('Name'), linkify=True ) + is_active = columns.BooleanColumn( + verbose_name=_('Active') + ) status = columns.ChoiceFieldColumn( - verbose_name=_('Status'), + verbose_name=_('Status') + ) + is_stale = columns.BooleanColumn( + true_mark=mark_safe(''), + false_mark=None, + verbose_name=_('Stale') ) conflicts = ConflictsColumn( verbose_name=_('Conflicts') @@ -72,11 +80,11 @@ class BranchTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = Branch fields = ( - 'pk', 'id', 'name', 'is_active', 'status', 'conflicts', 'schema_id', 'description', 'owner', 'tags', - 'created', 'last_updated', + 'pk', 'id', 'name', 'is_active', 'status', 'is_stale', 'conflicts', 'schema_id', 'description', 'owner', + 'tags', 'created', 'last_updated', ) default_columns = ( - 'pk', 'name', 'is_active', 'status', 'owner', 'conflicts', 'schema_id', 'description', + 'pk', 'name', 'is_active', 'status', 'is_stale', 'owner', 'conflicts', 'schema_id', 'description', ) def render_is_active(self, value): diff --git a/netbox_branching/templates/netbox_branching/branch.html b/netbox_branching/templates/netbox_branching/branch.html index 75eff0c..08705d8 100644 --- a/netbox_branching/templates/netbox_branching/branch.html +++ b/netbox_branching/templates/netbox_branching/branch.html @@ -82,11 +82,24 @@
{% trans "Branch" %}
{% trans "Last synced" %} - {{ object.synced_time|isodatetime }} + + {{ object.synced_time|isodatetime }} + {% if object.is_stale %} + + + + {% endif %} +
{{ object.synced_time|timesince }} {% trans "ago" %}
+ {% trans "Last activity" %} - {{ latest_change.time|isodatetime|placeholder }} + + {{ latest_change.time|isodatetime|placeholder }} + {% if latest_change %} +
{{ latest_change.time|timesince }} {% trans "ago" %}
+ {% endif %} + {% trans "Conflicts" %} diff --git a/netbox_branching/templates/netbox_branching/buttons/branch_sync.html b/netbox_branching/templates/netbox_branching/buttons/branch_sync.html index fcf21bb..dc0e422 100644 --- a/netbox_branching/templates/netbox_branching/buttons/branch_sync.html +++ b/netbox_branching/templates/netbox_branching/buttons/branch_sync.html @@ -1,5 +1,5 @@ {% load i18n %} -{% if perms.netbox_branching.sync_branch %} +{% if perms.netbox_branching.sync_branch and not branch.is_stale %} {% trans "Sync" %} diff --git a/netbox_branching/tests/test_branches.py b/netbox_branching/tests/test_branches.py index 3bb263b..66a9a6b 100644 --- a/netbox_branching/tests/test_branches.py +++ b/netbox_branching/tests/test_branches.py @@ -1,8 +1,10 @@ import re +from datetime import timedelta from django.core.exceptions import ValidationError from django.db import connection from django.test import TransactionTestCase, override_settings +from django.utils import timezone from netbox_branching.choices import BranchStatusChoices from netbox_branching.constants import MAIN_SCHEMA @@ -125,3 +127,18 @@ def test_max_branches(self): branch = Branch(name='Branch 4') with self.assertRaises(ValidationError): branch.full_clean() + + @override_settings(CHANGELOG_RETENTION=10) + def test_is_stale(self): + branch = Branch(name='Branch 1') + branch.save(provision=False) + + # Set creation time to 9 days in the past + branch.last_sync = timezone.now() - timedelta(days=9) + branch.save() + self.assertFalse(branch.is_stale) + + # Set creation time to 11 days in the past + branch.last_sync = timezone.now() - timedelta(days=11) + branch.save() + self.assertTrue(branch.is_stale) From 940b1a2a16eafeb858b62288baaf37d6db0e400b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 4 Feb 2025 13:01:26 -0500 Subject: [PATCH 8/8] Release v0.5.3 --- docs/changelog.md | 14 ++++++++++++++ netbox_branching/__init__.py | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 6594aeb..9652c79 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,19 @@ # Change Log +## v0.5.3 + +### Enhancements + +* [#209](https://github.com/netboxlabs/netbox-branching/issues/209) - Prevent merging branches whose `last_sync` time exceeds the configured changelog retention window + +### Bug Fixes + +* [#87](https://github.com/netboxlabs/netbox-branching/issues/87) - Deactivate the active branch (if any) when creating a new branch +* [#148](https://github.com/netboxlabs/netbox-branching/issues/148) - Fix `IntegrityError` exception raised when executing custom scripts within a branch +* [#178](https://github.com/netboxlabs/netbox-branching/issues/178) - Fix display of assigned tags in the branches list + +--- + ## v0.5.2 ### Bug Fixes diff --git a/netbox_branching/__init__.py b/netbox_branching/__init__.py index 32d1c2b..26c07ba 100644 --- a/netbox_branching/__init__.py +++ b/netbox_branching/__init__.py @@ -9,7 +9,7 @@ class AppConfig(PluginConfig): name = 'netbox_branching' verbose_name = 'NetBox Branching' description = 'A git-like branching implementation for NetBox' - version = '0.5.2' + version = '0.5.3' base_url = 'branching' min_version = '4.1.9' middleware = [