import Set from 'typescript-collections/dist/lib/Set' ; import * as vscode from 'vscode' ; import { ScanCacheManager } from '../scanCache/scanCacheManager' ; import { IIssueCacheObject } from '../types/issueCacheObject' ; import { Severity , SeverityUtils } from '../types/severity' ; import { Consts } from '../utils/consts' ; import { IconsPaths } from '../utils/iconsPaths' ; import { DependenciesTreeNode } from './dependenciesTree/dependenciesTreeNode' ; import { TreeDataHolder } from './utils/treeDataHolder' ; /**
 * The component issues details tree.
 */ export class IssuesDataProvider implements vscode . TreeDataProvider < vscode . TreeItem > { private _onDidChangeTreeData : vscode . EventEmitter < vscode . TreeItem | undefined > = new vscode . EventEmitter < vscode . TreeItem | undefined > ( ) ; readonly onDidChangeTreeData : vscode . Event < vscode . TreeItem | undefined > = this . _onDidChangeTreeData . event ; private _selectedNode : DependenciesTreeNode | undefined ; constructor ( protected _scanCacheManager ) { } refresh ( ) { this . _onDidChangeTreeData . fire ( ) ; } getTreeItem ( element ) { if ( ! ( element instanceof TreeDataHolder ) ) { return element ; } let holder = < TreeDataHolder > element ; let treeItem = new vscode . TreeItem ( holder . key ) ; treeItem . description = holder . value ; if ( holder . link ) { treeItem . command = { command : 'vscode.open' , arguments : [ vscode . Uri . parse ( holder . link ) ] } as vscode . Command ; } return treeItem ; } getChildren ( element ? ) { if ( ! this . _selectedNode ) { return Promise . resolve ( [ ] ) ; } if ( ! element ) { return Promise . resolve ( this . getTitleNodes ( this . _selectedNode ) ) ; } if ( element instanceof VulnerabilitiesTitleNode ) { return Promise . resolve ( this . getVulnerabilityNodes ( this . _selectedNode ) ) ; } if ( element instanceof VulnerabilityNode ) { return Promise . resolve ( this . getVulnerabilityDetailsNodes ( element ) ) ; } if ( element instanceof LicensesTitleNode ) { return Promise . resolve ( this . getLicenseNodes ( element ) ) ; } return Promise . resolve ( this . getViolatedLicenseComponentsNodes ( element as ViolatedLicenseNode ) ) ; } param selectedNode public selectNode ( selectedNode ) { this . _selectedNode = selectedNode ; this . refresh ( ) ; } param selectedNode returns private getTitleNodes ( selectedNode ) { let violatedLicenses = selectedNode . getViolatedLicenses ( ) ; if ( violatedLicenses . size > 0 ) { return [ new VulnerabilitiesTitleNode ( ) , new LicensesTitleNode ( violatedLicenses ) ] as vscode . TreeItem [ ] ; } return this . getVulnerabilityNodes ( selectedNode ) ; } param selectedNode returns private getVulnerabilityNodes ( selectedNode ) { let children = [ ] ; selectedNode . issues . forEach ( xrayIssueId => { if ( xrayIssueId . issue_id === Consts . MISSING_COMPONENT ) { return ; } let issue = this . _scanCacheManager . getIssue ( xrayIssueId . issue_id ) ; if ( ! issue ) { return ; } let issueNode = new VulnerabilityNode ( issue . severity , issue . summary , issue . cves , xrayIssueId . component , issue . fixedVersions ) ; children . push ( issueNode ) ; } ) ; children . sort ( ( lhs , rhs ) => rhs . severity - lhs . severity ) ; return children ; } param node returns private getVulnerabilityDetailsNodes ( node ) { let children = [ new TreeDataHolder ( 'Severity' , SeverityUtils . getString ( node . severity ) ) , new TreeDataHolder ( 'Component' , node . component ) ] ; let cves = node . cves ; if ( cves && cves . length > 0 ) { children . push ( new TreeDataHolder ( 'CVEs' , cves . toString ( ) ) ) ; } let fixedVersions = node . fixedVersions ; if ( fixedVersions && fixedVersions . length > 0 ) { children . push ( new TreeDataHolder ( 'Fixed Versions' , fixedVersions . join ( ', ' ) ) ) ; } return children ; } param node returns private getLicenseNodes ( node ) { let children = [ ] ; for ( let [ violatedLicense , components ] of node . violatedLicenses ) { children . push ( new ViolatedLicenseNode ( violatedLicense , components ) ) ; } return children ; } param node returns private getViolatedLicenseComponentsNodes ( node ) { let children = [ ] ; node . components . forEach ( component => { children . push ( new TreeDataHolder ( component ) ) ; } ) ; return children ; } } /**
 * Represents "Vulnerabilities" node.
 */ export class VulnerabilitiesTitleNode extends vscode . TreeItem { constructor ( ) { super ( 'Vulnerabilities' , vscode . TreeItemCollapsibleState . Expanded ) ; } } /**
 * Represents "Violated Licenses" node.
 */ export class LicensesTitleNode extends vscode . TreeItem { private _violatedLicenses : Map < string , Set < string > > ; constructor ( violatedLicenses ) { super ( 'Violated Licences' , vscode . TreeItemCollapsibleState . Expanded ) ; this . _violatedLicenses = violatedLicenses ; } public get violatedLicenses ( ) : Map < string , Set < string > > { return this . _violatedLicenses ; } } /**
 * Represents a violated license node.
 */ export class ViolatedLicenseNode extends vscode . TreeItem { constructor ( readonly licenseName , readonly components ) { super ( licenseName , vscode . TreeItemCollapsibleState . Collapsed ) ; this . iconPath = IconsPaths . VIOLATED_LICENSE ; } } /**
 * Represents a vulnerability node.
 */ export class VulnerabilityNode extends vscode . TreeItem { constructor ( readonly severity , readonly summary , readonly cves ? , readonly component ? , readonly fixedVersions ? ) { super ( summary , vscode . TreeItemCollapsibleState . Collapsed ) ; this . iconPath = SeverityUtils . getIcon ( severity ? severity : Severity . Normal ) ; } }