Fix PR feedback

This commit is contained in:
Dave Bartolomeo
2024-07-30 16:02:49 -04:00
parent 4e310ce652
commit 76354c4518
2 changed files with 167 additions and 157 deletions

View File

@@ -1,12 +1,4 @@
import type {
Location as SarifLogLocation,
ReportingDescriptorReference,
ThreadFlowLocation,
Run,
PhysicalLocation,
ArtifactLocation,
ToolComponent,
} from "sarif";
import type { ThreadFlowLocation, Run } from "sarif";
import type {
PathNode,
Result as ResultKeysResult,
@@ -18,150 +10,7 @@ import { selectableZebraStripe } from "./result-table-utils";
import { useCallback, useMemo } from "react";
import { VerticalRule } from "../common/VerticalRule";
import type { UserSettings } from "../../common/interface-types";
/** The definition of a taxon for a data extension model row. */
interface ModelTaxon {
location: SarifLogLocation;
}
/** Resolve an `ArtifactLocation` that might contain a relative reference instead of an absolute
* URI.
*/
function resolveArtifactLocation(
location: ArtifactLocation,
baseUri: URL,
): ArtifactLocation {
if (location.uri === undefined) {
// No URI at all. Just return the original location.
return location;
}
return {
...location,
uri: new URL(location.uri, baseUri).toString(),
};
}
/** Get the URI of the pack's local root directory, if available. */
function getLocalPackUri(extension: ToolComponent): URL | undefined {
if (extension.locations === undefined) {
return undefined;
}
const localPackLocation = extension.locations.find(
(loc) =>
loc.properties !== undefined &&
loc.properties.tags !== undefined &&
loc.properties.tags.includes("CodeQL/LocalPackRoot"),
);
if (localPackLocation === undefined || localPackLocation.uri === undefined) {
return undefined;
}
return new URL(localPackLocation.uri);
}
/** Resolve a `ReportingDescriptorReference` to the `ReportingDescriptor` for the taxon that it
* refers to.
*/
function resolveTaxonDefinition(
run: Run,
taxonRef: ReportingDescriptorReference,
): ModelTaxon | undefined {
const extensions = run.tool.extensions;
if (extensions === undefined) {
return undefined;
}
const extensionIndex = taxonRef.toolComponent?.index;
if (
extensionIndex === undefined ||
extensionIndex < 0 ||
extensionIndex >= extensions.length
) {
return undefined;
}
const extension = extensions[extensionIndex];
if (extension.taxa === undefined) {
return undefined;
}
const localPackUri = getLocalPackUri(extension);
if (localPackUri === undefined) {
return undefined;
}
const taxonIndex = taxonRef.index;
if (
taxonIndex === undefined ||
taxonIndex < 0 ||
taxonIndex >= extension.taxa.length
) {
return undefined;
}
const taxonDef = extension.taxa[taxonIndex];
if (taxonDef.properties === undefined) {
return undefined;
}
const location: PhysicalLocation =
taxonDef.properties["CodeQL/DataExtensionLocation"];
if (location === undefined || location.artifactLocation === undefined) {
return undefined;
}
return {
location: {
physicalLocation: {
...location,
artifactLocation: resolveArtifactLocation(
location.artifactLocation,
localPackUri,
),
},
},
};
}
/** Generate the React elements for each taxon. */
function taxaLocations(
taxa: ReportingDescriptorReference[] | undefined,
run: Run | undefined,
onClick: () => void,
) {
if (taxa === undefined || taxa.length === 0 || run === undefined) {
return [];
}
return taxa.flatMap((taxonRef, index) => {
if (taxonRef.properties === undefined) {
return [];
}
const role = taxonRef.properties["CodeQL/DataflowRole"];
if (typeof role !== "string") {
return [];
}
const taxonDef = resolveTaxonDefinition(run, taxonRef);
if (taxonDef === undefined) {
return [];
}
return (
<div key={index}>
{`(${role}) `}
<SarifLocation
loc={taxonDef.location}
databaseUri={undefined}
sourceLocationPrefix=""
onClick={onClick}
/>
</div>
);
});
}
import { TaxaLocations } from "./locations/TaxaLocations";
interface Props {
step: ThreadFlowLocation;
@@ -252,11 +101,13 @@ export function AlertTablePathNodeRow(props: Props) {
>
{userSettings.shouldShowProvenance ? (
<div className="vscode-codeql__taxa-cell-div">
{taxaLocations(step.taxa, run, handleSarifLocationClicked)}
<TaxaLocations
taxa={step.taxa}
run={run}
onClick={handleSarifLocationClicked}
/>
</div>
) : (
[]
)}
) : null}
</td>
<td
{...selectableZebraStripe(

View File

@@ -0,0 +1,159 @@
import type {
Location as SarifLogLocation,
ArtifactLocation,
PhysicalLocation,
ReportingDescriptorReference,
Run,
ToolComponent,
} from "sarif";
import { SarifLocation } from "./SarifLocation";
/** The definition of a taxon for a data extension model row. */
interface ModelTaxon {
location: SarifLogLocation;
}
/** Resolve an `ArtifactLocation` that might contain a relative reference instead of an absolute
* URI.
*/
function resolveArtifactLocation(
location: ArtifactLocation,
baseUri: URL,
): ArtifactLocation {
if (location.uri === undefined) {
// No URI at all. Just return the original location.
return location;
}
return {
...location,
uri: new URL(location.uri, baseUri).toString(),
};
}
/** Get the URI of the pack's local root directory, if available. */
function getLocalPackUri(extension: ToolComponent): URL | undefined {
if (extension.locations === undefined) {
return undefined;
}
const localPackLocation = extension.locations.find(
(loc) =>
loc.properties !== undefined &&
loc.properties.tags !== undefined &&
loc.properties.tags.includes("CodeQL/LocalPackRoot"),
);
if (localPackLocation === undefined || localPackLocation.uri === undefined) {
return undefined;
}
return new URL(localPackLocation.uri);
}
/** Resolve a `ReportingDescriptorReference` to the `ReportingDescriptor` for the taxon that it
* refers to.
*/
function resolveTaxonDefinition(
run: Run,
taxonRef: ReportingDescriptorReference,
): ModelTaxon | undefined {
const extensions = run.tool.extensions;
if (extensions === undefined) {
return undefined;
}
const extensionIndex = taxonRef.toolComponent?.index;
if (
extensionIndex === undefined ||
extensionIndex < 0 ||
extensionIndex >= extensions.length
) {
return undefined;
}
const extension = extensions[extensionIndex];
if (extension.taxa === undefined) {
return undefined;
}
const localPackUri = getLocalPackUri(extension);
if (localPackUri === undefined) {
return undefined;
}
const taxonIndex = taxonRef.index;
if (
taxonIndex === undefined ||
taxonIndex < 0 ||
taxonIndex >= extension.taxa.length
) {
return undefined;
}
const taxonDef = extension.taxa[taxonIndex];
if (taxonDef.properties === undefined) {
return undefined;
}
const location: PhysicalLocation =
taxonDef.properties["CodeQL/DataExtensionLocation"];
if (location === undefined || location.artifactLocation === undefined) {
return undefined;
}
return {
location: {
physicalLocation: {
...location,
artifactLocation: resolveArtifactLocation(
location.artifactLocation,
localPackUri,
),
},
},
};
}
interface Props {
taxa: ReportingDescriptorReference[] | undefined;
run: Run | undefined;
onClick: () => void;
}
/** Generate the React elements for each taxon. */
export function TaxaLocations({
taxa,
run,
onClick,
}: Props): React.JSX.Element[] {
if (taxa === undefined || taxa.length === 0 || run === undefined) {
return [];
}
return taxa.flatMap((taxonRef, index) => {
if (taxonRef.properties === undefined) {
return [];
}
const role = taxonRef.properties["CodeQL/DataflowRole"];
if (typeof role !== "string") {
return [];
}
const taxonDef = resolveTaxonDefinition(run, taxonRef);
if (taxonDef === undefined) {
return [];
}
return (
<div key={index}>
{`(${role}) `}
<SarifLocation
loc={taxonDef.location}
databaseUri={undefined}
sourceLocationPrefix=""
onClick={onClick}
/>
</div>
);
});
}