Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate props in retrospective post (aider assisted) #1973

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions webapp/src/components/retrospective_post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import {Metric, MetricType} from 'src/types/playbook';

import {RunMetricData} from 'src/types/playbook_run';

import {
isArrayOf,
isMetric,
isMetricData,
safeJSONParse,
} from 'src/utils';

import {ClockOutline, DollarSign, PoundSign} from './backstage/playbook_edit/styles';
import {metricToString} from './backstage/playbook_edit/metrics/shared';

Expand All @@ -33,8 +40,11 @@ export const RetrospectivePost = (props: Props) => {

const mdText = (text: string) => messageHtmlToComponent(formatText(text, markdownOptions), true, messageHtmlToComponentOptions);

const metricsConfigs: Array<Metric> = JSON.parse(props.post.props.metricsConfigs);
const metricsData: Array<RunMetricData> = JSON.parse(props.post.props.metricsData);
const parsedMetricsConfigs = safeJSONParse<unknown>(props.post.props?.metricsConfigs);
const parsedMetricsData = safeJSONParse<unknown>(props.post.props?.metricsData);

const metricsConfigs: Array<Metric> = isArrayOf(parsedMetricsConfigs, isMetric) ? parsedMetricsConfigs : [];
const metricsData: Array<RunMetricData> = isArrayOf(parsedMetricsData, isMetricData) ? parsedMetricsData : [];

return (
<>
Expand Down
41 changes: 39 additions & 2 deletions webapp/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useState} from 'react';

import {PlaybookRun} from 'src/types/playbook_run';
import {Checklist} from 'src/types/playbook';
import {PlaybookRun, RunMetricData} from 'src/types/playbook_run';
import {Checklist, Metric} from 'src/types/playbook';

let idCounter = 0;

Expand Down Expand Up @@ -132,3 +132,40 @@ export function runCallsSlashCommand(command: string, channelId: string, teamId:
},
}, window.origin);
}

export function safeJSONParse<T>(value: unknown): T | null {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I like the semantics of undefined better than null, but it is just about taste.

if (!value || typeof value !== 'string') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be enough:

Suggested change
if (!value || typeof value !== 'string') {
if (typeof value !== 'string') {

We can let the case of empty string being handled by JSON.parse.

return null;
}

try {
return JSON.parse(value) as T;
} catch {
return null;
}
}

export function isArrayOf<T>(value: unknown, typeGuard: (value: unknown) => value is T): value is T[] {
return Array.isArray(value) && value.every(typeGuard);
}

export function isMetric(value: unknown): value is Metric {
if (!value || typeof value !== 'object') {
return false;
}

const metric = value as Metric;
return typeof metric.id === 'string' &&
typeof metric.title === 'string' &&
typeof metric.type === 'string';
}

export function isMetricData(value: unknown): value is RunMetricData {
if (!value || typeof value !== 'object') {
return false;
}

const metricData = value as RunMetricData;
return typeof metricData.metric_config_id === 'string' &&
(typeof metricData.value === 'string' || typeof metricData.value === 'number');
}
Loading