Skip to content

Commit

Permalink
delete webhook on service deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
adb-sh committed Nov 10, 2024
1 parent b6baa86 commit 7604223
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/components/cloud/CreateServiceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const createGithubWebhook = async (namespace: string, service) => {
const ok = new Octokit({
auth: account.access_token,
});
ok.repos.createWebhook({
return await ok.repos.createWebhook({
owner: service.ghPackageOwner,
repo: service.ghPackageRepo,
config: {
Expand Down Expand Up @@ -101,7 +101,11 @@ const createServiceFromForm = async (form: FormData) => {
try {
service.webhookSecret = crypto.randomBytes(16).toString("hex");
await ensureGithubPullSecret(user.name);
await createGithubWebhook(user.name, service);
const { data: hook } = await createGithubWebhook(user.name, service);
service.annotations = {
"apps.deploycat.io/gh-webhook-id": hook.id.toString(),
"apps.deploycat.io/gh-repo": service.ghPackageRepo,
};
} catch (e) {
console.error(e);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/knative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class Knative {
annotations: {
"apps.deploycat.io/webhook-secret": service.webhookSecret,
"apps.deploycat.io/source": source,
...(service.annotations ?? {}),
},
},
spec: {
Expand Down
23 changes: 23 additions & 0 deletions src/routes/cloud/apps/[app].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { rangeQuery } from "~/lib/prometheus";
import { CheckBadgeIcon } from "@deploy-cat/heroicons-solid/24/solid/esm";
import { StatusBadge } from "~/components/cloud/service/StatusBadge";
import type { Service as KnativeService } from "~/lib/knative";
import { getAccount } from "~/lib/auth";
import { Octokit } from "@octokit/rest";

const getService = cache(async (app: string) => {
"use server";
Expand All @@ -35,6 +37,27 @@ const deleteServiceFromForm = async (form: FormData) => {
name: form.get("name") as string,
};
const user = await getUser();
const currentService = await knative.getService(service.name, user.name);
if (
currentService.raw.metadata?.annotations["apps.deploycat.io/source"] ===
"ghcr"
) {
const account = await getAccount();
const ok = new Octokit({
auth: account.access_token,
});
await ok.repos.deleteWebhook({
owner: user.name,
repo: currentService.raw.metadata.annotations[
"apps.deploycat.io/gh-repo"
],
hook_id: Number(
currentService.raw.metadata.annotations[
"apps.deploycat.io/gh-webhook-id"
]
),
});
}
await knative.deleteService(service.name, user.name);
throw redirect("/cloud/apps");
};
Expand Down

0 comments on commit 7604223

Please sign in to comment.