Update model alert processing to track repo results (#3508)

This commit is contained in:
Charis Kyriakou 2024-03-25 12:15:16 +00:00 коммит произвёл GitHub
Родитель dea68e928a
Коммит 8b6a9352f6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
6 изменённых файлов: 80 добавлений и 33 удалений

Просмотреть файл

@ -2,35 +2,58 @@ import type { AnalysisAlert } from "../../variant-analysis/shared/analysis-resul
import type { ModeledMethod } from "../modeled-method";
import { EndpointType } from "../method";
import type { ModelAlerts } from "./model-alerts";
import type {
VariantAnalysis,
VariantAnalysisScannedRepositoryResult,
} from "../../variant-analysis/shared/variant-analysis";
/**
* Calculate which model has contributed to each alert.
* @param alerts The alerts to process.
* @param repoResults The analysis results for each repo.
* @returns The alerts grouped by modeled method.
*/
export function calculateModelAlerts(alerts: AnalysisAlert[]): ModelAlerts[] {
// Temporary logging to use alerts variable.
console.log(`Processing ${alerts.length} alerts`);
export function calculateModelAlerts(
variantAnalysis: VariantAnalysis,
repoResults: VariantAnalysisScannedRepositoryResult[],
): ModelAlerts[] {
// For now we just return some mock data, but once we have provenance information
// we'll be able to calculate this properly based on the alerts that are passed in
// and potentially some other information.
return [
{
model: createModeledMethod(),
alerts: [createMockAlert()],
},
];
const modelAlerts: ModelAlerts[] = [];
const repoMap = new Map<number, string>();
for (const scannedRepo of variantAnalysis.scannedRepos || []) {
repoMap.set(scannedRepo.repository.id, scannedRepo.repository.fullName);
}
for (const [i, repoResult] of repoResults.entries()) {
modelAlerts.push({
model: createModeledMethod(i.toString()),
alerts: [
{
alert: createMockAlert(),
repository: {
id: repoResult.repositoryId,
fullName: repoMap.get(repoResult.repositoryId) || "",
},
},
],
});
}
return modelAlerts;
}
function createModeledMethod(): ModeledMethod {
function createModeledMethod(suffix: string): ModeledMethod {
return {
libraryVersion: "1.6.0",
signature: "org.sql2o.Connection#createQuery(String)",
signature: `org.sql2o.Connection#createQuery${suffix}(String)`,
endpointType: EndpointType.Method,
packageName: "org.sql2o",
typeName: "Connection",
methodName: "createQuery",
methodName: `createQuery${suffix}`,
methodParameters: "(String)",
type: "sink",
input: "Argument[0]",

Просмотреть файл

@ -3,5 +3,11 @@ import type { ModeledMethod } from "../modeled-method";
export interface ModelAlerts {
model: ModeledMethod;
alerts: AnalysisAlert[];
alerts: Array<{
alert: AnalysisAlert;
repository: {
id: number;
fullName: string;
};
}>;
}

Просмотреть файл

@ -2,6 +2,7 @@ import type { Meta, StoryFn } from "@storybook/react";
import { ModelAlerts as ModelAlertsComponent } from "../../view/model-alerts/ModelAlerts";
import { createMockVariantAnalysis } from "../../../test/factories/variant-analysis/shared/variant-analysis";
import type { VariantAnalysisScannedRepositoryResult } from "../../variant-analysis/shared/variant-analysis";
export default {
title: "Model Alerts/Model Alerts",
@ -12,19 +13,30 @@ const Template: StoryFn<typeof ModelAlertsComponent> = (args) => (
<ModelAlertsComponent {...args} />
);
const variantAnalysis = createMockVariantAnalysis({
modelPacks: [
{
name: "Model pack 1",
path: "/path/to/model-pack-1",
},
{
name: "Model pack 2",
path: "/path/to/model-pack-2",
},
],
});
const repoResults: VariantAnalysisScannedRepositoryResult[] = (
variantAnalysis.scannedRepos || []
).map((repo) => ({
variantAnalysisId: variantAnalysis.id,
repositoryId: repo.repository.id,
interpretedResults: [],
}));
export const ModelAlerts = Template.bind({});
ModelAlerts.args = {
initialViewState: { title: "codeql/sql2o-models" },
variantAnalysis: createMockVariantAnalysis({
modelPacks: [
{
name: "Model pack 1",
path: "/path/to/model-pack-1",
},
{
name: "Model pack 2",
path: "/path/to/model-pack-2",
},
],
}),
variantAnalysis,
repoResults,
};

Просмотреть файл

@ -17,6 +17,14 @@ export const ModelAlertsResults = Template.bind({});
ModelAlertsResults.args = {
modelAlerts: {
model: createSinkModeledMethod(),
alerts: [createMockAnalysisAlert()],
alerts: [
{
repository: {
id: 1,
fullName: "expressjs/express",
},
alert: createMockAnalysisAlert(),
},
],
},
};

Просмотреть файл

@ -93,14 +93,12 @@ export function ModelAlerts({
}, []);
const modelAlerts = useMemo(() => {
if (!repoResults) {
if (!repoResults || !variantAnalysis) {
return [];
}
const alerts = repoResults.flatMap((a) => a.interpretedResults ?? []);
return calculateModelAlerts(alerts);
}, [repoResults]);
return calculateModelAlerts(variantAnalysis, repoResults);
}, [variantAnalysis, repoResults]);
if (viewState === undefined || variantAnalysis === undefined) {
return <></>;

Просмотреть файл

@ -80,7 +80,7 @@ export const ModelAlertsResults = ({
<AlertsContainer>
{modelAlerts.alerts.map((r, i) => (
<Alert key={i}>
<AnalysisAlertResult alert={r} />
<AnalysisAlertResult alert={r.alert} />
</Alert>
))}
</AlertsContainer>