Skip to content

Commit

Permalink
Change frontend to use code impacts for severity
Browse files Browse the repository at this point in the history
  • Loading branch information
frederic-tingaud-sonarsource committed Feb 19, 2025
1 parent dc3f77f commit a27c13b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
7 changes: 6 additions & 1 deletion frontend/src/deployment/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';

import { maxSeverity } from '../types/Severities';
import { LanguageSupport } from '../types/RuleMetadata';
import { getRulesDirectories, listSupportedLanguages } from './utils';

Expand Down Expand Up @@ -135,5 +136,9 @@ function getRuleMetadata(srcDir: string, language?: string) {
})();
const genericFile = path.join(srcDir, 'metadata.json');
const genericJson = fs.existsSync(genericFile) ? JSON.parse(fs.readFileSync(genericFile, 'utf8')) : {};
return { ...genericJson, ...languageSpecificJson };
const merged = { ...genericJson, ...languageSpecificJson };
if (merged.hasOwnProperty('code') && merged.code.hasOwnProperty('impacts')) {
merged['severity'] = maxSeverity(merged.code.impacts);
}
return merged;
}
15 changes: 10 additions & 5 deletions frontend/src/deployment/searchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import path from 'path';
import { stripHtml } from 'string-strip-html';
import lunr, { Token } from 'lunr';

import { IndexedRule, IndexStore, Severity, Type, IndexAggregates } from '../types/IndexStore';
import { Severity, maxSeverity } from '../types/Severities';
import { IndexedRule, IndexStore, Type, IndexAggregates } from '../types/IndexStore';
import { logger as rootLogger } from './deploymentLogger';
import { LanguageSupport } from '../types/RuleMetadata';

Expand Down Expand Up @@ -52,8 +53,12 @@ function buildOneRuleRecord(allLanguages: string[], rulesPath: string, ruleDir:
metadata.extra?.legacyKeys?.forEach((legacyKey: string) => allKeys.add(legacyKey));
titles.add(metadata.title);
types.add(metadata.type);
severities.add(metadata.defaultSeverity as Severity);
supportedLanguages.push({name: lang, status: metadata.status});
if (!metadata.hasOwnProperty('code')) {
severities.add(Severity.INFO);
} else {
severities.add(maxSeverity(metadata.code.impacts));
}
supportedLanguages.push({ name: lang, status: metadata.status });
if (metadata.tags) {
for (const tag of metadata.tags) {
tags.add(tag);
Expand Down Expand Up @@ -105,7 +110,7 @@ function buildOneRuleIndexedRecord(rulesPath: string, ruleDir: string)
id: ruleDir,
supportedLanguages: Array.from(record.supportedLanguages).sort((a, b) => a.name.localeCompare(b.name)),
types: Array.from(record.types).sort((a, b) => a.localeCompare(b)),
severities: Array.from(record.severities).sort((a, b) => a.localeCompare(b)),
severities: Array.from(record.severities).sort((a, b) => b - a),
all_keys: Array.from(record.allKeys).sort((a, b) => a.localeCompare(b)),
titles: Array.from(record.titles).sort((a, b) => a.localeCompare(b)),
tags: Array.from(record.tags).sort((a, b) => a.localeCompare(b)),
Expand Down Expand Up @@ -192,7 +197,7 @@ export function buildSearchIndex(ruleIndexStore: IndexStore) {
this.field('titles', { extractor: (doc) => (doc as IndexedRule).titles.join('\n') });
this.field('types');
this.field('languages', { extractor: (doc) => (doc as IndexedRule).supportedLanguages.map(lang => lang.name) });
this.field('defaultSeverity');
this.field('severity');
this.field('tags');
this.field('qualityProfiles');
this.field('descriptions');
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/types/IndexStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {LanguageSupport} from './RuleMetadata';

export type Severity = 'Blocker'|'Critical'|'Major'|'Minor'|'Info';
import { Severity } from './Severities';
export type Type = 'BUG'|'CODE_SMELL'|'VULNERABILITY'|'SECURITY_HOTSPOT';

export interface IndexedRule {
Expand Down
20 changes: 20 additions & 0 deletions frontend/src/types/Severities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

export enum Severity {
INFO,
LOW,
MEDIUM,
HIGH,
BLOCKER,
};

export function maxSeverity(impacts: any): Severity {
let maxSeverity: Severity = Severity.INFO;
for (const key in impacts) {
const val = impacts[key];
const sev: Severity = Severity[val as keyof typeof Severity];
if (sev > maxSeverity) {
maxSeverity = sev;
}
}
return maxSeverity;
}

0 comments on commit a27c13b

Please sign in to comment.