remove project in real time with webhooks

This commit is contained in:
Chris Zhu
2025-02-03 20:49:16 -08:00
parent 3101c371f0
commit ab0d7ebc96
6 changed files with 14 additions and 9 deletions

View File

@@ -1,10 +1,10 @@
class Projects::DestroyJob < ApplicationJob
def perform(project)
project.destroying!
kubeconfig = project.cluster.kubeconfig
kubectl = K8::Kubectl.new(kubeconfig)
kubectl.call("delete namespace #{project.name}")
client = K8::Client.from_cluster(project.cluster)
if (namespace = client.get_namespaces.find { |n| n.metadata.name == project.name }).present?
client.delete_namespace(namespace.metadata.name)
end
# Delete the github webhook for the project IF there are no more projects that refer to that repository
unless Project.where(repository_url: project.repository_url).where.not(id: project.id).exists?
@@ -15,7 +15,7 @@ class Projects::DestroyJob < ApplicationJob
def remove_github_webhook(project)
client = Github::Client.new(project)
client.remove_hook!
client.remove_webhook!
rescue Octokit::NotFound
# If the hook is not found, do nothing
end

View File

@@ -53,6 +53,10 @@ class Project < ApplicationRecord
broadcast_replace_to [ self, :status ], target: dom_id(self, :status), partial: "projects/status", locals: { project: self }
end
after_destroy_commit do
broadcast_remove_to [ :projects, self.account ], target: dom_id(self, :index)
end
enum :status, {
creating: 0,
deployed: 1,

View File

@@ -39,7 +39,7 @@ class Service < ApplicationRecord
unhealthy: 2,
updated: 3
}
scope :running, -> { where(status: [:healthy, :unhealthy, :updated]) }
scope :running, -> { where(status: [ :healthy, :unhealthy, :updated ]) }
has_one :cron_schedule, dependent: :destroy
validates :cron_schedule, presence: true, if: :cron_job?

View File

@@ -33,7 +33,7 @@ class Github::Client
def remove_webhook!
if webhook_exists?
client.remove_hook(project.repository_url, hook.id)
client.remove_hook(project.repository_url, webhook.id)
end
end

View File

@@ -1,3 +1,5 @@
<%= turbo_stream_from [:projects, current_account] %>
<div class="overflow-auto">
<table class="table mt-2 rounded-box" id="shop_table" data-component="table">
<thead>
@@ -21,7 +23,7 @@
</thead>
<tbody>
<% projects.order(created_at: :desc).each do |project| %>
<tr class="cursor-pointer hover:bg-base-200/40">
<tr class="cursor-pointer hover:bg-base-200/40" id="<%= dom_id(project, :index) %>">
<td>
<div class="font-medium"><%= link_to project.name, project_deployments_path(project), class: "text-inherit hover:underline" %></div>
</td>

View File

@@ -1,5 +1,4 @@
<%= content_for :title, "Projects" %>
<%= turbo_stream_from :projects %>
<div>
<div class="flex items-center justify-between">