Skip to content

Commit

Permalink
Fixes Terraform table/list page crashing (#3705)
Browse files Browse the repository at this point in the history
* More defensively parse timestamps
  • Loading branch information
foot authored Dec 11, 2023
1 parent bfeadee commit aa8c216
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
11 changes: 9 additions & 2 deletions ui/src/components/Terraform/TerraformListTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ type Props = {

export const getLastApplied = (tf: TerraformObject) => {
const timestamp = _.find(tf?.conditions, { type: 'Apply' })?.timestamp;
if (!timestamp) return '-';
return new Date(timestamp).toISOString();
if (!timestamp) {
return '-';
}

try {
return new Date(timestamp).toISOString();
} catch (e) {
return '-';
}
};

function TerraformListTable({ className, rows }: Props) {
Expand Down
62 changes: 62 additions & 0 deletions ui/src/components/Terraform/__tests__/TerraformListTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { TerraformObject } from '../../../api/terraform/types.pb';
import { getLastApplied } from '../TerraformListTable';

describe('getLastApplied', () => {
const obj: TerraformObject = {
conditions: [
{
type: 'Apply',
status: 'True',
timestamp: '2021-08-11T14:23:39Z',
},
],
};

it('should return the timestamp', () => {
expect(getLastApplied(obj)).toBe('2021-08-11T14:23:39.000Z');
});

it('should return "-" if no timestamp', () => {
const obj2: TerraformObject = {
conditions: [
{
type: 'Apply',
status: 'True',
},
],
};
expect(getLastApplied(obj2)).toBe('-');
});

it('should return "-" if no conditions', () => {
const obj3: TerraformObject = {};
expect(getLastApplied(obj3)).toBe('-');
});

it('should return "-" if no conditions with type Apply', () => {
const obj4: TerraformObject = {
conditions: [
{
type: 'Apply1',
status: 'True',
timestamp: '2021-08-11T14:23:39Z',
},
],
};
expect(getLastApplied(obj4)).toBe('-');
});

it('should return "-" if the timestamp is not valid', () => {
const obj5: TerraformObject = {
conditions: [
{
type: 'Apply',
status: 'True',
timestamp: 'foo',
},
],
};

expect(getLastApplied(obj5)).toBe('-');
});
});

0 comments on commit aa8c216

Please sign in to comment.