feat: Implement configuration and job duplication functionality

- Added duplication feature for configurations and jobs, allowing users to create copies of existing entries.
- Introduced new buttons in the UI for duplicating configurations and jobs, enhancing user experience.
- Implemented backend logic to handle duplication requests, ensuring proper ownership checks and data integrity.
- Added notifications for successful duplication actions to inform users of the outcome.
- Updated relevant templates and handlers to support the new duplication functionality.
This commit is contained in:
StarFleetCPTN
2025-03-22 22:19:05 -07:00
parent 49a586db53
commit db6259f8a3
20 changed files with 2433 additions and 347 deletions
+313 -139
View File
@@ -8,7 +8,7 @@ import (
type NotificationService struct {
ID uint
Name string
Type string // "email", "slack", "webhook", etc.
Type string // "email", "webhook", etc.
IsEnabled bool
Config map[string]string
Description string
@@ -113,134 +113,275 @@ templ Settings(ctx context.Context, data SettingsData) {
</button>
</div>
} else {
<div class="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
for _, service := range data.NotificationServices {
<div class="p-4 bg-white border border-gray-200 rounded-lg shadow-sm dark:bg-gray-800 dark:border-gray-700">
<div class="flex items-center justify-between mb-3">
<div class="flex items-center">
if service.Type == "email" {
<div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center text-blue-600 dark:bg-blue-900 dark:text-blue-400">
<i class="fas fa-envelope"></i>
<div class="bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 dark:bg-gray-800 overflow-hidden">
<ul class="divide-y divide-gray-200 dark:divide-gray-700">
for _, service := range data.NotificationServices {
<li>
<div class="block hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors">
<div class="px-4 py-4 sm:px-6">
<div class="flex items-center justify-between">
<div class="flex items-center">
if service.Type == "email" {
<div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center text-blue-600 dark:bg-blue-900 dark:text-blue-400 mr-3">
<i class="fas fa-envelope"></i>
</div>
} else if service.Type == "webhook" {
<div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center text-green-600 dark:bg-green-900 dark:text-green-400 mr-3">
<i class="fas fa-code"></i>
</div>
} else {
<div class="w-10 h-10 rounded-full bg-gray-100 flex items-center justify-center text-gray-600 dark:bg-gray-700 dark:text-gray-400 mr-3">
<i class="fas fa-bell"></i>
</div>
}
<div>
<p class="text-sm font-medium text-blue-600 dark:text-blue-400 truncate">
{ service.Name }
</p>
<p class="text-sm text-gray-500 dark:text-gray-400 mt-1">
{ service.Description }
</p>
</div>
</div>
<div class="ml-2 flex-shrink-0 flex space-x-2">
<button
type="button"
class="text-gray-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 mr-1 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
data-modal-target="edit-notification-modal"
data-modal-toggle="edit-notification-modal"
data-service-id={ fmt.Sprint(service.ID) }
>
<i class="fas fa-edit"></i>
</button>
<button
type="button"
class="text-red-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 dark:bg-gray-800 dark:text-red-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
hx-delete={ "/admin/settings/notifications/" + fmt.Sprint(service.ID) }
hx-confirm="Are you sure you want to delete this notification service? This cannot be undone."
hx-target="body"
>
<i class="fas fa-trash-alt"></i>
</button>
</div>
</div>
} else if service.Type == "slack" {
<div class="w-10 h-10 rounded-full bg-purple-100 flex items-center justify-center text-purple-600 dark:bg-purple-900 dark:text-purple-400">
<i class="fab fa-slack"></i>
<div class="mt-3 sm:flex sm:justify-between">
<div class="sm:flex flex-col md:flex-row gap-2 md:gap-6">
<div class="flex items-center">
<span class={ "px-2 py-1 text-xs font-medium rounded-full",
templ.KV("bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300", service.IsEnabled),
templ.KV("bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300", !service.IsEnabled) }>
if service.IsEnabled {
Active
} else {
Disabled
}
</span>
<span class="ml-2 px-2 py-1 text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300 rounded-full">
{ service.Type }
</span>
if len(service.EventTriggers) > 0 && service.Type == "webhook" {
<span class="ml-2 px-2 py-1 text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300 rounded-full">
{ fmt.Sprintf("%d triggers", len(service.EventTriggers)) }
</span>
}
if service.SuccessCount > 0 || service.FailureCount > 0 {
<span class="ml-2 px-2 py-1 text-xs font-medium bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300 rounded-full">
{ fmt.Sprintf("%d/%d", service.SuccessCount, service.SuccessCount + service.FailureCount) }
</span>
}
</div>
</div>
if service.Type == "webhook" {
<div class="mt-2 md:mt-0 flex items-center space-x-4">
<div class="text-xs">
<span class="text-gray-500 dark:text-gray-400">Events:</span>
<span class="ml-1 text-gray-900 dark:text-gray-300">
if len(service.EventTriggers) == 0 {
None
} else {
for i, trigger := range service.EventTriggers {
if i > 0 {
<span>, </span>
}
{ trigger }
}
}
</span>
</div>
<div class="text-xs">
<span class="text-gray-500 dark:text-gray-400">Retry:</span>
<span class="ml-1 text-gray-900 dark:text-gray-300">
if service.RetryPolicy == "" {
Default
} else {
{ service.RetryPolicy }
}
</span>
</div>
</div>
} else {
<div class="mt-2 md:mt-0 flex items-center text-sm text-gray-500 dark:text-gray-400">
<i class="far fa-clock w-4 h-4 mr-1.5 text-gray-400 dark:text-gray-500"></i>
<p>Last sent:
if service.SuccessCount > 0 {
"Recently"
} else {
"Never"
}
</p>
</div>
}
</div>
} else if service.Type == "webhook" {
<div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center text-green-600 dark:bg-green-900 dark:text-green-400">
<i class="fas fa-code"></i>
</div>
} else {
<div class="w-10 h-10 rounded-full bg-gray-100 flex items-center justify-center text-gray-600 dark:bg-gray-700 dark:text-gray-400">
<i class="fas fa-bell"></i>
</div>
}
<h3 class="ml-3 text-lg font-semibold text-gray-900 dark:text-white">{ service.Name }</h3>
</div>
<div class="flex">
<div class="inline-flex">
<button
type="button"
class="text-gray-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 mr-1 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
data-modal-target="edit-notification-modal"
data-modal-toggle="edit-notification-modal"
data-service-id={ fmt.Sprint(service.ID) }
>
<i class="fas fa-edit"></i>
</button>
<button
type="button"
class="text-red-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 dark:bg-gray-800 dark:text-red-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
hx-delete={ "/settings/notifications/" + fmt.Sprint(service.ID) }
hx-confirm="Are you sure you want to delete this notification service? This cannot be undone."
hx-target="body"
>
<i class="fas fa-trash-alt"></i>
</button>
</div>
</div>
</div>
<div class="flex items-center mb-2">
<span class={ "px-2 py-1 text-xs font-medium rounded-full",
templ.KV("bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300", service.IsEnabled),
templ.KV("bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300", !service.IsEnabled) }>
if service.IsEnabled {
Active
} else {
Disabled
}
</span>
<span class="ml-2 px-2 py-1 text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300 rounded-full">
{ service.Type }
</span>
if len(service.EventTriggers) > 0 && service.Type == "webhook" {
<span class="ml-2 px-2 py-1 text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300 rounded-full">
{ fmt.Sprintf("%d triggers", len(service.EventTriggers)) }
</span>
}
if service.SuccessCount > 0 || service.FailureCount > 0 {
<span class="ml-2 px-2 py-1 text-xs font-medium bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300 rounded-full">
{ fmt.Sprintf("%d/%d", service.SuccessCount, service.SuccessCount + service.FailureCount) }
</span>
}
</div>
<p class="text-sm text-gray-500 dark:text-gray-400 mb-2">{ service.Description }</p>
if service.Type == "webhook" {
<div class="mt-3 pt-3 border-t border-gray-200 dark:border-gray-700">
<h4 class="text-sm font-medium text-gray-900 dark:text-white mb-2">Webhook Configuration</h4>
<div class="grid grid-cols-2 gap-2">
<div class="text-xs">
<span class="text-gray-500 dark:text-gray-400">Events:</span>
<span class="ml-1 text-gray-900 dark:text-gray-300">
if len(service.EventTriggers) == 0 {
None
} else {
for i, trigger := range service.EventTriggers {
if i > 0 {
<span>, </span>
}
{ trigger }
}
}
</span>
</div>
<div class="text-xs">
<span class="text-gray-500 dark:text-gray-400">Retry:</span>
<span class="ml-1 text-gray-900 dark:text-gray-300">
if service.RetryPolicy == "" {
Default
} else {
{ service.RetryPolicy }
}
</span>
</div>
<div class="text-xs">
<span class="text-gray-500 dark:text-gray-400">Secret Key:</span>
<span class="ml-1 text-gray-900 dark:text-gray-300">
if service.SecretKey == "" {
None
} else {
<i class="fas fa-check-circle text-green-500"></i> Configured
}
</span>
</div>
<div class="text-xs">
<span class="text-gray-500 dark:text-gray-400">Custom Payload:</span>
<span class="ml-1 text-gray-900 dark:text-gray-300">
if service.PayloadTemplate == "" {
Default
} else {
<i class="fas fa-check-circle text-green-500"></i> Custom
}
</span>
</div>
</div>
</div>
}
</div>
}
</li>
}
</ul>
</div>
}
// if len(data.NotificationServices) == 0 {
// <div class="text-center py-8">
// <div class="inline-flex items-center justify-center w-16 h-16 rounded-full bg-blue-100 dark:bg-blue-900 mb-4">
// <i class="fas fa-bell text-2xl text-blue-600 dark:text-blue-400"></i>
// </div>
// <h3 class="mb-2 text-lg font-semibold text-gray-900 dark:text-white">No notification services configured</h3>
// <p class="text-gray-500 dark:text-gray-400 mb-4">Add your first notification service to start receiving alerts about jobs and system events.</p>
// <button
// type="button"
// class="px-4 py-2 text-sm font-medium text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 rounded-lg dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800"
// data-modal-target="add-notification-modal"
// data-modal-toggle="add-notification-modal"
// >
// <i class="fas fa-plus mr-2"></i>Add Notification Service
// </button>
// </div>
// } else {
// <div class="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
// for _, service := range data.NotificationServices {
// <div class="p-4 bg-white border border-gray-200 rounded-lg shadow-sm dark:bg-gray-800 dark:border-gray-700">
// <div class="flex items-center justify-between mb-3">
// <div class="flex items-center">
// if service.Type == "email" {
// <div class="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center text-blue-600 dark:bg-blue-900 dark:text-blue-400">
// <i class="fas fa-envelope"></i>
// </div>
// } else if service.Type == "webhook" {
// <div class="w-10 h-10 rounded-full bg-green-100 flex items-center justify-center text-green-600 dark:bg-green-900 dark:text-green-400">
// <i class="fas fa-code"></i>
// </div>
// } else {
// <div class="w-10 h-10 rounded-full bg-gray-100 flex items-center justify-center text-gray-600 dark:bg-gray-700 dark:text-gray-400">
// <i class="fas fa-bell"></i>
// </div>
// }
// <h3 class="ml-3 text-lg font-semibold text-gray-900 dark:text-white">{ service.Name }</h3>
// </div>
// <div class="flex">
// <div class="inline-flex">
// <button
// type="button"
// class="text-gray-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 mr-1 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
// data-modal-target="edit-notification-modal"
// data-modal-toggle="edit-notification-modal"
// data-service-id={ fmt.Sprint(service.ID) }
// >
// <i class="fas fa-edit"></i>
// </button>
// <button
// type="button"
// class="text-red-500 bg-white focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-200 rounded-lg text-sm p-2 dark:bg-gray-800 dark:text-red-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
// hx-delete={ "/admin/settings/notifications/" + fmt.Sprint(service.ID) }
// hx-confirm="Are you sure you want to delete this notification service? This cannot be undone."
// hx-target="body"
// >
// <i class="fas fa-trash-alt"></i>
// </button>
// </div>
// </div>
// </div>
// <div class="flex items-center mb-2">
// <span class={ "px-2 py-1 text-xs font-medium rounded-full",
// templ.KV("bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300", service.IsEnabled),
// templ.KV("bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300", !service.IsEnabled) }>
// if service.IsEnabled {
// Active
// } else {
// Disabled
// }
// </span>
// <span class="ml-2 px-2 py-1 text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-300 rounded-full">
// { service.Type }
// </span>
// if len(service.EventTriggers) > 0 && service.Type == "webhook" {
// <span class="ml-2 px-2 py-1 text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-300 rounded-full">
// { fmt.Sprintf("%d triggers", len(service.EventTriggers)) }
// </span>
// }
// if service.SuccessCount > 0 || service.FailureCount > 0 {
// <span class="ml-2 px-2 py-1 text-xs font-medium bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300 rounded-full">
// { fmt.Sprintf("%d/%d", service.SuccessCount, service.SuccessCount + service.FailureCount) }
// </span>
// }
// </div>
// <p class="text-sm text-gray-500 dark:text-gray-400 mb-2">{ service.Description }</p>
// if service.Type == "webhook" {
// <div class="mt-3 pt-3 border-t border-gray-200 dark:border-gray-700">
// <h4 class="text-sm font-medium text-gray-900 dark:text-white mb-2">Webhook Configuration</h4>
// <div class="grid grid-cols-2 gap-2">
// <div class="text-xs">
// <span class="text-gray-500 dark:text-gray-400">Events:</span>
// <span class="ml-1 text-gray-900 dark:text-gray-300">
// if len(service.EventTriggers) == 0 {
// None
// } else {
// for i, trigger := range service.EventTriggers {
// if i > 0 {
// <span>, </span>
// }
// { trigger }
// }
// }
// </span>
// </div>
// <div class="text-xs">
// <span class="text-gray-500 dark:text-gray-400">Retry:</span>
// <span class="ml-1 text-gray-900 dark:text-gray-300">
// if service.RetryPolicy == "" {
// Default
// } else {
// { service.RetryPolicy }
// }
// </span>
// </div>
// <div class="text-xs">
// <span class="text-gray-500 dark:text-gray-400">Secret Key:</span>
// <span class="ml-1 text-gray-900 dark:text-gray-300">
// if service.SecretKey == "" {
// None
// } else {
// <i class="fas fa-check-circle text-green-500"></i> Configured
// }
// </span>
// </div>
// <div class="text-xs">
// <span class="text-gray-500 dark:text-gray-400">Custom Payload:</span>
// <span class="ml-1 text-gray-900 dark:text-gray-300">
// if service.PayloadTemplate == "" {
// Default
// } else {
// <i class="fas fa-check-circle text-green-500"></i> Custom
// }
// </span>
// </div>
// </div>
// </div>
// }
// </div>
// }
// </div>
// }
</div>
<!-- General Tab -->
@@ -308,13 +449,12 @@ templ Settings(ctx context.Context, data SettingsData) {
</button>
</div>
<div class="p-6 space-y-6">
<form id="add-notification-form" hx-post="/settings/notifications" hx-target="body">
<form id="add-notification-form" hx-post="/admin/settings/notifications" hx-target="body">
<div class="mb-6">
<label for="notification_type" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Notification Type</label>
<select id="notification_type" name="type" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option value="">Select a type</option>
<option value="email">Email</option>
<option value="slack">Slack</option>
<option value="webhook">Webhook</option>
</select>
</div>
@@ -351,17 +491,6 @@ templ Settings(ctx context.Context, data SettingsData) {
</div>
</div>
<div id="slack_fields" class="hidden notification-fields">
<div class="mb-6">
<label for="webhook_url" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Webhook URL</label>
<input type="url" id="webhook_url" name="webhook_url" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="https://hooks.slack.com/services/..." />
</div>
<div class="mb-6">
<label for="channel" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Channel</label>
<input type="text" id="channel" name="channel" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="#general" />
</div>
</div>
<div id="webhook_fields" class="hidden notification-fields">
<div class="mb-6">
<label for="webhook_url" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Webhook URL</label>
@@ -380,8 +509,31 @@ templ Settings(ctx context.Context, data SettingsData) {
</div>
<div class="mb-6">
<label for="payload_template" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Payload Template (JSON)</label>
<textarea id="payload_template" name="payload_template" rows="5" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder='{"job_id": "{{job.id}}", "status": "{{job.status}}", "message": "{{job.message}}", "timestamp": "{{job.timestamp}}"}'></textarea>
<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">Use {`job.field`} as placeholders for job data</p>
<textarea id="payload_template" name="payload_template" rows="5" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder='{
"event": "{{job.event}}",
"job": {
"id": "{{job.id}}",
"name": "{{job.name}}",
"status": "{{job.status}}",
"message": "{{job.message}}",
"started_at": "{{job.started_at}}",
"completed_at": "{{job.completed_at}}",
"duration_seconds": {{job.duration_seconds}},
"config_id": "{{job.config_id}}",
"config_name": "{{job.config_name}}",
"transfer_bytes": {{job.transfer_bytes}},
"file_count": {{job.file_count}}
},
"instance": {
"id": "{{instance.id}}",
"name": "{{instance.name}}",
"version": "{{instance.version}}",
"environment": "{{instance.environment}}"
},
"timestamp": "{{timestamp}}",
"notification_id": "{{notification.id}}"
}'></textarea>
<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">Use {`variable`} placeholders for dynamic values. Available variables: job.*, instance.*, timestamp, notification.*</p>
</div>
<div class="mb-6">
<label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Event Triggers</label>
@@ -413,6 +565,27 @@ templ Settings(ctx context.Context, data SettingsData) {
<option value="exponential">Exponential backoff</option>
</select>
</div>
<!-- Test notification button -->
<div class="mb-6 p-4 bg-gray-50 border border-gray-200 rounded-lg dark:bg-gray-700 dark:border-gray-600">
<div class="flex items-center justify-between mb-2">
<h4 class="text-base font-medium text-gray-900 dark:text-white">Test Configuration</h4>
<button
type="button"
id="test-webhook-btn" hx-post="/admin/settings/notifications/test" hx-trigger="click" hx-target="#test-notification-result" hx-swap="outerHTML"
class="px-3 py-2 text-xs font-medium text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 rounded-lg dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800"
>
<i class="fas fa-paper-plane mr-1"></i>
Send Test Notification
</button>
</div>
<p class="text-sm text-gray-500 dark:text-gray-400">
Send a test notification to verify your configuration works correctly before saving.
</p>
<div id="test-notification-result" class="mt-3 hidden">
<!-- Result will be shown here -->
</div>
</div>
</div>
<div class="flex items-start mb-6">
@@ -433,6 +606,7 @@ templ Settings(ctx context.Context, data SettingsData) {
</div>
</div>
}
@toggleNotificationFields()
}
script toggleNotificationFields() {
@@ -454,4 +628,4 @@ script toggleNotificationFields() {
}
});
});
}
}