Skip to content

Commit 76ac298

Browse files
committed
Added git sync action to API
1 parent 16cb83d commit 76ac298

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

netbox_script_manager/api/views.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from rest_framework.exceptions import PermissionDenied
1414
from rest_framework.response import Response
1515
from rest_framework.routers import APIRootView
16-
from rest_framework.views import APIView
1716
from utilities.permissions import get_permission_for_model
1817
from utilities.utils import copy_safe_request
1918

@@ -74,6 +73,24 @@ def load(self, request):
7473

7574
return Response(ScriptInstanceSerializer(loaded_scripts, many=True, context={"request": request}).data)
7675

76+
@action(detail=False, methods=["post"])
77+
def sync(self, request):
78+
permission = get_permission_for_model(self.queryset.model, "sync")
79+
80+
if not request.user.has_perm(permission):
81+
raise PermissionDenied(f"Missing permission: {permission}")
82+
83+
try:
84+
result = util.pull_scripts()
85+
except Exception as e:
86+
return Response({"error": f"Failed to pull git repository: {e}"}, status=http_status.HTTP_500_INTERNAL_SERVER_ERROR)
87+
88+
messages = [f"Pulled git repository"]
89+
if result:
90+
messages.append(result)
91+
92+
return Response({"messages": messages}, status=http_status.HTTP_200_OK)
93+
7794
@action(detail=True, methods=["post"])
7895
def run(self, request, pk):
7996
# TODO: Add schema definitions.

0 commit comments

Comments
 (0)