-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1444 from flanksource/1436-playbook-actions-resul…
…ts-improvements fix: decode logs in results object and remove overflow of caused by text
- Loading branch information
Showing
15 changed files
with
204 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/components/Playbooks/Runs/Actions/PlaybookRunActionFetch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { getPlaybookRunActionById } from "../../../../api/services/playbooks"; | ||
import TableSkeletonLoader from "../../../SkeletonLoader/TableSkeletonLoader"; | ||
import PlaybooksRunActionsResults from "./PlaybooksActionsResults"; | ||
|
||
type Props = { | ||
playbookRunActionId: string; | ||
}; | ||
|
||
export default function PlaybookRunActionFetch({ playbookRunActionId }: Props) { | ||
const { data: action, isLoading } = useQuery({ | ||
queryKey: ["playbookRunAction", playbookRunActionId], | ||
queryFn: () => getPlaybookRunActionById(playbookRunActionId), | ||
enabled: !!playbookRunActionId | ||
}); | ||
|
||
if (isLoading || !action) { | ||
return <TableSkeletonLoader />; | ||
} | ||
|
||
return <PlaybooksRunActionsResults action={action} />; | ||
} |
6 changes: 3 additions & 3 deletions
6
...Playbooks/Runs/PlaybookRunsActionItem.tsx → ...s/Runs/Actions/PlaybookRunsActionItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
src/components/Playbooks/Runs/Actions/PlaybooksActionsResults.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Convert from "ansi-to-html"; | ||
import { PlaybookRunAction } from "../PlaybookRunTypes"; | ||
|
||
const convert = new Convert(); | ||
|
||
type Props = { | ||
action: Pick<PlaybookRunAction, "result" | "error">; | ||
className?: string; | ||
}; | ||
|
||
export default function PlaybooksRunActionsResults({ | ||
action, | ||
className = "whitespace-pre-wrap break-all" | ||
}: Props) { | ||
const { result, error } = action; | ||
|
||
if (!result && !error) { | ||
return <>No result</>; | ||
} | ||
|
||
if (action.error) { | ||
return <pre className={className}>{action.error}</pre>; | ||
} | ||
|
||
if (result?.stdout) { | ||
return <pre className={className}>{result.stdout}</pre>; | ||
} | ||
|
||
if (result?.logs) { | ||
const html = convert.toHtml(result.logs); | ||
|
||
return ( | ||
<pre className={className} dangerouslySetInnerHTML={{ __html: html }} /> | ||
); | ||
} | ||
|
||
const json = JSON.stringify(result, null, 2); | ||
|
||
return <pre className={className}>{json}</pre>; | ||
} |
49 changes: 49 additions & 0 deletions
49
src/components/Playbooks/Runs/Actions/__tests__/PlaybooksActionsResults.unit.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import PlaybooksRunActionsResults from "../PlaybooksActionsResults"; | ||
|
||
describe("PlaybooksRunActionsResults", () => { | ||
it("renders 'No result' when result is falsy", () => { | ||
render(<PlaybooksRunActionsResults action={{}} />); | ||
expect(screen.getByText("No result")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders stdout when result has stdout", () => { | ||
const action = { result: { stdout: "Hello, world!" } }; | ||
render(<PlaybooksRunActionsResults action={action} />); | ||
expect(screen.getByText("Hello, world!")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders logs when result has logs", () => { | ||
const action = { result: { logs: "Hello, world!" } }; | ||
render(<PlaybooksRunActionsResults action={action} />); | ||
expect(screen.getByText("Hello, world!")).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders JSON when result has neither stdout nor logs", () => { | ||
const action = { result: { foo: "bar" } }; | ||
render(<PlaybooksRunActionsResults action={action} />); | ||
expect( | ||
screen.getByText('{ "foo": "bar" }', { | ||
exact: false | ||
}) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("applies the className prop to the pre element", () => { | ||
const action = { result: { stdout: "Hello, world!" } }; | ||
render( | ||
<PlaybooksRunActionsResults action={action} className="text-red-500" /> | ||
); | ||
expect(screen.getByText("Hello, world!")).toHaveClass("text-red-500"); | ||
}); | ||
|
||
it("renders error when action has error", () => { | ||
const action = { error: "Something went wrong" }; | ||
render(<PlaybooksRunActionsResults action={action} />); | ||
expect( | ||
screen.getByText("Something went wrong", { | ||
exact: false | ||
}) | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ConfigItem } from "../../../api/services/configs"; | ||
import { User } from "../../../api/services/users"; | ||
import { Topology } from "../../../context/TopologyPageContext"; | ||
import { HealthCheck } from "../../../types/healthChecks"; | ||
import { PlaybookSpec } from "../Settings/PlaybookSpecsTable"; | ||
|
||
export type PlaybookRunStatus = | ||
| "scheduled" | ||
| "running" | ||
| "cancelled" | ||
| "completed" | ||
| "failed" | ||
| "pending"; | ||
|
||
export type PlaybookRunAction = { | ||
id: string; | ||
name: string; | ||
status: PlaybookRunStatus; | ||
playbook_run_id: string; | ||
start_time: string; | ||
scheduled_time?: string; | ||
end_time?: string; | ||
result?: { | ||
stdout?: string; | ||
logs?: string; | ||
[key: string]: unknown; | ||
}; | ||
error?: string; | ||
}; | ||
|
||
export type PlaybookRun = { | ||
id: string; | ||
playbook_id?: string; | ||
status: PlaybookRunStatus; | ||
start_time: string; | ||
scheduled_time?: string; | ||
end_time?: string; | ||
created_at?: string; | ||
created_by?: User; | ||
check_id?: string; | ||
config_id?: string; | ||
component_id?: string; | ||
parameters?: Record<string, unknown>; | ||
agent_id?: string; | ||
/* relationships */ | ||
playbooks?: PlaybookSpec; | ||
component?: Pick<Topology, "id" | "name" | "icon">; | ||
check?: Pick<HealthCheck, "id" | "name" | "icon">; | ||
config?: Pick<ConfigItem, "id" | "name" | "type" | "config_class">; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.