Skip to content

Commit

Permalink
changelog extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ITJamie committed Nov 4, 2024
1 parent a53208f commit a0f89a8
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 34 deletions.
8 changes: 3 additions & 5 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
FROM mcr.microsoft.com/devcontainers/python:3



# Run the commands in the Dockerfile
RUN python -m pip install --upgrade pip \
&& python -m pip install 'flit>=3.8.0'

ENV FLIT_ROOT_INSTALL=1

COPY pyproject.toml .
RUN touch README.md \
&& mkdir -p src/python_package \
&& python -m flit install --only-deps --deps develop \
&& rm -r pyproject.toml README.md src
16 changes: 12 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"ms-python.flake8",
"ms-python.black-formatter",
"ms-vsliveshare.vsliveshare",
"ryanluker.vscode-coverage-gutters",
"bungcip.better-toml",
"GitHub.copilot"
"ryanluker.vscode-coverage-gutters"
// "bungcip.better-toml"
// "GitHub.copilot"
],
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
Expand All @@ -40,5 +40,13 @@
}
}
},
"onCreateCommand": "pre-commit install-hooks"
"features": {
"ghcr.io/itsmechlark/features/postgresql:1": {
"version": "16"
},
"ghcr.io/itsmechlark/features/redis-server:1": {
"version": "latest"
}
}
}

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,5 @@ dmypy.json

# Cython debug symbols
cython_debug/

netbox-src
6 changes: 5 additions & 1 deletion netbox_changelog_diff_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = """Jamie Murphy"""
__email__ = "[email protected]"
__version__ = "0.2.1"
__version__ = "0.3.0"


from netbox.plugins import PluginConfig
Expand All @@ -18,6 +18,10 @@ class ChangeLogDiffConfig(PluginConfig):
"change_log_format": "yaml",
"hide_native_diff": False,
}
def ready(self):
super().ready()
from netbox_changelog_diff_plugin.tables import register_changelog
register_changelog()


config = ChangeLogDiffConfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.core.management.base import BaseCommand
from extras.models import ObjectChange
from .models import ChangeLogSummary
from core.models import ObjectChange
from netbox_changelog_diff_plugin.models import ChangeLogSummary

class Command(BaseCommand):
help = 'Creates ChangeLogSummary objects for ObjectChanges that do not have one'
Expand All @@ -10,38 +10,47 @@ def handle(self, *args, **options):
object_changes = ObjectChange.objects.filter(
action='update'
).exclude(
id__in=ChangeLogSummary.objects.values_list('changelog_id', flat=True)
id__in=ChangeLogSummary.objects.values_list('changelog', flat=True)
)
# for change in object_changes:
# print(f"Creating ChangeLogSummary for {change}")
# self.stdout.write(
# self.style.SUCCESS(f'Successfully created {change.id} change')
# )

count = 0
for change in object_changes:
summary = []

if change.prechange_data and change.postchange_data:
pre_keys = set(change.prechange_data.keys())
post_keys = set(change.postchange_data.keys())

# Find added keys
added_keys = post_keys - pre_keys
for key in added_keys:
summary.append(f"Added {key}")

# Find removed keys
removed_keys = pre_keys - post_keys
for key in removed_keys:
summary.append(f"Removed {key}")

# Check for updated values in common keys
common_keys = pre_keys & post_keys
for key in common_keys:
if change.prechange_data[key] != change.postchange_data[key]:
summary.append(f"Updated {key}")

ChangeLogSummary.objects.create(
summary_msg = ", ".join(summary) if summary else "No changes detected"
print(f"summary: `{summary_msg}` for change id: {change.id}")
obj, created = ChangeLogSummary.objects.get_or_create(
changelog=change,
summary=", ".join(summary) if summary else "No changes detected"
defaults={
"summary": summary_msg
}
)
count += 1
# count += 1

self.stdout.write(
self.style.SUCCESS(f'Successfully created {count} new changelog summaries')
)
# self.stdout.write(
# self.style.SUCCESS(f'Successfully created {count} new changelog summaries')
# )
40 changes: 40 additions & 0 deletions netbox_changelog_diff_plugin/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 5.0.9 on 2024-11-04 22:09

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
("core", "0012_job_object_type_optional"),
]

operations = [
migrations.CreateModel(
name="ChangeLogSummary",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False
),
),
("summary", models.TextField()),
(
"changelog",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="human_summary",
to="core.objectchange",
),
),
],
options={
"verbose_name": "Changelog Summary",
"verbose_name_plural": "Changelog Summaries",
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.0.9 on 2024-11-04 23:19

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0012_job_object_type_optional"),
("netbox_changelog_diff_plugin", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="changelogsummary",
name="changelog",
field=models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="human_summary",
to="core.objectchange",
),
),
]
19 changes: 11 additions & 8 deletions netbox_changelog_diff_plugin/models.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
from django.db import models
from django.urls import reverse
from netbox.models import NetBoxModel
# from netbox.core.models import ObjectChange

class ChangeLogSummary(NetBoxModel):

class ChangeLogSummary(models.Model):
"""Model to store human-readable summaries of changelogs"""
changelog = models.ForeignKey(
to='extras.ObjectChange',

changelog = models.OneToOneField(
to='core.ObjectChange',
on_delete=models.CASCADE,
related_name='summaries'
related_name='human_summary',
unique=True
)

summary = models.TextField(
help_text="Human readable summary of the changes made"
)

class Meta:
verbose_name = "Changelog Summary"
verbose_name_plural = "Changelog Summaries"
ordering = ['-changelog__time']
# ordering = ['-id']

def __str__(self):
return f"Summary for change {self.changelog.id}"
return f"{self.summary}"

def get_absolute_url(self):
return reverse('plugins:netbox_changelog_diff_plugin:changelogsummary', args=[self.pk])
17 changes: 15 additions & 2 deletions netbox_changelog_diff_plugin/tables.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
import django_tables2 as tables
from netbox.tables import NetBoxTable, ChoiceFieldColumn
import django_tables2
from django.utils.translation import gettext_lazy as _

from .models import ChangeLogSummary
from core.tables.change_logging import ObjectChangeTable
from utilities.tables import register_table_column

mycol_2 = django_tables2.Column(
verbose_name=_('Change Summary'),
accessor=django_tables2.A('human_summary'),
default="- -"
)

def register_changelog():
register_table_column(mycol_2, 'human_summary', ObjectChangeTable)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "netbox-changelog-diff-plugin"
version = "0.2.1"
version = "0.3.0"
authors = [
{name = "Jamie Murphy", email = "[email protected]"},
]
Expand Down

0 comments on commit a0f89a8

Please sign in to comment.