Skip to content

Commit

Permalink
fix: cache file path for showVulnerabilityFoundPrompt
Browse files Browse the repository at this point in the history
Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov committed Apr 4, 2024
1 parent 48d5207 commit caf7e73
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@fabric8-analytics/fabric8-analytics-lsp-server": "^0.9.4-ea.8",
"@fabric8-analytics/fabric8-analytics-lsp-server": "^0.9.4-ea.9",
"@redhat-developer/vscode-redhat-telemetry": "^0.7.0",
"@RHEcosystemAppEng/exhort-javascript-api": "^0.1.1-ea.27",
"fs": "^0.0.1-security",
Expand Down
33 changes: 18 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ export function activate(context: vscode.ExtensionContext) {

const disposableStackAnalysisCommand = vscode.commands.registerCommand(
commands.STACK_ANALYSIS_COMMAND,
async (uri: vscode.Uri) => {
// uri will be null in case the user has used the context menu/file explorer
const fileUri = uri ? uri : vscode.window.activeTextEditor.document.uri;
async (filePath: string) => {
filePath = filePath ? filePath : vscode.window.activeTextEditor.document.uri.fsPath;
const fileName = path.basename(filePath);
try {
await generateRHDAReport(context, fileUri);
record(context, TelemetryActions.vulnerabilityReportDone, { manifest: path.basename(fileUri.fsPath), fileName: path.basename(fileUri.fsPath) });
await generateRHDAReport(context, filePath);
record(context, TelemetryActions.vulnerabilityReportDone, { manifest: fileName, fileName: fileName });
} catch (error) {
const message = applySettingNameMappings(error.message);
vscode.window.showErrorMessage(message);
record(context, TelemetryActions.vulnerabilityReportFailed, { manifest: path.basename(fileUri.fsPath), fileName: path.basename(fileUri.fsPath), error: message });
record(context, TelemetryActions.vulnerabilityReportFailed, { manifest: fileName, fileName: fileName, error: message });
}
}
);
Expand Down Expand Up @@ -142,10 +142,11 @@ export function activate(context: vscode.ExtensionContext) {

lspClient.start().then(() => {

const showVulnerabilityFoundPrompt = async (msg: string, fileName: string) => {
const showVulnerabilityFoundPrompt = async (msg: string, filePath: string) => {
const fileName = path.basename(filePath);
const selection = await vscode.window.showWarningMessage(`${msg}`, PromptText.FULL_STACK_PROMPT_TEXT);
if (selection === PromptText.FULL_STACK_PROMPT_TEXT) {
vscode.commands.executeCommand(commands.STACK_ANALYSIS_COMMAND);
vscode.commands.executeCommand(commands.STACK_ANALYSIS_COMMAND, filePath);
record(context, TelemetryActions.vulnerabilityReportPopupOpened, { manifest: fileName, fileName: fileName });
}
else {
Expand All @@ -157,7 +158,7 @@ export function activate(context: vscode.ExtensionContext) {
const notification = new CANotification(respData);
caStatusBarProvider.showSummary(notification.statusText(), notification.origin());
if (notification.hasWarning()) {
showVulnerabilityFoundPrompt(notification.popupText(), path.basename(notification.origin()));
showVulnerabilityFoundPrompt(notification.popupText(), notification.origin());
record(context, TelemetryActions.componentAnalysisDone, { manifest: path.basename(notification.origin()), fileName: path.basename(notification.origin()) });
}
});
Expand Down Expand Up @@ -259,21 +260,23 @@ function showRHRepositoryRecommendationNotification() {
*/
function registerStackAnalysisCommands(context: vscode.ExtensionContext) {

const invokeFullStackReport = async (uri: vscode.Uri) => {
const invokeFullStackReport = async (filePath: string) => {
const fileName = path.basename(filePath);
try {
await generateRHDAReport(context, uri);
record(context, TelemetryActions.vulnerabilityReportDone, { manifest: path.basename(uri.fsPath), fileName: path.basename(uri.fsPath) });
await generateRHDAReport(context, filePath);
record(context, TelemetryActions.vulnerabilityReportDone, { manifest: fileName, fileName: fileName });
} catch (error) {
const message = applySettingNameMappings(error.message);
vscode.window.showErrorMessage(message);
record(context, TelemetryActions.vulnerabilityReportFailed, { manifest: path.basename(uri.fsPath), fileName: path.basename(uri.fsPath), error: message });
record(context, TelemetryActions.vulnerabilityReportFailed, { manifest: fileName, fileName: fileName, error: message });
}
};

const recordAndInvoke = (origin: string, uri: vscode.Uri) => {
const fileUri = uri || vscode.window.activeTextEditor.document.uri;
record(context, origin, { manifest: fileUri.fsPath.split('/').pop(), fileName: fileUri.fsPath.split('/').pop() });
invokeFullStackReport(fileUri);
const filePath = fileUri.fsPath;
record(context, origin, { manifest: filePath.split('/').pop(), fileName: filePath.split('/').pop() });
invokeFullStackReport(filePath);
};

const registerCommand = (cmd: string, action: TelemetryActions) => {
Expand Down
8 changes: 4 additions & 4 deletions src/stackAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ async function triggerWebviewPanel(context) {
* @param uri The URI of the manifest file for analysis.
* @returns A promise that resolves once the report generation is complete.
*/
async function generateRHDAReport(context, uri) {
if (uri.fsPath && supportedFiles.includes(path.basename(uri.fsPath))) {
async function generateRHDAReport(context, filePath) {
if (supportedFiles.includes(path.basename(filePath))) {
try {

await triggerWebviewPanel(context);
const resp = await executeStackAnalysis(uri.fsPath);
const resp = await executeStackAnalysis(filePath);
/* istanbul ignore else */
if (DependencyReportPanel.currentPanel) {
await writeReportToFile(resp);
Expand All @@ -150,7 +150,7 @@ async function generateRHDAReport(context, uri) {
}
} else {
vscode.window.showInformationMessage(
`File ${uri.fsPath} is not supported!!`
`File ${filePath} is not supported!!`
);
}
}
Expand Down
16 changes: 8 additions & 8 deletions test/stackAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ chai.use(sinonChai);

suite('StackAnalysis module', () => {
let sandbox: sinon.SinonSandbox;
const MockUri = vscode.Uri.file('/mock/path/pom.xml');
const mockPath = '/mock/path/pom.xml';
const mockReponse = '<html> mockResponse </html>';

setup(() => {
Expand All @@ -36,16 +36,16 @@ suite('StackAnalysis module', () => {
});

test('should ignore unsoported file', async () => {
const unsupportedUri = vscode.Uri.file('/mock/path/yarn.lock');
const unsupportedFilePath = '/mock/path/yarn.lock';
const authorizeRHDAStub = sandbox.stub(globalConfig, 'authorizeRHDA').resolves();
const stackAnalysisServiceStub = sandbox.stub(exhortServices, 'stackAnalysisService').resolves(mockReponse)
const showInformationMessageSpy = sandbox.spy(vscode.window, 'showInformationMessage');

await generateRHDAReport(context, unsupportedUri);
await generateRHDAReport(context, unsupportedFilePath);

expect(authorizeRHDAStub.calledOnce).to.be.false;
expect(stackAnalysisServiceStub.calledOnce).to.be.false;
expect(showInformationMessageSpy.calledOnceWith(`File ${unsupportedUri.fsPath} is not supported!!`)).to.be.true;
expect(showInformationMessageSpy.calledOnceWith(`File ${unsupportedFilePath} is not supported!!`)).to.be.true;
});

test('should generate RHDA report for supported file and successfully save HTML data locally', async () => {
Expand All @@ -56,7 +56,7 @@ suite('StackAnalysis module', () => {
callback(null);
});

await generateRHDAReport(context, MockUri);
await generateRHDAReport(context, mockPath);

expect(authorizeRHDAStub.calledOnce).to.be.true;
expect(stackAnalysisServiceStub.calledOnce).to.be.true;
Expand All @@ -69,7 +69,7 @@ suite('StackAnalysis module', () => {
const authorizeRHDAStub = sandbox.stub(globalConfig, 'authorizeRHDA').resolves();
const stackAnalysisServiceStub = sandbox.stub(exhortServices, 'stackAnalysisService').rejects(new Error('Mock Error'));

await generateRHDAReport(context, MockUri)
await generateRHDAReport(context, mockPath)
.then(() => {
throw (new Error('should have thrown error'))
})
Expand All @@ -89,7 +89,7 @@ suite('StackAnalysis module', () => {
callback(new Error('Mock Error'));
});

await generateRHDAReport(context, MockUri)
await generateRHDAReport(context, mockPath)
.then(() => {
throw (new Error('should have thrown error'))
})
Expand All @@ -108,7 +108,7 @@ suite('StackAnalysis module', () => {
sandbox.stub(fs, 'existsSync').returns(false);
const mkdirSyncStub = sandbox.stub(fs, 'mkdirSync').throws(new Error('Mock Error'));

await generateRHDAReport(context, MockUri)
await generateRHDAReport(context, mockPath)
.then(() => {
throw (new Error('should have thrown error'))

Expand Down

0 comments on commit caf7e73

Please sign in to comment.