Skip to content

Commit b156268

Browse files
authored
Add script to help with scenario recording (#1671)
1 parent f04c346 commit b156268

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ To record a new mock MRVA scenario, follow these steps:
181181

182182
If you want to cancel recording, run the `CodeQL: Mock GitHub API Server: Cancel Scenario Recording` command.
183183

184+
Once the scenario has been recorded, it's often useful to remove some of the requests to speed up the replay, particularly ones that fetch the variant analysis status. Once some of the request files have manually been removed, the [fix-scenario-file-numbering script](./extensions/ql-vscode/scripts/fix-scenario-file-numbering.ts) can be used to update the number of the files. See the script file for details on how to use.
185+
184186
#### Scenario data location
185187

186188
Pre-recorded scenarios are stored in `./src/mocks/scenarios`. However, it's possible to configure the location, by setting the `codeQL.mockGitHubApiServer.scenariosPath` configuration property in the VS Code user settings.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* This scripts helps after recording a scenario to be used for replaying
3+
* with the mock GitHub API server.
4+
*
5+
* Once the scenario has been recorded, it's often useful to remove some of
6+
* the requests to speed up the replay, particularly ones that fetch the
7+
* variant analysis status. Once some of the requests have manually been
8+
* removed, this script can be used to update the numbering of the files.
9+
*
10+
* Usage: npx ts-node scripts/fix-scenario-file-numbering.ts <scenario-name>
11+
*/
12+
13+
import * as fs from 'fs-extra';
14+
import * as path from 'path';
15+
16+
if (process.argv.length !== 3) {
17+
console.error('Expected 1 argument - the scenario name')
18+
}
19+
20+
const scenarioName = process.argv[2];
21+
22+
const extensionDirectory = path.resolve(__dirname, '..');
23+
const scenariosDirectory = path.resolve(extensionDirectory, 'src/mocks/scenarios');
24+
const scenarioDirectory = path.resolve(scenariosDirectory, scenarioName);
25+
26+
async function fixScenarioFiles() {
27+
console.log(scenarioDirectory);
28+
if (!(await fs.pathExists(scenarioDirectory))) {
29+
console.error('Scenario directory does not exist: ' + scenarioDirectory);
30+
return;
31+
}
32+
33+
const files = await fs.readdir(scenarioDirectory);
34+
35+
const orderedFiles = files.sort((a, b) => {
36+
const aNum = parseInt(a.split('-')[0]);
37+
const bNum = parseInt(b.split('-')[0]);
38+
return aNum - bNum;
39+
});
40+
41+
let index = 0;
42+
for (let file of orderedFiles) {
43+
const ext = path.extname(file);
44+
if (ext === '.json') {
45+
const fileName = path.basename(file, ext);
46+
const fileCurrentIndex = parseInt(fileName.split('-')[0]);
47+
const fileNameWithoutIndex = fileName.split('-')[1];
48+
if (fileCurrentIndex !== index) {
49+
const newFileName = `${index}-${fileNameWithoutIndex}${ext}`;
50+
const oldFilePath = path.join(scenarioDirectory, file);
51+
const newFilePath = path.join(scenarioDirectory, newFileName);
52+
console.log(`Rename: ${oldFilePath} -> ${newFilePath}`);
53+
await fs.rename(oldFilePath, newFilePath);
54+
55+
if (fileNameWithoutIndex === 'getVariantAnalysisRepoResult') {
56+
const oldZipFileName = `${fileCurrentIndex}-getVariantAnalysisRepoResult.body.zip`;
57+
const newZipFileName = `${index}-getVariantAnalysisRepoResult.body.zip`;
58+
const oldZipFilePath = path.join(scenarioDirectory, oldZipFileName);
59+
const newZipFilePath = path.join(scenarioDirectory, newZipFileName);
60+
console.log(`Rename: ${oldZipFilePath} -> ${newZipFilePath}`);
61+
await fs.rename(oldZipFilePath, newZipFilePath);
62+
63+
const json = await fs.readJson(newFilePath);
64+
json.response.body = `file:${newZipFileName}`;
65+
console.log(`Response.body change to ${json.response.body}`);
66+
await fs.writeJSON(newFilePath, json);
67+
}
68+
}
69+
70+
index++;
71+
}
72+
}
73+
}
74+
75+
fixScenarioFiles().catch(e => {
76+
console.error(e);
77+
process.exit(2);
78+
});

0 commit comments

Comments
 (0)