Skip to content

Commit 47ec074

Browse files
committed
Tidy-up and address review comments
1 parent e44835e commit 47ec074

5 files changed

Lines changed: 56 additions & 33 deletions

File tree

extensions/ql-vscode/src/pure/location-link-utils.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,3 @@ export function createRemoteFileRef(
1313
return `${fileLink.fileLinkPrefix}/${fileLink.filePath}`;
1414
}
1515
}
16-
17-
/**
18-
* Creates a markdown link to a remote file.
19-
* If the "link text" is not provided, we use the file path.
20-
*/
21-
export function createMarkdownRemoteFileRef(
22-
fileLink: FileLink,
23-
startLine?: number,
24-
endLine?: number,
25-
linkText?: string,
26-
): string {
27-
const markdownLink = `[${linkText || fileLink.filePath}](${createRemoteFileRef(fileLink, startLine, endLine)})`;
28-
return markdownLink;
29-
}

extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import { createMarkdownRemoteFileRef } from '../pure/location-link-utils';
1+
import { createRemoteFileRef } from '../pure/location-link-utils';
22
import { RemoteQuery } from './remote-query';
3-
import { AnalysisAlert, AnalysisResults } from './shared/analysis-result';
3+
import { AnalysisAlert, AnalysisResults, FileLink } from './shared/analysis-result';
44

55
// Each array item is a line of the markdown file.
66
export type MarkdownFile = string[];
77

8+
/**
9+
* Generates markdown files with variant analysis results.
10+
*/
811
export function generateMarkdown(query: RemoteQuery, analysesResults: AnalysisResults[]): MarkdownFile[] {
912
const files: MarkdownFile[] = [];
1013
for (const analysisResult of analysesResults) {
1114
if (analysisResult.interpretedResults.length === 0) {
15+
// TODO: We'll add support for non-interpreted results later.
1216
continue;
1317
}
1418
const lines = [
@@ -31,17 +35,15 @@ function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert,
3135
interpretedResult.fileLink,
3236
interpretedResult.highlightedRegion?.startLine,
3337
interpretedResult.highlightedRegion?.endLine
34-
), '');
38+
));
39+
lines.push('');
3540
const codeSnippet = interpretedResult.codeSnippet?.text;
3641
if (codeSnippet) {
3742
lines.push(
38-
`\`\`\`${language}`,
39-
...codeSnippet.split('\n'),
40-
'```',
41-
''
43+
...generateMarkdownForCodeSnippet(codeSnippet, language),
4244
);
4345
}
44-
const alertMessage = buildAlertMessage(interpretedResult);
46+
const alertMessage = buildMarkdownAlertMessage(interpretedResult);
4547
lines.push(alertMessage);
4648

4749
// Padding between results
@@ -53,7 +55,18 @@ function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert,
5355
return lines;
5456
}
5557

56-
function buildAlertMessage(interpretedResult: AnalysisAlert): string {
58+
function generateMarkdownForCodeSnippet(codeSnippet: string, language: string): MarkdownFile {
59+
const lines: MarkdownFile = [];
60+
lines.push(
61+
`\`\`\`${language}`,
62+
...codeSnippet.split('\n'),
63+
'```',
64+
);
65+
lines.push('');
66+
return lines;
67+
}
68+
69+
function buildMarkdownAlertMessage(interpretedResult: AnalysisAlert): string {
5770
let alertMessage = '';
5871
for (const token of interpretedResult.message.tokens) {
5972
if (token.t === 'text') {
@@ -67,5 +80,20 @@ function buildAlertMessage(interpretedResult: AnalysisAlert): string {
6780
);
6881
}
6982
}
70-
return alertMessage;
83+
// Italicize the alert message
84+
return `*${alertMessage}*`;
85+
}
86+
87+
/**
88+
* Creates a markdown link to a remote file.
89+
* If the "link text" is not provided, we use the file path.
90+
*/
91+
export function createMarkdownRemoteFileRef(
92+
fileLink: FileLink,
93+
startLine?: number,
94+
endLine?: number,
95+
linkText?: string,
96+
): string {
97+
const markdownLink = `[${linkText || fileLink.filePath}](${createRemoteFileRef(fileLink, startLine, endLine)})`;
98+
return markdownLink;
7199
}

extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo1.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function cleanupTemp() {
1010

1111
```
1212

13-
This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/src/Security/CWE-078/examples/shell-command-injection-from-environment.js#L4-L4).
13+
*This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/src/Security/CWE-078/examples/shell-command-injection-from-environment.js#L4-L4).*
1414

1515
----------------------------------------
1616

@@ -25,7 +25,7 @@ This shell command depends on an uncontrolled [absolute path](https://github.com
2525

2626
```
2727
28-
This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L6-L6).
28+
*This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L6-L6).*
2929
3030
----------------------------------------
3131
@@ -40,7 +40,7 @@ This shell command depends on an uncontrolled [absolute path](https://github.com
4040

4141
```
4242
43-
This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L8-L8).
43+
*This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L8-L8).*
4444
4545
----------------------------------------
4646
@@ -55,6 +55,6 @@ This shell command depends on an uncontrolled [absolute path](https://github.com
5555

5656
```
5757
58-
This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L9-L9).
58+
*This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L9-L9).*
5959
6060
----------------------------------------

extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/data/results-repo2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
```
1313

14-
This shell command depends on an uncontrolled [absolute path](https://github.com/meteor/meteor/blob/73b538fe201cbfe89dd0c709689023f9b3eab1ec/npm-packages/meteor-installer/config.js#L39-L39).
14+
*This shell command depends on an uncontrolled [absolute path](https://github.com/meteor/meteor/blob/73b538fe201cbfe89dd0c709689023f9b3eab1ec/npm-packages/meteor-installer/config.js#L39-L39).*
1515

1616
----------------------------------------

extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/interpreted-results/markdown-generation.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,20 @@ describe('markdown generation', async function() {
2020
const markdownFile1 = markdownFiles[0]; // results for github/codeql repo
2121
const markdownFile2 = markdownFiles[1]; // results for meteor/meteor repo
2222

23-
const expectedTestOutput1 = await fs.readFile(path.join(__dirname, 'data/results-repo1.md'), 'utf8');
24-
const expectedTestOutput2 = await fs.readFile(path.join(__dirname, 'data/results-repo2.md'), 'utf8');
23+
const expectedTestOutput1 = await readTestOutputFile('data/results-repo1.md');
24+
const expectedTestOutput2 = await readTestOutputFile('data/results-repo2.md');
2525

2626
// Check that markdown output is correct, after making line endings consistent
27-
expect(markdownFile1.join('\n')).to.equal(expectedTestOutput1.replace(/\r?\n/g, '\n'));
28-
expect(markdownFile2.join('\n')).to.equal(expectedTestOutput2.replace(/\r?\n/g, '\n'));
27+
expect(markdownFile1.join('\n')).to.equal(expectedTestOutput1);
28+
expect(markdownFile2.join('\n')).to.equal(expectedTestOutput2);
2929
});
3030
});
31+
32+
/**
33+
* Reads a test output file and returns it as a string.
34+
* Replaces line endings with '\n' for consistency across operating systems.
35+
*/
36+
async function readTestOutputFile(relativePath: string): Promise<string> {
37+
const file = await fs.readFile(path.join(__dirname, relativePath), 'utf8');
38+
return file.replace(/\r?\n/g, '\n');
39+
}

0 commit comments

Comments
 (0)