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 1972350 commit df6e1a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
58 changes: 42 additions & 16 deletions ui/admin/app/components/webhooks/WebhookForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect, useState } from "react";
import useSWR from "swr";

import { WorkflowService } from "~/lib/service/api/workflowService";
Expand All @@ -9,7 +8,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 +39,25 @@ export function WebhookFormContent() {
const { form, handleSubmit, isLoading, isEdit, hasToken, hasSecret } =
useWebhookFormContext();

const { setValue, watch } = form;

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

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 +119,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>No 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 df6e1a3

Please sign in to comment.