Skip to content

Commit

Permalink
feat: playbook run modal - populate parameters & resource from the URL
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
adityathebe committed Jan 7, 2025
1 parent ea90fef commit f726af2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/api/types/playbooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type PlaybookRunAction = {
stdout?: string;
logs?: string;
markdown?: string;
recommendedPlaybooks: string;
slack?: string;
stderr?: string;
[key: string]: unknown;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,22 @@ export default function PlaybooksRunActionsResults({
return (
<div className="relative flex h-full w-full flex-col">
{action.error && <pre className={className}>{action.error}</pre>}
{result?.stderr || result?.stdout || result?.logs || result?.markdown ? (
{result?.stderr ||
result?.stdout ||
result?.logs ||
result?.slack ||
result?.recommendedPlaybooks ||
result?.markdown ? (
<div className={`flex flex-col gap-2 ${className}`}>
<DisplayStdout className={className} stdout={result?.stdout} />
<DisplayStderr className={className} stderr={result?.stderr} />
<DisplayLogs className={className} logs={result?.logs} />
<DisplayMarkdown className={className} md={result?.markdown} />
<DisplayLogs
className={className}
logs={result?.recommendedPlaybooks}
/>
<DisplayLogs className={className} logs={result?.slack} />
</div>
) : (
<pre className={className}>
Expand Down
40 changes: 35 additions & 5 deletions src/components/Playbooks/Runs/Filter/PlaybookRunsFilterBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { tables } from "@flanksource-ui/context/UserAccessContext/permissions";
import { Button } from "@flanksource-ui/ui/Buttons/Button";
import { TimeRangePicker } from "@flanksource-ui/ui/Dates/TimeRangePicker";
import useTimeRangeParams from "@flanksource-ui/ui/Dates/TimeRangePicker/useTimeRangeParams";
import { useState } from "react";
import { URLSearchParamsInit } from "react-router-dom";
import { useEffect, useState } from "react";
import { URLSearchParamsInit, useSearchParams } from "react-router-dom";
import SubmitPlaybookRunForm from "../Submit/SubmitPlaybookRunForm";
import PlaybookSpecsDropdown from "./PlaybookSpecsDropdown";
import PlaybookStatusDropdown from "./PlaybookStatusDropdown";
Expand Down Expand Up @@ -36,9 +36,36 @@ export default function PlaybookRunsFilterBar({
);
const [isSubmitPlaybookRunFormOpen, setIsSubmitPlaybookRunFormOpen] =
useState(false);

const [searchParams, setSearchParams] = useSearchParams();
const range = getTimeRangeFromUrl();

const configID = searchParams.get("config_id") ?? undefined;
let playbookParameters: Record<string, string> = {};
searchParams.forEach((val, key) => {
if (key.startsWith("params.")) {
const k = key.replace("params.", "");
playbookParameters[k] = val;
}
});

useEffect(() => {
const shouldOpenRunForm = searchParams.get("run") === "true";
if (shouldOpenRunForm) {
setIsSubmitPlaybookRunFormOpen(true);
}
}, [searchParams]);

const handleCloseRunForm = () => {
setIsSubmitPlaybookRunFormOpen(false);
searchParams.delete("run");
setSearchParams(searchParams);
};

const handleOpenRunForm = () => {
searchParams.set("run", "true");
setSearchParams(searchParams);
};

return (
<div className="flex w-full flex-row gap-2">
<FormikFilterForm
Expand Down Expand Up @@ -77,7 +104,7 @@ export default function PlaybookRunsFilterBar({
<>
<Button
disabled={isLoading}
onClick={() => setIsSubmitPlaybookRunFormOpen(true)}
onClick={handleOpenRunForm}
className="btn-primary min-w-max"
>
<VscPlay />
Expand All @@ -89,7 +116,10 @@ export default function PlaybookRunsFilterBar({
>
<SubmitPlaybookRunForm
isOpen={isSubmitPlaybookRunFormOpen}
onClose={() => setIsSubmitPlaybookRunFormOpen(false)}
configId={configID}
overrideParams={configID !== ""}
params={playbookParameters}
onClose={handleCloseRunForm}
playbook={playbookSpec}
/>
</AuthorizationAccessCheck>
Expand Down
8 changes: 4 additions & 4 deletions src/components/Playbooks/Runs/Submit/PlaybookRunParams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ function parseCodeDefaultValue(parameter: PlaybookParam) {
type PlaybookRunParamsProps = {
isResourceRequired: boolean;
playbook: Pick<PlaybookSpec, "spec">;
isReRun: boolean;
overrideParams: boolean;
};

export default function PlaybookRunParams({
isResourceRequired = false,
playbook,
isReRun = false
overrideParams = false
}: PlaybookRunParamsProps) {
const [, setModalSize] = useAtom(submitPlaybookRunFormModalSizesAtom);

Expand Down Expand Up @@ -95,13 +95,13 @@ export default function PlaybookRunParams({
// We don't want to override form values if they are already set by user
// action, like for instance when re-running a playbook with the same
// parameters, we don't want to set the default values again
if (param.default !== undefined && !isReRun) {
if (param.default !== undefined && !overrideParams) {
const defaultValue = parseCodeDefaultValue(param);
setFieldValue(`params.${param.name}`, defaultValue);
}
});
}
}, [data, isReRun, setFieldValue]);
}, [data, overrideParams, setFieldValue]);

// if no resource is selected, show a message and hide the parameters
if (!componentId && !configId && !checkId && isResourceRequired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function ReRunPlaybookWithParamsButton({
checkId={checkId}
configId={configId}
params={params}
isReRun
overrideParams
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Props = {
componentId?: string;
configId?: string;
params?: Record<string, any>;
isReRun?: boolean;
overrideParams?: boolean;
};

export default function SubmitPlaybookRunForm({
Expand All @@ -49,7 +49,7 @@ export default function SubmitPlaybookRunForm({
checkId,
componentId,
configId,
isReRun = false,
overrideParams = false,
params = {}
}: Props) {
const [modalSize, setModalSize] = useAtom(
Expand Down Expand Up @@ -139,7 +139,7 @@ export default function SubmitPlaybookRunForm({
<PlaybookRunParams
isResourceRequired={isResourceRequired}
playbook={playbook}
isReRun={isReRun}
overrideParams={overrideParams}
/>
</div>

Expand Down

0 comments on commit f726af2

Please sign in to comment.