import { Tooltip } from 'azure-devops-ui/TooltipEx' ; import * as React from 'react' ; import { Result } from 'sarif' ; import { getRepoUri } from './getRepoUri' ; import { Hi } from './Hi' ; import './RunCard.renderCell.scss' ; import { TooltipSpan } from './TooltipSpan' ; import { tryLink , tryOr } from './try' ; function isValidURL ( url ) { try { return ! ! new URL ( url ) ; } catch ( error ) { return false ; } } interface SimpleRegion { startLine ? : number ; startColumn ? : number ; endColumn ? : number ; } function openInNewTab ( fileName , text , region ) { const line = region ?. startLine ?? 1 ; const col = region ?. startColumn ?? 1 ; const length = ( region ?. endColumn ?? 1 ) - col ; const [ _ , pre , hi , post ] = new RegExp ( `((?:.*?\\n){${ line - 1 }}.{${ col - 1 }})(.{${ length }})((?:.|\\n)*)` , 's' ) . exec ( text ) ; const escape = unsafe => unsafe . replace ( /&/g , "&amp;" ) . replace ( /</g , "&lt;" ) . replace ( />/g , "&gt;" ) . replace ( /"/g , "&quot;" ) . replace ( /'/g , "&#039;" ) ; const { document } = window . open ( ) ; document . title = fileName ; document . body . innerHTML = `<pre>${ escape ( pre ) }<mark>${ escape ( hi ) }</mark>${ escape ( post ) }</pre>` ; setTimeout ( ( ) => document . body . querySelector ( 'mark' ) . scrollIntoView ( { block : 'center' } ) ) ; } export function renderPathCell ( result ) { const ploc = result . locations ?. [ 0 ] ?. physicalLocation ; const resArtLoc = ploc ?. artifactLocation ?? result . analysisTarget ; const runArt = result . run . artifacts ?. [ resArtLoc ?. index ?? - 1 ] ; const runArtLoc = runArt ?. location ; const uri = resArtLoc ?. description ?. text ?? runArtLoc ?. description ?. text ?? resArtLoc ?. uri ?? runArtLoc ?. uri ; const [ path , fileName ] = ( ( ) => { if ( ! uri ) return [ '—' ] ; const index = uri . lastIndexOf ( '/' ) ; return index >= 0 ? [ uri . slice ( 0 , index ) , uri . slice ( index + 1 ) ] : [ uri ] ; } ) ( ) ; const uriWithEllipsis = fileName ? < span className = "midEllipsis" > < span > < Hi > { path } < /Hi></span > < span > < Hi > /{fileName}</Hi > < /span> < /span> : < Hi > { uri ?? '—' } < /Hi>; const href = resArtLoc ?. properties ?. [ 'href' ] ; const runArtContentsText = runArt ?. contents ?. text ; const repoUri = getRepoUri ( uri , result . run , ploc ?. region ) ?? uri ; const getHref = ( ) => { if ( uri ?. endsWith ( '.dll' ) ) return undefined ; if ( href ) return href ; if ( runArtContentsText ) return '#' ; if ( ! isValidURL ( repoUri ) ) return undefined ; return repoUri ; } ; const region = ploc ?. region ; const onClick = event => { if ( href ) return ; if ( ! runArtContentsText ) return ; event . preventDefault ( ) ; event . stopPropagation ( ) ; openInNewTab ( fileName , runArtContentsText , region ) ; } ; const rowClasses = 'bolt-table-two-line-cell-item flex-row scroll-hidden' ; return tryOr ( ( ) => < div className = "flex-column scroll-hidden" > < div className = { rowClasses } > < div className = "fontsize font-size swcWidth100" > { } < Tooltip overflowOnly = { true } > < pre style = { { margin : 0 } } > < code > < Hi > { result . locations [ 0 ] . logicalLocations [ 0 ] . fullyQualifiedName } < /Hi></code > < /pre> < /Tooltip> < /div> < /div> { tryOr ( ( ) => { if ( ! uri ) throw undefined ; return < div className = { rowClasses } > { } < TooltipSpan overflowOnly = { true } text = { href ?? uri } > { tryLink ( getHref , uriWithEllipsis , 'fontSize font-size secondary-text swcColorUnset swcWidth100' , onClick ) } < /TooltipSpan> < /div>; } ) } < /div>, () => <div className="flex-row scroll-hidden">{/ * From Advanced table demo . * /} { } { } < TooltipSpan text = { href ?? uri } > { tryLink ( getHref , uriWithEllipsis , 'swcColorUnset' , onClick ) } < /TooltipSpan> < /div>); }