|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + ObservableArray, |
| 4 | + ObservableValue, |
| 5 | +} from 'azure-devops-ui/Core/Observable'; |
| 6 | +import { |
| 7 | + ColumnSorting, |
| 8 | + ISimpleTableCell, |
| 9 | + renderSimpleCell, |
| 10 | + sortItems, |
| 11 | + SortOrder, |
| 12 | + Table, |
| 13 | + TableColumnLayout, |
| 14 | +} from 'azure-devops-ui/Table'; |
| 15 | +import { |
| 16 | + Report, |
| 17 | + Result, |
| 18 | + Severity, |
| 19 | + ExperimentalModifiedFindings, |
| 20 | +} from './trivy'; |
| 21 | +import { ISimpleListCell } from 'azure-devops-ui/List'; |
| 22 | +import { ZeroData } from 'azure-devops-ui/ZeroData'; |
| 23 | +import { compareSeverity, renderSeverity } from './severity'; |
| 24 | +import { ITableColumn } from 'azure-devops-ui/Components/Table/Table.Props'; |
| 25 | +import { ArrayItemProvider } from 'azure-devops-ui/Utilities/Provider'; |
| 26 | + |
| 27 | +interface SuppressedTableProps { |
| 28 | + report: Report; |
| 29 | +} |
| 30 | + |
| 31 | +interface ListSuppressed extends ISimpleTableCell { |
| 32 | + Type: ISimpleTableCell; |
| 33 | + ID: ISimpleTableCell; |
| 34 | + Status: ISimpleTableCell; |
| 35 | + Statement: ISimpleTableCell; |
| 36 | + Source: ISimpleTableCell; |
| 37 | + Title: ISimpleTableCell; |
| 38 | + Severity: ISimpleListCell; |
| 39 | + FilePath: ISimpleListCell; |
| 40 | +} |
| 41 | + |
| 42 | +function renderSuppressedSeverity( |
| 43 | + rowIndex: number, |
| 44 | + columnIndex: number, |
| 45 | + tableColumn: ITableColumn<ListSuppressed>, |
| 46 | + tableItem: ListSuppressed |
| 47 | +): JSX.Element { |
| 48 | + return renderSeverity( |
| 49 | + rowIndex, |
| 50 | + columnIndex, |
| 51 | + tableColumn, |
| 52 | + tableItem.Severity.text as Severity |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +const fixedColumns = [ |
| 57 | + { |
| 58 | + columnLayout: TableColumnLayout.singleLine, |
| 59 | + id: 'ID', |
| 60 | + name: 'ID', |
| 61 | + readonly: true, |
| 62 | + renderCell: renderSimpleCell, |
| 63 | + width: 150, |
| 64 | + }, |
| 65 | + { |
| 66 | + columnLayout: TableColumnLayout.singleLine, |
| 67 | + id: 'Type', |
| 68 | + name: 'Type', |
| 69 | + readonly: true, |
| 70 | + renderCell: renderSimpleCell, |
| 71 | + width: 150, |
| 72 | + sortProps: { |
| 73 | + ariaLabelAscending: 'Sorted A to Z', |
| 74 | + ariaLabelDescending: 'Sorted Z to A', |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + columnLayout: TableColumnLayout.singleLine, |
| 79 | + id: 'Status', |
| 80 | + name: 'Status', |
| 81 | + readonly: true, |
| 82 | + renderCell: renderSimpleCell, |
| 83 | + width: 100, |
| 84 | + }, |
| 85 | + { |
| 86 | + columnLayout: TableColumnLayout.singleLine, |
| 87 | + id: 'Statement', |
| 88 | + name: 'Statement', |
| 89 | + readonly: true, |
| 90 | + renderCell: renderSimpleCell, |
| 91 | + width: new ObservableValue(-20), |
| 92 | + }, |
| 93 | + { |
| 94 | + columnLayout: TableColumnLayout.singleLine, |
| 95 | + id: 'Source', |
| 96 | + name: 'Source', |
| 97 | + readonly: true, |
| 98 | + renderCell: renderSimpleCell, |
| 99 | + width: new ObservableValue(-8), |
| 100 | + }, |
| 101 | + { |
| 102 | + columnLayout: TableColumnLayout.singleLine, |
| 103 | + id: 'Title', |
| 104 | + name: 'Title', |
| 105 | + readonly: true, |
| 106 | + renderCell: renderSimpleCell, |
| 107 | + width: new ObservableValue(-20), |
| 108 | + }, |
| 109 | + { |
| 110 | + columnLayout: TableColumnLayout.singleLine, |
| 111 | + id: 'Severity', |
| 112 | + name: 'Severity', |
| 113 | + readonly: true, |
| 114 | + renderCell: renderSuppressedSeverity, |
| 115 | + width: 120, |
| 116 | + sortProps: { |
| 117 | + ariaLabelAscending: 'Sorted by severity ascending', |
| 118 | + ariaLabelDescending: 'Sorted by severity descending', |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + columnLayout: TableColumnLayout.singleLine, |
| 123 | + id: 'FilePath', |
| 124 | + name: 'Location', |
| 125 | + readonly: true, |
| 126 | + renderCell: renderSimpleCell, |
| 127 | + width: new ObservableValue(-35), |
| 128 | + sortProps: { |
| 129 | + ariaLabelAscending: 'Sorted A to Z', |
| 130 | + ariaLabelDescending: 'Sorted Z to A', |
| 131 | + }, |
| 132 | + }, |
| 133 | +]; |
| 134 | + |
| 135 | +const sortFunctions = [ |
| 136 | + (item1: ListSuppressed, item2: ListSuppressed): number => { |
| 137 | + const severity1: ISimpleListCell = item1.Severity; |
| 138 | + const severity2: ISimpleListCell = item2.Severity; |
| 139 | + return compareSeverity(severity1.text, severity2.text); |
| 140 | + }, |
| 141 | + (item1: ListSuppressed, item2: ListSuppressed): number => { |
| 142 | + const value1: ISimpleListCell = item1.Type; |
| 143 | + const value2: ISimpleListCell = item2.Type; |
| 144 | + return value1.text?.localeCompare(value2.text ?? '') || 0; |
| 145 | + }, |
| 146 | + null, |
| 147 | + (item1: ListSuppressed, item2: ListSuppressed): number => { |
| 148 | + const value1: ISimpleListCell = item1.FilePath; |
| 149 | + const value2: ISimpleListCell = item2.FilePath; |
| 150 | + return value1.text?.localeCompare(value2.text ?? '') || 0; |
| 151 | + }, |
| 152 | + null, |
| 153 | +]; |
| 154 | + |
| 155 | +export class SuppressedTable extends React.Component<SuppressedTableProps> { |
| 156 | + private readonly results: ObservableArray<ListSuppressed> = |
| 157 | + new ObservableArray<ListSuppressed>([]); |
| 158 | + |
| 159 | + constructor(props: SuppressedTableProps) { |
| 160 | + super(props); |
| 161 | + this.results = new ObservableArray<ListSuppressed>( |
| 162 | + convertSuppressed(props.report.Results || []) |
| 163 | + ); |
| 164 | + // sort by severity desc by default |
| 165 | + this.results.splice( |
| 166 | + 0, |
| 167 | + this.results.length, |
| 168 | + ...sortItems<ListSuppressed>( |
| 169 | + 0, |
| 170 | + SortOrder.descending, |
| 171 | + sortFunctions, |
| 172 | + fixedColumns, |
| 173 | + this.results.value |
| 174 | + ) |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + render() { |
| 179 | + const sortingBehavior = new ColumnSorting<ListSuppressed>( |
| 180 | + (columnIndex: number, proposedSortOrder: SortOrder) => { |
| 181 | + this.results.splice( |
| 182 | + 0, |
| 183 | + this.results.length, |
| 184 | + ...sortItems<ListSuppressed>( |
| 185 | + columnIndex, |
| 186 | + proposedSortOrder, |
| 187 | + sortFunctions, |
| 188 | + fixedColumns, |
| 189 | + this.results.value |
| 190 | + ) |
| 191 | + ); |
| 192 | + } |
| 193 | + ); |
| 194 | + |
| 195 | + return this.results.length == 0 ? ( |
| 196 | + <ZeroData |
| 197 | + primaryText="No problems found." |
| 198 | + secondaryText={ |
| 199 | + <span>No suppressions were found for this scan target.</span> |
| 200 | + } |
| 201 | + imageAltText="trivy" |
| 202 | + imagePath={'images/trivy.png'} |
| 203 | + /> |
| 204 | + ) : ( |
| 205 | + <Table |
| 206 | + pageSize={this.results.length} |
| 207 | + selectableText={true} |
| 208 | + ariaLabel="Suppressed Table" |
| 209 | + role="table" |
| 210 | + behaviors={[sortingBehavior]} |
| 211 | + columns={fixedColumns} |
| 212 | + itemProvider={new ArrayItemProvider(this.results.value)} |
| 213 | + containerClassName="h-scroll-auto" |
| 214 | + /> |
| 215 | + ); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +function convertSuppressed(results: Result[]): ListSuppressed[] { |
| 220 | + const output: ListSuppressed[] = []; |
| 221 | + results.forEach((result) => { |
| 222 | + if ( |
| 223 | + Object.prototype.hasOwnProperty.call( |
| 224 | + result, |
| 225 | + 'ExperimentalModifiedFindings' |
| 226 | + ) && |
| 227 | + result.ExperimentalModifiedFindings !== null |
| 228 | + ) { |
| 229 | + const target = result.Target; |
| 230 | + result.ExperimentalModifiedFindings.forEach(function ( |
| 231 | + suppressed: ExperimentalModifiedFindings |
| 232 | + ) { |
| 233 | + let id = ''; |
| 234 | + if (suppressed.Finding.ID) { |
| 235 | + id = suppressed.Finding.ID; |
| 236 | + } else if (suppressed.Finding.VulnerabilityID) { |
| 237 | + id = suppressed.Finding.VulnerabilityID; |
| 238 | + } |
| 239 | + |
| 240 | + output.push({ |
| 241 | + ID: { text: id }, |
| 242 | + Type: { |
| 243 | + text: suppressed.Type.split(' ') |
| 244 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| 245 | + .join(' '), |
| 246 | + }, |
| 247 | + Severity: { text: suppressed.Finding.Severity }, |
| 248 | + Status: { |
| 249 | + text: suppressed.Status.split(' ') |
| 250 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| 251 | + .join(' '), |
| 252 | + }, |
| 253 | + Statement: { text: suppressed.Statement }, |
| 254 | + Source: { text: suppressed.Source }, |
| 255 | + Title: { text: suppressed.Finding.Title }, |
| 256 | + FilePath: { text: target }, |
| 257 | + }); |
| 258 | + }); |
| 259 | + } |
| 260 | + }); |
| 261 | + return output; |
| 262 | +} |
0 commit comments