Skip to content

Commit ffe7fdc

Browse files
committed
Rename methods and address comments
1 parent 0383a91 commit ffe7fdc

9 files changed

Lines changed: 79 additions & 44 deletions

File tree

extensions/ql-vscode/src/remote-queries/analyses-results-manager.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { extractAnalysisAlerts } from './sarif-processing';
1414
import { CodeQLCliServer } from '../cli';
1515
import { extractRawResults } from './bqrs-processing';
1616
import { asyncFilter, getErrorMessage } from '../pure/helpers-pure';
17-
import { toDownloadPath } from './download-link';
17+
import { createDownloadPath } from './download-link';
1818

1919
export class AnalysesResultsManager {
2020
// Store for the results of various analyses for each remote query.
@@ -51,17 +51,17 @@ export class AnalysesResultsManager {
5151
* it will be downloaded. If it is already downloaded, it will be loaded into memory.
5252
* If it is already in memory, this will be a no-op.
5353
*
54-
* @param allAnalysesToDownload List of analyses to ensure are downloaded and in memory
54+
* @param allAnalysesToLoad List of analyses to ensure are downloaded and in memory
5555
* @param token Optional cancellation token
5656
* @param publishResults Optional function to publish the results after loading
5757
*/
5858
public async loadAnalysesResults(
59-
allAnalysesToDownload: AnalysisSummary[],
59+
allAnalysesToLoad: AnalysisSummary[],
6060
token?: CancellationToken,
6161
publishResults: (analysesResults: AnalysisResults[]) => Promise<void> = () => Promise.resolve()
6262
): Promise<void> {
6363
// Filter out analyses that we have already in memory.
64-
const analysesToDownload = allAnalysesToDownload.filter(x => !this.isAnalysisInMemory(x));
64+
const analysesToDownload = allAnalysesToLoad.filter(x => !this.isAnalysisInMemory(x));
6565

6666
const credentials = await Credentials.initialize(this.ctx);
6767

@@ -166,11 +166,11 @@ export class AnalysesResultsManager {
166166
public async loadDownloadedArtifacts(
167167
allAnalysesToCheck: AnalysisSummary[]
168168
) {
169-
const allDownloadedAnalyses = await asyncFilter(allAnalysesToCheck, x => this.isDownloadedNotInMemory(x));
169+
const allDownloadedAnalyses = await asyncFilter(allAnalysesToCheck, x => this.isAnalysisDownloadedNotInMemory(x));
170170
await this.loadAnalysesResults(allDownloadedAnalyses);
171171
}
172172

173-
private async isDownloadedNotInMemory(analysis: AnalysisSummary): Promise<boolean> {
173+
private async isAnalysisDownloadedNotInMemory(analysis: AnalysisSummary): Promise<boolean> {
174174
const queryId = analysis.downloadLink.queryId;
175175
const resultsForQuery = this.internalGetAnalysesResults(queryId);
176176
const analysisResults = resultsForQuery.find(r => r.nwo === analysis.nwo);
@@ -180,7 +180,7 @@ export class AnalysesResultsManager {
180180
}
181181

182182
// Check if the analysis results are already downloaded, but not in memory
183-
return await fs.pathExists(toDownloadPath(this.storagePath, analysis.downloadLink));
183+
return await fs.pathExists(createDownloadPath(this.storagePath, analysis.downloadLink));
184184
}
185185

186186
private async readBqrsResults(filePath: string, fileLinkPrefix: string): Promise<AnalysisRawResults> {

extensions/ql-vscode/src/remote-queries/download-link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ export interface DownloadLink {
3535
*
3636
* @returns A full path to the download location of the artifact
3737
*/
38-
export function toDownloadPath(storagePath: string, downloadLink: DownloadLink, extension = '') {
38+
export function createDownloadPath(storagePath: string, downloadLink: DownloadLink, extension = '') {
3939
return path.join(storagePath, downloadLink.queryId, downloadLink.id + (extension ? `.${extension}` : ''));
4040
}

extensions/ql-vscode/src/remote-queries/gh-actions-api-client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { showAndLogWarningMessage, tmpDir } from '../helpers';
55
import { Credentials } from '../authentication';
66
import { logger } from '../logging';
77
import { RemoteQueryWorkflowResult } from './remote-query-workflow-result';
8-
import { DownloadLink, toDownloadPath } from './download-link';
8+
import { DownloadLink, createDownloadPath } from './download-link';
99
import { RemoteQuery } from './remote-query';
1010
import { RemoteQueryFailureIndexItem, RemoteQueryResultIndex, RemoteQuerySuccessIndexItem } from './remote-query-result-index';
1111

@@ -82,14 +82,14 @@ export async function downloadArtifactFromLink(
8282

8383
const octokit = await credentials.getOctokit();
8484

85-
const extractedPath = toDownloadPath(storagePath, downloadLink);
85+
const extractedPath = createDownloadPath(storagePath, downloadLink);
8686

8787
// first check if we already have the artifact
8888
if (!(await fs.pathExists(extractedPath))) {
8989
// Download the zipped artifact.
9090
const response = await octokit.request(`GET ${downloadLink.urlPath}/zip`, {});
9191

92-
const zipFilePath = toDownloadPath(storagePath, downloadLink, 'zip');
92+
const zipFilePath = createDownloadPath(storagePath, downloadLink, 'zip');
9393
await saveFile(`${zipFilePath}`, response.data as ArrayBuffer);
9494

9595
// Extract the zipped artifact.

extensions/ql-vscode/src/remote-queries/remote-queries-interface.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ export class RemoteQueriesInterfaceManager {
4646
this.getPanel().reveal(undefined, true);
4747

4848
await this.waitForPanelLoaded();
49+
const model = await this.buildViewModel(query, queryResult);
4950
await this.postMessage({
5051
t: 'setRemoteQueryResult',
51-
queryResult: await this.buildViewModel(query, queryResult)
52+
queryResult: model
5253
});
5354

55+
// Ensure all pre-downloaded artifacts are loaded into memory
56+
await this.analysesResultsManager.loadDownloadedArtifacts(model.analysisSummaries);
57+
5458
await this.setAnalysisResults(this.analysesResultsManager.getAnalysesResults(queryResult.queryId));
5559
}
5660

@@ -69,7 +73,7 @@ export class RemoteQueriesInterfaceManager {
6973
const analysisSummaries = this.buildAnalysisSummaries(queryResult.analysisSummaries);
7074
const affectedRepositories = queryResult.analysisSummaries.filter(r => r.resultCount > 0);
7175

72-
const model = {
76+
return {
7377
queryTitle: query.queryName,
7478
queryFileName: queryFileName,
7579
queryFilePath: query.queryFilePath,
@@ -84,10 +88,6 @@ export class RemoteQueriesInterfaceManager {
8488
analysisSummaries: analysisSummaries,
8589
analysisFailures: queryResult.analysisFailures,
8690
};
87-
88-
// Ensure all pre-downloaded artifacts are loaded into memory
89-
await this.analysesResultsManager.loadDownloadedArtifacts(model.analysisSummaries);
90-
return model;
9191
}
9292

9393
getPanel(): WebviewPanel {

extensions/ql-vscode/src/remote-queries/sarif-processing.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ function extractResultAlerts(
5454
for (const location of result.locations ?? []) {
5555
const physicalLocation = location.physicalLocation!;
5656
const filePath = physicalLocation.artifactLocation!.uri!;
57-
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!);
57+
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion, physicalLocation.region);
5858
const highlightedRegion = physicalLocation.region
59-
? getHighlightedRegion(physicalLocation.region!)
59+
? getHighlightedRegion(physicalLocation.region)
6060
: undefined;
6161

6262
const analysisAlert: AnalysisAlert = {
@@ -156,24 +156,21 @@ export function tryGetRule(
156156
return undefined;
157157
}
158158

159-
function getCodeSnippet(region: sarif.Region): CodeSnippet {
159+
function getCodeSnippet(region?: sarif.Region, alternateRegion?: sarif.Region): CodeSnippet | undefined {
160+
region = region ?? alternateRegion;
160161

161162
if (!region) {
162-
// Handle SARIF generated from queries that do not have a region.
163-
return {
164-
startLine: 1,
165-
endLine: 1,
166-
text: ''
167-
};
163+
return undefined;
168164
}
169-
const text = region.snippet!.text!;
165+
166+
const text = region.snippet?.text || '';
170167
const { startLine, endLine } = parseSarifRegion(region);
171168

172169
return {
173170
startLine,
174171
endLine,
175172
text
176-
} as CodeSnippet;
173+
};
177174
}
178175

179176
function getHighlightedRegion(region: sarif.Region): HighlightedRegion {
@@ -204,7 +201,7 @@ function getCodeFlows(
204201
for (const threadFlowLocation of threadFlow.locations) {
205202
const physicalLocation = threadFlowLocation!.location!.physicalLocation!;
206203
const filePath = physicalLocation!.artifactLocation!.uri!;
207-
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion!);
204+
const codeSnippet = getCodeSnippet(physicalLocation.contextRegion, physicalLocation.region);
208205
const highlightedRegion = physicalLocation.region
209206
? getHighlightedRegion(physicalLocation.region)
210207
: undefined;

extensions/ql-vscode/src/remote-queries/shared/analysis-result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface AnalysisAlert {
2121
shortDescription: string;
2222
severity: ResultSeverity;
2323
fileLink: FileLink;
24-
codeSnippet: CodeSnippet;
24+
codeSnippet?: CodeSnippet;
2525
highlightedRegion?: HighlightedRegion;
2626
codeFlows: CodeFlow[];
2727
}

extensions/ql-vscode/src/remote-queries/view/FileCodeSnippet.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,17 @@ const FileCodeSnippet = ({
181181
messageChildren,
182182
}: {
183183
fileLink: FileLink,
184-
codeSnippet: CodeSnippet,
184+
codeSnippet?: CodeSnippet,
185185
highlightedRegion?: HighlightedRegion,
186186
severity?: ResultSeverity,
187187
message?: AnalysisMessage,
188188
messageChildren?: React.ReactNode,
189189
}) => {
190190

191-
const code = codeSnippet.text.split('\n');
191+
const code = codeSnippet?.text.split('\n') || [];
192192

193-
const startingLine = codeSnippet.startLine;
194-
const endingLine = codeSnippet.endLine;
193+
const startingLine = codeSnippet?.startLine || 0;
194+
const endingLine = codeSnippet?.endLine || 0;
195195

196196
const titleFileUri = createRemoteFileRef(
197197
fileLink,

extensions/ql-vscode/src/vscode-tests/no-workspace/download-link.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from 'chai';
22
import 'mocha';
33
import * as path from 'path';
44

5-
import { DownloadLink, toDownloadPath } from '../../remote-queries/download-link';
5+
import { DownloadLink, createDownloadPath } from '../../remote-queries/download-link';
66

77
describe('toDownloadPath', () => {
88
it('should return the correct path', () => {
@@ -12,9 +12,11 @@ describe('toDownloadPath', () => {
1212
innerFilePath: '',
1313
queryId: 'def'
1414
};
15-
1615
const expectedPath = path.join('storage', 'def', 'abc');
17-
expect(toDownloadPath('storage', downloadLink)).to.equal(expectedPath);
16+
17+
const actualPath = createDownloadPath('storage', downloadLink);
18+
19+
expect(actualPath).to.equal(expectedPath);
1820
});
1921

2022
it('should return the correct path with extension', () => {
@@ -26,6 +28,9 @@ describe('toDownloadPath', () => {
2628
};
2729

2830
const expectedPath = path.join('storage', 'def', 'abc.zip');
29-
expect(toDownloadPath('storage', downloadLink, 'zip')).to.equal(expectedPath);
31+
32+
const actualPath = createDownloadPath('storage', downloadLink, 'zip');
33+
34+
expect(actualPath).to.equal(expectedPath);
3035
});
3136
});

extensions/ql-vscode/test/pure-tests/sarif-processing.test.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,42 @@ describe('SARIF processing', () => {
419419
expectResultParsingError(result.errors[0]);
420420
});
421421

422-
it('should return errors for result locations with no context region', () => {
422+
it('should not return errors for result locations with no snippet', () => {
423423
const sarif = buildValidSarifLog();
424424
sarif.runs![0]!.results![0]!.locations![0]!.physicalLocation!.contextRegion!.snippet = undefined;
425425

426426
const result = extractAnalysisAlerts(sarif, fakefileLinkPrefix);
427427

428+
const expectedCodeSnippet = {
429+
startLine: result.alerts[0].codeSnippet!.startLine,
430+
endLine: result.alerts[0].codeSnippet!.endLine,
431+
text: ''
432+
};
433+
434+
const actualCodeSnippet = result.alerts[0].codeSnippet;
435+
428436
expect(result).to.be.ok;
429-
expect(result.errors.length).to.equal(1);
430-
expectResultParsingError(result.errors[0]);
437+
expectNoParsingError(result);
438+
expect(actualCodeSnippet).to.deep.equal(expectedCodeSnippet);
439+
});
440+
441+
it('should not return errors for result locations with no contextRegion', () => {
442+
const sarif = buildValidSarifLog();
443+
sarif.runs![0]!.results![0]!.locations![0]!.physicalLocation!.contextRegion = undefined;
444+
445+
const result = extractAnalysisAlerts(sarif, fakefileLinkPrefix);
446+
447+
const expectedCodeSnippet = {
448+
startLine: result.alerts[0].highlightedRegion!.startLine,
449+
endLine: result.alerts[0].highlightedRegion!.endLine,
450+
text: ''
451+
};
452+
453+
const actualCodeSnippet = result.alerts[0].codeSnippet;
454+
455+
expect(result).to.be.ok;
456+
expectNoParsingError(result);
457+
expect(actualCodeSnippet).to.deep.equal(expectedCodeSnippet);
431458
});
432459

433460
it('should not return errors for result locations with no region', () => {
@@ -438,6 +465,7 @@ describe('SARIF processing', () => {
438465

439466
expect(result).to.be.ok;
440467
expect(result.alerts.length).to.equal(1);
468+
expectNoParsingError(result);
441469
});
442470

443471
it('should return errors for result locations with no physical location', () => {
@@ -537,9 +565,9 @@ describe('SARIF processing', () => {
537565
expect(result).to.be.ok;
538566
expect(result.errors.length).to.equal(0);
539567
expect(result.alerts.length).to.equal(3);
540-
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet.text === 'foo')).to.be.ok;
541-
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet.text === 'bar')).to.be.ok;
542-
expect(result.alerts.find(a => getMessageText(a.message) === 'msg2' && a.codeSnippet.text === 'baz')).to.be.ok;
568+
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet!.text === 'foo')).to.be.ok;
569+
expect(result.alerts.find(a => getMessageText(a.message) === 'msg1' && a.codeSnippet!.text === 'bar')).to.be.ok;
570+
expect(result.alerts.find(a => getMessageText(a.message) === 'msg2' && a.codeSnippet!.text === 'baz')).to.be.ok;
543571
expect(result.alerts.every(a => a.severity === 'Warning')).to.be.true;
544572
});
545573

@@ -595,6 +623,11 @@ describe('SARIF processing', () => {
595623
expect(msg.startsWith('Error when processing SARIF result')).to.be.true;
596624
}
597625

626+
function expectNoParsingError(result: { errors: string[] | undefined }) {
627+
const array = result.errors || [];
628+
expect(array.length, array.join()).to.equal(0);
629+
}
630+
598631
function buildValidSarifLog(): sarif.Log {
599632
return {
600633
version: '2.1.0',

0 commit comments

Comments
 (0)