Skip to content

Commit

Permalink
feat: add ability to remove secret from webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhopperlowe committed Nov 21, 2024
1 parent 6ffb427 commit 3ecf654
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
63 changes: 47 additions & 16 deletions ui/admin/app/components/webhooks/WebhookForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect } from "react";
import useSWR from "swr";

import { WorkflowService } from "~/lib/service/api/workflowService";
Expand All @@ -9,7 +9,8 @@ import {
ControlledInput,
} from "~/components/form/controlledInputs";
import { Button } from "~/components/ui/button";
import { FormItem, FormLabel } from "~/components/ui/form";
import { Checkbox } from "~/components/ui/checkbox";
import { FormControl, FormItem, FormLabel } from "~/components/ui/form";
import { MultiSelect } from "~/components/ui/multi-select";
import { ScrollArea } from "~/components/ui/scroll-area";
import {
Expand Down Expand Up @@ -39,19 +40,29 @@ export function WebhookFormContent() {
const { form, handleSubmit, isLoading, isEdit, hasToken, hasSecret } =
useWebhookFormContext();

const { setValue, watch } = form;

const getWorkflows = useSWR(WorkflowService.getWorkflows.key(), () =>
WorkflowService.getWorkflows()
);

useEffect(() => {
return watch(console.log).unsubscribe;
}, []);

Check warning on line 51 in ui/admin/app/components/webhooks/WebhookForm.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'watch'. Either include it or remove the dependency array

const workflows = getWorkflows.data;

// note(ryanhopperlowe): this will change depending on webhook type
const validationHeader = form.watch("validationHeader");
useEffect(() => {
if (!validationHeader) {
form.setValue("validationHeader", "X-Hub-Signature-256");
}
}, [form, validationHeader]);
const validationHeader = watch("validationHeader");
const secret = watch("secret");

const removeSecret = () => {
setValue("secret", "");
setValue("validationHeader", "");
};

const addSecret = () => setValue("validationHeader", "X-Hub-Signature-256");

const secretIsRemoved = hasSecret && !validationHeader && !secret;

return (
<ScrollArea className="h-full">
Expand Down Expand Up @@ -113,13 +124,33 @@ export function WebhookFormContent() {
)}
</ControlledCustomInput>

<ControlledInput
control={form.control}
name="secret"
label="Secret"
description="This secret should match the secret you provide to GitHub."
placeholder={hasSecret ? "(unchanged)" : ""}
/>
<div className="space-y-2">
<ControlledInput
control={form.control}
name="secret"
label="Secret"
description="This secret should match the secret you provide to GitHub."
placeholder={hasSecret ? "(unchanged)" : ""}
disabled={secretIsRemoved}
onChange={(e) => {
if (!hasSecret && e.target.value) addSecret();
}}
/>

<FormItem className="flex items-center gap-2 space-y-0">
<FormControl>
<Checkbox
checked={secretIsRemoved}
onCheckedChange={(val) => {
if (val) removeSecret();
else addSecret();
}}
/>
</FormControl>

<FormLabel>Delete Secret</FormLabel>
</FormItem>
</div>

<TypographyH4>Advanced</TypographyH4>

Expand Down
3 changes: 2 additions & 1 deletion ui/admin/app/components/webhooks/WebhookFormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ export function WebhookFormContextProvider({
alias: webhook?.alias ?? "",
workflow: webhook?.workflow ?? "",
headers: webhook?.headers ?? [],
secret: "",
validationHeader: webhook?.validationHeader ?? "",
secret: "",
token: "",
type: "GitHub",
}),
[webhook]
);
Expand Down
6 changes: 2 additions & 4 deletions ui/admin/app/lib/model/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ export const WebhookSchema = z.object({
workflow: z.string().min(1, "Workflow is required").default(""),
headers: z.array(z.string()).default([]),
secret: z.string().default(""),
validationHeader: z
.string()
.min(1, "Validation header is required")
.default(""),
validationHeader: z.string().default(""),
token: z.string().default(""),
type: z.enum(["GitHub"]),
});
export type WebhookFormType = z.infer<typeof WebhookSchema>;

0 comments on commit 3ecf654

Please sign in to comment.